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
4 changes: 4 additions & 0 deletions apps/desktop/src/app/DesktopApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import * as PreviewPasskeys from "../preview/Passkeys.ts";
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
import * as DesktopShellEnvironment from "../shell/DesktopShellEnvironment.ts";
import * as DesktopSnapShot from "../snapShot/DesktopSnapShot.ts";
import * as DesktopSpellcheck from "../settings/DesktopSpellcheck.ts";
import * as DesktopRendererHistory from "../telemetry/DesktopRendererHistory.ts";
import * as DesktopUpdates from "../updates/DesktopUpdates.ts";
import * as DesktopApplicationMenu from "../window/DesktopApplicationMenu.ts";
Expand Down Expand Up @@ -160,6 +161,9 @@ const runStubbedDesktop = (stub: StubbedDesktop) =>
Effect.provideService(DesktopLegacyLocalStorage.DesktopLegacyLocalStorage, {
load: () => Effect.void,
} as unknown as DesktopLegacyLocalStorage.DesktopLegacyLocalStorage["Service"]),
Effect.provideService(DesktopSpellcheck.DesktopSpellcheck, {
initialize: Effect.void,
} as DesktopSpellcheck.DesktopSpellcheck["Service"]),
Effect.provideService(DesktopSnapShot.DesktopSnapShot, {
initialize: Effect.void,
} as DesktopSnapShot.DesktopSnapShot["Service"]),
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/app/DesktopApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import * as DesktopState from "./DesktopState.ts";
import * as DesktopRemoteUpdates from "../updates/DesktopRemoteUpdates.ts";
import * as DesktopUpdates from "../updates/DesktopUpdates.ts";
import * as DesktopSnapShot from "../snapShot/DesktopSnapShot.ts";
import * as DesktopSpellcheck from "../settings/DesktopSpellcheck.ts";
import * as DesktopWslBackend from "../wsl/DesktopWslBackend.ts";
import * as DesktopRendererHistory from "../telemetry/DesktopRendererHistory.ts";
import * as HostProcess from "@t3tools/shared/HostProcess";
Expand Down Expand Up @@ -290,6 +291,7 @@ const bootstrap = Effect.fn("desktop.bootstrap")(function* (backendStarted: bool
}

yield* snapShot.initialize;
yield* (yield* DesktopSpellcheck.DesktopSpellcheck).initialize;
// Only the user's own login shell PATH shows every t3 a new terminal runs; a fresh install has
// no cached one. Without it, the command is left to Settings.
const installCli = (yield* DesktopCliCommand.DesktopCliCommand).installOnFirstLaunch;
Expand Down
7 changes: 6 additions & 1 deletion apps/desktop/src/ipc/DesktopIpcHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import * as Effect from "effect/Effect";
import { receiveProviderAuthCallback, cancelProviderAuthCallback } from "./methods/providerAuth.ts";
import * as DesktopIpc from "./DesktopIpc.ts";
import { installNotificationBadge } from "./methods/notificationBadge.ts";
import { getClientSettings, setClientSettings } from "./methods/clientSettings.ts";
import {
getClientSettings,
getSpellcheckLanguages,
setClientSettings,
} from "./methods/clientSettings.ts";
import { getConnectionCatalog, setConnectionCatalog } from "./methods/connectionCatalog.ts";
import {
getHostEnvironmentId,
Expand Down Expand Up @@ -115,6 +119,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
yield* ipc.handle(restartAudioService);
yield* ipc.handle(getClientSettings);
yield* ipc.handle(setClientSettings);
yield* ipc.handleSync(getSpellcheckLanguages);
yield* ipc.handle(getConnectionCatalog);
yield* ipc.handle(getSnapShotState);
yield* ipc.handle(setupSnapShot);
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/ipc/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const GET_LOCAL_ENVIRONMENT_BEARER_TOKEN_CHANNEL =
"desktop:get-local-environment-bearer-token";
export const GET_CLIENT_SETTINGS_CHANNEL = "desktop:get-client-settings";
export const SET_CLIENT_SETTINGS_CHANNEL = "desktop:set-client-settings";
export const GET_SPELLCHECK_LANGUAGES_CHANNEL = "desktop:get-spellcheck-languages";
export const SETUP_SNAP_SHOT_CHANNEL = "desktop:setup-snap-shot";
export const PREVIEW_SNAP_SHOT_CONFIG_CHANNEL = "desktop:preview-snap-shot-config";
export const APPLY_SNAP_SHOT_CONFIG_CHANNEL = "desktop:apply-snap-shot-config";
Expand Down
12 changes: 12 additions & 0 deletions apps/desktop/src/ipc/methods/clientSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as Schema from "effect/Schema";

import { applyLanguage } from "../../i18n/messages.ts";
import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts";
import * as DesktopSpellcheck from "../../settings/DesktopSpellcheck.ts";
import * as DesktopSnapShot from "../../snapShot/DesktopSnapShot.ts";
import * as IpcChannels from "../channels.ts";
import * as DesktopIpc from "../DesktopIpc.ts";
Expand All @@ -26,8 +27,19 @@ export const setClientSettings = DesktopIpc.makeIpcMethod({
handler: Effect.fn("desktop.ipc.clientSettings.set")(function* (settings) {
const clientSettings = yield* DesktopClientSettings.DesktopClientSettings;
const snapShot = yield* DesktopSnapShot.DesktopSnapShot;
const spellcheck = yield* DesktopSpellcheck.DesktopSpellcheck;
yield* clientSettings.set(settings);
yield* snapShot.configure(settings);
yield* applyLanguage(settings.language);
yield* spellcheck.configure(settings);
Comment thread
maria-rcks marked this conversation as resolved.
Resolved
}),
});

export const getSpellcheckLanguages = DesktopIpc.makeSyncIpcMethod({
channel: IpcChannels.GET_SPELLCHECK_LANGUAGES_CHANNEL,
result: Schema.Array(Schema.String),
handler: Effect.fn("desktop.ipc.clientSettings.getSpellcheckLanguages")(function* () {
const spellcheck = yield* DesktopSpellcheck.DesktopSpellcheck;
return yield* spellcheck.availableLanguages;
}),
});
2 changes: 2 additions & 0 deletions apps/desktop/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import * as DesktopShutdown from "./app/DesktopShutdown.ts";
import * as DesktopObservability from "./app/DesktopObservability.ts";
import * as DesktopServerExposure from "./backend/DesktopServerExposure.ts";
import * as DesktopClientSettings from "./settings/DesktopClientSettings.ts";
import * as DesktopSpellcheck from "./settings/DesktopSpellcheck.ts";
import * as DesktopSavedEnvironments from "./settings/DesktopSavedEnvironments.ts";
import * as DesktopSnapShot from "./snapShot/DesktopSnapShot.ts";
import * as DesktopAppSettings from "./settings/DesktopAppSettings.ts";
Expand Down Expand Up @@ -213,6 +214,7 @@ const layerDesktopApplication = Layer.mergeAll(
DesktopApplicationMenu.layer,
DesktopLinuxUrlHandler.layer,
DesktopCliCommand.layer,
DesktopSpellcheck.layer,
).pipe(
Layer.provideMerge(layerDesktopSnapShot),
Layer.provideMerge(
Expand Down
4 changes: 4 additions & 0 deletions apps/desktop/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ contextBridge.exposeInMainWorld("desktopBridge", {
getClientSettings: () => ipcRenderer.invoke(IpcChannels.GET_CLIENT_SETTINGS_CHANNEL),
setClientSettings: (settings) =>
ipcRenderer.invoke(IpcChannels.SET_CLIENT_SETTINGS_CHANNEL, settings),
getSpellcheckLanguages: () => {
const result: unknown = ipcRenderer.sendSync(IpcChannels.GET_SPELLCHECK_LANGUAGES_CHANNEL);
return Array.isArray(result) && result.every((code) => typeof code === "string") ? result : [];
},
requestSnapShotPermissions: (includeAccessibility) =>
ipcRenderer.invoke(IpcChannels.REQUEST_SNAP_SHOT_PERMISSIONS_CHANNEL, includeAccessibility),
getSnapShotState: () => ipcRenderer.invoke(IpcChannels.GET_SNAP_SHOT_STATE_CHANNEL),
Expand Down
91 changes: 91 additions & 0 deletions apps/desktop/src/settings/DesktopSpellcheck.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import { expect, vi } from "vite-plus/test";

const session = vi.hoisted(() => ({
languages: ["en-US"] as string[],
availableSpellCheckerLanguages: ["en-US", "en-GB", "fr-FR", "de-DE"],
getSpellCheckerLanguages: () => [...session.languages],
setSpellCheckerLanguages: (languages: string[]) => {
session.languages = [...languages];
},
}));
vi.mock("electron", () => ({
app: { getLocale: () => "en-US", getSystemLocale: () => "en-GB" },
session: { defaultSession: session },
}));

import * as DesktopClientSettings from "./DesktopClientSettings.ts";
import * as DesktopSpellcheck from "./DesktopSpellcheck.ts";

const withLanguages = (spellcheckLanguages: ReadonlyArray<string>) => ({
...DEFAULT_CLIENT_SETTINGS,
spellcheckLanguages,
});

// `persisted` is the list Electron restored from its Preferences file at launch.
const run = (
platform: NodeJS.Platform,
stored: ReadonlyArray<string>,
persisted: ReadonlyArray<string> = ["en-GB"],
) =>
Effect.gen(function* () {
session.languages = [...persisted];
return yield* DesktopSpellcheck.DesktopSpellcheck;
}).pipe(
Effect.provide(
DesktopSpellcheck.layer.pipe(
Layer.provide(DesktopClientSettings.layerTest(Option.some(withLanguages(stored)))),
),
),
Effect.provideService(HostProcess.Platform, platform),
);

it.effect("applies stored languages, drops unknown codes, and restores the locale default", () =>
Effect.gen(function* () {
const spellcheck = yield* run("linux", ["fr-FR", "xx-XX", "en-US", "fr-FR"]);
yield* spellcheck.initialize;
expect(session.languages).toEqual(["fr-FR", "en-US"]);

yield* spellcheck.configure(withLanguages(["de-DE"]));
expect(session.languages).toEqual(["de-DE"]);

yield* spellcheck.configure(withLanguages(["xx-XX"]));
expect(session.languages).toEqual(["en-GB"]);

yield* spellcheck.configure(withLanguages([]));
expect(session.languages).toEqual(["en-GB"]);
expect(yield* spellcheck.availableLanguages).toEqual(session.availableSpellCheckerLanguages);
}),
);

it.effect("clearing an override after a restart restores the locale default", () =>
Effect.gen(function* () {
const spellcheck = yield* run("linux", ["fr-FR"], ["fr-FR"]);
yield* spellcheck.initialize;
yield* spellcheck.configure(withLanguages([]));
expect(session.languages).toEqual(["en-GB"]);
}),
);

it.effect("leaves a list it never overrode alone", () =>
Effect.gen(function* () {
const spellcheck = yield* run("linux", [], ["en-US", "fr-FR"]);
yield* spellcheck.initialize;
yield* spellcheck.configure(withLanguages([]));
expect(session.languages).toEqual(["en-US", "fr-FR"]);
}),
);

it.effect("leaves the native macOS spellchecker alone", () =>
Effect.gen(function* () {
const spellcheck = yield* run("darwin", ["fr-FR"]);
yield* spellcheck.initialize;
expect(session.languages).toEqual(["en-GB"]);
expect(yield* spellcheck.availableLanguages).toEqual([]);
}),
);
77 changes: 77 additions & 0 deletions apps/desktop/src/settings/DesktopSpellcheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { DEFAULT_CLIENT_SETTINGS, type ClientSettings } from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Ref from "effect/Ref";
import * as Electron from "electron";

import * as DesktopClientSettings from "./DesktopClientSettings.ts";

export class DesktopSpellcheck extends Context.Service<
DesktopSpellcheck,
{
readonly initialize: Effect.Effect<void>;
readonly configure: (settings: ClientSettings) => Effect.Effect<void>;
/** Empty where the OS spellchecker picks languages itself (macOS). */
readonly availableLanguages: Effect.Effect<ReadonlyArray<string>>;
}
>()("@t3tools/desktop/settings/DesktopSpellcheck") {}

/** Electron seeds its list from the system locale; Chromium falls back to the base language. */
function localeDefaultLanguages(
locale: string,
available: ReadonlyArray<string>,
): ReadonlyArray<string> {
const base = locale.split("-")[0] ?? locale;
const match =
available.find((language) => language === locale) ??
available.find((language) => language === base) ??
available.find((language) => language.startsWith(`${base}-`));
return [match ?? "en-US"];
}

/** @public */
export const make = Effect.gen(function* () {
const clientSettings = yield* DesktopClientSettings.DesktopClientSettings;
const platform = yield* HostProcess.Platform;
// Electron persists the list across restarts, so only undo an override this app applied;
// a list the user set in Chromium's Preferences by hand is left alone.
const overriddenRef = yield* Ref.make(false);

const configure = Effect.fn("desktop.spellcheck.configure")(function* (settings: ClientSettings) {
// macOS always uses the native spellchecker, which ignores Electron's language list.
if (platform === "darwin") return;
const session = Electron.session.defaultSession;
const available = session.availableSpellCheckerLanguages;
const supported = new Set(available);
const requested = [...new Set(settings.spellcheckLanguages)].filter((language) =>
supported.has(language),
);
const overridden = yield* Ref.getAndSet(overriddenRef, requested.length > 0);
if (requested.length === 0 && !overridden) return;
const languages =
requested.length > 0
? requested
: localeDefaultLanguages(Electron.app.getSystemLocale(), available);
yield* Effect.annotateCurrentSpan({ languages: languages.join(",") });
// Only throws for codes outside availableSpellCheckerLanguages, which are filtered out above.
yield* Effect.sync(() => session.setSpellCheckerLanguages([...languages]));
});

return DesktopSpellcheck.of({
initialize: clientSettings.get.pipe(
Effect.flatMap((stored) =>
configure(Option.getOrElse(stored, () => DEFAULT_CLIENT_SETTINGS)),
),
Effect.ignore,
),
configure,
availableLanguages: Effect.sync(() =>
platform === "darwin" ? [] : Electron.session.defaultSession.availableSpellCheckerLanguages,
),
});
});

export const layer = Layer.effect(DesktopSpellcheck, make);
34 changes: 34 additions & 0 deletions apps/web/src/components/settings/SettingsPanels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ import {
} from "../../components/desktopUpdate.logic";
import { ProviderModelPicker } from "../chat/ProviderModelPicker";
import { TraitsPicker } from "../chat/TraitsPicker";
import {
SpellcheckLanguagesPicker,
readAvailableSpellcheckLanguages,
} from "./SpellcheckLanguagesPicker";
import { isElectron } from "../../env";
import { buildHostedChannelSelectionUrl, type HostedAppChannel } from "../../hostedPairing";
import {
Expand Down Expand Up @@ -576,6 +580,7 @@ export function useSettingsRestore(onRestored?: () => void) {
...(settings.sendShortcut !== DEFAULT_UNIFIED_SETTINGS.sendShortcut
? [gt("Send shortcut")]
: []),
...(settings.spellcheckLanguages.length > 0 ? [gt("Spellcheck languages")] : []),
...(settings.followUpBehavior !== DEFAULT_UNIFIED_SETTINGS.followUpBehavior
? [gt("Follow-up behavior")]
: []),
Expand Down Expand Up @@ -709,6 +714,7 @@ export function useSettingsRestore(onRestored?: () => void) {
showSkillsInSlashMenu: DEFAULT_UNIFIED_SETTINGS.showSkillsInSlashMenu,
composerRichTextEnabled: DEFAULT_UNIFIED_SETTINGS.composerRichTextEnabled,
sendShortcut: DEFAULT_UNIFIED_SETTINGS.sendShortcut,
spellcheckLanguages: DEFAULT_UNIFIED_SETTINGS.spellcheckLanguages,
followUpBehavior: DEFAULT_UNIFIED_SETTINGS.followUpBehavior,
toolCallVisibility: DEFAULT_UNIFIED_SETTINGS.toolCallVisibility,
screenReaderAnnouncements: DEFAULT_UNIFIED_SETTINGS.screenReaderAnnouncements,
Expand Down Expand Up @@ -1224,6 +1230,7 @@ export function ChatSettingsPanel() {
] as const;
const settings = useScopedSettings();
const updateSettings = useUpdateScopedSettings();
const spellcheckLanguages = readAvailableSpellcheckLanguages();

return (
<SettingsPageContainer>
Expand Down Expand Up @@ -1300,6 +1307,33 @@ export function ChatSettingsPanel() {
</Select>
}
/>
{spellcheckLanguages.length > 0 ? (
<SettingsRow
{...setting("spellcheck-languages")}
description={gt(
"Check spelling in these languages. System default follows your operating system language.",
)}
resetAction={
settings.spellcheckLanguages.length > 0 ? (
<SettingResetButton
ariaLabel={gt("Reset spellcheck languages to default")}
onClick={() =>
updateSettings({
spellcheckLanguages: DEFAULT_UNIFIED_SETTINGS.spellcheckLanguages,
})
}
/>
) : null
}
control={
<SpellcheckLanguagesPicker
available={spellcheckLanguages}
selected={settings.spellcheckLanguages}
onChange={(languages) => updateSettings({ spellcheckLanguages: languages })}
/>
}
/>
) : null}
<AdvancedSettings targetIds={[setting("skills-in-slash-menu").id]}>
<SettingsRow
{...setting("skills-in-slash-menu")}
Expand Down
Loading
Loading