Skip to content

Commit b6a5b15

Browse files
committed
fix(desktop): show project and thread in window titles
1 parent 8ddf200 commit b6a5b15

3 files changed

Lines changed: 95 additions & 6 deletions

File tree

‎apps/desktop/src/window/DesktopWindow.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function makeFakeBrowserWindow() {
7878
focus: vi.fn(),
7979
isDestroyed: vi.fn(() => false),
8080
getURL: vi.fn(() => "t3code-dev://app/"),
81+
getTitle: vi.fn(() => "T3 Code Dev"),
8182
getZoomLevel: vi.fn(() => zoomLevel),
8283
getZoomFactor: vi.fn(() => 1.2 ** zoomLevel),
8384
setZoomLevel: vi.fn((level: number) => {
@@ -611,8 +612,61 @@ describe("DesktopWindow", () => {
611612
navigationUrl: "not a url",
612613
}),
613614
);
615+
assert.isFalse(
616+
DesktopWindow.isSameOriginRendererNavigation({
617+
applicationUrl: "t3code://app/",
618+
navigationUrl: "t3code://other/",
619+
}),
620+
);
621+
assert.isFalse(
622+
DesktopWindow.isSameOriginRendererNavigation({
623+
applicationUrl: "t3code://app/",
624+
navigationUrl: "file:///tmp/page.html",
625+
}),
626+
);
614627
});
615628

629+
it.effect("keeps application titles on load and falls back for empty or external titles", () =>
630+
Effect.gen(function* () {
631+
const fakeWindow = makeFakeBrowserWindow();
632+
const createCount = yield* Ref.make(0);
633+
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
634+
const layer = layerTest({ window: fakeWindow.window, createCount, mainWindow });
635+
636+
yield* Effect.gen(function* () {
637+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
638+
const environment = yield* DesktopEnvironment.DesktopEnvironment.pipe(
639+
Effect.provide(layerDesktopEnvironment),
640+
);
641+
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
642+
const updateTitle = fakeWindow.windowListeners.get("page-title-updated");
643+
const finishLoad = fakeWindow.webContentsListeners.get("did-finish-load");
644+
if (!updateTitle || !finishLoad) {
645+
return yield* Effect.die("title listeners were not registered");
646+
}
647+
const event = { preventDefault: vi.fn() };
648+
const title = "project / thread - T3 Code Dev";
649+
updateTitle(event, title);
650+
assert.equal(vi.mocked(fakeWindow.window.setTitle).mock.lastCall?.[0], title);
651+
vi.mocked(fakeWindow.window.webContents.getTitle).mockReturnValue(title);
652+
finishLoad();
653+
assert.equal(vi.mocked(fakeWindow.window.setTitle).mock.lastCall?.[0], title);
654+
updateTitle(event, " ");
655+
assert.equal(
656+
vi.mocked(fakeWindow.window.setTitle).mock.lastCall?.[0],
657+
environment.displayName,
658+
);
659+
vi.mocked(fakeWindow.window.webContents.getURL).mockReturnValue("https://example.com/");
660+
updateTitle(event, "external page");
661+
assert.equal(
662+
vi.mocked(fakeWindow.window.setTitle).mock.lastCall?.[0],
663+
environment.displayName,
664+
);
665+
assert.equal(event.preventDefault.mock.calls.length, 3);
666+
}).pipe(Effect.provide(layer));
667+
}),
668+
);
669+
616670
it.effect("does not open a development window until the backend is ready", () =>
617671
Effect.gen(function* () {
618672
const fakeWindow = makeFakeBrowserWindow();

‎apps/desktop/src/window/DesktopWindow.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,14 @@ export function isSameOriginRendererNavigation(input: {
216216
readonly navigationUrl: string;
217217
}): boolean {
218218
try {
219-
return new URL(input.applicationUrl).origin === new URL(input.navigationUrl).origin;
219+
const application = new URL(input.applicationUrl);
220+
const navigation = new URL(input.navigationUrl);
221+
// Custom application protocols have an opaque ("null") URL origin.
222+
return (
223+
application.protocol === navigation.protocol &&
224+
application.host === navigation.host &&
225+
application.origin === navigation.origin
226+
);
220227
} catch {
221228
return false;
222229
}
@@ -671,9 +678,16 @@ export const make = Effect.gen(function* () {
671678
if (input.type === "gestureScrollEnd") window.webContents.send(TRACKPAD_SCROLL_END_CHANNEL);
672679
});
673680

674-
window.on("page-title-updated", (event) => {
681+
const syncWindowTitle = (title: string) => {
682+
const trusted = isSameOriginRendererNavigation({
683+
applicationUrl,
684+
navigationUrl: window.webContents.getURL(),
685+
});
686+
window.setTitle(trusted && title.trim() ? title : environment.displayName);
687+
};
688+
window.on("page-title-updated", (event, title) => {
675689
event.preventDefault();
676-
window.setTitle(environment.displayName);
690+
syncWindowTitle(title);
677691
});
678692
window.on("resize", scheduleBoundsPersist);
679693
window.on("move", scheduleBoundsPersist);
@@ -748,7 +762,7 @@ export const make = Effect.gen(function* () {
748762
}
749763
clearDevelopmentLoadRetry();
750764
developmentLoadRetryIndex = 0;
751-
window.setTitle(environment.displayName);
765+
syncWindowTitle(window.webContents.getTitle());
752766
if (environment.platform === "darwin") syncMacosWindowButtons(window);
753767
});
754768
window.webContents.on(

‎apps/web/src/routes/__root.tsx‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type ErrorComponentProps,
1010
useLocation,
1111
useNavigate,
12+
useParams,
1213
useRouter,
1314
} from "@tanstack/react-router";
1415
import { Check, Copy } from "lucide";
@@ -75,7 +76,14 @@ import {
7576
primaryServerConfigEventAtom,
7677
primaryServerWelcomeAtom,
7778
} from "../state/server";
78-
import { readProject, setActiveEnvironmentId, useActiveEnvironmentId } from "../state/entities";
79+
import {
80+
readProject,
81+
setActiveEnvironmentId,
82+
useActiveEnvironmentId,
83+
useProject,
84+
useThreadShell,
85+
} from "../state/entities";
86+
import { resolveThreadRouteRef } from "../threadRoutes";
7987
import {
8088
createKeybindingsUpdateToastController,
8189
type KeybindingsUpdateToastController,
@@ -337,14 +345,27 @@ function FontAppearanceSync() {
337345
}
338346

339347
function DocumentTitleSync() {
348+
const { environmentId, threadId } = useParams({ strict: false });
349+
const threadRef = useMemo(
350+
() => resolveThreadRouteRef({ environmentId, threadId }),
351+
[environmentId, threadId],
352+
);
353+
const thread = useThreadShell(threadRef);
354+
const project = useProject(
355+
thread ? scopeProjectRef(thread.environmentId, thread.projectId) : null,
356+
);
340357
const primaryServerVersion =
341358
useAtomValue(primaryServerConfigAtom)?.environment.serverVersion ?? null;
342-
const title = resolveServerBackedAppDisplayName({
359+
const appName = resolveServerBackedAppDisplayName({
343360
baseName: APP_BASE_NAME,
344361
fallbackDisplayName: APP_DISPLAY_NAME,
345362
fallbackStageLabel: APP_STAGE_LABEL,
346363
primaryServerVersion,
347364
});
365+
const title =
366+
thread && !thread.deletedAt && project
367+
? `${project.title} / ${thread.title} - ${appName}`
368+
: appName;
348369

349370
useEffect(() => {
350371
document.title = title;

0 commit comments

Comments
 (0)