Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

PRD-10048: Open a Finder file in its already-open project window (#7818)

Andrew Mikofalvydf774cf3d31e
diff --git a/.changeset/finder-open-into-open-project.md b/.changeset/finder-open-into-open-project.md
new file mode 100644
index 000000000..0e4214b4a
--- /dev/null
+++ b/.changeset/finder-open-into-open-project.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge-desktop": patch
+---
+
+Opening a Markdown file from Finder or Open With now opens that file in its project's window when the window is already open or still loading, instead of only bringing the window to the front.
diff --git a/.changeset/share-branch-switch-while-loading.md b/.changeset/share-branch-switch-while-loading.md
new file mode 100644
index 000000000..abedf5500
--- /dev/null
+++ b/.changeset/share-branch-switch-while-loading.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge-desktop": patch
+---
+
+A share link that needs a branch switch now shows its branch-switch prompt when the project's window is still loading, instead of being dropped.
diff --git a/packages/desktop/src/main/index.ts b/packages/desktop/src/main/index.ts
index cdc1b63a7..0ee91659c 100644
--- a/packages/desktop/src/main/index.ts
+++ b/packages/desktop/src/main/index.ts
@@ -596,7 +596,9 @@ import {
bringWindowToFront,
collabUrlFromApiOrigin,
heldChildServer,
+ pendingDeepLinkPayload,
setWindowInstanceLabels,
+ shareBranchSwitchPayload,
type UtilityProcessLike,
WindowManager,
type WindowManagerDeps,
@@ -5088,19 +5090,13 @@ function registerIpcHandlers() {
| (BrowserWindowLike & { webContents: BrowserWindowLike['webContents'] })
| null;
if (existing) {
- sendToRenderer(existing.webContents, 'ok:deep-link', {
- doc: request.pendingDeepLinkTarget.path,
- kind: request.pendingDeepLinkTarget.kind,
- branch: request.pendingBranch ?? null,
- multiCandidate: request.pendingMultiCandidate === true,
- ...(request.pendingDeepLinkTarget.repositoryPath === undefined
- ? {}
- : { repositoryPath: request.pendingDeepLinkTarget.repositoryPath }),
- ...(request.pendingDeepLinkTarget.contentRootDepth === undefined
- ? {}
- : { contentRootDepth: request.pendingDeepLinkTarget.contentRootDepth }),
- ...(targetMissing ? { targetMissing: true } : {}),
+ const deepLink = pendingDeepLinkPayload({
+ pendingDeepLinkTarget: request.pendingDeepLinkTarget,
+ pendingBranch: request.pendingBranch,
+ pendingMultiCandidate: request.pendingMultiCandidate,
+ pendingTargetMissing: targetMissing,
});
+ if (deepLink) sendToRenderer(existing.webContents, 'ok:deep-link', deepLink);
return undefined;
}
}
@@ -5109,12 +5105,11 @@ function registerIpcHandlers() {
| (BrowserWindowLike & { webContents: BrowserWindowLike['webContents'] })
| null;
if (existing) {
- sendToRenderer(existing.webContents, 'ok:share:received', {
- kind: 'project-branch-switch' as const,
- share: request.pendingShareBranchSwitch.share,
- projectPath: request.pendingShareBranchSwitch.projectPath,
- currentBranch: request.pendingShareBranchSwitch.currentBranch,
- });
+ sendToRenderer(
+ existing.webContents,
+ 'ok:share:received',
+ shareBranchSwitchPayload(request.pendingShareBranchSwitch),
+ );
return undefined;
}
}
diff --git a/packages/desktop/src/main/window-manager.ts b/packages/desktop/src/main/window-manager.ts
index efaca6b15..c0dc9e00e 100644
--- a/packages/desktop/src/main/window-manager.ts
+++ b/packages/desktop/src/main/window-manager.ts
@@ -9,6 +9,10 @@ import {
desktopChannelLabel,
sliceLastSpawnAttempt,
} from '@inkeep/open-knowledge-core';
+import type {
+ OkDeepLinkPayload,
+ OkShareReceivedPayload,
+} from '@inkeep/open-knowledge-core/desktop-bridge';
import type { KeepaliveHandle } from '@inkeep/open-knowledge-core/keepalive';
import { projectSpawnErrorLogPath } from '@inkeep/open-knowledge-core/server';
import {
@@ -34,7 +38,7 @@ import {
utilityInitPhaseEvent,
} from '../shared/boot-narration.ts';
import type { OkServerRestartOutcome } from '../shared/bridge-contract.ts';
-import { registerPendingDelivery } from '../shared/ipc-send.ts';
+import { registerPendingDelivery, sendToRenderer } from '../shared/ipc-send.ts';
import type { AssetOpenResult } from './asset-allowlist.ts';
import { attachAssetSafetyNet } from './asset-safety-net.ts';
import { startBootHeartbeat } from './boot-heartbeat.ts';
@@ -375,8 +379,7 @@ export interface EphemeralOpenIdentity {
docName: string;
}
-interface CreateProjectWindowOpts {
- projectPath: string;
+export interface PendingDeepLinkOpts {
pendingDeepLinkTarget?: {
kind: 'doc' | 'folder';
path: string;
@@ -386,6 +389,10 @@ interface CreateProjectWindowOpts {
pendingBranch?: string | null;
pendingMultiCandidate?: boolean;
pendingTargetMissing?: boolean;
+}
+
+interface CreateProjectWindowOpts extends PendingDeepLinkOpts {
+ projectPath: string;
pendingShareBranchSwitch?: ShareDeepLinkBranchSwitchPayload;
didEnsureGit?: boolean;
consentVersion?: number;
@@ -394,6 +401,31 @@ interface CreateProjectWindowOpts {
freshlyCreated?: boolean;
}
+export function pendingDeepLinkPayload(opts: PendingDeepLinkOpts): OkDeepLinkPayload | undefined {
+ const target = opts.pendingDeepLinkTarget;
+ if (target === undefined) return undefined;
+ return {
+ doc: target.path,
+ kind: target.kind,
+ branch: opts.pendingBranch ?? null,
+ multiCandidate: opts.pendingMultiCandidate === true,
+ ...(target.repositoryPath === undefined ? {} : { repositoryPath: target.repositoryPath }),
+ ...(target.contentRootDepth === undefined ? {} : { contentRootDepth: target.contentRootDepth }),
+ ...(opts.pendingTargetMissing === true ? { targetMissing: true } : {}),
+ };
+}
+
+export function shareBranchSwitchPayload(
+ branchSwitch: ShareDeepLinkBranchSwitchPayload,
+): OkShareReceivedPayload {
+ return {
+ kind: 'project-branch-switch',
+ share: branchSwitch.share,
+ projectPath: branchSwitch.projectPath,
+ currentBranch: branchSwitch.currentBranch,
+ };
+}
+
export interface SpawnedServer {
readonly pid: number;
readExit(): { code: number | null; signal: string | null } | null;
@@ -632,6 +664,25 @@ export class WindowManager {
bringWindowToFront(win, () => this.deps.activateApp?.(), opts);
}
+ private focusOpenProjectWindow(
+ ctx: ProjectContext,
+ opts: CreateProjectWindowOpts,
+ ): ProjectContext {
+ this.bringToFront(ctx.window);
+ const deepLink = pendingDeepLinkPayload(opts);
+ if (deepLink) {
+ sendToRenderer(ctx.window.webContents, 'ok:deep-link', deepLink);
+ }
+ if (opts.pendingShareBranchSwitch) {
+ sendToRenderer(
+ ctx.window.webContents,
+ 'ok:share:received',
+ shareBranchSwitchPayload(opts.pendingShareBranchSwitch),
+ );
+ }
+ return ctx;
+ }
+
getContextForBrowserWindow(win: BrowserWindowLike): ProjectContext | undefined {
for (const ctx of this.windowsByPath.values()) {
if (ctx.window === win) return ctx;
@@ -1447,8 +1498,7 @@ export class WindowManager {
if (inFlight) {
const ctx = await inFlight;
if (ctx.window.isDestroyed?.() !== true) {
- this.bringToFront(ctx.window);
- return ctx;
+ return this.focusOpenProjectWindow(ctx, opts);
}
}
const work = (async (): Promise<ProjectContext> => {
@@ -1470,8 +1520,7 @@ export class WindowManager {
const existing = this.windowsByPath.get(canonicalKey);
if (existing) {
if (existing.window.isDestroyed?.() !== true) {
- this.bringToFront(existing.window);
- return existing;
+ return this.focusOpenProjectWindow(existing, opts);
}
this.deps.log?.warn(
{ canonicalKey },
@@ -1803,34 +1852,17 @@ export class WindowManager {
this.deps.startup?.markWindowCreated?.();
this.attachSafetyNet(window.webContents, apiOrigin, projectPath);
- if (opts.pendingDeepLinkTarget) {
- const doc = opts.pendingDeepLinkTarget.path;
- const kind = opts.pendingDeepLinkTarget.kind;
- const branch = opts.pendingBranch ?? null;
- const multiCandidate = opts.pendingMultiCandidate === true;
- registerPendingDelivery(window.webContents, 'ok:deep-link', {
- doc,
- kind,
- branch,
- multiCandidate,
- ...(opts.pendingDeepLinkTarget.repositoryPath === undefined
- ? {}
- : { repositoryPath: opts.pendingDeepLinkTarget.repositoryPath }),
- ...(opts.pendingDeepLinkTarget.contentRootDepth === undefined
- ? {}
- : { contentRootDepth: opts.pendingDeepLinkTarget.contentRootDepth }),
- ...(opts.pendingTargetMissing === true ? { targetMissing: true } : {}),
- });
+ const deepLink = pendingDeepLinkPayload(opts);
+ if (deepLink) {
+ registerPendingDelivery(window.webContents, 'ok:deep-link', deepLink);
}
if (opts.pendingShareBranchSwitch) {
- const branchSwitch = opts.pendingShareBranchSwitch;
- registerPendingDelivery(window.webContents, 'ok:share:received', {
- kind: 'project-branch-switch' as const,
- share: branchSwitch.share,
- projectPath: branchSwitch.projectPath,
- currentBranch: branchSwitch.currentBranch,
- });
+ registerPendingDelivery(
+ window.webContents,
+ 'ok:share:received',
+ shareBranchSwitchPayload(opts.pendingShareBranchSwitch),
+ );
}
const disposeShowGate = this.deps.showGate.register(window, { kind: 'editor' });
@@ -2544,33 +2576,22 @@ export class WindowManager {
return upgradeOk;
}
- private async attachToExistingServer(args: {
- projectPath: string;
- canonicalKey: string;
- projectName: string;
- lock: ServerLockMetadataLike;
- pendingDeepLinkTarget?: {
- kind: 'doc' | 'folder';
- path: string;
- repositoryPath?: string;
- contentRootDepth?: number;
- };
- pendingBranch?: string | null;
- pendingMultiCandidate?: boolean;
- pendingTargetMissing?: boolean;
- pendingShareBranchSwitch?: ShareDeepLinkBranchSwitchPayload;
- pendingServerRestartedToast?: boolean;
- freshlyCreated?: boolean;
- }): Promise<ProjectContext> {
+ private async attachToExistingServer(
+ args: PendingDeepLinkOpts & {
+ projectPath: string;
+ canonicalKey: string;
+ projectName: string;
+ lock: ServerLockMetadataLike;
+ pendingShareBranchSwitch?: ShareDeepLinkBranchSwitchPayload;
+ pendingServerRestartedToast?: boolean;
+ freshlyCreated?: boolean;
+ },
+ ): Promise<ProjectContext> {
const {
projectPath,
canonicalKey,
projectName,
lock,
- pendingDeepLinkTarget,
- pendingBranch,
- pendingMultiCandidate,
- pendingTargetMissing,
pendingShareBranchSwitch,
pendingServerRestartedToast,
freshlyCreated,
@@ -2613,34 +2634,17 @@ export class WindowManager {
this.deps.startup?.markWindowCreated?.();
this.attachSafetyNet(window.webContents, apiOrigin, projectPath);
- if (pendingDeepLinkTarget) {
- const doc = pendingDeepLinkTarget.path;
- const kind = pendingDeepLinkTarget.kind;
- const branch = pendingBranch ?? null;
- const multiCandidate = pendingMultiCandidate === true;
- registerPendingDelivery(window.webContents, 'ok:deep-link', {
- doc,
- kind,
- branch,
- multiCandidate,
- ...(pendingDeepLinkTarget.repositoryPath === undefined
- ? {}
- : { repositoryPath: pendingDeepLinkTarget.repositoryPath }),
- ...(pendingDeepLinkTarget.contentRootDepth === undefined
- ? {}
- : { contentRootDepth: pendingDeepLinkTarget.contentRootDepth }),
- ...(pendingTargetMissing === true ? { targetMissing: true } : {}),
- });
+ const deepLink = pendingDeepLinkPayload(args);
+ if (deepLink) {
+ registerPendingDelivery(window.webContents, 'ok:deep-link', deepLink);
}
if (pendingShareBranchSwitch) {
- const branchSwitch = pendingShareBranchSwitch;
- registerPendingDelivery(window.webContents, 'ok:share:received', {
- kind: 'project-branch-switch' as const,
- share: branchSwitch.share,
- projectPath: branchSwitch.projectPath,
- currentBranch: branchSwitch.currentBranch,
- });
+ registerPendingDelivery(
+ window.webContents,
+ 'ok:share:received',
+ shareBranchSwitchPayload(pendingShareBranchSwitch),
+ );
}
const selfProtocol = this.deps.selfProtocolVersion;
diff --git a/packages/desktop/tests/main/window-manager.test.ts b/packages/desktop/tests/main/window-manager.test.ts
index 157863438..55695740e 100644
--- a/packages/desktop/tests/main/window-manager.test.ts
+++ b/packages/desktop/tests/main/window-manager.test.ts
@@ -4947,6 +4947,73 @@ describe('WindowManager — pendingDeepLinkTarget dom-ready gate (M4 US-007 / Fi
multiCandidate: false,
});
});
+
+ test('open window: a second open with pendingDeepLinkTarget focuses it and delivers the target', async () => {
+ const wm = new WindowManager(env.deps);
+ const first = wm.createProjectWindow({ projectPath: '/tmp/deep-link-open' });
+ env.utilities[0]?.fire({ type: 'ready', port: 51224, apiOrigin: 'http://localhost:51224' });
+ const ctx = await first;
+ const window = env.windows[0];
+ if (!window) throw new Error('expected window to be created');
+ window.fireDomReady();
+ const send = window.webContents.send as ReturnType<typeof vi.fn>;
+ expect(send.mock.calls.filter((c) => c[0] === 'ok:deep-link')).toEqual([]);
+
+ const second = await wm.createProjectWindow({
+ projectPath: '/tmp/deep-link-open',
+ pendingDeepLinkTarget: { kind: 'doc', path: 'notes/opened-from-finder' },
+ pendingBranch: 'feature',
+ });
+
+ expect(second).toBe(ctx);
+ expect(env.windows.length).toBe(1);
+ expect(env.utilities.length).toBe(1);
+ expect(window.focus).toHaveBeenCalled();
+ expect(send.mock.calls.filter((c) => c[0] === 'ok:deep-link')).toEqual([
+ [
+ 'ok:deep-link',
+ { doc: 'notes/opened-from-finder', kind: 'doc', branch: 'feature', multiCandidate: false },
+ ],
+ ]);
+ });
+
+ test('opening window: an open with pendingDeepLinkTarget while the window loads delivers it without a later dom-ready', async () => {
+ const wm = new WindowManager(env.deps);
+ const first = wm.createProjectWindow({ projectPath: '/tmp/deep-link-opening' });
+ const second = wm.createProjectWindow({
+ projectPath: '/tmp/deep-link-opening',
+ pendingDeepLinkTarget: { kind: 'doc', path: 'notes/while-loading' },
+ });
+ env.utilities[0]?.fire({ type: 'ready', port: 51225, apiOrigin: 'http://localhost:51225' });
+ const [ctx1, ctx2] = await Promise.all([first, second]);
+
+ expect(ctx2).toBe(ctx1);
+ expect(env.windows.length).toBe(1);
+ const window = env.windows[0];
+ if (!window) throw new Error('expected window to be created');
+ const send = window.webContents.send as ReturnType<typeof vi.fn>;
+ expect(send.mock.calls.filter((c) => c[0] === 'ok:deep-link')).toEqual([
+ [
+ 'ok:deep-link',
+ { doc: 'notes/while-loading', kind: 'doc', branch: null, multiCandidate: false },
+ ],
+ ]);
+ });
+
+ test('open window: a second open without pendingDeepLinkTarget sends no deep link', async () => {
+ const wm = new WindowManager(env.deps);
+ const first = wm.createProjectWindow({ projectPath: '/tmp/deep-link-refocus' });
+ env.utilities[0]?.fire({ type: 'ready', port: 51226, apiOrigin: 'http://localhost:51226' });
+ await first;
+ const window = env.windows[0];
+ if (!window) throw new Error('expected window to be created');
+ window.fireDomReady();
+
+ await wm.createProjectWindow({ projectPath: '/tmp/deep-link-refocus' });
+
+ const send = window.webContents.send as ReturnType<typeof vi.fn>;
+ expect(send.mock.calls.filter((c) => c[0] === 'ok:deep-link')).toEqual([]);
+ });
});
describe('WindowManager — pendingShareBranchSwitch dom-ready gate (US-004)', () => {
@@ -5113,6 +5180,36 @@ describe('WindowManager — pendingShareBranchSwitch dom-ready gate (US-004)', (
currentBranch: 'main',
});
});
+
+ test('opening window: an open with pendingShareBranchSwitch while the window loads delivers it without a later dom-ready', async () => {
+ const branchSwitch = {
+ share: {
+ owner: 'inkeep',
+ repo: 'playbooks',
+ branch: 'feature/y',
+ path: 'docs/while-loading.md',
+ blobUrl: 'https://github.com/inkeep/playbooks/blob/feature/y/docs/while-loading.md',
+ },
+ projectPath: '/tmp/share-branch-switch-opening',
+ currentBranch: 'main',
+ };
+ const wm = new WindowManager(env.deps);
+ const first = wm.createProjectWindow({ projectPath: '/tmp/share-branch-switch-opening' });
+ const second = wm.createProjectWindow({
+ projectPath: '/tmp/share-branch-switch-opening',
+ pendingShareBranchSwitch: branchSwitch,
+ });
+ env.utilities[0]?.fire({ type: 'ready', port: 51890, apiOrigin: 'http://localhost:51890' });
+ const [ctx1, ctx2] = await Promise.all([first, second]);
+
+ expect(ctx2).toBe(ctx1);
+ const window = env.windows[0];
+ if (!window) throw new Error('expected window to be created');
+ const send = window.webContents.send as ReturnType<typeof vi.fn>;
+ expect(send.mock.calls.filter((c) => c[0] === 'ok:share:received')).toEqual([
+ ['ok:share:received', { kind: 'project-branch-switch', ...branchSwitch }],
+ ]);
+ });
});
describe('WindowManager.getWindowFor — canonicalization symmetry with focusWindowForProject', () => {