diff --git a/locales/en.json b/locales/en.json index a4e8a59b4..b79621f53 100644 --- a/locales/en.json +++ b/locales/en.json @@ -65,7 +65,9 @@ "signIn": "Log in to Comfy Cloud" }, "apps": { - "loadFailed": "The apps page at {url} could not load." + "catalogFailed": "Couldn't load the apps.", + "comingSoon": "Coming soon", + "loadFailed": "The app at {url} could not load." }, "home": { "title": "Welcome. Let's create!", diff --git a/locales/zh.json b/locales/zh.json index 3ff84ef0c..fec143b83 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -65,7 +65,9 @@ "signIn": "登录 Comfy Cloud" }, "apps": { - "loadFailed": "无法加载应用页面:{url}" + "catalogFailed": "无法加载应用列表。", + "comingSoon": "即将推出", + "loadFailed": "无法加载应用:{url}" }, "home": { "title": "欢迎。开始创作吧!", diff --git a/src/main/apps/appsRun.test.ts b/src/main/apps/appsRun.test.ts index e46ae55c3..d2984ed67 100644 --- a/src/main/apps/appsRun.test.ts +++ b/src/main/apps/appsRun.test.ts @@ -71,7 +71,7 @@ vi.mock('./localComfy', () => ({ syncLocalSocket: () => {} })) vi.mock('./openInComfy', () => ({ localBuilds: async () => [], openInComfy: async () => ({}) })) -vi.mock('./appsSwitch', () => ({ isAppsEnabled: () => true })) +vi.mock('./appsSwitch', () => ({ isAppsEnabled: () => true, onAppsEnabledChanged: () => {} })) vi.mock('./appEvents', () => ({ trackFromPage: () => {} })) const desktopApps = vi.hoisted(() => ({ diff --git a/src/main/apps/appsView.ts b/src/main/apps/appsView.ts index 67ef62045..489552c91 100644 --- a/src/main/apps/appsView.ts +++ b/src/main/apps/appsView.ts @@ -18,6 +18,7 @@ import { cloudPromptBody, collectOutputs, isAppOpen, + isAppPage, isAllowedAppsPermission, isAppsFrameUrl, isDocumentFailure, @@ -53,6 +54,7 @@ import { startLocal, syncLocalSocket } from './localComfy' +import { listCatalogApps } from './catalog' import { localBuilds, openInComfy } from './openInComfy' import { isAppsEnabled, onAppsEnabledChanged } from './appsSwitch' import { @@ -75,9 +77,14 @@ const failedViews = new WeakSet() export const APPS_LOAD_FAILED_CHANNEL = 'comfy-apps:load-failed' export const APPS_APP_OPEN_CHANNEL = 'comfy-apps:app-open' +export const APPS_SETTLED_CHANNEL = 'comfy-apps:settled' +export const APPS_PAGE_SETTLED_CHANNEL = 'comfy-apps:page-settled' +export const APPS_CATALOG_CHANNEL = 'comfy-apps:catalog' export const APPS_ENABLED_CHANNEL = 'comfy-apps:enabled' const appOpen = new Map() +const awaitingCommit = new Set() +const settledPages = new Set() export interface AppsRect { x: number @@ -85,6 +92,7 @@ export interface AppsRect { width: number height: number fresh?: boolean + url?: string } export type AppsAuthState = { signedIn: false } | { signedIn: true; email?: string } @@ -112,6 +120,11 @@ function tellPanelAppOpen(key: number): void { panel.send(APPS_APP_OPEN_CHANNEL, { open: appOpen.get(key) === true }) } +function tellPanelSettled(key: number): void { + const panel = comfyWindows.get(key)?.panelView?.webContents + if (panel && !panel.isDestroyed()) panel.send(APPS_SETTLED_CHANNEL) +} + function trackAppOpen(key: number, url: string): void { appOpen.set(key, isAppOpen(url, APPS_URL)) tellPanelAppOpen(key) @@ -143,7 +156,7 @@ function keepInApps(event: { preventDefault(): void }, url: string): void { openOutside(url) } -function createAppsView(key: number): WebContentsView { +function createAppsView(key: number, url: string): WebContentsView { secureAppsSession() const view = new WebContentsView({ webPreferences: { @@ -168,17 +181,33 @@ function createAppsView(key: number): WebContentsView { if (isMainFrame && isPageLoadFailure(errorCode)) markLoadFailed(key, view, url) }) wc.on('did-navigate', (_event, url, httpResponseCode) => { + awaitingCommit.delete(key) + settledPages.delete(key) if (isDocumentFailure(httpResponseCode)) markLoadFailed(key, view, url) trackAppOpen(key, url) }) wc.on('did-navigate-in-page', (_event, url, isMainFrame) => { if (isMainFrame) trackAppOpen(key, url) }) - wc.loadURL(APPS_URL).catch(() => {}) + appOpen.set(key, isAppOpen(url, APPS_URL)) + awaitingCommit.add(key) + settledPages.delete(key) + wc.loadURL(url).catch(() => {}) void desktopAppIds() return view } +function requestedUrl(r: Partial): string { + return typeof r.url === 'string' && isAppsFrameUrl(r.url, APPS_URL) ? r.url : APPS_URL +} + +function openUrl(key: number, view: WebContentsView, url: string): void { + appOpen.set(key, isAppOpen(url, APPS_URL)) + awaitingCommit.add(key) + settledPages.delete(key) + view.webContents.loadURL(url).catch(() => {}) +} + function entryForPanel( sender: WebContents ): [number, NonNullable>] | null { @@ -197,19 +226,24 @@ function showApps(event: IpcMainEvent, rect: unknown): void { const r = rect as Partial | null if (!r || ![r.x, r.y, r.width, r.height].every((n) => typeof n === 'number' && isFinite(n))) return + const url = requestedUrl(r) let view = views.get(key) if (view?.webContents.isDestroyed()) view = undefined if (view && failedViews.has(view)) { if (r.fresh !== true) return failedViews.delete(view) - view.webContents.loadURL(APPS_URL).catch(() => {}) + openUrl(key, view, url) + } else if (view && r.fresh === true && view.webContents.getURL() !== url) { + openUrl(key, view, url) } if (!view) { - view = createAppsView(key) + view = createAppsView(key, url) views.set(key, view) entry.window.once('closed', () => { views.delete(key) appOpen.delete(key) + awaitingCommit.delete(key) + settledPages.delete(key) if (!view!.webContents.isDestroyed()) view!.webContents.close() syncLocalSocket() }) @@ -223,7 +257,19 @@ function showApps(event: IpcMainEvent, rect: unknown): void { }) entry.window.contentView.addChildView(view) view.setVisible(true) - if (r.fresh === true) tellPanelAppOpen(key) + if (r.fresh !== true) return + tellPanelAppOpen(key) + if (settledPages.has(key)) tellPanelSettled(key) +} + +function relayPageSettled(event: IpcMainEvent): void { + for (const [key, view] of views) { + if (view.webContents !== event.sender) continue + if (awaitingCommit.has(key) || !isAppPage(event.senderFrame?.url ?? '', APPS_URL)) return + settledPages.add(key) + tellPanelSettled(key) + return + } } function hideApps(event: IpcMainEvent): void { @@ -668,6 +714,10 @@ function appsPages(): WebContents[] { export function registerAppsIpc(): void { ipcMain.on('comfy-apps:show', showApps) ipcMain.on('comfy-apps:hide', hideApps) + ipcMain.on(APPS_PAGE_SETTLED_CHANNEL, relayPageSettled) + ipcMain.handle(APPS_CATALOG_CHANNEL, (event) => + entryForPanel(event.sender) ? listCatalogApps(APPS_URL) : [] + ) onAppsEnabledChanged(tellPanelsAppsEnabled) initLocalComfy(appsPages) diff --git a/src/main/apps/catalog.test.ts b/src/main/apps/catalog.test.ts new file mode 100644 index 000000000..89dc38b04 --- /dev/null +++ b/src/main/apps/catalog.test.ts @@ -0,0 +1,181 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import { _resetCatalogAppsForTest, catalogUrl, listCatalogApps, toCatalogApp } from './catalog' + +const LIST = 'https://apps.example/apps/' +const fetchMock = vi.fn() + +function item(uid: string, data: Record) { + return { uid, data } +} + +function catalogApp(slug: string, title: string, order: number) { + return item(`uid-${slug}`, { + title, + slug, + description: `${title} description`, + sort_order: order, + website_media: { '$.static_thumb': `/catalog-media/${slug}/v1` } + }) +} + +beforeEach(() => { + _resetCatalogAppsForTest() + vi.stubGlobal('fetch', fetchMock) +}) + +afterEach(() => { + vi.unstubAllGlobals() + vi.clearAllMocks() +}) + +describe('catalogUrl', () => { + it('asks the Cloud catalog for the apps of the Desktop surface', () => { + const url = new URL(catalogUrl()) + expect(url.origin + url.pathname).toBe('https://cloud.comfy.org/api/v1/catalog/items') + expect(Object.fromEntries(url.searchParams)).toEqual({ + type: 'app', + surface: 'desktop_starters', + limit: '100' + }) + }) +}) + +describe('toCatalogApp', () => { + it('takes the title, description and thumbnail, and opens the app on its slug route', () => { + expect(toCatalogApp(catalogApp('text-to-image', 'Text to Image', 2), LIST)).toEqual({ + id: 'uid-text-to-image', + title: 'Text to Image', + description: 'Text to Image description', + thumbnailUrl: 'https://comfy.org/catalog-media/text-to-image/v1', + url: 'https://apps.example/apps/#/text-to-image' + }) + }) + + it('keeps an odd slug inside the route', () => { + const app = toCatalogApp(item('a', { title: 'A', slug: 'a b/../c' }), LIST) + expect(app?.url).toBe('https://apps.example/apps/#/a%20b%2F..%2Fc') + expect(app?.description).toBe('') + expect(app?.thumbnailUrl).toBeNull() + }) + + it('falls back to the hero poster and ignores media outside the catalog', () => { + const poster = item('a', { + title: 'A', + slug: 'a', + website_media: { '$.hero_poster': '/catalog-media/a/poster' } + }) + const outside = item('b', { + title: 'B', + slug: 'b', + website_media: { '$.static_thumb': 'https://evil.example/x.png' } + }) + expect(toCatalogApp(poster, LIST)?.thumbnailUrl).toBe( + 'https://comfy.org/catalog-media/a/poster' + ) + expect(toCatalogApp(outside, LIST)?.thumbnailUrl).toBeNull() + }) + + it('ignores media that climbs out of the catalog folder', () => { + const climbing = item('a', { + title: 'A', + slug: 'a', + website_media: { + '$.static_thumb': '/catalog-media/../account/delete', + '$.hero_poster': '/catalog-media/a/poster' + } + }) + expect(toCatalogApp(climbing, LIST)?.thumbnailUrl).toBe( + 'https://comfy.org/catalog-media/a/poster' + ) + }) + + it('skips an app without a title or a slug', () => { + expect(toCatalogApp(item('a', { slug: 'a' }), LIST)).toBeNull() + expect(toCatalogApp(item('b', { title: 'No slug' }), LIST)).toBeNull() + expect(toCatalogApp(item('c', { title: 'Blank slug', slug: ' ' }), LIST)).toBeNull() + }) +}) + +describe('listCatalogApps', () => { + it('lists every app the catalog returns, in catalog order, and leaves out ones it cannot open', async () => { + fetchMock.mockResolvedValue( + Response.json({ + items: [ + catalogApp('product-ads', 'Second', 1), + item('x', { title: 'No slug' }), + catalogApp('upscale', 'Third', 2), + catalogApp('genjutsu', 'First', 0) + ] + }) + ) + const apps = await listCatalogApps(LIST) + expect(apps.map((app) => app.title)).toEqual(['First', 'Second', 'Third']) + }) + + it('skips entries that are not objects and lists each app once', async () => { + fetchMock.mockResolvedValue( + Response.json({ + items: [ + null, + 'genjutsu', + catalogApp('genjutsu', 'First', 0), + catalogApp('genjutsu', 'Again', 1) + ] + }) + ) + const apps = await listCatalogApps(LIST) + expect(apps.map((app) => app.title)).toEqual(['First']) + }) + + it('keeps an empty catalog empty', async () => { + fetchMock.mockResolvedValue(Response.json({ items: [] })) + await expect(listCatalogApps(LIST)).resolves.toEqual([]) + }) + + it('fails when the catalog answers without an item list', async () => { + fetchMock.mockResolvedValue(Response.json({ items: null })) + await expect(listCatalogApps(LIST)).rejects.toThrow('no item list') + fetchMock.mockResolvedValue(Response.json(null)) + await expect(listCatalogApps(LIST)).rejects.toThrow('no item list') + }) + + it('fails when the catalog does not answer', async () => { + fetchMock.mockResolvedValue(new Response(null, { status: 503 })) + await expect(listCatalogApps(LIST)).rejects.toThrow('HTTP 503') + }) + + it('answers again from the last list without asking the catalog', async () => { + fetchMock.mockResolvedValue(Response.json({ items: [catalogApp('genjutsu', 'First', 0)] })) + await listCatalogApps(LIST) + const again = await listCatalogApps(LIST) + expect(again.map((app) => app.title)).toEqual(['First']) + expect(fetchMock).toHaveBeenCalledTimes(1) + }) + + it('shares one request between callers that ask at the same time', async () => { + fetchMock.mockImplementation(async () => + Response.json({ items: [catalogApp('genjutsu', 'First', 0)] }) + ) + const [first, second] = await Promise.all([listCatalogApps(LIST), listCatalogApps(LIST)]) + expect(second).toBe(first) + expect(fetchMock).toHaveBeenCalledTimes(1) + }) + + it('keeps the last list when a later request fails', async () => { + vi.useFakeTimers() + try { + fetchMock.mockResolvedValueOnce( + Response.json({ items: [catalogApp('genjutsu', 'First', 0)] }) + ) + await listCatalogApps(LIST) + vi.advanceTimersByTime(11 * 60 * 1000) + fetchMock.mockResolvedValueOnce(new Response(null, { status: 503 })) + const apps = await listCatalogApps(LIST) + expect(apps.map((app) => app.title)).toEqual(['First']) + expect(fetchMock).toHaveBeenCalledTimes(2) + } finally { + vi.useRealTimers() + } + }) +}) diff --git a/src/main/apps/catalog.ts b/src/main/apps/catalog.ts new file mode 100644 index 000000000..147d4addd --- /dev/null +++ b/src/main/apps/catalog.ts @@ -0,0 +1,116 @@ +import { CLOUD_CONFIG } from '../cloud/config' +import type { CatalogApp } from '../../types/ipc' + +const DESKTOP_SURFACE = 'desktop_starters' +const CATALOG_MEDIA_ORIGIN = 'https://comfy.org' +const CATALOG_MEDIA_PATH = '/catalog-media/' +const CATALOG_TIMEOUT_MS = 8000 +const CATALOG_CACHE_TTL_MS = 10 * 60 * 1000 +const THUMBNAIL_FIELDS = ['$.static_thumb', '$.hero_poster'] + +interface CatalogItem { + uid?: unknown + data?: { + title?: unknown + description?: unknown + slug?: unknown + sort_order?: unknown + website_media?: Record | null + } | null +} + +export function catalogUrl(): string { + const url = new URL('/api/v1/catalog/items', CLOUD_CONFIG.issuer) + url.searchParams.set('type', 'app') + url.searchParams.set('surface', DESKTOP_SURFACE) + url.searchParams.set('limit', '100') + return url.href +} + +function thumbnailUrl(media: Record | null | undefined): string | null { + for (const field of THUMBNAIL_FIELDS) { + const path = media?.[field] + if (typeof path !== 'string' || !path.startsWith(CATALOG_MEDIA_PATH)) continue + const url = new URL(path, CATALOG_MEDIA_ORIGIN) + if (url.origin === CATALOG_MEDIA_ORIGIN && url.pathname.startsWith(CATALOG_MEDIA_PATH)) + return url.href + } + return null +} + +export function toCatalogApp(item: CatalogItem, listUrl: string): CatalogApp | null { + const uid = item.uid + const title = item.data?.title + if (typeof uid !== 'string' || typeof title !== 'string' || title.trim() === '') return null + const slug = item.data?.slug + if (typeof slug !== 'string' || slug.trim() === '') return null + const url = new URL(`#/${encodeURIComponent(slug)}`, listUrl).href + const description = item.data?.description + return { + id: uid, + title, + description: typeof description === 'string' ? description : '', + thumbnailUrl: thumbnailUrl(item.data?.website_media), + url + } +} + +function sortOrder(item: CatalogItem): number { + const order = item.data?.sort_order + return typeof order === 'number' && isFinite(order) ? order : Number.MAX_SAFE_INTEGER +} + +function isCatalogItem(item: unknown): item is CatalogItem { + return typeof item === 'object' && item !== null +} + +async function fetchCatalogApps(listUrl: string): Promise { + const res = await fetch(catalogUrl(), { signal: AbortSignal.timeout(CATALOG_TIMEOUT_MS) }) + if (!res.ok) throw new Error(`Catalog request failed: HTTP ${res.status}`) + const body = (await res.json()) as { items?: unknown } | null + if (!Array.isArray(body?.items)) throw new Error('Catalog response has no item list') + const seen = new Set() + return body.items + .filter(isCatalogItem) + .sort((a, b) => sortOrder(a) - sortOrder(b)) + .map((item) => toCatalogApp(item, listUrl)) + .filter((entry): entry is CatalogApp => { + if (entry === null || seen.has(entry.id)) return false + seen.add(entry.id) + return true + }) +} + +interface CatalogCache { + listUrl: string + loadedAt: number + apps: CatalogApp[] +} + +let cache: CatalogCache | null = null +let inFlight: { listUrl: string; request: Promise } | null = null + +export function listCatalogApps(listUrl: string): Promise { + if (cache?.listUrl === listUrl && Date.now() - cache.loadedAt < CATALOG_CACHE_TTL_MS) + return Promise.resolve(cache.apps) + if (inFlight?.listUrl === listUrl) return inFlight.request + const request = fetchCatalogApps(listUrl) + .then((apps) => { + cache = { listUrl, loadedAt: Date.now(), apps } + return apps + }) + .catch((err: unknown) => { + if (cache?.listUrl === listUrl) return cache.apps + throw err + }) + .finally(() => { + if (inFlight?.request === request) inFlight = null + }) + inFlight = { listUrl, request } + return request +} + +export function _resetCatalogAppsForTest(): void { + cache = null + inFlight = null +} diff --git a/src/main/apps/guards.test.ts b/src/main/apps/guards.test.ts index d0ec608bb..fbf13ad70 100644 --- a/src/main/apps/guards.test.ts +++ b/src/main/apps/guards.test.ts @@ -20,6 +20,7 @@ import { jobsAnswer, DEFAULT_APPS_URL, isAppOpen, + isAppPage, isAppsFrameUrl, isAppSlug, isBuildId, @@ -94,6 +95,21 @@ describe('appsUrl', () => { }) }) +describe('isAppPage', () => { + const list = 'https://apps.example/apps/' + + it('is an app page when it is on the apps site but not the list', () => { + expect(isAppPage('https://apps.example/apps/genjutsu-studio/desktop.html', list)).toBe(true) + }) + + it('is not an app page on the list, even with an app route, or on another site', () => { + expect(isAppPage('https://apps.example/apps/', list)).toBe(false) + expect(isAppPage('https://apps.example/apps/#/genjutsu', list)).toBe(false) + expect(isAppPage('https://other.example/apps/x/desktop.html', list)).toBe(false) + expect(isAppPage('', list)).toBe(false) + }) +}) + describe('isAppOpen', () => { const list = 'https://apps.example/apps/' diff --git a/src/main/apps/guards.ts b/src/main/apps/guards.ts index b12465841..77e66b593 100644 --- a/src/main/apps/guards.ts +++ b/src/main/apps/guards.ts @@ -51,6 +51,16 @@ export function isAppsFrameUrl(frameUrl: string, appsPage: string): boolean { } } +export function isAppPage(pageUrl: string, listUrl: string): boolean { + try { + const page = new URL(pageUrl) + const list = new URL(listUrl) + return page.origin === list.origin && page.pathname !== list.pathname + } catch { + return false + } +} + export function isAppOpen(pageUrl: string, listUrl: string): boolean { let page: URL let list: URL diff --git a/src/main/apps/wiring.test.ts b/src/main/apps/wiring.test.ts index 7c246e39d..6c7daf124 100644 --- a/src/main/apps/wiring.test.ts +++ b/src/main/apps/wiring.test.ts @@ -44,6 +44,19 @@ describe('Apps bridge wiring', () => { expect(main).not.toMatch(/ipcMain\.handle\('comfy-apps:/) }) + it('the only handler outside the apps-page check answers the panel alone', () => { + expect(all(main, /ipcMain\.handle\((\w+)/g)).toEqual(['channel', 'APPS_CATALOG_CHANNEL']) + expect(main).toMatch( + /ipcMain\.handle\(APPS_CATALOG_CHANNEL, \(event\) =>\s+entryForPanel\(event\.sender\)/ + ) + }) + + it('a page says it settled on the channel main relays to the panel', () => { + const settled = main.match(/APPS_PAGE_SETTLED_CHANNEL = '([\w:-]+)'/)![1] + expect(preload).toContain(`ipcRenderer.send('${settled}')`) + expect(main).toContain('ipcMain.on(APPS_PAGE_SETTLED_CHANNEL, relayPageSettled)') + }) + it('a Cloud run takes only the model from the page and the app from its address', () => { expect(preload).toMatch(/const BRIDGE = 3\n/) expect(preload).toContain('run: (workflow: unknown, options?: { model?: string }) =>') @@ -89,7 +102,7 @@ describe('Apps bridge wiring', () => { it('every apps-page check is held to the Apps URL path, not just its origin', () => { const checks = all(main, /isAppsFrameUrl\([^,]+, (\w+)\)/g) - expect(checks).toHaveLength(3) + expect(checks).toHaveLength(4) expect(new Set(checks)).toEqual(new Set(['APPS_URL'])) }) diff --git a/src/preload/api.ts b/src/preload/api.ts index 1388576ff..30e759532 100644 --- a/src/preload/api.ts +++ b/src/preload/api.ts @@ -103,6 +103,7 @@ export function buildElectronApi(): ElectronApi { ipcRenderer.invoke('assets:download', installationId, assetId, fileName), showApps: (rect) => ipcRenderer.send('comfy-apps:show', rect), hideApps: () => ipcRenderer.send('comfy-apps:hide'), + listCatalogApps: () => ipcRenderer.invoke('comfy-apps:catalog'), onAppsLoadFailed: (callback) => { const handler = (_event: IpcRendererEvent, info: unknown) => callback(info as Parameters[0]) @@ -115,6 +116,11 @@ export function buildElectronApi(): ElectronApi { ipcRenderer.on('comfy-apps:app-open', handler) return () => ipcRenderer.removeListener('comfy-apps:app-open', handler) }, + onAppsSettled: (callback) => { + const handler = () => callback() + ipcRenderer.on('comfy-apps:settled', handler) + return () => ipcRenderer.removeListener('comfy-apps:settled', handler) + }, listHomeWorkflows: () => ipcRenderer.invoke('catalog:list-home-workflows'), openCatalogWorkflow: (id) => ipcRenderer.invoke('catalog:open-workflow', id), openCatalogWorkflowHub: () => ipcRenderer.invoke('catalog:open-workflow-hub'), diff --git a/src/preload/appsPreload.ts b/src/preload/appsPreload.ts index d4bb152e2..24996f67b 100644 --- a/src/preload/appsPreload.ts +++ b/src/preload/appsPreload.ts @@ -2,6 +2,8 @@ import { contextBridge, ipcRenderer } from 'electron' import type { IpcRendererEvent } from 'electron' const BRIDGE = 3 +const SETTLE_QUIET_MS = 400 +const SETTLE_MAX_MS = 3000 const CALLS = [ 'getInfo', 'getAuthState', @@ -84,3 +86,28 @@ contextBridge.exposeInMainWorld('comfyDesktopApps', { } } }) + +function reportWhenSettled(): void { + let quiet: ReturnType | undefined + const settle = (): void => { + clearTimeout(quiet) + clearTimeout(cap) + observer.disconnect() + ipcRenderer.send('comfy-apps:page-settled') + } + const wait = (): void => { + clearTimeout(quiet) + quiet = setTimeout(settle, SETTLE_QUIET_MS) + } + const observer = new MutationObserver(wait) + const cap = setTimeout(settle, SETTLE_MAX_MS) + observer.observe(document.documentElement, { + subtree: true, + childList: true, + attributes: true, + characterData: true + }) + wait() +} + +window.addEventListener('DOMContentLoaded', reportWhenSettled, { once: true }) diff --git a/src/renderer/src/panel/AppSidebar.test.ts b/src/renderer/src/panel/AppSidebar.test.ts index 42b5f7894..5dd125012 100644 --- a/src/renderer/src/panel/AppSidebar.test.ts +++ b/src/renderer/src/panel/AppSidebar.test.ts @@ -166,4 +166,31 @@ describe('AppSidebar', () => { const reopened = await mountSidebar() expect(reopened.get('[data-testid="app-sidebar"]').attributes('data-collapsed')).toBe('true') }) + + it('folds while an app is open without changing the saved setting', async () => { + window.localStorage.setItem('comfy.desktop.sidebar.collapsed', 'false') + const wrapper = await mountSidebar() + const sidebar = () => wrapper.get('[data-testid="app-sidebar"]') + + await wrapper.setProps({ folded: true }) + expect(sidebar().attributes('data-collapsed')).toBe('true') + expect(sidebar().attributes('style')).toContain('width: 72px') + expect(window.localStorage.getItem('comfy.desktop.sidebar.collapsed')).toBe('false') + + await wrapper.setProps({ folded: false }) + expect(sidebar().attributes('data-collapsed')).toBe('false') + expect(sidebar().attributes('style')).toContain('width: 244px') + }) + + it('expands from the toggle while folded, even when saved as collapsed', async () => { + window.localStorage.setItem('comfy.desktop.sidebar.collapsed', 'true') + const wrapper = await mountSidebar() + await wrapper.setProps({ folded: true }) + + await wrapper.get('[data-testid="app-sidebar-toggle"]').trigger('click') + await flushPromises() + + expect(wrapper.get('[data-testid="app-sidebar"]').attributes('data-collapsed')).toBe('false') + expect(window.localStorage.getItem('comfy.desktop.sidebar.collapsed')).toBe('false') + }) }) diff --git a/src/renderer/src/panel/AppSidebar.vue b/src/renderer/src/panel/AppSidebar.vue index 3f66fa9ac..50b7d0742 100644 --- a/src/renderer/src/panel/AppSidebar.vue +++ b/src/renderer/src/panel/AppSidebar.vue @@ -1,5 +1,5 @@ + + diff --git a/src/renderer/src/panel/AppsHost.vue b/src/renderer/src/panel/AppsHost.vue index b5f0cc8a2..be5825e49 100644 --- a/src/renderer/src/panel/AppsHost.vue +++ b/src/renderer/src/panel/AppsHost.vue @@ -3,28 +3,52 @@ import { computed, onBeforeUnmount, onMounted, useTemplateRef, watch } from 'vue import { useDialogs } from '../composables/useDialogs' import { useModal } from '../composables/useModal' -const props = defineProps<{ obscured?: boolean }>() +const SLIDE_MS = 480 +const EASE_OUT_CUBIC = 'cubic-bezier(0.33, 1, 0.68, 1)' +const HOLD_MAX_MS = 1000 +const REVEAL_MAX_MS = 4000 + +const { + url, + growBy = 0, + obscured = false +} = defineProps<{ + url: string + growBy?: number + obscured?: boolean +}>() const emit = defineEmits<{ failed: [url: string] 'app-open': [open: boolean] + entered: [] }>() const slot = useTemplateRef('slot') +const surface = useTemplateRef('surface') let observer: ResizeObserver | null = null let stopListening: (() => void) | null = null let stopAppOpen: (() => void) | null = null +let stopSettled: (() => void) | null = null +let parked = true +let gone = false +let holdWidth = 0 +let holdTimer: ReturnType | undefined +let revealTimer: ReturnType | undefined let freshPending = true const { state: modal } = useModal() const { state: dialog } = useDialogs() -const hidden = computed(() => props.obscured === true || modal.visible || dialog.open) +const hidden = computed(() => obscured || modal.visible || dialog.open) function report(): void { const el = slot.value - if (!el || hidden.value) return + if (!el || gone || hidden.value) return const r = el.getBoundingClientRect() - window.api.showApps({ x: r.x, y: r.y, width: r.width, height: r.height, fresh: freshPending }) + if (holdWidth && r.width >= holdWidth) holdWidth = 0 + const width = Math.max(r.width, holdWidth) + const x = parked ? window.innerWidth : r.x + window.api.showApps({ x, y: r.y, width, height: r.height, fresh: freshPending, url }) freshPending = false } @@ -32,18 +56,59 @@ watch(hidden, (isHidden) => (isHidden ? window.api.hideApps() : report())) const reportResize = (): void => report() -onMounted(() => { - stopListening = window.api.onAppsLoadFailed(({ url }) => emit('failed', url)) +function slideIn(): Promise { + const animation = surface.value?.animate( + [{ transform: 'translateX(100%)' }, { transform: 'none' }], + { duration: SLIDE_MS, easing: EASE_OUT_CUBIC } + ) + return animation + ? animation.finished.then( + () => {}, + () => {} + ) + : Promise.resolve() +} + +function pageSettled(): Promise { + return new Promise((done) => { + stopSettled = window.api.onAppsSettled(done) + revealTimer = setTimeout(done, REVEAL_MAX_MS) + }) +} + +function releaseHoldLater(): void { + holdTimer = setTimeout(() => { + holdWidth = 0 + report() + }, HOLD_MAX_MS) +} + +onMounted(async () => { + stopListening = window.api.onAppsLoadFailed(({ url: failed }) => emit('failed', failed)) stopAppOpen = window.api.onAppsAppOpen(({ open }) => emit('app-open', open)) + const settled = pageSettled() observer = new ResizeObserver(reportResize) if (slot.value) observer.observe(slot.value) window.addEventListener('resize', reportResize) + const width = slot.value?.getBoundingClientRect().width ?? 0 + if (growBy > 0) holdWidth = width + growBy + report() + if (!window.matchMedia('(prefers-reduced-motion: reduce)').matches) await slideIn() + if (gone) return + emit('entered') + if (holdWidth) releaseHoldLater() + await settled + parked = false report() }) onBeforeUnmount(() => { + gone = true + clearTimeout(holdTimer) + clearTimeout(revealTimer) stopListening?.() stopAppOpen?.() + stopSettled?.() observer?.disconnect() window.removeEventListener('resize', reportResize) window.api.hideApps() @@ -51,5 +116,11 @@ onBeforeUnmount(() => { diff --git a/src/renderer/src/panel/AppsView.test.ts b/src/renderer/src/panel/AppsView.test.ts index 953363a30..8303540ce 100644 --- a/src/renderer/src/panel/AppsView.test.ts +++ b/src/renderer/src/panel/AppsView.test.ts @@ -2,18 +2,39 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount } from '@vue/test-utils' import AppsView from './AppsView.vue' +import type { CatalogApp } from '../types/ipc' import { useModal } from '../composables/useModal' const showApps = vi.fn() const hideApps = vi.fn() +const listCatalogApps = vi.fn() let appsLoadFailed: ((info: { url: string }) => void) | null = null let appsAppOpen: ((info: { open: boolean }) => void) | null = null +let appsSettled: (() => void) | null = null + +async function mountApps(props: { growBy?: number; obscured?: boolean } = {}) { + const wrapper = mount(AppsView, { props }) + await flushPromises() + return wrapper +} + +function catalogApp(id: string, title: string): CatalogApp { + return { + id, + title, + description: `${title} description`, + thumbnailUrl: `https://comfy.org/catalog-media/${id}/v1`, + url: `https://apps.example/apps/${id}/desktop.html` + } +} beforeEach(() => { vi.clearAllMocks() + listCatalogApps.mockResolvedValue([]) ;(window as unknown as { api: unknown }).api = { showApps, hideApps, + listCatalogApps, onAppsLoadFailed: vi.fn((callback: (info: { url: string }) => void) => { appsLoadFailed = callback return () => (appsLoadFailed = null) @@ -21,49 +42,198 @@ beforeEach(() => { onAppsAppOpen: vi.fn((callback: (info: { open: boolean }) => void) => { appsAppOpen = callback return () => (appsAppOpen = null) + }), + onAppsSettled: vi.fn((callback: () => void) => { + appsSettled = callback + return () => (appsSettled = null) }) } }) describe('AppsView', () => { - it('shows the hosted Apps page', async () => { - const wrapper = mount(AppsView) - await flushPromises() + it('shows the app cards from the catalog', async () => { + listCatalogApps.mockResolvedValue([ + catalogApp('studio', 'Cinematic Studio'), + catalogApp('reshoot', 'Re-shoot a video') + ]) + const wrapper = await mountApps() - expect(wrapper.find('[data-testid="apps-host"]').exists()).toBe(true) - expect(showApps).toHaveBeenCalledWith(expect.objectContaining({ fresh: true })) + const cards = wrapper.findAll('[data-testid^="app-card-"]') + expect(cards.map((card) => card.find('span span').text())).toEqual([ + 'Cinematic Studio', + 'Re-shoot a video' + ]) + expect(cards[0]!.text()).toContain('Cinematic Studio description') + expect(cards[0]!.get('img').attributes('src')).toBe('https://comfy.org/catalog-media/studio/v1') + expect(showApps).not.toHaveBeenCalled() }) - it('shows the address when the apps page cannot load', async () => { - const wrapper = mount(AppsView) + it('opens an app from its card over the whole page and returns to the cards when the app goes back', async () => { + listCatalogApps.mockResolvedValue([catalogApp('studio', 'Cinematic Studio')]) + const wrapper = await mountApps() + + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') await flushPromises() - appsLoadFailed!({ url: 'https://apps.example/apps/' }) + expect(wrapper.find('[data-testid="apps-host"]').exists()).toBe(true) + expect(showApps).toHaveBeenCalledWith( + expect.objectContaining({ + fresh: true, + url: 'https://apps.example/apps/studio/desktop.html' + }) + ) + await vi.waitFor(() => expect(wrapper.emitted('app-entered')).toEqual([[true]])) + expect(wrapper.find('h1').exists()).toBe(false) + + appsAppOpen!({ open: true }) await flushPromises() + expect(wrapper.find('[data-testid="apps-host"]').exists()).toBe(true) + appsAppOpen!({ open: false }) + await flushPromises() expect(wrapper.find('[data-testid="apps-host"]').exists()).toBe(false) - expect(wrapper.get('[data-testid="apps-load-failed"]').text()).toContain( - 'https://apps.example/apps/' - ) expect(hideApps).toHaveBeenCalled() + expect(wrapper.find('h1').exists()).toBe(true) + expect(wrapper.find('[data-testid="app-card-studio"]').exists()).toBe(true) + expect(listCatalogApps).toHaveBeenCalledOnce() }) - it('hides the Apps title while an app fills the page, and shows it on the list again', async () => { - const wrapper = mount(AppsView) + it('opens a second app picked while the first one is still sliding in', async () => { + listCatalogApps.mockResolvedValue([ + catalogApp('studio', 'Cinematic Studio'), + catalogApp('reshoot', 'Re-shoot a video') + ]) + const wrapper = await mountApps() + + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') + await flushPromises() + await wrapper.get('[data-testid="app-card-reshoot"]').trigger('click') await flushPromises() - appsAppOpen!({ open: true }) + expect(showApps).toHaveBeenCalledWith( + expect.objectContaining({ + fresh: true, + url: 'https://apps.example/apps/reshoot/desktop.html' + }) + ) + }) + + it('slides in an empty surface and keeps the app off screen until its page settles', async () => { + listCatalogApps.mockResolvedValue([catalogApp('studio', 'Cinematic Studio')]) + const wrapper = await mountApps({ growBy: 172 }) + + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') await flushPromises() + + const slot = wrapper.get('[data-testid="apps-host"]').element.getBoundingClientRect() + const first = showApps.mock.calls[0]![0] + expect(first.fresh).toBe(true) + expect(first.x).toBe(window.innerWidth) + expect(Math.round(first.width)).toBe(Math.round(slot.width + 172)) + expect(wrapper.find('[data-testid="apps-host-surface"]').exists()).toBe(true) + expect(wrapper.get('[data-testid="app-card-studio"]').isVisible()).toBe(true) + expect(wrapper.find('h1').exists()).toBe(true) + + await vi.waitFor(() => expect(wrapper.emitted('app-entered')).toEqual([[true]])) expect(wrapper.find('h1').exists()).toBe(false) - expect(wrapper.find('[data-testid="apps-host"]').exists()).toBe(true) + expect(showApps.mock.calls.every(([rect]) => rect.x === window.innerWidth)).toBe(true) - appsAppOpen!({ open: false }) + appsSettled!() + await flushPromises() + const last = showApps.mock.calls.at(-1)![0] + expect(Math.round(last.x)).toBe(Math.round(slot.x)) + }) + + it('shows the app anyway when its page never says it settled', async () => { + listCatalogApps.mockResolvedValue([catalogApp('studio', 'Cinematic Studio')]) + const matchMedia = vi + .spyOn(window, 'matchMedia') + .mockImplementation((query) => ({ matches: query.includes('reduce') }) as MediaQueryList) + vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] }) + try { + const wrapper = await mountApps() + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') + await flushPromises() + expect(showApps.mock.calls.at(-1)![0].x).toBe(window.innerWidth) + + await vi.advanceTimersByTimeAsync(4000) + const slot = wrapper.get('[data-testid="apps-host"]').element.getBoundingClientRect() + expect(Math.round(showApps.mock.calls.at(-1)![0].x)).toBe(Math.round(slot.x)) + } finally { + vi.useRealTimers() + matchMedia.mockRestore() + } + }) + + it('shows an app in place without sliding when motion is reduced', async () => { + listCatalogApps.mockResolvedValue([catalogApp('studio', 'Cinematic Studio')]) + const matchMedia = vi + .spyOn(window, 'matchMedia') + .mockImplementation((query) => ({ matches: query.includes('reduce') }) as MediaQueryList) + try { + const wrapper = await mountApps() + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') + await flushPromises() + + expect(wrapper.emitted('app-entered')).toEqual([[true]]) + appsSettled!() + await flushPromises() + const slot = wrapper.get('[data-testid="apps-host"]').element.getBoundingClientRect() + expect(Math.round(showApps.mock.calls.at(-1)![0].x)).toBe(Math.round(slot.x)) + } finally { + matchMedia.mockRestore() + } + }) + + it('shows the address when an app cannot load, and opens it again from its card', async () => { + listCatalogApps.mockResolvedValue([catalogApp('studio', 'Cinematic Studio')]) + const wrapper = await mountApps() + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') + await flushPromises() + + appsLoadFailed!({ url: 'https://apps.example/apps/studio/desktop.html' }) + await flushPromises() + + expect(wrapper.find('[data-testid="apps-host"]').exists()).toBe(false) + expect(wrapper.get('[data-testid="apps-load-failed"]').text()).toContain( + 'https://apps.example/apps/studio/desktop.html' + ) + + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') await flushPromises() - expect(wrapper.find('h1').text()).toBe('Apps') + expect(wrapper.find('[data-testid="apps-host"]').exists()).toBe(true) + expect(wrapper.find('[data-testid="apps-load-failed"]').exists()).toBe(false) + }) + + it('shows Coming soon for an empty catalog and says so when it cannot be reached', async () => { + const empty = await mountApps() + expect(empty.get('[data-testid="apps-coming-soon"]').text()).toBe('Coming soon') + empty.unmount() + + listCatalogApps.mockRejectedValue(new Error('offline')) + const offline = await mountApps() + expect(offline.get('[data-testid="apps-catalog-failed"]').text()).toBe( + "Couldn't load the apps." + ) + expect(offline.find('[data-testid="apps-coming-soon"]').exists()).toBe(false) }) - it('hides the native Apps page under a takeover and shows it again after', async () => { - const wrapper = mount(AppsView) + it('tells the page owner the app closed when the page goes away with an app open', async () => { + listCatalogApps.mockResolvedValue([catalogApp('studio', 'Cinematic Studio')]) + const wrapper = await mountApps() + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') + await vi.waitFor(() => expect(wrapper.emitted('app-entered')).toEqual([[true]])) + + wrapper.unmount() + + expect(wrapper.emitted('app-open')!.at(-1)).toEqual([false]) + expect(wrapper.emitted('app-entered')!.at(-1)).toEqual([false]) + }) + + it('hides an open app under a takeover and shows it again after', async () => { + listCatalogApps.mockResolvedValue([catalogApp('studio', 'Cinematic Studio')]) + const wrapper = await mountApps() + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') await flushPromises() showApps.mockClear() @@ -74,8 +244,10 @@ describe('AppsView', () => { expect(showApps).toHaveBeenCalledWith(expect.objectContaining({ fresh: false })) }) - it('hides the native Apps page while a panel dialog is open', async () => { - mount(AppsView) + it('hides an open app while a panel dialog is open', async () => { + listCatalogApps.mockResolvedValue([catalogApp('studio', 'Cinematic Studio')]) + const wrapper = await mountApps() + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') await flushPromises() const modal = useModal() @@ -87,4 +259,17 @@ describe('AppsView', () => { await flushPromises() expect(showApps).toHaveBeenLastCalledWith(expect.objectContaining({ fresh: false })) }) + + it('opens an app picked under a takeover once the takeover closes', async () => { + listCatalogApps.mockResolvedValue([catalogApp('studio', 'Cinematic Studio')]) + const wrapper = await mountApps({ obscured: true }) + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') + await flushPromises() + expect(showApps).not.toHaveBeenCalled() + + await wrapper.setProps({ obscured: false }) + expect(showApps).toHaveBeenCalledWith( + expect.objectContaining({ fresh: true, url: 'https://apps.example/apps/studio/desktop.html' }) + ) + }) }) diff --git a/src/renderer/src/panel/AppsView.vue b/src/renderer/src/panel/AppsView.vue index d91e1cac5..4921c0811 100644 --- a/src/renderer/src/panel/AppsView.vue +++ b/src/renderer/src/panel/AppsView.vue @@ -1,39 +1,76 @@ diff --git a/src/renderer/src/panel/PanelApp.test.ts b/src/renderer/src/panel/PanelApp.test.ts index 7edb5bb5f..4da8b787b 100644 --- a/src/renderer/src/panel/PanelApp.test.ts +++ b/src/renderer/src/panel/PanelApp.test.ts @@ -20,6 +20,7 @@ vi.mock('../main', () => ({ vi.mock('../composables/useTheme', () => ({ useTheme: () => ({ theme: 'dark' }) })) const mockModal = { + state: { visible: false }, alert: vi.fn(), confirm: vi.fn(), close: vi.fn() @@ -1251,6 +1252,86 @@ describe('PanelApp', () => { expect(wrapper.get('[data-testid="chooser-view"]').attributes('data-visible')).toBe('true') }) + it('closes an open app when Apps is clicked in the sidebar, and otherwise keeps the page', async () => { + window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') + const listCatalogApps = vi.fn().mockResolvedValue([ + { + id: 'studio', + title: 'Cinematic Studio', + description: '', + thumbnailUrl: null, + url: 'https://apps.example/apps/studio/desktop.html' + } + ]) + Object.assign(window.api, { + listCatalogApps, + getAppsEnabled: vi.fn().mockResolvedValue(true), + showApps: vi.fn(), + hideApps: vi.fn(), + onAppsLoadFailed: vi.fn(() => () => {}), + onAppsAppOpen: vi.fn(() => () => {}), + onAppsSettled: vi.fn(() => () => {}) + }) + const wrapper = mountPanel() + await flushPromises() + const clickApps = async () => { + await wrapper.get('[data-testid="app-sidebar-apps"]').trigger('click') + await flushPromises() + } + + await clickApps() + await clickApps() + expect(listCatalogApps).toHaveBeenCalledOnce() + + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') + await flushPromises() + expect(wrapper.find('[data-testid="apps-host"]').exists()).toBe(true) + + await clickApps() + expect(wrapper.find('[data-testid="apps-host"]').exists()).toBe(false) + expect(wrapper.get('[data-testid="app-card-studio"]').isVisible()).toBe(true) + }) + + it('unfolds the sidebar when another page is picked while an app is open', async () => { + window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') + const matchMedia = vi + .spyOn(window, 'matchMedia') + .mockImplementation((query) => ({ matches: query.includes('reduce') }) as MediaQueryList) + Object.assign(window.api, { + listCatalogApps: vi.fn().mockResolvedValue([ + { + id: 'studio', + title: 'Cinematic Studio', + description: '', + thumbnailUrl: null, + url: 'https://apps.example/apps/studio/desktop.html' + } + ]), + getAppsEnabled: vi.fn().mockResolvedValue(true), + showApps: vi.fn(), + hideApps: vi.fn(), + onAppsLoadFailed: vi.fn(() => () => {}), + onAppsAppOpen: vi.fn(() => () => {}), + onAppsSettled: vi.fn(() => () => {}) + }) + try { + const wrapper = mountPanel() + await flushPromises() + await wrapper.get('[data-testid="app-sidebar-apps"]').trigger('click') + await flushPromises() + await wrapper.get('[data-testid="app-card-studio"]').trigger('click') + await flushPromises() + expect(wrapper.get('[data-testid="app-sidebar"]').attributes('data-collapsed')).toBe('true') + + await wrapper.get('[data-testid="app-sidebar-home"]').trigger('click') + await flushPromises() + + expect(wrapper.get('[data-testid="app-sidebar"]').attributes('data-collapsed')).toBe('false') + } finally { + matchMedia.mockRestore() + } + }) + it('opens on Home, with the list of builds hidden until Builds is picked', async () => { window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') const wrapper = mountPanel() diff --git a/src/renderer/src/panel/PanelApp.vue b/src/renderer/src/panel/PanelApp.vue index 5fbddc707..c7119f43f 100644 --- a/src/renderer/src/panel/PanelApp.vue +++ b/src/renderer/src/panel/PanelApp.vue @@ -1,6 +1,7 @@