diff --git a/cptr/frontend/src/lib/components/ChromeBrowser.svelte b/cptr/frontend/src/lib/components/ChromeBrowser.svelte index 37566cd..9b423fc 100644 --- a/cptr/frontend/src/lib/components/ChromeBrowser.svelte +++ b/cptr/frontend/src/lib/components/ChromeBrowser.svelte @@ -46,7 +46,7 @@ socket.onopen = () => { reconnectAttempt = 0; send({ type: 'visibility', visible: active }); - if (active) activate(); + if (active) focusBrowser(); else resize(); }; socket.onmessage = receive; @@ -139,7 +139,7 @@ function pointer(event: PointerEvent, type: 'move' | 'down' | 'up') { if (type === 'down') { - activate(); + focusBrowser(); canvas.focus(); canvas.setPointerCapture(event.pointerId); } @@ -165,7 +165,7 @@ function wheel(event: WheelEvent) { event.preventDefault(); - activate(); + focusBrowser(); send({ type: 'wheel', ...coordinates(event), @@ -178,7 +178,6 @@ function key(event: KeyboardEvent, type: 'keyDown' | 'keyUp') { event.preventDefault(); event.stopPropagation(); - if (type === 'keyDown') activate(); const message = { type: 'key', event: type, @@ -212,30 +211,30 @@ } export function navigate(url: string) { - activate(); + focusBrowser(); send({ type: 'navigate', url }); } export function back() { - activate(); + focusBrowser(); send({ type: 'back' }); } export function forward() { - activate(); + focusBrowser(); send({ type: 'forward' }); } export function reload() { - activate(); + focusBrowser(); send({ type: 'reload' }); } - export function activate() { - send({ type: 'activate' }); + function focusBrowser() { + send({ type: 'focus' }); resize(); } $effect(() => { if (socket?.readyState === WebSocket.OPEN) { send({ type: 'visibility', visible: active }); - if (active) activate(); + if (active) focusBrowser(); } }); diff --git a/cptr/frontend/src/lib/stores.ts b/cptr/frontend/src/lib/stores.ts index ad9efa9..cdf61c1 100644 --- a/cptr/frontend/src/lib/stores.ts +++ b/cptr/frontend/src/lib/stores.ts @@ -1116,7 +1116,9 @@ export function setActiveTab(tabId: string, groupId?: string): void { } export function setActiveGroup(groupId: string): void { - currentWorkspace.update((ws) => (ws ? { ...ws, activeGroupId: groupId } : ws)); + currentWorkspace.update((ws) => + ws && ws.activeGroupId !== groupId ? { ...ws, activeGroupId: groupId } : ws + ); } export function setFileBrowserCwd(cwd: string): void { diff --git a/cptr/frontend/src/routes/+page.svelte b/cptr/frontend/src/routes/+page.svelte index 94fea49..97f46bd 100644 --- a/cptr/frontend/src/routes/+page.svelte +++ b/cptr/frontend/src/routes/+page.svelte @@ -954,7 +954,8 @@
setActiveGroup(group.id)} + onpointerdown={() => setActiveGroup(group.id)} + onfocusin={() => setActiveGroup(group.id)} ondragover={(event) => handlePaneDragOver(event, group.id)} ondragleave={(event) => handlePaneDragLeave(event, group.id)} ondrop={(event) => handlePaneDrop(event, group.id)} @@ -999,7 +1000,7 @@ groupId={group.id} tabId={tab.id} initialUrl={tab.path} - active={tab.id === group.activeTabId} + active={tab.id === group.activeTabId && group.id === activeGroup?.id} />
{/each} diff --git a/cptr/utils/browser/viewer.py b/cptr/utils/browser/viewer.py index 5ff04a8..7ebd528 100644 --- a/cptr/utils/browser/viewer.py +++ b/cptr/utils/browser/viewer.py @@ -588,7 +588,7 @@ class ChromeViewerManager: session.status = "playing" self._wire_events(tab) await self._refresh_state(tab) - await self._activate_personal(tab) + await self._focus_personal(tab) return tab def viewer_for(self, session_id: str) -> ChromeViewer | PersonalTab | None: @@ -927,18 +927,18 @@ class ChromeViewerManager: if kind == "visibility": await self._set_visibility(viewer, peer, bool(data.get("visible"))) return - if kind == "activate" and viewer.personal: + if kind == "focus" and viewer.personal: became_visible = not peer.visible peer.visible = True if became_visible: await self._send_personal({"type": "resume"}) - await self._activate_personal(viewer, peer) + await self._focus_personal(viewer, peer) if became_visible: await self._request_keyframe(viewer) return if viewer.controller is not peer: return - if kind == "activate": + if kind == "focus": return if kind == "viewport": width = max(320, min(1920, int(data.get("width", 1280)))) @@ -1019,51 +1019,55 @@ class ChromeViewerManager: elif kind == "request_keyframe": await self._request_keyframe(viewer) - async def _activate_personal( + async def _focus_personal( self, tab: PersonalTab, peer: ViewerPeer | None = None ) -> None: - personal = self.personal - if not personal or personal.tabs.get(tab.session.session_id) is not tab: - return - already_active = personal.active_session_id == tab.session.session_id - if already_active and (peer is None or tab.controller is peer): - return - previous = personal.tabs.get(personal.active_session_id) - if previous and previous is not tab and previous.controller: - with contextlib.suppress(asyncio.QueueFull): - previous.controller.queue.put_nowait( - {"type": "ready", "mode": "chrome", "controller": False} - ) - for item in tab.peers: - while not item.queue.empty(): - with contextlib.suppress(asyncio.QueueEmpty): - item.queue.get_nowait() - item.waiting_keyframe = True - if personal.config: + async with self.lock: + personal = self.personal + if not personal or personal.tabs.get(tab.session.session_id) is not tab: + return + already_active = personal.active_session_id == tab.session.session_id + if already_active and (peer is None or tab.controller is peer): + await asyncio.wait_for(tab.target_cdp.send("Page.bringToFront"), 5) + return + previous = personal.tabs.get(personal.active_session_id) + if previous and previous is not tab and previous.controller: with contextlib.suppress(asyncio.QueueFull): - item.queue.put_nowait(personal.config) - if peer: - previous = tab.controller - tab.controller = peer - if previous is not peer: - if previous and previous in tab.peers: - with contextlib.suppress(asyncio.QueueFull): - previous.queue.put_nowait( - {"type": "ready", "mode": "chrome", "controller": False} - ) - with contextlib.suppress(asyncio.QueueFull): - peer.queue.put_nowait( - {"type": "ready", "mode": "chrome", "controller": True} + previous.controller.queue.put_nowait( + {"type": "ready", "mode": "chrome", "controller": False} ) - personal.active_session_id = tab.session.session_id - with contextlib.suppress(Exception): + for item in tab.peers: + while not item.queue.empty(): + with contextlib.suppress(asyncio.QueueEmpty): + item.queue.get_nowait() + item.waiting_keyframe = True + if tab.config and tab.keyframe: + with contextlib.suppress(asyncio.QueueFull): + item.queue.put_nowait(tab.config) + item.queue.put_nowait(tab.keyframe) + item.waiting_keyframe = False + elif personal.config: + with contextlib.suppress(asyncio.QueueFull): + item.queue.put_nowait(personal.config) + if peer: + previous = tab.controller + tab.controller = peer + if previous is not peer: + if previous and previous in tab.peers: + with contextlib.suppress(asyncio.QueueFull): + previous.queue.put_nowait( + {"type": "ready", "mode": "chrome", "controller": False} + ) + with contextlib.suppress(asyncio.QueueFull): + peer.queue.put_nowait( + {"type": "ready", "mode": "chrome", "controller": True} + ) await asyncio.wait_for( - personal.host.browser_cdp.send( - "Target.activateTarget", {"targetId": tab.target_id} - ), - 2, + tab.target_cdp.send("Page.bringToFront"), + 5, ) - await self._resize_personal(tab) + personal.active_session_id = tab.session.session_id + await self._resize_personal(tab) async def _resize_personal(self, tab: PersonalTab) -> None: personal = self.personal