Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 84 additions & 3 deletions src/main/apps/appPageView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import {
_resetForTest,
captureAppPage,
hideAppPage,
reloadAppPage,
showAppPage
} from './appPageView'

Expand Down Expand Up @@ -187,7 +186,7 @@ function sentToPanel(channel: string): unknown[][] {
}

function lastStatusSentToPanel(): unknown {
return sentToPanel(APP_PAGE_STATUS_CHANNEL).at(-1)?.[1]
return sentToPanel(APP_PAGE_STATUS_CHANNEL).at(-1)?.[2]
}

function openCutout(placement: Record<string, unknown> = PLACEMENT): Promise<string> {
Expand Down Expand Up @@ -484,14 +483,96 @@ describe('an app page that cannot load', () => {
reason: 'network'
})

reloadAppPage(panelContents())
expect(await openCutout({ ...PLACEMENT, retry: true })).toBe('loading')
expect(lastStatusSentToPanel()).toBe('loading')
expect(hostedPage().webContents.loadURL).toHaveBeenCalledTimes(2)
expect(hostedPage().webContents.loadURL).toHaveBeenLastCalledWith(CUTOUT_URL)

finishLoading()
expect(hostedPage().visible).toBe(true)
expect(lastStatusSentToPanel()).toBe('ready')
expect(mocks.capture).not.toHaveBeenCalledWith(APP_OPENED_EVENT, expect.anything())
})

it('follows a redirect that stays on the apps site while the page loads', async () => {
await openCutout()
const event = navigationEvent()
hostedPage().webContents.emit('will-redirect', event, `${CUTOUT_URL}?v=2`, false, true)

expect(event.preventDefault).not.toHaveBeenCalled()
expect(mocks.capture).not.toHaveBeenCalledWith(APP_PAGE_FAILED_EVENT, expect.anything())

finishLoading(`${CUTOUT_URL}?v=2`)
expect(lastStatusSentToPanel()).toBe('ready')
})

it('says the page did not load, without opening the browser, when the site sends it to another site while it loads', async () => {
await openCutout()
const event = navigationEvent()
hostedPage().webContents.emit(
'will-redirect',
event,
'https://www.comfy.org/apps/',
false,
true
)

expect(event.preventDefault).toHaveBeenCalled()
expect(mocks.openExternal).not.toHaveBeenCalled()
expect(hostedPage().visible).toBe(false)
expect(lastStatusSentToPanel()).toBe('failed')
expect(mocks.capture).toHaveBeenCalledWith(APP_PAGE_FAILED_EVENT, {
app_id: CUTOUT,
reason: 'redirected'
})
})

it('asks again whether a refused app may open when the person retries it, and loads it once it may', async () => {
await openCutout()
finishLoading()
hideAppPage(panelContents())
mocks.pages.delete(UPSCALE)
expect(await showAppPage(panelContents(), UPSCALE, PLACEMENT)).toBe('failed')

expect(await showAppPage(panelContents(), UPSCALE, { ...PLACEMENT, retry: true })).toBe(
'failed'
)
expect(hostedPage().webContents.loadURL).toHaveBeenCalledTimes(1)
expect(hostedPage().visible).toBe(false)

mocks.pages.set(UPSCALE, UPSCALE_URL)
expect(await showAppPage(panelContents(), UPSCALE, { ...PLACEMENT, retry: true })).toBe(
'loading'
)
expect(hostedPage().webContents.loadURL).toHaveBeenLastCalledWith(UPSCALE_URL)
finishLoading(UPSCALE_URL)
expect(hostedPage().visible).toBe(true)
})

it('does not load a failed app again on Try again once Apps is switched off', async () => {
await openCutout()
finishLoading(CUTOUT_URL, 503)
mocks.appsEnabled = false

expect(await openCutout({ ...PLACEMENT, retry: true })).toBe('failed')
expect(hostedPage().webContents.loadURL).toHaveBeenCalledTimes(1)
expect(hostedPage().visible).toBe(false)
})

