diff --git a/apps/desktop/src/window/DesktopWindow.test.ts b/apps/desktop/src/window/DesktopWindow.test.ts index 497b315ef522..5eb2c1f92d25 100644 --- a/apps/desktop/src/window/DesktopWindow.test.ts +++ b/apps/desktop/src/window/DesktopWindow.test.ts @@ -1,7 +1,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, describe, it } from "@effect/vitest"; import * as Deferred from "effect/Deferred"; -import { DesktopSnapShotId } from "@t3tools/contracts"; +import { DEFAULT_CLIENT_SETTINGS, DesktopSnapShotId } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; @@ -163,9 +163,9 @@ function makeFakeBrowserWindow() { }; } -const layerDesktopClientSettings = Layer.mock(DesktopClientSettings.DesktopClientSettings)({ - get: Effect.succeedNone, -}); +const layerDesktopClientSettings = ( + get: DesktopClientSettings.DesktopClientSettings["Service"]["get"] = Effect.succeedNone, +) => Layer.mock(DesktopClientSettings.DesktopClientSettings)({ get }); const layerElectronApp = Layer.mock(ElectronApp.ElectronApp)({ quit: Effect.void, @@ -229,6 +229,7 @@ function layerTest(input: { readonly mainWindow: Ref.Ref>; readonly createdWindowOptions?: Electron.BrowserWindowConstructorOptions[]; readonly desktopSettings?: DesktopAppSettings.DesktopSettings; + readonly readClientSettings?: DesktopClientSettings.DesktopClientSettings["Service"]["get"]; readonly mainWindowBoundsUpdates?: DesktopAppSettings.DesktopWindowBounds[]; readonly mainWindowMaximizedUpdates?: boolean[]; readonly beforeMainWindowBoundsUpdate?: ( @@ -316,7 +317,7 @@ function layerTest(input: { }), layerDesktopEnvironment, layerDesktopAppSettings, - layerDesktopClientSettings, + layerDesktopClientSettings(input.readClientSettings), layerDesktopServerExposure, DesktopState.layer, layerElectronApp, @@ -437,7 +438,7 @@ const makeSplashScenario = (createOutcomes: readonly (Electron.BrowserWindow | n }), layerDesktopEnvironment, DesktopAppSettings.layerTest(), - layerDesktopClientSettings, + layerDesktopClientSettings(), layerDesktopServerExposure, DesktopState.layer, layerElectronApp, @@ -1247,29 +1248,73 @@ describe("DesktopWindow", () => { yield* Effect.gen(function* () { const desktopWindow = yield* DesktopWindow.DesktopWindow; yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773")); - const swipe = fakeWindow.windowListeners.get("swipe"); - assert.isDefined(swipe); - if (!swipe) return; + const listener = fakeWindow.windowListeners.get("swipe"); + assert.isDefined(listener); + if (!listener) return; + const swipe = (direction: string) => Effect.promise(async () => listener({}, direction)); const history = fakeWindow.navigationHistory; - swipe({}, "left"); + yield* swipe("left"); assert.equal(history.goBack.mock.calls.length, 1); assert.equal(history.goForward.mock.calls.length, 0); - swipe({}, "right"); + yield* swipe("right"); assert.equal(history.goForward.mock.calls.length, 1); - swipe({}, "up"); - swipe({}, "down"); + yield* swipe("up"); + yield* swipe("down"); history.canGoBack.mockReturnValue(false); history.canGoForward.mockReturnValue(false); - swipe({}, "left"); - swipe({}, "right"); + yield* swipe("left"); + yield* swipe("right"); assert.equal(history.goBack.mock.calls.length, 1); assert.equal(history.goForward.mock.calls.length, 1); }).pipe(Effect.provide(layer)); }), ); + it.effect.each([ + { + name: "turned off", + read: Effect.succeedSome({ ...DEFAULT_CLIENT_SETTINGS, swipeNavigationEnabled: false }), + navigations: 0, + }, + { + name: "unreadable, so the default applies", + read: Effect.fail( + new DesktopClientSettings.DesktopClientSettingsReadError({ + operation: "decode-document", + path: "/fake/client-settings.json", + cause: new Error("bad json"), + }), + ), + navigations: 1, + }, + ])("handles macOS swipes when swipe navigation settings are $name", ({ read, navigations }) => + Effect.gen(function* () { + const fakeWindow = makeFakeBrowserWindow(); + const createCount = yield* Ref.make(0); + const mainWindow = yield* Ref.make>(Option.none()); + const layer = layerTest({ + window: fakeWindow.window, + createCount, + mainWindow, + readClientSettings: read, + }); + + yield* Effect.gen(function* () { + const desktopWindow = yield* DesktopWindow.DesktopWindow; + yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773")); + const listener = fakeWindow.windowListeners.get("swipe"); + assert.isDefined(listener); + if (!listener) return; + yield* Effect.promise(async () => listener({}, "left")); + yield* Effect.promise(async () => listener({}, "right")); + assert.equal(fakeWindow.navigationHistory.goBack.mock.calls.length, navigations); + assert.equal(fakeWindow.navigationHistory.goForward.mock.calls.length, navigations); + }).pipe(Effect.provide(layer)); + }), + ); + it.effect("persists the current main window bounds before the window closes", () => Effect.gen(function* () { const fakeWindow = makeFakeBrowserWindow(); diff --git a/apps/desktop/src/window/DesktopWindow.ts b/apps/desktop/src/window/DesktopWindow.ts index 43404e2d6cfb..2cbbb04e3918 100644 --- a/apps/desktop/src/window/DesktopWindow.ts +++ b/apps/desktop/src/window/DesktopWindow.ts @@ -832,11 +832,18 @@ export const make = Effect.gen(function* () { if (environment.platform === "darwin") { // Mouse drivers can deliver Back/Forward as macOS swipe gestures instead of buttons. - window.on("swipe", (_event, direction) => { - const history = window.webContents.navigationHistory; - if (direction === "left" && history.canGoBack()) history.goBack(); - else if (direction === "right" && history.canGoForward()) history.goForward(); - }); + window.on("swipe", (_event, direction) => + runPromise(clientSettings.get.pipe(Effect.orElseSucceed(Option.none))).then((settings) => { + const enabled = Option.match(settings, { + onNone: () => DEFAULT_CLIENT_SETTINGS.swipeNavigationEnabled, + onSome: (current) => current.swipeNavigationEnabled, + }); + if (!enabled || window.isDestroyed()) return; + const history = window.webContents.navigationHistory; + if (direction === "left" && history.canGoBack()) history.goBack(); + else if (direction === "right" && history.canGoForward()) history.goForward(); + }), + ); window.on("enter-full-screen", () => { window.webContents.send(WINDOW_FULLSCREEN_STATE_CHANNEL, true); }); diff --git a/apps/web/src/components/AppSidebarLayout.tsx b/apps/web/src/components/AppSidebarLayout.tsx index e4f3d690113e..19f741c7cb11 100644 --- a/apps/web/src/components/AppSidebarLayout.tsx +++ b/apps/web/src/components/AppSidebarLayout.tsx @@ -31,7 +31,7 @@ import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore"; import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; import { cn, isMacPlatform } from "../lib/utils"; import { primaryServerKeybindingsAtom } from "../state/server"; -import { useEnvironmentIdentificationMode } from "../hooks/useSettings"; +import { useClientSettings, useEnvironmentIdentificationMode } from "../hooks/useSettings"; import { PanelAnimationSuppressionProvider, usePanelAnimationSettings, @@ -421,6 +421,7 @@ export function AppSidebarLayout({ })); const { active: panelAnimationsActive, durationMs: panelAnimationDurationMs } = usePanelAnimationSettings(); + const swipeNavigationEnabled = useClientSettings((settings) => settings.swipeNavigationEnabled); const pathname = useLocation({ select: (location) => location.pathname }); const panelAnimationsSuppressed = usePanelNavigationSuppression(pathname); const routePanelAnimationsActive = panelAnimationsActive && !panelAnimationsSuppressed; @@ -491,7 +492,7 @@ export function AppSidebarLayout({ {children} - {isElectron ? : null} + {isElectron && swipeNavigationEnabled ? : null} ); diff --git a/apps/web/src/components/settings/SettingsPanels.tsx b/apps/web/src/components/settings/SettingsPanels.tsx index 82d6a35df681..df9e8a469262 100644 --- a/apps/web/src/components/settings/SettingsPanels.tsx +++ b/apps/web/src/components/settings/SettingsPanels.tsx @@ -578,6 +578,9 @@ export function useSettingsRestore(onRestored?: () => void) { ? ["Delete confirmation"] : []), ...(settings.confirmQuit !== DEFAULT_UNIFIED_SETTINGS.confirmQuit ? ["Quit shortcut"] : []), + ...(settings.swipeNavigationEnabled !== DEFAULT_UNIFIED_SETTINGS.swipeNavigationEnabled + ? ["Swipe navigation"] + : []), ...(isTextGenerationModelDirty ? ["Text generation model"] : []), ...(settings.threadTitleInstructions !== DEFAULT_UNIFIED_SETTINGS.threadTitleInstructions ? ["Thread title instructions"] @@ -689,6 +692,7 @@ export function useSettingsRestore(onRestored?: () => void) { confirmThreadDelete: DEFAULT_UNIFIED_SETTINGS.confirmThreadDelete, confirmThreadUnpin: DEFAULT_UNIFIED_SETTINGS.confirmThreadUnpin, confirmQuit: DEFAULT_UNIFIED_SETTINGS.confirmQuit, + swipeNavigationEnabled: DEFAULT_UNIFIED_SETTINGS.swipeNavigationEnabled, textGenerationModelSelection: DEFAULT_UNIFIED_SETTINGS.textGenerationModelSelection, threadTitleInstructions: DEFAULT_UNIFIED_SETTINGS.threadTitleInstructions, fontFamilySans: DEFAULT_UNIFIED_SETTINGS.fontFamilySans, @@ -1038,6 +1042,39 @@ export function GeneralSettingsPanel() { ) : null} + {isElectron ? ( + + Swipe left or right with two fingers on a trackpad to go back or forward. + } + resetAction={ + settings.swipeNavigationEnabled !== + DEFAULT_UNIFIED_SETTINGS.swipeNavigationEnabled ? ( + + updateSettings({ + swipeNavigationEnabled: DEFAULT_UNIFIED_SETTINGS.swipeNavigationEnabled, + }) + } + /> + ) : null + } + control={ + + updateSettings({ swipeNavigationEnabled: Boolean(checked) }) + } + aria-label={gt("Swipe to navigate back and forward")} + /> + } + /> + + ) : null} +