Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(cli): hide the Dock icon again after opening the GUI from a CLI run

Etienne Lescot4f4bb997cafc
diff --git a/electron/cli/cliMain.ts b/electron/cli/cliMain.ts
index 7c446bef3..0e5d9aea9 100644
--- a/electron/cli/cliMain.ts
+++ b/electron/cli/cliMain.ts
@@ -278,7 +278,7 @@ export function runCli(command: CliCommand): void {
// to this process on to the GUI (see openGui.ts).
if (process.platform === "darwin") {
app.dock?.hide();
- app.on("activate", () => openGuiFromCli());
+ app.on("activate", () => openGuiFromCli(process.execPath, Date.now(), () => app.dock?.hide()));
}
app.on("window-all-closed", () => {
diff --git a/electron/cli/openGui.test.ts b/electron/cli/openGui.test.ts
index d6c00230f..e0fe782f1 100644
--- a/electron/cli/openGui.test.ts
+++ b/electron/cli/openGui.test.ts
@@ -25,9 +25,19 @@ describe("appBundleOf", () => {
});
describe("openGuiFromCli", () => {
+ let child: { handlers: Map<string, () => void>; on: unknown; unref: unknown } | undefined;
+
beforeEach(() => {
spawn.mockReset();
- spawn.mockReturnValue({ on: vi.fn(), unref: vi.fn() });
+ spawn.mockImplementation(() => {
+ const handlers = new Map<string, () => void>();
+ child = {
+ handlers,
+ on: vi.fn((event, handler) => handlers.set(event, handler)),
+ unref: vi.fn(),
+ };
+ return child;
+ });
});
// Each test launches at its own time, far from the others, so the launch guard never
@@ -40,6 +50,18 @@ describe("openGuiFromCli", () => {
});
});
+ it("runs afterLaunch once open is done, and not before", () => {
+ const afterLaunch = vi.fn();
+ openGuiFromCli(
+ "/Applications/Openscreen.app/Contents/MacOS/Openscreen",
+ 1_500_000,
+ afterLaunch,
+ );
+ expect(afterLaunch).not.toHaveBeenCalled();
+ child?.handlers.get("exit")?.();
+ expect(afterLaunch).toHaveBeenCalledTimes(1);
+ });
+
it("does nothing outside a bundle", () => {
openGuiFromCli("/usr/local/bin/openscreen", 2_000_000);
expect(spawn).not.toHaveBeenCalled();
diff --git a/electron/cli/openGui.ts b/electron/cli/openGui.ts
index 4df915c9a..6fe114d1c 100644
--- a/electron/cli/openGui.ts
+++ b/electron/cli/openGui.ts
@@ -34,10 +34,18 @@ export function appBundleOf(execPath: string): string | null {
const LAUNCH_GUARD_MS = 2000;
let lastLaunchAt = Number.NEGATIVE_INFINITY;
-/** Start the GUI from its bundle as a new instance. A no-op outside a packaged `.app`. */
+/**
+ * Start the GUI from its bundle as a new instance. A no-op outside a packaged `.app`.
+ *
+ * `afterLaunch` runs once `open` is done. Launching an app through LaunchServices turns the
+ * launching process into a regular one, with its own Dock icon, for as long as it lives
+ * (measured on macOS 26.5 with a hidden Electron 41 process, #1233; `open -g` does the same).
+ * The CLI process hides its Dock icon again there.
+ */
export function openGuiFromCli(
execPath: string = process.execPath,
now: number = Date.now(),
+ afterLaunch?: () => void,
): void {
const bundle = appBundleOf(execPath);
if (!bundle) return;
@@ -47,5 +55,6 @@ export function openGuiFromCli(
child.on("error", () => {
// Nothing to report to: stdout and stderr belong to the CLI protocol.
});
+ if (afterLaunch) child.on("exit", afterLaunch);
child.unref();
}