Skip to content
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"fit": "Fit",
"zoom": "Zoom",
"zoomLevel": "Zoom: {level}",
"openWorkflow": "Open workflow",
"savedIn": "Saved in"
},
"kindSingular": {
Expand Down
1 change: 1 addition & 0 deletions locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"fit": "适应",
"zoom": "缩放",
"zoomLevel": "缩放:{level}",
"openWorkflow": "打开工作流",
"savedIn": "保存位置"
},
"kindSingular": {
Expand Down
135 changes: 135 additions & 0 deletions src/main/apps/appPageView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -60,13 +62,17 @@ 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,
_resetForTest,
captureAppPage,
hideAppPage,
loadedAppPage,
sendAppOutputAction,
showAppOutputs,
showAppPage
} from './appPageView'
import { trackFromAppsPage } from '../lib/ipc/registerAppsTelemetryHandlers'
Expand Down Expand Up @@ -132,6 +138,7 @@ class FakeView {

class FakeWindow extends EventEmitter {
stack: unknown[] = []
focused = true
contentView = {
get children(): unknown[] {
return [...stackOf(this)]
Expand All @@ -146,6 +153,9 @@ class FakeWindow extends EventEmitter {
isDestroyed(): boolean {
return false
}
isFocused(): boolean {
return this.focused
}
}

const windowsByContentView = new WeakMap<object, FakeWindow>()
Expand Down Expand Up @@ -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)
})
})
50 changes: 46 additions & 4 deletions src/main/apps/appPageView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'

Expand All @@ -34,6 +36,11 @@ export interface LoadedAppPage {
document: number
}

interface ShownOutputs {
page: LoadedAppPage
count: number
}

interface HostedPage {
view: WebContentsView
appId: string | null
Expand All @@ -45,6 +52,7 @@ interface HostedPage {
status: AppPageStatus
wanted: boolean
focusWhenReady: boolean
outputs: ShownOutputs | null
forget: () => void
}

Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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<string, unknown>
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 {
Expand All @@ -322,7 +334,8 @@ function parsePlacement(raw: unknown): AppPagePlacement | null {
height,
cornerRadius,
justOpened: justOpened === true,
retry: retry === true
retry: retry === true,
focus: focus === true
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -417,6 +430,35 @@ export async function captureAppPage(panel: WebContents): Promise<string | null>
}
}

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 }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium — ShownOutputs records only page and count, with no generation identifier, and is overwritten by each showOutput. If a page replaces its outputs while the dialog still displays the previous set, a click validates against the new count and the forwarded {action, index} resolves on the page against the newer set, so Download or Open workflow acts on an output the user never saw. Stamp each request with an incrementing id and include it in the action. Raised by 2 of 8 reviewers (claude-opus-5-thinking-max adversarial, gpt-5.6-sol-max adversarial).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing: a new request replaces the dialog's items as soon as it arrives, so the panel and the page use the same set; only a click in the same instant could race.

if (comfyWindows.get(windowKey)?.window.isFocused()) panel.focus()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium — comfyWindows.get(windowKey)?.window.isFocused() has no isDestroyed() guard, unlike hostOf which checks !entry.window.isDestroyed() before touching entry.window. If the window is torn down between the panelOf check and this call, isFocused() throws Object has been destroyed, which escapes showAppOutputs and rejects the page's showOutput invoke instead of resolving false as the bridge contract documents. Raised by 1 of 8 reviewers (claude-opus-5-thinking-max edge-case).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing: panelOf already returns null for a destroyed panel, and the window cannot be destroyed between these two synchronous calls.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium — sendAppOutputAction checks isStillLoaded but not isOnScreen(hosted), while showAppOutputs requires both, and hosted.outputs is never cleared once set. After closeForList conceals the page with pageStillShowsApp true, hosted.loaded survives, so an action can still be delivered to a page the user can no longer see. Raised by 3 of 8 reviewers (claude-opus-5-thinking-max edge-case, kimi-k2.7-code edge-case, claude-opus-5-thinking-max adversarial).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing: while the dialog is open the app page is hidden behind a still frame, so requiring it to be on screen would drop every action. isStillLoaded keeps actions to the page that sent the outputs, and closing the app removes the dialog.

hosted.view.webContents.send(APP_OUTPUT_ACTION_CHANNEL, action)
}

export function _resetForTest(): void {
hostedByWindow.clear()
}
Loading
Loading