it("names the app with every status, so the panel can tell a hidden app's page from the one it shows", async () => {
await openCutout()
hideAppPage(panelContents())
mocks.pages.delete(UPSCALE)
expect(await showAppPage(panelContents(), UPSCALE, PLACEMENT)).toBe('failed')

finishLoading()

expect(sentToPanel(APP_PAGE_STATUS_CHANNEL).at(-1)).toEqual([
APP_PAGE_STATUS_CHANNEL,
CUTOUT,
'ready'
])
expect(hostedPage().visible).toBe(false)
})

it('tries a failed app again when the person opens it once more', async () => {
Expand Down
37 changes: 22 additions & 15 deletions src/main/apps/appPageView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const NAVIGATION_ABORTED = -3
const FIRST_HTTP_ERROR_STATUS = 400
const PAGE_PERMISSIONS: ReadonlySet<string> = new Set(['clipboard-sanitized-write', 'fullscreen'])

type PageFailure = 'http_status' | 'network' | 'crashed'
type PageFailure = 'http_status' | 'network' | 'crashed' | 'redirected'

interface HostedPage {
view: WebContentsView
Expand Down Expand Up @@ -101,7 +101,7 @@ function setStatus(windowKey: number, hosted: HostedPage, status: AppPageStatus)
if (hosted.status === status) return
hosted.status = status
applyVisibility(hosted)
panelOf(windowKey)?.send(APP_PAGE_STATUS_CHANNEL, status)
panelOf(windowKey)?.send(APP_PAGE_STATUS_CHANNEL, hosted.appId, status)
}

function fail(windowKey: number, hosted: HostedPage, reason: PageFailure): void {
Expand Down Expand Up @@ -167,7 +167,13 @@ function createHostedPage({ windowKey, entry }: PageHost, location: AppsLocation
}
contents.on('will-navigate', (event, url) => steer(event, url, true))
contents.on('will-redirect', (event, url, _isInPlace, isMainFrame) => {
if (isMainFrame) steer(event, url, false)
if (!isMainFrame) return
if (hosted.status === 'loading' && !isInAppsScope(location, url)) {
event.preventDefault()
fail(windowKey, hosted, 'redirected')
return
}
steer(event, url, false)
})
contents.setWindowOpenHandler(({ url }) => {
openOutside(url)
Expand Down Expand Up @@ -218,10 +224,18 @@ 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 } = raw as Record<string, unknown>
const { x, y, width, height, cornerRadius, justOpened, retry } = raw as Record<string, unknown>
if (!isMeasure(x) || !isMeasure(y) || !isMeasure(width) || !isMeasure(height)) return null
if (!isMeasure(cornerRadius)) return null
return { x, y, width, height, cornerRadius, justOpened: justOpened === true }
return {
x,
y,
width,
height,
cornerRadius,
justOpened: justOpened === true,
retry: retry === true
}
}

function place(entry: ComfyWindowEntry, view: WebContentsView, placement: AppPagePlacement): void {
Expand Down Expand Up @@ -275,14 +289,15 @@ export async function showAppPage(
if (!pageUrl || !host) return 'failed'
const hosted = hostedFor(host) ?? createHostedPage(host, currentAppsLocation())
concealWhenPanelGoes(host.windowKey, panel)
const retriesFailedPage = placement.justOpened === true && hosted.status === 'failed'
const askedFresh = placement.justOpened === true || placement.retry === true
const retriesFailedPage = askedFresh && hosted.status === 'failed'
if (hosted.appId !== appId || retriesFailedPage) {
hosted.appId = appId
load(host.windowKey, hosted, pageUrl)
}
if (placement.justOpened) mainTelemetry.capture(APP_OPENED_EVENT, { app_id: appId })
hosted.wanted = true
hosted.focusWhenReady = hosted.focusWhenReady || placement.justOpened === true
hosted.focusWhenReady = hosted.focusWhenReady || askedFresh
place(host.entry, hosted.view, placement)
raiseAbovePanel(host.entry, hosted.view)
applyVisibility(hosted)
Expand All @@ -307,14 +322,6 @@ export async function captureAppPage(panel: WebContents): Promise<string | null>
}
}

export function reloadAppPage(panel: WebContents): void {
const host = hostOf(panel)
const hosted = host && hostedFor(host)
const pageUrl = hosted?.appId ? pageUrlForListedApp(hosted.appId) : null
if (!host || !hosted || !pageUrl) return
load(host.windowKey, hosted, pageUrl)
}

export function _resetForTest(): void {
hostedByWindow.clear()
}
4 changes: 1 addition & 3 deletions src/main/lib/ipc/registerAppsHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ipcMain } from 'electron'

import { captureAppPage, hideAppPage, reloadAppPage, showAppPage } from '../../apps/appPageView'
import { captureAppPage, hideAppPage, showAppPage } from '../../apps/appPageView'
import { listApps } from '../../apps/appsCatalog'
import { areAppsEnabled } from '../../apps/appsConfig'
import { isPanelSender } from '../../host/registry'
Expand All @@ -20,6 +20,4 @@ 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:reload-page', (event) => reloadAppPage(event.sender))
}
7 changes: 5 additions & 2 deletions src/preload/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ export function buildElectronApi(): ElectronApi {
showAppPage: (appId, placement) => ipcRenderer.invoke('apps:show-page', appId, placement),
captureAppPage: () => ipcRenderer.invoke('apps:capture-page'),
hideAppPage: () => ipcRenderer.send('apps:hide-page'),
reloadAppPage: () => ipcRenderer.send('apps:reload-page'),
onAppPageStatusChanged: (callback) => {
const handler = (_event: IpcRendererEvent, status: AppPageStatus): void => callback(status)
const handler = (
_event: IpcRendererEvent,
appId: string | null,
status: AppPageStatus
): void => callback(appId, status)
ipcRenderer.on('apps:page-status-changed', handler)
return () => ipcRenderer.removeListener('apps:page-status-changed', handler)
},
Expand Down
51 changes: 44 additions & 7 deletions src/renderer/src/panel/AppPageHost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ const FRAME =
const showAppPage = vi.fn<(appId: string, placement: AppPagePlacement) => Promise<AppPageStatus>>()
const captureAppPage = vi.fn<() => Promise<string | null>>()
const hideAppPage = vi.fn()
const reloadAppPage = vi.fn()

const mountPointFillsThePage = document.createElement('style')
mountPointFillsThePage.textContent = '[data-apps-page] > div { display: flex; flex: 1 1 auto; }'
document.head.append(mountPointFillsThePage)

let page: HTMLElement
let tellStatus: (status: AppPageStatus) => void = () => {}
let tellStatusOf: (appId: string | null, status: AppPageStatus) => void = () => {}
let stoppedListening = false

function tellStatus(status: AppPageStatus): void {
tellStatusOf('remove-background', status)
}

async function nextFrame(): Promise<void> {
await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))
await flushPromises()
Expand Down Expand Up @@ -63,9 +66,8 @@ beforeEach(() => {
showAppPage,
captureAppPage,
hideAppPage,
reloadAppPage,
onAppPageStatusChanged: (listener: (status: AppPageStatus) => void) => {
tellStatus = listener
onAppPageStatusChanged: (listener: (appId: string | null, status: AppPageStatus) => void) => {
tellStatusOf = listener
return () => {
stoppedListening = true
}
Expand All @@ -88,7 +90,8 @@ describe('AppPageHost', () => {
width: PAGE_WIDTH,
height: PAGE_HEIGHT,
cornerRadius: 14,
justOpened: true
justOpened: true,
retry: false
})
})

Expand Down Expand Up @@ -129,9 +132,15 @@ describe('AppPageHost', () => {
"Remove Background didn't load"
)

showAppPage.mockClear()
showAppPage.mockResolvedValue('loading')
await wrapper.get('[data-testid="app-page-retry"]').trigger('click')
await flushPromises()

expect(reloadAppPage).toHaveBeenCalledTimes(1)
expect(showAppPage).toHaveBeenCalledExactlyOnceWith(
'remove-background',
expect.objectContaining({ justOpened: false, retry: true })
)
expect(wrapper.get('[data-testid="app-page-loading"]').isVisible()).toBe(true)

tellStatus('failed')
Expand All @@ -141,6 +150,34 @@ describe('AppPageHost', () => {
expect(wrapper.emitted('close')).toHaveLength(1)
})

it('asks for a fresh load only on the Try again that was pressed, not on the next resize', async () => {
const wrapper = await mountHost('failed')
showAppPage.mockResolvedValue('loading')
await wrapper.get('[data-testid="app-page-retry"]').trigger('click')
await flushPromises()
showAppPage.mockClear()

page.style.width = `${PAGE_WIDTH - 180}px`
await nextFrame()
await nextFrame()

expect(showAppPage).toHaveBeenLastCalledWith(
'remove-background',
expect.objectContaining({ justOpened: false, retry: false })
)
})

it("keeps saying the app did not load when another app's hidden page finishes", async () => {
const wrapper = await mountHost('failed')

tellStatusOf('upscale', 'ready')
tellStatusOf(null, 'loading')
await flushPromises()

expect(wrapper.get('[data-testid="app-page-failed"]').isVisible()).toBe(true)
expect(wrapper.get('[data-testid="app-page-host"]').attributes('data-status')).toBe('failed')
})

it('follows the host when it is resized, without counting another opening', async () => {
await mountHost()
showAppPage.mockClear()
Expand Down
12 changes: 8 additions & 4 deletions src/renderer/src/panel/AppPageHost.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const status = ref<AppPageStatus | null>(null)
const frozenFrame = ref<string | null>(null)

let justOpened = true
let retrying = false
let latestTurn = 0
let stopStatusUpdates: (() => void) | null = null

Expand All @@ -38,9 +39,11 @@ function takePlacement(): AppPagePlacement | null {
width: rect.width,
height: rect.height,
cornerRadius,
justOpened
justOpened,
retry: retrying
}
justOpened = false
retrying = false
return placement
}

Expand Down Expand Up @@ -73,7 +76,8 @@ async function conceal(): Promise<void> {

function retry(): void {
status.value = 'loading'
window.api.reloadAppPage()
retrying = true
void reveal()
}

watch(covered, (isCovered) => void (isCovered ? conceal() : reveal()))
Expand All @@ -93,8 +97,8 @@ useResizeObserver(host, () => {
})

onMounted(() => {
stopStatusUpdates = window.api.onAppPageStatusChanged((next) => {
status.value = next
stopStatusUpdates = window.api.onAppPageStatusChanged((pageAppId, next) => {
if (pageAppId === appId) status.value = next
})
if (!covered.value) void reveal()
})
Expand Down
1 change: 0 additions & 1 deletion src/renderer/src/panel/AppsView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ beforeEach(() => {
showAppPage,
captureAppPage: vi.fn().mockResolvedValue(null),
hideAppPage,
reloadAppPage: vi.fn(),
onAppPageStatusChanged: vi.fn(() => () => {}),
onAppPageClosed: (listener: () => void) => {
appPageClosed = listener
Expand Down
1 change: 0 additions & 1 deletion src/renderer/src/panel/PanelApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ function installMockApi(initial?: {
showAppPage: vi.fn(async () => 'loading'),
captureAppPage: vi.fn(async () => null),
hideAppPage: vi.fn(),
reloadAppPage: vi.fn(),
onAppPageStatusChanged: vi.fn(() => () => {}),
onAppPageClosed: vi.fn(() => () => {}),
getListActions: vi.fn(async () => []),
Expand Down
Loading
Loading