Skip to content
Open
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
7 changes: 4 additions & 3 deletions apps/desktop/src/app/DesktopLifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ function layerElectronWindow(destroyAll: Effect.Effect<void> = Effect.void) {
create: () => Effect.die("unexpected window creation"),
main: Effect.die("unexpected main window read"),
currentMainOrFirst: Effect.die("unexpected current window read"),
focusedMainOrFirst: Effect.die("unexpected focused window read"),
apps: Effect.succeed([]),
focusedApp: Effect.succeedNone,
registerApp: () => Effect.void,
setMain: () => Effect.void,
clearMain: () => Effect.void,
prepareReveal: () => Effect.succeed(false),
Expand All @@ -90,7 +92,7 @@ function layerDesktopWindow(
readonly flushMainWindowBounds?: Effect.Effect<void>;
} = {},
) {
return Layer.succeed(DesktopWindow.DesktopWindow, {
return Layer.mock(DesktopWindow.DesktopWindow)({
createMain: Effect.die("unexpected window creation"),
ensureMain: Effect.die("unexpected window creation"),
revealOrCreateMain: Effect.die("unexpected window creation"),
Expand All @@ -103,7 +105,6 @@ function layerDesktopWindow(
prepareCaptureReveal: Effect.void,
dispatchMenuAction: () => Effect.void,
dispatchSnapShotEvent: () => Effect.void,
zoomMain: () => Effect.void,
runMainContentsCommand: () => Effect.void,
syncAppearance: Effect.void,
});
Expand Down
5 changes: 2 additions & 3 deletions apps/desktop/src/backend/DesktopBackendPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function layerPool(
showErrorBox: () => Effect.void,
} satisfies ElectronDialog.ElectronDialog["Service"]),
ElectronApp.layer,
Layer.succeed(DesktopWindow.DesktopWindow, {
Layer.mock(DesktopWindow.DesktopWindow)({
createMain: Effect.die("unexpected window create"),
ensureMain: Effect.die("unexpected window ensure"),
revealOrCreateMain: Effect.die("unexpected window reveal"),
Expand All @@ -216,11 +216,10 @@ function layerPool(
prepareCaptureReveal: Effect.void,
dispatchMenuAction: () => Effect.die("unexpected menu action"),
dispatchSnapShotEvent: () => Effect.void,
zoomMain: () => Effect.die("unexpected zoom"),
runMainContentsCommand:
options.onMainContentsCommand ?? (() => Effect.die("unexpected main contents command")),
syncAppearance: Effect.void,
} satisfies DesktopWindow.DesktopWindow["Service"]),
}),
),
),
);
Expand Down
41 changes: 32 additions & 9 deletions apps/desktop/src/electron/ElectronWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ export class ElectronWindow extends Context.Service<
) => Effect.Effect<Electron.BrowserWindow, ElectronWindowCreateError>;
readonly main: Effect.Effect<Option.Option<Electron.BrowserWindow>>;
readonly currentMainOrFirst: Effect.Effect<Option.Option<Electron.BrowserWindow>>;
readonly focusedMainOrFirst: Effect.Effect<Option.Option<Electron.BrowserWindow>>;
/** Windows that load the app renderer: the main window first, then secondary windows. */
readonly apps: Effect.Effect<ReadonlyArray<Electron.BrowserWindow>>;
/** The focused app window, else the main window; never a popup or auxiliary window. */
readonly focusedApp: Effect.Effect<Option.Option<Electron.BrowserWindow>>;
readonly registerApp: (window: Electron.BrowserWindow) => Effect.Effect<void>;
readonly setMain: (window: Electron.BrowserWindow) => Effect.Effect<void>;
readonly clearMain: (window: Option.Option<Electron.BrowserWindow>) => Effect.Effect<void>;
readonly prepareReveal: (window: Electron.BrowserWindow) => Effect.Effect<boolean>;
Expand Down Expand Up @@ -148,6 +152,7 @@ export const make = Effect.gen(function* () {
yield* Effect.addFinalizer(() => Effect.sync(() => windowsForegroundFocus?.close()));
const mainWindowRef = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const auxiliaryWindows = new WeakSet<Electron.BrowserWindow>();
const appWindows = new Set<Electron.BrowserWindow>();
const titleBarOverlayWindows = new WeakSet<Electron.BrowserWindow>();

const listWindows = Effect.try({
Expand Down Expand Up @@ -198,9 +203,22 @@ export const make = Effect.gen(function* () {
return first;
});

const focusedMainOrFirst = Effect.gen(function* () {
const liveApps = Effect.gen(function* () {
const main = yield* liveMain;
const apps: Electron.BrowserWindow[] = Option.isSome(main) ? [main.value] : [];
for (const window of appWindows) {
if (yield* isWindowDestroyed(window)) {
appWindows.delete(window);
} else if (!apps.includes(window)) {
apps.push(window);
}
}
return apps;
});

const focusedApp = Effect.gen(function* () {
const focused = yield* Effect.try({
try: () => Option.fromNullishOr(Electron.BrowserWindow.getFocusedWindow() ?? null),
try: () => Electron.BrowserWindow.getFocusedWindow(),
catch: (cause) =>
new ElectronWindowOperationError({
operation: "get-focused-window",
Expand All @@ -210,10 +228,9 @@ export const make = Effect.gen(function* () {
cause,
}),
}).pipe(Effect.orDie);
if (Option.isSome(focused) && !(yield* isWindowDestroyed(focused.value))) {
return focused;
}
return yield* currentMainOrFirst;
const apps = yield* liveApps;
if (focused !== null && apps.includes(focused)) return Option.some(focused);
return yield* liveMain;
});

return ElectronWindow.of({
Expand Down Expand Up @@ -255,9 +272,15 @@ export const make = Effect.gen(function* () {
}),
main: liveMain,
currentMainOrFirst,
focusedMainOrFirst,
apps: liveApps,
focusedApp,
registerApp: Effect.fn("ElectronWindow.registerApp")(function (window) {
return Effect.sync(() => void appWindows.add(window));
}),
setMain: Effect.fn("ElectronWindow.setMain")(function (window) {
return Ref.set(mainWindowRef, Option.some(window));
return Ref.set(mainWindowRef, Option.some(window)).pipe(
Effect.andThen(Effect.sync(() => void appWindows.add(window))),
);
}),
clearMain: Effect.fn("ElectronWindow.clearMain")(function (window) {
return Ref.update(mainWindowRef, (current) => {
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/i18n/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"df6ed2014f1737a8": "Verkleinern",
"50fe114d093a5bc7": "Vollbildmodus ein/aus",
"d8c6d6d9845353da": "Fenster",
"c015841b7b2523c6": "Neues Fenster",
"f184aa190588625e": "Minimieren",
"c20a9deaed71bd1c": "Zoomen",
"d207ba3a23f94223": "Alle nach vorne bringen",
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/i18n/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"df6ed2014f1737a8": "Zoom Out",
"50fe114d093a5bc7": "Toggle Full Screen",
"d8c6d6d9845353da": "Window",
"c015841b7b2523c6": "New Window",
"f184aa190588625e": "Minimize",
"c20a9deaed71bd1c": "Zoom",
"d207ba3a23f94223": "Bring All to Front",
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/i18n/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"df6ed2014f1737a8": "Alejar",
"50fe114d093a5bc7": "Activar/desactivar pantalla completa",
"d8c6d6d9845353da": "Ventana",
"c015841b7b2523c6": "Nueva ventana",
"f184aa190588625e": "Minimizar",
"c20a9deaed71bd1c": "Zoom",
"d207ba3a23f94223": "Traer todo al frente",
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/i18n/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"df6ed2014f1737a8": "Zoom arrière",
"50fe114d093a5bc7": "Activer/désactiver le plein écran",
"d8c6d6d9845353da": "Fenêtre",
"c015841b7b2523c6": "Nouvelle fenêtre",
"f184aa190588625e": "Réduire",
"c20a9deaed71bd1c": "Zoom",
"d207ba3a23f94223": "Tout ramener au premier plan",
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/i18n/translations/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"df6ed2014f1737a8": "縮小",
"50fe114d093a5bc7": "フルスクリーンに切り替え",
"d8c6d6d9845353da": "ウインドウ",
"c015841b7b2523c6": "新規ウインドウ",
"f184aa190588625e": "しまう",
"c20a9deaed71bd1c": "拡大/縮小",
"d207ba3a23f94223": "すべてを手前に移動",
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/i18n/translations/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"df6ed2014f1737a8": "축소",
"50fe114d093a5bc7": "전체 화면 전환",
"d8c6d6d9845353da": "윈도우",
"c015841b7b2523c6": "새 윈도우",
"f184aa190588625e": "최소화",
"c20a9deaed71bd1c": "확대/축소",
"d207ba3a23f94223": "모두 앞으로 가져오기",
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/i18n/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"df6ed2014f1737a8": "Diminuir zoom",
"50fe114d093a5bc7": "Alternar tela cheia",
"d8c6d6d9845353da": "Janela",
"c015841b7b2523c6": "Nova janela",
"f184aa190588625e": "Minimizar",
"c20a9deaed71bd1c": "Zoom",
"d207ba3a23f94223": "Trazer tudo para a frente",
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/i18n/translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"df6ed2014f1737a8": "Уменьшить",
"50fe114d093a5bc7": "Полноэкранный режим",
"d8c6d6d9845353da": "Окно",
"c015841b7b2523c6": "Новое окно",
"f184aa190588625e": "Свернуть",
"c20a9deaed71bd1c": "Масштаб",
"d207ba3a23f94223": "Все окна на передний план",
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/i18n/translations/sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"df6ed2014f1737a8": "Zooma ut",
"50fe114d093a5bc7": "Växla helskärm",
"d8c6d6d9845353da": "Fönster",
"c015841b7b2523c6": "Nytt fönster",
"f184aa190588625e": "Minimera",
"c20a9deaed71bd1c": "Zooma",
"d207ba3a23f94223": "Lägg alla överst",
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/i18n/translations/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"df6ed2014f1737a8": "缩小",
"50fe114d093a5bc7": "切换全屏",
"d8c6d6d9845353da": "窗口",
"c015841b7b2523c6": "新建窗口",
"f184aa190588625e": "最小化",
"c20a9deaed71bd1c": "缩放",
"d207ba3a23f94223": "前置全部窗口",
Expand Down
28 changes: 21 additions & 7 deletions apps/desktop/src/ipc/DesktopIpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ vi.mock("electron", () => ({}));
import * as ElectronWindow from "../electron/ElectronWindow.ts";
import * as DesktopIpc from "./DesktopIpc.ts";

it.effect("answers only the main window's top frame", () =>
it.effect("answers only app windows' top frames and tells handlers which window sent", () =>
Effect.gen(function* () {
const mainFrame = {};
const secondaryFrame = {};
const mainWindow = { id: 10, webContents: { id: 1, mainFrame } };
const secondaryWindow = { id: 11, webContents: { id: 3, mainFrame: secondaryFrame } };
const invoke = new Map<string, DesktopIpc.DesktopIpcHandleListener>();
const sync = new Map<string, DesktopIpc.DesktopIpcSyncListener>();
const ipc = yield* DesktopIpc.DesktopIpc.pipe(
Expand All @@ -24,9 +27,11 @@ it.effect("answers only the main window's top frame", () =>
}).pipe(
Layer.provide(
Layer.mock(ElectronWindow.ElectronWindow, {
main: Effect.succeedSome({
webContents: { id: 1, mainFrame },
} as unknown as Electron.BrowserWindow),
main: Effect.succeedSome(mainWindow as unknown as Electron.BrowserWindow),
apps: Effect.succeed([
mainWindow,
secondaryWindow,
] as unknown as ReadonlyArray<Electron.BrowserWindow>),
}),
),
),
Expand All @@ -37,13 +42,21 @@ it.effect("answers only the main window's top frame", () =>
channel: "token",
payload: Schema.Void,
result: Schema.String,
handler: () => Effect.succeed("secret"),
handler: () =>
Effect.map(
DesktopIpc.DesktopIpcSender,
({ window, isMain }) => `secret-${window.id}${isMain ? "-main" : ""}`,
),
}),
);
yield* ipc.handleSync({ channel: "token-sync", handler: () => Effect.succeed("secret") });
yield* ipc.handleSync({
channel: "token-sync",
handler: () => Effect.map(DesktopIpc.DesktopIpcSender, ({ window }) => `secret-${window.id}`),
});

const senders = {
app: { sender: { id: 1 }, senderFrame: mainFrame },
secondary: { sender: { id: 3 }, senderFrame: secondaryFrame },
appSubframe: { sender: { id: 1 }, senderFrame: {} },
previewGuest: { sender: { id: 2 }, senderFrame: {} },
};
Expand All @@ -59,7 +72,8 @@ it.effect("answers only the main window's top frame", () =>
];
}
assert.deepEqual(results, {
app: ["secret", "secret"],
app: ["secret-10-main", "secret-10"],
secondary: ["secret-11", "secret-11"],
appSubframe: ["rejected", undefined],
previewGuest: ["rejected", undefined],
});
Expand Down
Loading