Skip to content

The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.

Desktop: keep macOS window close separate from quit (#62)

mikasa5b185c0fac0
diff --git a/.changeset/tidy-windows-stay.md b/.changeset/tidy-windows-stay.md
new file mode 100644
index 0000000..763dbe9
--- /dev/null
+++ b/.changeset/tidy-windows-stay.md
@@ -0,0 +1,5 @@
+---
+"@openfilm/desktop": patch
+---
+
+Keep the app running when its window is closed on macOS, so the Dock can reopen it without losing tabs or stopping agents. Command-W closes the window when no project tab is open on macOS. Explicit Quit exits even while Studio is starting or after startup fails; Windows and Linux keep their existing Close Tab behavior.
diff --git a/apps/desktop/src/main.mjs b/apps/desktop/src/main.mjs
index d314bb5..6aacd28 100644
--- a/apps/desktop/src/main.mjs
+++ b/apps/desktop/src/main.mjs
@@ -19,6 +19,7 @@ import { folderDialogOptions, programDialogOptions } from './folder-dialog.mjs';
import { childEnvironment, loginShellEnvironment } from './environment.mjs';
import { startWindowStudio } from './studio.mjs';
import { CHAT_MIN, STUDIO_MIN, WINDOW_MIN, placeOnDisplays, readWindowState, writeWindowState } from './window-state.mjs';
+import { closeTabOrWindow, keepWindowOnClose, quitAfterStudio } from './window-lifecycle.mjs';
import { applicationMenu } from './menu.mjs';
import { registerLocalAgentIpc } from './local-agents-ipc.mjs';
import { readPreferences, registerChatIpc } from './chat-ipc.mjs';
@@ -398,7 +399,7 @@ async function start() {
win.on('moved', saveState);
win.on('enter-full-screen', () => broadcast('desktop:full-screen', true));
win.on('leave-full-screen', () => broadcast('desktop:full-screen', false));
- win.on('close', saveState);
+ keepWindowOnClose(win, { mac, isQuitting: () => quitting, save: saveState });
win.on('closed', () => { win = null; views.clear(); app.quit(); });
ipcMain.on('desktop:theme', (event, next) => { if (fromPage(event)) setTheme(next); });
@@ -596,7 +597,7 @@ async function start() {
tabs: {
/* the projects window, in the page in front, to pick the project a new tab shows */
open: () => activeView()?.webContents.send('desktop:tabs:pick') ?? views.get(HOME)?.webContents.focus(),
- close: () => { if (tabs?.activeId()) void closeTab(tabs.activeId()); },
+ close: () => closeTabOrWindow(win, { mac, activeId: tabs?.activeId(), closeTab }),
reopen: () => { const tab = tabs?.reopen(); if (tab) showTab(tab.id); },
step: (by) => {
const list = tabs?.list() ?? [];
@@ -637,9 +638,8 @@ process.on('SIGTERM', () => app.quit());
app.on('activate', () => focusWindow());
app.on('window-all-closed', () => app.quit());
app.on('before-quit', (event) => {
- localAgents?.closeAll();
- if (quitting || !studio) return;
- event.preventDefault();
- quitting = true;
- void studio.quit().finally(() => app.quit());
+ quitAfterStudio(event, {
+ quitting, markQuitting: () => { quitting = true; }, studio,
+ closeAgents: () => localAgents?.closeAll(), quit: () => app.quit(),
+ });
});
diff --git a/apps/desktop/src/window-lifecycle.mjs b/apps/desktop/src/window-lifecycle.mjs
new file mode 100644
index 0000000..22a8aaf
--- /dev/null
+++ b/apps/desktop/src/window-lifecycle.mjs
@@ -0,0 +1,26 @@
+/** Keep the macOS window available for Dock activation without ending Studio or agent sessions. */
+export function keepWindowOnClose(win, { mac, isQuitting, save }) {
+ win.on('close', (event) => {
+ save();
+ if (mac && !isQuitting()) {
+ event.preventDefault();
+ win.hide();
+ }
+ });
+}
+
+/** An empty projects page closes its window only on macOS. */
+export function closeTabOrWindow(win, { mac, activeId, closeTab }) {
+ if (activeId) void closeTab(activeId);
+ else if (mac) win?.close();
+}
+
+/** Mark Quit before window close can run, even before Studio has started or after startup failed. */
+export function quitAfterStudio(event, { quitting, markQuitting, studio, closeAgents, quit }) {
+ closeAgents();
+ if (quitting) return;
+ markQuitting();
+ if (!studio) return;
+ event.preventDefault();
+ void studio.quit().then(quit, quit);
+}
diff --git a/apps/desktop/src/window-lifecycle.test.mjs b/apps/desktop/src/window-lifecycle.test.mjs
new file mode 100644
index 0000000..442275e
--- /dev/null
+++ b/apps/desktop/src/window-lifecycle.test.mjs
@@ -0,0 +1,103 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { EventEmitter } from 'node:events';
+import { closeTabOrWindow, keepWindowOnClose, quitAfterStudio } from './window-lifecycle.mjs';
+
+class Window extends EventEmitter {
+ visible = true;
+ destroyed = false;
+ hide() { this.visible = false; }
+ show() { this.visible = true; }
+ close() {
+ let prevented = false;
+ this.emit('close', { preventDefault() { prevented = true; } });
+ if (!prevented) { this.destroyed = true; this.emit('closed'); }
+ }
+}
+
+test('macOS close preserves the window and sessions for activation, but explicit quit still closes it', () => {
+ const win = new Window();
+ let quitting = false, saved = 0, closed = 0;
+ win.on('closed', () => closed++);
+ keepWindowOnClose(win, { mac: true, isQuitting: () => quitting, save: () => saved++ });
+ win.close();
+ assert.equal(win.visible, false);
+ assert.equal(win.destroyed, false);
+ assert.equal(closed, 0, 'the closed handler must not quit the app or discard its views');
+ assert.equal(saved, 1);
+ win.show();
+ assert.equal(win.visible, true, 'Dock activation can show the retained window');
+ win.close();
+ assert.equal(win.destroyed, false, 'closing again after activation also keeps the window');
+ quitting = true;
+ win.close();
+ assert.equal(win.destroyed, true, 'Quit must not be intercepted as another hide');
+ assert.equal(closed, 1);
+ assert.equal(saved, 3);
+});
+
+test('Windows and Linux window close still follows the existing quit path', () => {
+ const win = new Window();
+ let saved = 0;
+ keepWindowOnClose(win, { mac: false, isQuitting: () => false, save: () => saved++ });
+ win.close();
+ assert.equal(win.destroyed, true);
+ assert.equal(saved, 1);
+});
+
+for (const platform of ['darwin', 'win32', 'linux']) {
+ test(`Close Tab on ${platform} closes a project, and only macOS closes an empty window`, () => {
+ const win = new Window(), closed = [];
+ const mac = platform === 'darwin';
+ keepWindowOnClose(win, { mac, isQuitting: () => false, save: () => {} });
+ closeTabOrWindow(win, { mac, activeId: 'project', closeTab: (id) => closed.push(id) });
+ assert.deepEqual(closed, ['project']);
+ assert.equal(win.visible, true);
+ closeTabOrWindow(win, { mac, activeId: null, closeTab: (id) => closed.push(id) });
+ assert.equal(win.visible, !mac);
+ assert.equal(win.destroyed, false, 'Ctrl-W must not exit Windows/Linux from the projects page');
+ assert.deepEqual(closed, ['project']);
+ });
+}
+
+for (const phase of ['Studio starting', 'Studio startup failed']) {
+ test(`explicit Quit closes macOS window while ${phase}`, () => {
+ const win = new Window();
+ let quitting = false, agentsClosed = 0;
+ keepWindowOnClose(win, { mac: true, isQuitting: () => quitting, save: () => {} });
+ quitAfterStudio({ preventDefault() { assert.fail('without Studio, Quit must proceed'); } }, {
+ quitting, markQuitting: () => { quitting = true; }, studio: null,
+ closeAgents: () => agentsClosed++, quit: () => assert.fail('no second Quit needed'),
+ });
+ win.close();
+ assert.equal(quitting, true);
+ assert.equal(win.destroyed, true);
+ assert.equal(agentsClosed, 1);
+ });
+}
+
+for (const fail of [false, true]) {
+ test(`Quit waits for Studio then re-enters without another shutdown, failure=${fail}`, async () => {
+ const win = new Window();
+ let quitting = false, stopped = 0, resolve, reject;
+ const done = new Promise((yes, no) => { resolve = yes; reject = no; });
+ const studio = { quit: () => { stopped++; return done; } };
+ keepWindowOnClose(win, { mac: true, isQuitting: () => quitting, save: () => {} });
+ let prevented = false;
+ const options = () => ({
+ quitting, markQuitting: () => { quitting = true; }, studio, closeAgents: () => {},
+ quit: () => {
+ quitAfterStudio({ preventDefault() { assert.fail('second Quit must proceed'); } }, options());
+ win.close();
+ },
+ });
+ quitAfterStudio({ preventDefault() { prevented = true; } }, options());
+ assert.equal(prevented, true);
+ assert.equal(quitting, true);
+ assert.equal(win.destroyed, false);
+ if (fail) reject(new Error('Studio already stopped')); else resolve();
+ await done.catch(() => {});
+ assert.equal(win.destroyed, true);
+ assert.equal(stopped, 1);
+ });
+}