From 650fb254a2bbc89c9eb170eb667874e83b6e7ec7 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 11 Jul 2026 17:23:13 -0500 Subject: [PATCH] refac --- .../src/lib/components/ChromeBrowser.svelte | 7 +++- cptr/frontend/src/lib/stores.ts | 42 ++++++++++++++++++- cptr/frontend/src/routes/+page.svelte | 38 ++++++++--------- 3 files changed, 63 insertions(+), 24 deletions(-) diff --git a/cptr/frontend/src/lib/components/ChromeBrowser.svelte b/cptr/frontend/src/lib/components/ChromeBrowser.svelte index b48b2a9..9b089d6 100644 --- a/cptr/frontend/src/lib/components/ChromeBrowser.svelte +++ b/cptr/frontend/src/lib/components/ChromeBrowser.svelte @@ -310,9 +310,12 @@ function resize(debounce = false) { if (!container || !ready) return; const rect = container.getBoundingClientRect(); + const width = Math.round(rect.width); + const height = Math.round(rect.height); + if (width <= 0 || height <= 0) return; viewport = { - width: Math.max(320, Math.min(1920, Math.round(rect.width))), - height: Math.max(240, Math.min(1080, Math.round(rect.height))) + width, + height }; if (viewportTimer) clearTimeout(viewportTimer); if (debounce || (mobile && document.activeElement === keyboardAntenna)) { diff --git a/cptr/frontend/src/lib/stores.ts b/cptr/frontend/src/lib/stores.ts index 76912f5..31a6372 100644 --- a/cptr/frontend/src/lib/stores.ts +++ b/cptr/frontend/src/lib/stores.ts @@ -113,6 +113,7 @@ export interface UserPreferences { pwa?: PwaPreferences; textScale?: number | null; widescreenMode?: boolean; + homeGroup?: EditorGroup; } // ── ID generation ─────────────────────────────────────────────── @@ -254,6 +255,11 @@ function normalizeLayout( /** The workspace currently displayed in THIS browser tab. Null = welcome page. */ export const currentWorkspace = writable(null); +export const homeGroup = writable({ + id: 'home', + tabs: [{ id: 'home', type: 'home', label: 'Home', permanent: true }], + activeTabId: 'home' +}); /** List of all workspace summaries for the sidebar. */ export const workspaceList = writable<{ path: string; name: string }[]>([]); @@ -408,7 +414,8 @@ function persistPreferences(): void { showUpdateToast: get(showUpdateToastPref), pwa: get(pwaPreferences), textScale: get(textScale) ?? undefined, - widescreenMode: get(widescreenMode) + widescreenMode: get(widescreenMode), + homeGroup: get(homeGroup) }; savePreferences(prefs as unknown as Record).catch(() => {}); }, 300); @@ -421,6 +428,9 @@ function subscribeForPersistence() { currentWorkspace.subscribe(() => { if (get(stateLoaded)) persistWorkspace(); }); + homeGroup.subscribe(() => { + if (get(stateLoaded)) persistPreferences(); + }); theme.subscribe(() => { if (get(stateLoaded)) persistPreferences(); }); @@ -507,6 +517,36 @@ export async function loadPreferences(): Promise { : null ); if (prefs.widescreenMode !== undefined) widescreenMode.set(prefs.widescreenMode as boolean); + const savedHomeGroup = prefs.homeGroup as EditorGroup | undefined; + if (savedHomeGroup && Array.isArray(savedHomeGroup.tabs)) { + const [terminalIds, browserIds] = await Promise.all([ + listSessions().catch(() => []), + listBrowserSessions().catch(() => []) + ]); + const aliveTerminals = new Set(terminalIds); + const aliveBrowsers = new Set(browserIds); + const tabs = savedHomeGroup.tabs.filter( + (tab) => + tab.type !== 'terminal' || + (tab.sessionId !== undefined && aliveTerminals.has(tab.sessionId)) + ); + const liveTabs = tabs.filter( + (tab) => + tab.type !== 'browser' || + (tab.browserSessionId !== undefined && aliveBrowsers.has(tab.browserSessionId)) + ); + if (!liveTabs.some((tab) => tab.type === 'home')) { + liveTabs.unshift({ id: 'home', type: 'home', label: 'Home', permanent: true }); + } + homeGroup.set({ + ...savedHomeGroup, + id: 'home', + tabs: liveTabs, + activeTabId: liveTabs.some((tab) => tab.id === savedHomeGroup.activeTabId) + ? savedHomeGroup.activeTabId + : 'home' + }); + } const pwaPrefs = prefs.pwa; if (pwaPrefs) pwaPreferences.set({ diff --git a/cptr/frontend/src/routes/+page.svelte b/cptr/frontend/src/routes/+page.svelte index e30b848..1dd49a0 100644 --- a/cptr/frontend/src/routes/+page.svelte +++ b/cptr/frontend/src/routes/+page.svelte @@ -17,7 +17,8 @@ appVersion, showChangelog, showSearch, - pwaPreferences + pwaPreferences, + homeGroup } from '$lib/stores'; import type { Tab, EditorGroup, EditorLayout, SplitDirection, WorkspaceState } from '$lib/stores'; import { chatEnabled } from '$lib/stores/chat'; @@ -48,22 +49,17 @@ let pendingIntent = $state(null); let folderPickerIntent = $state(null); let folderPickerWorkspace = $state(null); - let homeGroup = $state({ - id: 'home', - tabs: [{ id: 'home', type: 'home', label: 'Home', permanent: true }], - activeTabId: 'home' - }); const activeHomeTab = $derived( - homeGroup.tabs.find((tab) => tab.id === homeGroup.activeTabId) ?? homeGroup.tabs[0] + $homeGroup.tabs.find((tab) => tab.id === $homeGroup.activeTabId) ?? $homeGroup.tabs[0] ); function updateHomeTabs(update: (tabs: Tab[]) => { tabs: Tab[]; activeTabId: string }) { - const next = update(homeGroup.tabs); - homeGroup = { ...homeGroup, ...next }; + const next = update($homeGroup.tabs); + homeGroup.set({ ...$homeGroup, ...next }); } function openHomeChat(chatId?: string) { - const existing = homeGroup.tabs.find( + const existing = $homeGroup.tabs.find( (tab) => tab.type === 'chat' && (chatId @@ -71,7 +67,7 @@ : tab.path?.startsWith('new-') || tab.path?.startsWith('pending-')) ); if (existing) { - homeGroup = { ...homeGroup, activeTabId: existing.id }; + homeGroup.set({ ...$homeGroup, activeTabId: existing.id }); return; } const tab: Tab = { @@ -115,30 +111,30 @@ } function closeHomeTab(tabId: string) { - const index = homeGroup.tabs.findIndex((tab) => tab.id === tabId); - const tab = homeGroup.tabs[index]; + const index = $homeGroup.tabs.findIndex((tab) => tab.id === tabId); + const tab = $homeGroup.tabs[index]; if (!tab || tab.permanent) return; if (tab.type === 'terminal' && tab.sessionId) deleteSession(tab.sessionId); if (tab.type === 'browser' && tab.browserSessionId) deleteBrowserSession(tab.browserSessionId); - const tabs = homeGroup.tabs.filter((item) => item.id !== tabId); + const tabs = $homeGroup.tabs.filter((item) => item.id !== tabId); const activeTabId = - homeGroup.activeTabId === tabId + $homeGroup.activeTabId === tabId ? (tabs[Math.max(0, index - 1)]?.id ?? 'home') - : homeGroup.activeTabId; - homeGroup = { ...homeGroup, tabs, activeTabId }; + : $homeGroup.activeTabId; + homeGroup.set({ ...$homeGroup, tabs, activeTabId }); } function updateHomeChatTab(tabId: string, chatId: string, label: string) { updateHomeTabs((tabs) => ({ tabs: tabs.map((tab) => (tab.id === tabId ? { ...tab, path: chatId, label } : tab)), - activeTabId: homeGroup.activeTabId + activeTabId: $homeGroup.activeTabId })); } function updateHomeBrowserTab(tabId: string, label: string) { updateHomeTabs((tabs) => ({ tabs: tabs.map((tab) => (tab.id === tabId ? { ...tab, label } : tab)), - activeTabId: homeGroup.activeTabId + activeTabId: $homeGroup.activeTabId })); } const INTENT_URL_KEYS = [ @@ -898,10 +894,10 @@ {#if !$currentWorkspace}
(homeGroup = { ...homeGroup, activeTabId: tabId })} + onHomeSelect={(tabId) => homeGroup.set({ ...$homeGroup, activeTabId: tabId })} onHomeClose={closeHomeTab} onHomeNewChat={openHomeChat} onHomeNewTerminal={openHomeTerminal}