diff --git a/locales/en.json b/locales/en.json index beaa05d83..bf2bcc972 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 817daa6f2..5d16f5b78 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..e8d46069f --- /dev/null +++ b/src/main/apps/outputRequest.test.ts @@ -0,0 +1,153 @@ +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 a name with hidden characters, and drops such tags', () => { + for (const name of [ + 'cut\u202eout.png', + 'cut\nout.png', + 'cut\u0000out.png', + 'cut\u200fout.png', + 'cut\u200bout.png', + 'cut\u0085out.png', + 'cut\ufeffout.png' + ]) { + expect(readOutputRequest({ items: [picture({ name })], index: 0 }), name).toBeNull() + } + const request = readOutputRequest({ + items: [picture({ tags: ['portrait', 'sea\u2066side', 'night\u007f'] })], + index: 0 + }) + expect(request?.items[0]?.tags).toEqual(['portrait']) + }) + + it('checks only the part of a long name it keeps', () => { + const request = readOutputRequest({ + items: [picture({ name: 'n'.repeat(5_000_000) + '\u202e' })], + index: 0 + }) + + expect(request?.items[0]?.name).toBe('n'.repeat(200)) + }) + + 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..11e5acbfb --- /dev/null +++ b/src/main/apps/outputRequest.ts @@ -0,0 +1,78 @@ +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 INVISIBLE_FORMATTING = /[\u0080-\u009f\u061c\u200b-\u200f\u2028-\u202e\u2066-\u2069\ufeff]/ +const FIRST_PRINTABLE_CODE = 32 +const DELETE_CODE = 127 +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 hasHiddenCharacters(value: string): boolean { + return ( + INVISIBLE_FORMATTING.test(value) || + Array.from(value).some((char) => { + const code = char.charCodeAt(0) + return code < FIRST_PRINTABLE_CODE || code === DELETE_CODE + }) + ) +} + +function trimmed(value: unknown, max: number): string | null { + if (typeof value !== 'string') return null + const text = value.trim().slice(0, max) + return text === '' || hasHiddenCharacters(text) ? null : text +} + +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/registerAppsOutputsHandlers.test.ts b/src/main/lib/ipc/registerAppsOutputsHandlers.test.ts new file mode 100644 index 000000000..19025cead --- /dev/null +++ b/src/main/lib/ipc/registerAppsOutputsHandlers.test.ts @@ -0,0 +1,148 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +type Handler = (event: unknown, request: unknown) => Promise +type Listener = (event: unknown, raw: unknown) => void +interface LoadedPage { + appId: string + document: number +} + +const mocks = vi.hoisted(() => ({ + handlers: new Map(), + listeners: new Map(), + sendAppOutputAction: vi.fn(), + 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), + on: (channel: string, listener: Listener) => mocks.listeners.set(channel, listener) + } +})) +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, + sendAppOutputAction: mocks.sendAppOutputAction +})) + +import { + APPS_OUTPUT_ACTION_CHANNEL, + 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.listeners.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() + }) +}) + +describe('an output action from the panel', () => { + const PANEL_EVENT = { sender: {} } + + function takeAction(raw: unknown): void { + const listener = mocks.listeners.get(APPS_OUTPUT_ACTION_CHANNEL) + if (!listener) throw new Error('No listener for output actions') + listener(PANEL_EVENT, raw) + } + + it('listens on the channel the panel sends on', () => { + expect(APPS_OUTPUT_ACTION_CHANNEL).toBe('apps:output-action') + }) + + it('sends Download to the page', () => { + takeAction({ action: 'download', index: 0 }) + + expect(mocks.sendAppOutputAction).toHaveBeenCalledExactlyOnceWith(PANEL_EVENT.sender, { + action: 'download', + index: 0 + }) + }) + + it('drops an action it cannot read', () => { + takeAction({ action: 'delete', index: 0 }) + + expect(mocks.sendAppOutputAction).not.toHaveBeenCalled() + }) +}) diff --git a/src/main/lib/ipc/registerAppsOutputsHandlers.ts b/src/main/lib/ipc/registerAppsOutputsHandlers.ts new file mode 100644 index 000000000..a8e93cb10 --- /dev/null +++ b/src/main/lib/ipc/registerAppsOutputsHandlers.ts @@ -0,0 +1,35 @@ +import { ipcMain } from 'electron' +import type { IpcMainEvent, IpcMainInvokeEvent } from 'electron' + +import { loadedAppPage, sendAppOutputAction, showAppOutputs } from '../../apps/appPageView' +import { areAppsEnabled } from '../../apps/appsConfig' +import { isAppsPageSender } from '../../apps/appsPages' +import { readOutputAction, readOutputRequest } from '../../apps/outputRequest' + +export const APPS_OUTPUTS_SHOW_CHANNEL = 'apps-outputs:show' +export const APPS_OUTPUT_ACTION_CHANNEL = 'apps:output-action' + +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 takeOutputAction(event: IpcMainEvent, raw: unknown): void { + const action = readOutputAction(raw) + if (action) sendAppOutputAction(event.sender, action) +} + +export function registerAppsOutputsHandlers(): void { + ipcMain.handle(APPS_OUTPUTS_SHOW_CHANNEL, showOutputFromAppsPage) + ipcMain.on(APPS_OUTPUT_ACTION_CHANNEL, takeOutputAction) +} 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