From 9166ad48c4040d97c2732a7c1ba7f4850059c4fa Mon Sep 17 00:00:00 2001 From: PabloWiedemann Date: Mon, 16 Nov 2026 11:54:01 -0800 Subject: [PATCH 1/4] feat(apps): show an app page's outputs in Desktop's output dialog An app page can now ask Desktop to show its outputs through a new bridge, window.comfyDesktopOutputs. showOutput({ items, index }) takes each output's name, media type, bytes, tags and whether it has a saved workflow, the same payload the Apps pages send today. Desktop opens the output dialog that Assets uses over the Apps view, with the outputs and the one to show first. Main checks the request the way it checks other page calls: only from the hosted Apps page, while Apps is on, and only for the page document that is still loaded. It takes at most 40 outputs and 100 MB in all, and only image, video and audio types the dialog can show. A request it cannot show is rejected; it resolves false while the app is not on screen. Download, and Open workflow for an output with a saved workflow, go back to the page on onOutputAction, so the page uses its own download link and its own Open in ComfyUI. Main sends an action only to the page document that showed the outputs, and only for an index it sent. The dialog covers the app page, so the page host swaps the app for its still frame as it does for any cover. When the dialog closes, the app gets the keyboard back. The panel CSP allows blob: for the output previews. This is the Apps half of #2008, rebuilt on main's Apps page. The Assets half landed in #2140. --- locales/en.json | 1 + locales/zh.json | 1 + src/main/apps/appPageView.test.ts | 135 ++++++++++++++++++ src/main/apps/appPageView.ts | 50 ++++++- src/main/apps/outputRequest.test.ts | 125 ++++++++++++++++ src/main/apps/outputRequest.ts | 63 ++++++++ src/main/lib/ipc/index.ts | 2 + src/main/lib/ipc/registerAppsHandlers.ts | 13 +- .../ipc/registerAppsOutputsHandlers.test.ts | 112 +++++++++++++++ .../lib/ipc/registerAppsOutputsHandlers.ts | 28 ++++ src/preload/api.ts | 15 +- src/preload/appsPreload.ts | 15 +- src/renderer/csp.test.ts | 5 + src/renderer/panel.html | 2 +- src/renderer/src/panel/AppPageHost.test.ts | 129 ++++++++++++++++- src/renderer/src/panel/AppPageHost.vue | 78 +++++++++- src/renderer/src/panel/AppsView.test.ts | 1 + src/renderer/src/panel/OutputDetailDialog.vue | 26 +++- src/renderer/src/panel/PanelApp.test.ts | 1 + src/renderer/src/preview/fakeBridge.ts | 2 + src/renderer/src/types/ipc.ts | 2 + src/types/comfyDesktopBridge.ts | 46 ++++++ src/types/ipc.ts | 21 +++ 23 files changed, 858 insertions(+), 15 deletions(-) create mode 100644 src/main/apps/outputRequest.test.ts create mode 100644 src/main/apps/outputRequest.ts create mode 100644 src/main/lib/ipc/registerAppsOutputsHandlers.test.ts create mode 100644 src/main/lib/ipc/registerAppsOutputsHandlers.ts diff --git a/locales/en.json b/locales/en.json index fd076b3c9..a4300ce03 100644 --- a/locales/en.json +++ b/locales/en.json @@ -120,6 +120,7 @@ "fit": "Fit", "zoom": "Zoom", "zoomLevel": "Zoom: {level}", + "openWorkflow": "Open workflow", "savedIn": "Saved in" }, "kindSingular": { diff --git a/locales/zh.json b/locales/zh.json index 8c605a76c..e3aba74eb 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -120,6 +120,7 @@ "fit": "适应", "zoom": "缩放", "zoomLevel": "缩放:{level}", + "openWorkflow": "打开工作流", "savedIn": "保存位置" }, "kindSingular": { diff --git a/src/main/apps/appPageView.test.ts b/src/main/apps/appPageView.test.ts index 68e2a28de..736b19656 100644 --- a/src/main/apps/appPageView.test.ts +++ b/src/main/apps/appPageView.test.ts @@ -2,6 +2,8 @@ import { EventEmitter } from 'node:events' import { beforeEach, describe, expect, it, vi } from 'vitest' import type { IpcMainEvent, WebContents, WebFrameMain } from 'electron' +import type { AppOutputs } from '../../types/ipc' + type PermissionCheck = (contents: unknown, permission: string, requestingOrigin: string) => boolean type PermissionRequest = ( contents: unknown, @@ -60,6 +62,8 @@ vi.mock('./appsConfig', () => ({ import { _resetForTest as forgetAppsPages, isAppsPageSender, liveAppsPages } from './appsPages' import { APP_OPENED_EVENT, + APP_OUTPUT_ACTION_CHANNEL, + APP_OUTPUTS_CHANNEL, APP_PAGE_CLOSED_CHANNEL, APP_PAGE_FAILED_EVENT, APP_PAGE_STATUS_CHANNEL, @@ -67,6 +71,8 @@ import { captureAppPage, hideAppPage, loadedAppPage, + sendAppOutputAction, + showAppOutputs, showAppPage } from './appPageView' import { trackFromAppsPage } from '../lib/ipc/registerAppsTelemetryHandlers' @@ -132,6 +138,7 @@ class FakeView { class FakeWindow extends EventEmitter { stack: unknown[] = [] + focused = true contentView = { get children(): unknown[] { return [...stackOf(this)] @@ -146,6 +153,9 @@ class FakeWindow extends EventEmitter { isDestroyed(): boolean { return false } + isFocused(): boolean { + return this.focused + } } const windowsByContentView = new WeakMap() @@ -975,3 +985,128 @@ describe('usage events from a hosted page that switches apps', () => { expect(runsSent()).toEqual([]) }) }) + +describe('outputs an app page shows', () => { + const OUTPUTS = { + items: [ + { + name: 'cutout-1.png', + mimeType: 'image/png', + bytes: new Uint8Array([1]), + tags: [], + workflow: true + }, + { + name: 'cutout-2.png', + mimeType: 'image/png', + bytes: new Uint8Array([2]), + tags: [], + workflow: false + } + ], + index: 0 + } as AppOutputs + + function pageContents(): WebContents { + return hostedPage().webContents as unknown as WebContents + } + + function shownFromPage(): boolean { + return showAppOutputs(pageContents(), loadedAppPage(pageContents())!, OUTPUTS) + } + + function sentToPage(): unknown[][] { + return hostedPage().webContents.send.mock.calls.filter( + ([channel]) => channel === APP_OUTPUT_ACTION_CHANNEL + ) + } + + beforeEach(async () => { + await openCutout() + finishLoading() + }) + + it('hands them to the panel with the app they came from, and gives the panel the keyboard', () => { + expect(shownFromPage()).toBe(true) + + expect(sentToPanel(APP_OUTPUTS_CHANNEL)).toEqual([[APP_OUTPUTS_CHANNEL, CUTOUT, OUTPUTS]]) + expect(panel.webContents.focus).toHaveBeenCalledTimes(1) + }) + + it('leaves the keyboard alone while the window is in the background', () => { + hostWindow.focused = false + + expect(shownFromPage()).toBe(true) + expect(panel.webContents.focus).not.toHaveBeenCalled() + }) + + it('shows nothing while the app is not on screen', () => { + hideAppPage(panelContents()) + + expect(shownFromPage()).toBe(false) + expect(sentToPanel(APP_OUTPUTS_CHANNEL)).toEqual([]) + }) + + it('shows nothing from a page that was replaced while Desktop checked it', () => { + const sentFrom = loadedAppPage(pageContents())! + finishLoading() + + expect(showAppOutputs(pageContents(), sentFrom, OUTPUTS)).toBe(false) + expect(sentToPanel(APP_OUTPUTS_CHANNEL)).toEqual([]) + }) + + it('shows nothing for contents Desktop does not host', () => { + const other = new FakeContents() as unknown as WebContents + + expect(showAppOutputs(other, { appId: CUTOUT, document: 1 }, OUTPUTS)).toBe(false) + }) + + it('sends Download and Open workflow back to the page that showed the outputs', () => { + shownFromPage() + + sendAppOutputAction(panelContents(), { action: 'download', index: 1 }) + sendAppOutputAction(panelContents(), { action: 'open-workflow', index: 0 }) + + expect(sentToPage()).toEqual([ + [APP_OUTPUT_ACTION_CHANNEL, { action: 'download', index: 1 }], + [APP_OUTPUT_ACTION_CHANNEL, { action: 'open-workflow', index: 0 }] + ]) + }) + + it('drops an action before any outputs were shown, and one for an output the page did not send', () => { + sendAppOutputAction(panelContents(), { action: 'download', index: 0 }) + shownFromPage() + sendAppOutputAction(panelContents(), { action: 'download', index: 2 }) + + expect(sentToPage()).toEqual([]) + }) + + it('drops an action once the page that showed the outputs is replaced', () => { + shownFromPage() + finishLoading() + + sendAppOutputAction(panelContents(), { action: 'download', index: 0 }) + + expect(sentToPage()).toEqual([]) + }) + + it('takes actions only from the panel of the window the page is in', () => { + shownFromPage() + + sendAppOutputAction(new FakeContents() as unknown as WebContents, { + action: 'download', + index: 0 + }) + + expect(sentToPage()).toEqual([]) + }) + + it('gives the page the keyboard when the panel asks for it, and not on a plain resize', async () => { + await openCutout() + expect(hostedPage().webContents.focus).not.toHaveBeenCalled() + + await openCutout({ ...PLACEMENT, focus: true }) + + expect(hostedPage().webContents.focus).toHaveBeenCalledTimes(1) + }) +}) diff --git a/src/main/apps/appPageView.ts b/src/main/apps/appPageView.ts index d51c47db4..059e33d1d 100644 --- a/src/main/apps/appPageView.ts +++ b/src/main/apps/appPageView.ts @@ -4,7 +4,7 @@ import path from 'path' import { isAppsListUrl, isInAppsScope, isWebUrl } from '../../shared/appsLocation' import type { AppsLocation } from '../../shared/appsLocation' -import type { AppPagePlacement, AppPageStatus } from '../../types/ipc' +import type { AppOutputAction, AppOutputs, AppPagePlacement, AppPageStatus } from '../../types/ipc' import { comfyWindows } from '../host/registry' import type { ComfyWindowEntry } from '../host/registry' import { attachSessionDownloadHandler } from '../lib/comfyDownloadManager' @@ -20,6 +20,8 @@ import { registerAppsPage } from './appsPages' export const APPS_PARTITION = 'persist:comfy-apps' export const APP_PAGE_STATUS_CHANNEL = 'apps:page-status-changed' export const APP_PAGE_CLOSED_CHANNEL = 'apps:page-closed' +export const APP_OUTPUTS_CHANNEL = 'apps:outputs' +export const APP_OUTPUT_ACTION_CHANNEL = 'apps-outputs:action' export const APP_OPENED_EVENT = 'comfy.desktop.apps.app_opened' export const APP_PAGE_FAILED_EVENT = 'comfy.desktop.apps.app_page_failed' @@ -34,6 +36,11 @@ export interface LoadedAppPage { document: number } +interface ShownOutputs { + page: LoadedAppPage + count: number +} + interface HostedPage { view: WebContentsView appId: string | null @@ -45,6 +52,7 @@ interface HostedPage { status: AppPageStatus wanted: boolean focusWhenReady: boolean + outputs: ShownOutputs | null forget: () => void } @@ -238,6 +246,7 @@ function createHostedPage({ windowKey, entry }: PageHost, location: AppsLocation status: 'loading', wanted: false, focusWhenReady: false, + outputs: null, forget: registerAppsPage(contents) } const steer = (event: Event, url: string, pageStillShowsApp: boolean): void => { @@ -312,7 +321,10 @@ function isMeasure(value: unknown): value is number { function parsePlacement(raw: unknown): AppPagePlacement | null { if (!raw || typeof raw !== 'object') return null - const { x, y, width, height, cornerRadius, justOpened, retry } = raw as Record + const { x, y, width, height, cornerRadius, justOpened, retry, focus } = raw as Record< + string, + unknown + > if (!isMeasure(x) || !isMeasure(y) || !isMeasure(width) || !isMeasure(height)) return null if (!isMeasure(cornerRadius)) return null return { @@ -322,7 +334,8 @@ function parsePlacement(raw: unknown): AppPagePlacement | null { height, cornerRadius, justOpened: justOpened === true, - retry: retry === true + retry: retry === true, + focus: focus === true } } @@ -392,7 +405,7 @@ export async function showAppPage( } if (placement.justOpened) mainTelemetry.capture(APP_OPENED_EVENT, { app_id: appId }) hosted.wanted = true - hosted.focusWhenReady = hosted.focusWhenReady || askedFresh + hosted.focusWhenReady = hosted.focusWhenReady || askedFresh || placement.focus === true place(host.entry, hosted.view, placement) raiseAbovePanel(host.entry, hosted.view) applyVisibility(hosted) @@ -417,6 +430,35 @@ export async function captureAppPage(panel: WebContents): Promise } } +function isStillLoaded(hosted: HostedPage, page: LoadedAppPage): boolean { + return hosted.loaded?.appId === page.appId && hosted.loaded.document === page.document +} + +export function showAppOutputs( + contents: WebContents, + sentFrom: LoadedAppPage, + outputs: AppOutputs +): boolean { + for (const [windowKey, hosted] of hostedByWindow) { + if (hosted.view.webContents !== contents) continue + const panel = panelOf(windowKey) + if (!panel || !isOnScreen(hosted) || !isStillLoaded(hosted, sentFrom)) return false + hosted.outputs = { page: sentFrom, count: outputs.items.length } + if (comfyWindows.get(windowKey)?.window.isFocused()) panel.focus() + panel.send(APP_OUTPUTS_CHANNEL, sentFrom.appId, outputs) + return true + } + return false +} + +export function sendAppOutputAction(panel: WebContents, action: AppOutputAction): void { + const host = hostOf(panel) + const hosted = host && hostedFor(host) + const shown = hosted?.outputs + if (!hosted || !shown || action.index >= shown.count || !isStillLoaded(hosted, shown.page)) return + hosted.view.webContents.send(APP_OUTPUT_ACTION_CHANNEL, action) +} + export function _resetForTest(): void { hostedByWindow.clear() } diff --git a/src/main/apps/outputRequest.test.ts b/src/main/apps/outputRequest.test.ts new file mode 100644 index 000000000..7632e9cb6 --- /dev/null +++ b/src/main/apps/outputRequest.test.ts @@ -0,0 +1,125 @@ +import { describe, expect, it } from 'vitest' + +import { + MAX_OUTPUTS, + MAX_OUTPUTS_BYTES, + readOutputAction, + readOutputRequest +} from './outputRequest' + +const picture = (extra: Record = {}): Record => ({ + name: 'picture-1.png', + mimeType: 'image/png', + bytes: new Uint8Array([1, 2, 3]), + ...extra +}) + +describe('readOutputRequest', () => { + it('keeps the outputs and the one to show first', () => { + const request = readOutputRequest({ + items: [ + picture({ tags: ['Upscaled', 7, ' '], workflow: true }), + picture({ name: ' clip.mp4 ', mimeType: 'video/mp4' }) + ], + index: 1 + }) + + expect(request?.index).toBe(1) + expect(request?.items[0]).toMatchObject({ + name: 'picture-1.png', + tags: ['Upscaled'], + workflow: true + }) + expect(request?.items[1]).toMatchObject({ name: 'clip.mp4', tags: [], workflow: false }) + }) + + it('keeps what the Apps pages send today and drops the fields Desktop does not show', () => { + const request = readOutputRequest({ + items: [picture({ tags: [], workflow: false, savedOn: 'computer', folder: '/tmp' })], + index: 0 + }) + + expect(request?.items[0]).toEqual({ + name: 'picture-1.png', + mimeType: 'image/png', + bytes: new Uint8Array([1, 2, 3]), + tags: [], + workflow: false + }) + }) + + it('cuts long names and tags, and keeps at most eight tags', () => { + const request = readOutputRequest({ + items: [ + picture({ name: 'n'.repeat(300), tags: Array.from({ length: 12 }, () => 't'.repeat(60)) }) + ], + index: 0 + }) + + expect(request?.items[0]?.name).toHaveLength(200) + expect(request?.items[0]?.tags).toHaveLength(8) + expect(request?.items[0]?.tags[0]).toHaveLength(40) + }) + + it('refuses outputs it cannot show', () => { + expect(readOutputRequest(null)).toBeNull() + expect(readOutputRequest({ items: [], index: 0 })).toBeNull() + expect(readOutputRequest({ items: [picture()], index: 1 })).toBeNull() + expect(readOutputRequest({ items: [picture()], index: -1 })).toBeNull() + expect(readOutputRequest({ items: [picture()], index: 0.5 })).toBeNull() + expect(readOutputRequest({ items: [picture({ mimeType: 'text/html' })], index: 0 })).toBeNull() + expect( + readOutputRequest({ items: [picture({ mimeType: 'image/svg+xml' })], index: 0 }) + ).toBeNull() + expect(readOutputRequest({ items: [picture({ bytes: 'abc' })], index: 0 })).toBeNull() + expect(readOutputRequest({ items: [picture({ name: ' ' })], index: 0 })).toBeNull() + }) + + it(`refuses more than ${MAX_OUTPUTS} outputs`, () => { + const items = Array.from({ length: MAX_OUTPUTS + 1 }, () => picture()) + + expect(readOutputRequest({ items, index: 0 })).toBeNull() + expect(readOutputRequest({ items: items.slice(1), index: 0 })).not.toBeNull() + }) + + it('refuses more bytes in all than Desktop takes', () => { + const half = new Uint8Array(MAX_OUTPUTS_BYTES / 2 + 1) + + expect( + readOutputRequest({ items: [picture({ bytes: half }), picture({ bytes: half })], index: 0 }) + ).toBeNull() + }) + + it('counts the whole buffer behind each output', () => { + const view = new Uint8Array(new ArrayBuffer(MAX_OUTPUTS_BYTES + 1), 0, 1) + + expect(readOutputRequest({ items: [picture({ bytes: view })], index: 0 })).toBeNull() + }) + + it('refuses an items list with holes', () => { + const items = [picture(), picture()] + delete items[0] + + expect(readOutputRequest({ items, index: 1 })).toBeNull() + }) +}) + +describe('readOutputAction', () => { + it('passes on a download or an open-workflow for one output', () => { + expect(readOutputAction({ action: 'download', index: 2 })).toEqual({ + action: 'download', + index: 2 + }) + expect(readOutputAction({ action: 'open-workflow', index: 0 })).toEqual({ + action: 'open-workflow', + index: 0 + }) + }) + + it('drops anything else', () => { + expect(readOutputAction({ action: 'delete', index: 0 })).toBeNull() + expect(readOutputAction({ action: 'download', index: 1.5 })).toBeNull() + expect(readOutputAction({ action: 'download', index: -1 })).toBeNull() + expect(readOutputAction('download')).toBeNull() + }) +}) diff --git a/src/main/apps/outputRequest.ts b/src/main/apps/outputRequest.ts new file mode 100644 index 000000000..01a42b5b1 --- /dev/null +++ b/src/main/apps/outputRequest.ts @@ -0,0 +1,63 @@ +import type { AppOutput, AppOutputAction, AppOutputs } from '../../types/ipc' + +export const MAX_OUTPUTS = 40 +export const MAX_OUTPUTS_BYTES = 100_000_000 +const MAX_NAME_LENGTH = 200 +const MAX_TAGS = 8 +const MAX_TAG_LENGTH = 40 +const SHOWN_MEDIA_TYPE = + /^(image\/(png|jpeg|webp|gif|avif)|video\/(mp4|webm|quicktime)|audio\/(mpeg|mp4|wav|x-wav|ogg|webm|flac|aac))$/ + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +function isIndex(value: unknown): value is number { + return typeof value === 'number' && Number.isInteger(value) && value >= 0 +} + +function trimmed(value: unknown, max: number): string | null { + return typeof value === 'string' && value.trim() !== '' ? value.trim().slice(0, max) : null +} + +function readTags(value: unknown): string[] { + if (!Array.isArray(value)) return [] + return value + .slice(0, MAX_TAGS) + .map((tag) => trimmed(tag, MAX_TAG_LENGTH)) + .filter((tag): tag is string => tag !== null) +} + +function readOutput(value: unknown): AppOutput | null { + if (!isRecord(value)) return null + const name = trimmed(value['name'], MAX_NAME_LENGTH) + const { mimeType, bytes } = value + if (!name || typeof mimeType !== 'string' || !SHOWN_MEDIA_TYPE.test(mimeType)) return null + if (!(bytes instanceof Uint8Array)) return null + return { + name, + mimeType, + bytes: bytes as Uint8Array, + tags: readTags(value['tags']), + workflow: value['workflow'] === true + } +} + +export function readOutputRequest(raw: unknown): AppOutputs | null { + if (!isRecord(raw)) return null + const { items, index } = raw + if (!Array.isArray(items) || items.length === 0 || items.length > MAX_OUTPUTS) return null + if (!isIndex(index) || index >= items.length) return null + const outputs = Array.from(items, readOutput) + if (outputs.some((output) => output === null)) return null + const shown = outputs as AppOutput[] + const sentBytes = shown.reduce((sum, output) => sum + output.bytes.buffer.byteLength, 0) + return sentBytes > MAX_OUTPUTS_BYTES ? null : { items: shown, index } +} + +export function readOutputAction(raw: unknown): AppOutputAction | null { + if (!isRecord(raw)) return null + const { action, index } = raw + if (action !== 'download' && action !== 'open-workflow') return null + return isIndex(index) ? { action, index } : null +} diff --git a/src/main/lib/ipc/index.ts b/src/main/lib/ipc/index.ts index ea21ede8b..61f8c380d 100644 --- a/src/main/lib/ipc/index.ts +++ b/src/main/lib/ipc/index.ts @@ -40,6 +40,7 @@ import { registerEmbeddedSessionHandlers } from './registerEmbeddedSessionHandle import { registerCatalogHandlers } from './registerCatalogHandlers' import { registerAppsHandlers } from './registerAppsHandlers' import { registerAppsLocalHandlers } from './registerAppsLocalHandlers' +import { registerAppsOutputsHandlers } from './registerAppsOutputsHandlers' import { registerAppsTelemetryHandlers } from './registerAppsTelemetryHandlers' import { reconcileAdoptedSettings } from '../desktopAdopt' import { @@ -288,6 +289,7 @@ export function register(callbacks: RegisterCallbacks = {}): Promise { registerCatalogHandlers() registerAppsHandlers() registerAppsLocalHandlers() + registerAppsOutputsHandlers() registerAppsTelemetryHandlers() return startupRecovery.then(() => undefined) } diff --git a/src/main/lib/ipc/registerAppsHandlers.ts b/src/main/lib/ipc/registerAppsHandlers.ts index 3e7168cb6..013cefdc4 100644 --- a/src/main/lib/ipc/registerAppsHandlers.ts +++ b/src/main/lib/ipc/registerAppsHandlers.ts @@ -1,8 +1,14 @@ import { ipcMain } from 'electron' -import { captureAppPage, hideAppPage, showAppPage } from '../../apps/appPageView' +import { + captureAppPage, + hideAppPage, + sendAppOutputAction, + showAppPage +} from '../../apps/appPageView' import { listApps } from '../../apps/appsCatalog' import { areAppsEnabled } from '../../apps/appsConfig' +import { readOutputAction } from '../../apps/outputRequest' import { isPanelSender } from '../../host/registry' export function registerAppsHandlers(): void { @@ -20,4 +26,9 @@ export function registerAppsHandlers(): void { ipcMain.handle('apps:capture-page', (event) => captureAppPage(event.sender)) ipcMain.on('apps:hide-page', (event) => hideAppPage(event.sender)) + + ipcMain.on('apps:output-action', (event, raw: unknown) => { + const action = readOutputAction(raw) + if (action) sendAppOutputAction(event.sender, action) + }) } diff --git a/src/main/lib/ipc/registerAppsOutputsHandlers.test.ts b/src/main/lib/ipc/registerAppsOutputsHandlers.test.ts new file mode 100644 index 000000000..4df358035 --- /dev/null +++ b/src/main/lib/ipc/registerAppsOutputsHandlers.test.ts @@ -0,0 +1,112 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +type Handler = (event: unknown, request: unknown) => Promise +interface LoadedPage { + appId: string + document: number +} + +const mocks = vi.hoisted(() => ({ + handlers: new Map(), + isAppsPage: true, + appsEnabled: true, + page: { appId: 'remove-background', document: 1 } as LoadedPage | null, + showAppOutputs: vi.fn((_contents: unknown, _sentFrom: unknown, _outputs: unknown) => true) +})) + +vi.mock('electron', () => ({ + ipcMain: { + handle: (channel: string, handler: Handler) => mocks.handlers.set(channel, handler) + } +})) +vi.mock('../../apps/appsConfig', () => ({ areAppsEnabled: async () => mocks.appsEnabled })) +vi.mock('../../apps/appsPages', () => ({ isAppsPageSender: () => mocks.isAppsPage })) +vi.mock('../../apps/appPageView', () => ({ + loadedAppPage: () => mocks.page, + showAppOutputs: mocks.showAppOutputs +})) + +import { + APPS_OUTPUTS_SHOW_CHANNEL, + registerAppsOutputsHandlers +} from './registerAppsOutputsHandlers' + +const SENDER = {} +const EVENT = { sender: SENDER, senderFrame: {} } +const REQUEST = { + items: [ + { name: 'cutout.png', mimeType: 'image/png', bytes: new Uint8Array([1]), workflow: true } + ], + index: 0 +} + +function showOutput(request: unknown = REQUEST): Promise { + const handler = mocks.handlers.get(APPS_OUTPUTS_SHOW_CHANNEL) + if (!handler) throw new Error('No handler for showOutput') + return handler(EVENT, request) +} + +beforeEach(() => { + vi.clearAllMocks() + mocks.handlers.clear() + mocks.isAppsPage = true + mocks.appsEnabled = true + mocks.page = { appId: 'remove-background', document: 1 } + mocks.showAppOutputs.mockReturnValue(true) + registerAppsOutputsHandlers() +}) + +describe('showOutput from the hosted Apps page', () => { + it('answers on the channel the Apps preload invokes', () => { + expect(APPS_OUTPUTS_SHOW_CHANNEL).toBe('apps-outputs:show') + }) + + it('shows the checked outputs for the app page that sent them', async () => { + expect(await showOutput()).toBe(true) + + expect(mocks.showAppOutputs).toHaveBeenCalledExactlyOnceWith( + SENDER, + { appId: 'remove-background', document: 1 }, + { + items: [ + { + name: 'cutout.png', + mimeType: 'image/png', + bytes: new Uint8Array([1]), + tags: [], + workflow: true + } + ], + index: 0 + } + ) + }) + + it('says so when Desktop does not show them', async () => { + mocks.showAppOutputs.mockReturnValue(false) + + expect(await showOutput()).toBe(false) + }) + + it('refuses a request it cannot show', async () => { + await expect(showOutput({ items: [], index: 0 })).rejects.toThrow( + 'Desktop cannot show these outputs.' + ) + expect(mocks.showAppOutputs).not.toHaveBeenCalled() + }) + + it('shows nothing for a sender that is not the hosted Apps page, or while Apps is off', async () => { + mocks.isAppsPage = false + expect(await showOutput()).toBe(false) + + mocks.isAppsPage = true + mocks.appsEnabled = false + expect(await showOutput()).toBe(false) + + mocks.appsEnabled = true + mocks.page = null + expect(await showOutput()).toBe(false) + + expect(mocks.showAppOutputs).not.toHaveBeenCalled() + }) +}) diff --git a/src/main/lib/ipc/registerAppsOutputsHandlers.ts b/src/main/lib/ipc/registerAppsOutputsHandlers.ts new file mode 100644 index 000000000..9edf5f5d8 --- /dev/null +++ b/src/main/lib/ipc/registerAppsOutputsHandlers.ts @@ -0,0 +1,28 @@ +import { ipcMain } from 'electron' +import type { IpcMainInvokeEvent } from 'electron' + +import { loadedAppPage, showAppOutputs } from '../../apps/appPageView' +import { areAppsEnabled } from '../../apps/appsConfig' +import { isAppsPageSender } from '../../apps/appsPages' +import { readOutputRequest } from '../../apps/outputRequest' + +export const APPS_OUTPUTS_SHOW_CHANNEL = 'apps-outputs:show' + +async function isFromAppsPage(event: IpcMainInvokeEvent): Promise { + return isAppsPageSender(event.sender, event.senderFrame) && (await areAppsEnabled()) +} + +export async function showOutputFromAppsPage( + event: IpcMainInvokeEvent, + request: unknown +): Promise { + const sentFrom = loadedAppPage(event.sender) + if (sentFrom === null || !(await isFromAppsPage(event))) return false + const outputs = readOutputRequest(request) + if (!outputs) throw new Error('Desktop cannot show these outputs.') + return showAppOutputs(event.sender, sentFrom, outputs) +} + +export function registerAppsOutputsHandlers(): void { + ipcMain.handle(APPS_OUTPUTS_SHOW_CHANNEL, showOutputFromAppsPage) +} diff --git a/src/preload/api.ts b/src/preload/api.ts index 7130d7a1a..711714003 100644 --- a/src/preload/api.ts +++ b/src/preload/api.ts @@ -1,6 +1,12 @@ import { ipcRenderer, webUtils } from 'electron' import type { IpcRendererEvent } from 'electron' -import type { AppPageStatus, ElectronApi, ResolvedTheme, AdoptPromptRequest } from '../types/ipc' +import type { + AppOutputs, + AppPageStatus, + ElectronApi, + ResolvedTheme, + AdoptPromptRequest +} from '../types/ipc' export function buildElectronApi(): ElectronApi { return { @@ -123,6 +129,13 @@ export function buildElectronApi(): ElectronApi { ipcRenderer.on('apps:page-closed', handler) return () => ipcRenderer.removeListener('apps:page-closed', handler) }, + onAppOutputs: (callback) => { + const handler = (_event: IpcRendererEvent, appId: string, outputs: AppOutputs): void => + callback(appId, outputs) + ipcRenderer.on('apps:outputs', handler) + return () => ipcRenderer.removeListener('apps:outputs', handler) + }, + sendAppOutputAction: (action) => ipcRenderer.send('apps:output-action', action), getBuildStorageSnapshot: () => ipcRenderer.invoke('comfy-window:build-storage-snapshot'), resolveStartupRestoreReveal: (result) => ipcRenderer.send('comfy-window:startup-restore-reveal', { result }), diff --git a/src/preload/appsPreload.ts b/src/preload/appsPreload.ts index f3f81c348..f748221bd 100644 --- a/src/preload/appsPreload.ts +++ b/src/preload/appsPreload.ts @@ -4,8 +4,10 @@ import type { ComfyDesktop2AuthState, ComfyDesktopHostedAuthBridge, ComfyDesktopHostedLocalBridge, + ComfyDesktopHostedOutputsBridge, ComfyDesktopHostedTelemetryBridge, - ComfyDesktopLocalEvent + ComfyDesktopLocalEvent, + ComfyDesktopOutputAction } from '../types/comfyDesktopBridge' const auth: ComfyDesktopHostedAuthBridge = { @@ -42,6 +44,17 @@ const telemetry: ComfyDesktopHostedTelemetryBridge = { track: (name, props) => ipcRenderer.send('apps-telemetry:track', { name, props }) } +const outputs: ComfyDesktopHostedOutputsBridge = { + showOutput: (request) => ipcRenderer.invoke('apps-outputs:show', request), + onOutputAction: (callback) => { + const handler = (_event: IpcRendererEvent, action: ComfyDesktopOutputAction): void => + callback(action) + ipcRenderer.on('apps-outputs:action', handler) + return () => ipcRenderer.removeListener('apps-outputs:action', handler) + } +} + contextBridge.exposeInMainWorld('comfyDesktopAuth', auth) contextBridge.exposeInMainWorld('comfyDesktopLocal', local) contextBridge.exposeInMainWorld('comfyDesktopTelemetry', telemetry) +contextBridge.exposeInMainWorld('comfyDesktopOutputs', outputs) diff --git a/src/renderer/csp.test.ts b/src/renderer/csp.test.ts index ad4376fc7..2a48daf28 100644 --- a/src/renderer/csp.test.ts +++ b/src/renderer/csp.test.ts @@ -65,6 +65,11 @@ describe('Content-Security-Policy: panel.html', () => { it('allows GitHub-hosted starter-template thumbnails in img-src', () => { expect(csp['img-src']).toContain('https://raw.githubusercontent.com') }) + + it('shows the outputs an app page sends, which the panel holds as blob URLs', () => { + expect(csp['img-src']).toContain('blob:') + expect(csp['media-src']).toContain('blob:') + }) }) describe.each(TELEMETRY_RENDERER_HTMLS)( diff --git a/src/renderer/panel.html b/src/renderer/panel.html index e0ee5a255..209d5ecc2 100644 --- a/src/renderer/panel.html +++ b/src/renderer/panel.html @@ -5,7 +5,7 @@ Comfy Desktop — Panel