mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
feat(desktop): spellcheck toggle and system-language dictionaries (#59129)
Co-authored-by: Brendonovich <14191578+Brendonovich@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
a88d3ca0bc
commit
bf94de965d
@@ -8,6 +8,7 @@ import { setForceFocus } from "../native/debug"
|
||||
import { showCliInstaller } from "../native/install-cli"
|
||||
import { DesktopLogging, scoped } from "../native/logging"
|
||||
import { createMenu, sendMenuCommand } from "../native/menu"
|
||||
import { getSpellcheckEnabled, setSpellcheckEnabled } from "../native/spellcheck"
|
||||
import { setNativeTranslations } from "../native/translations"
|
||||
import { IpcPortHandoff } from "../ipc-transport"
|
||||
import { ApplicationLifecycle } from "../lifecycle"
|
||||
@@ -37,6 +38,8 @@ export const appHandlers = AppRpcs.toLayer(
|
||||
AppCheckAppExists: ({ appName }) => checkAppExists(appName).pipe(Effect.orDie),
|
||||
AppResolveAppPath: ({ appName }) => resolveAppPath(appName).pipe(Effect.orDie),
|
||||
AppSetBackgroundColor: ({ color }) => Effect.sync(() => setBackgroundColor(color)),
|
||||
AppGetSpellcheckEnabled: () => Effect.sync(getSpellcheckEnabled),
|
||||
AppSetSpellcheckEnabled: ({ enabled }) => Effect.sync(() => setSpellcheckEnabled(enabled)),
|
||||
AppExportDebugLogs: () => logging.exportDebug,
|
||||
AppSetForceFocus: ({ enabled }, context) => promise(() => setForceFocus(sender(handoff, context), enabled)),
|
||||
AppRecordFatalRendererError: ({ error }) =>
|
||||
|
||||
@@ -6,12 +6,18 @@ import { DesktopPaths } from "../paths"
|
||||
import { getUserShell, loadShellEnv } from "../service/shell-env"
|
||||
import { registerRendererProtocol, setDockIcon, setProtocolReporter } from "../windows"
|
||||
import { scoped } from "../native/logging"
|
||||
import { applySpellcheck, spellcheckMenuItems } from "../native/spellcheck"
|
||||
|
||||
// electron-context-menu attaches to every existing and future window, so it can load once the first
|
||||
// window is up instead of holding up startup with its dependency tree.
|
||||
export const installContextMenu = Effect.gen(function* () {
|
||||
const { default: contextMenu } = yield* Effect.promise(() => import("electron-context-menu"))
|
||||
contextMenu({ showSaveImageAs: true, showLookUpSelection: false, showSearchWithGoogle: false })
|
||||
contextMenu({
|
||||
showSaveImageAs: true,
|
||||
showLookUpSelection: false,
|
||||
showSearchWithGoogle: false,
|
||||
append: (_actions, params) => (params.isEditable ? spellcheckMenuItems() : []),
|
||||
})
|
||||
})
|
||||
|
||||
export const prepareApplicationEnvironment = Effect.gen(function* () {
|
||||
@@ -45,12 +51,13 @@ export const prepareDesktop = Effect.gen(function* () {
|
||||
)
|
||||
registerRendererProtocol(paths.rendererRoot)
|
||||
setDockIcon(path, paths)
|
||||
applySpellcheck()
|
||||
})
|
||||
|
||||
export const loadProxyEnvironment = Effect.gen(function* () {
|
||||
yield* Effect.try(() => {
|
||||
ensureLoopbackNoProxy()
|
||||
// Electron 41.2 has a newer Node API than the current @types/node package.
|
||||
// SAFETY: Electron 41.2 ships a Node with http.setGlobalProxyFromEnv, which the current @types/node lacks.
|
||||
const proxyAwareHttp = http as typeof http & { setGlobalProxyFromEnv(): void }
|
||||
proxyAwareHttp.setGlobalProxyFromEnv()
|
||||
}).pipe(Effect.catch((error) => Effect.logWarning("failed to load proxy environment", { error })))
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { defaultSpellcheckLanguages } from "./spellcheck-languages"
|
||||
|
||||
// A slice of Electron 44's Linux and Windows list, including aliases such as de and de-DE.
|
||||
const available = ["de", "de-DE", "en", "en-AU", "en-GB", "en-US", "es", "es-MX", "fr", "fr-FR", "pt", "pt-BR"]
|
||||
|
||||
test.each([
|
||||
[["de-DE"], ["de-DE", "en-US"]],
|
||||
[
|
||||
["de-DE", "de", "en-US", "en"],
|
||||
["de-DE", "en-US"],
|
||||
],
|
||||
[
|
||||
["de-AT", "fr-CA"],
|
||||
["de", "fr", "en-US"],
|
||||
],
|
||||
[
|
||||
["en-GB", "de-DE"],
|
||||
["en-GB", "de-DE"],
|
||||
],
|
||||
[["en-NZ"], ["en"]],
|
||||
[
|
||||
["es-MX", "es-ES"],
|
||||
["es-MX", "en-US"],
|
||||
],
|
||||
[["pt-AO"], ["pt", "en-US"]],
|
||||
[["DE-de"], ["de-DE", "en-US"]],
|
||||
[["ja-JP"], ["en-US"]],
|
||||
[[], ["en-US"]],
|
||||
])("system languages %p use dictionaries %p", (preferred, languages) => {
|
||||
expect(defaultSpellcheckLanguages(available, preferred)).toEqual(languages)
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
const base = (tag: string) => tag.split("-")[0].toLowerCase()
|
||||
|
||||
// One dictionary per system language, matched exactly or by base language (de-AT -> de). Chromium
|
||||
// lists aliases such as de and de-DE separately, so a later tag for the same language is skipped.
|
||||
// US English is added when none of them is English, because prompts mix prose with English code terms.
|
||||
export function defaultSpellcheckLanguages(available: readonly string[], preferred: readonly string[]) {
|
||||
const find = (tag: string) => available.find((language) => language.toLowerCase() === tag.toLowerCase())
|
||||
|
||||
const languages = preferred
|
||||
.flatMap((tag) => find(tag) ?? find(base(tag)) ?? [])
|
||||
.filter((language, index, all) => all.findIndex((other) => base(other) === base(language)) === index)
|
||||
|
||||
if (languages.some((language) => base(language) === "en") || !available.includes("en-US")) return languages
|
||||
|
||||
return [...languages, "en-US"]
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { app, session, type MenuItemConstructorOptions } from "electron"
|
||||
import { SPELLCHECK_ENABLED_KEY, SPELLCHECK_LANGUAGES_KEY } from "../storage/keys"
|
||||
import { getStore } from "../storage/store"
|
||||
import { defaultSpellcheckLanguages } from "./spellcheck-languages"
|
||||
import { nativeLanguageOptions, nativeT } from "./translations"
|
||||
|
||||
// macOS checks spelling with the system spellchecker, which detects the language itself; Electron
|
||||
// ignores dictionary choices there.
|
||||
const choosesLanguages = process.platform !== "darwin"
|
||||
|
||||
export function applySpellcheck() {
|
||||
// Setting languages also turns the spellchecker on, so the stored on/off state goes last.
|
||||
if (choosesLanguages) session.defaultSession.setSpellCheckerLanguages(spellcheckLanguages())
|
||||
|
||||
session.defaultSession.setSpellCheckerEnabled(getSpellcheckEnabled())
|
||||
}
|
||||
|
||||
export function getSpellcheckEnabled() {
|
||||
return getStore().get(SPELLCHECK_ENABLED_KEY) !== false
|
||||
}
|
||||
|
||||
export function setSpellcheckEnabled(enabled: boolean) {
|
||||
getStore().set(SPELLCHECK_ENABLED_KEY, enabled)
|
||||
session.defaultSession.setSpellCheckerEnabled(enabled)
|
||||
}
|
||||
|
||||
// Mirrors the Spellcheck menu in chat apps: a checkbox, and a dictionary picker where Electron owns
|
||||
// the dictionaries.
|
||||
export function spellcheckMenuItems(): MenuItemConstructorOptions[] {
|
||||
const enabled = getSpellcheckEnabled()
|
||||
const current = session.defaultSession.getSpellCheckerLanguages()
|
||||
|
||||
const toggle: MenuItemConstructorOptions = {
|
||||
label: nativeT("desktop.contextMenu.spellcheck"),
|
||||
type: "checkbox",
|
||||
checked: enabled,
|
||||
click: () => setSpellcheckEnabled(!enabled),
|
||||
}
|
||||
|
||||
if (!choosesLanguages) return [{ type: "separator" }, toggle]
|
||||
|
||||
return [
|
||||
{ type: "separator" },
|
||||
toggle,
|
||||
{
|
||||
label: nativeT("desktop.contextMenu.spellcheckLanguages"),
|
||||
enabled,
|
||||
submenu: nativeLanguageOptions(session.defaultSession.availableSpellCheckerLanguages).map((option) => ({
|
||||
label: option.label,
|
||||
type: "checkbox",
|
||||
checked: current.includes(option.tag),
|
||||
// The last dictionary stays; the Spellcheck checkbox is how to stop checking entirely.
|
||||
enabled: !(current.length === 1 && current[0] === option.tag),
|
||||
click: () => {
|
||||
const next = current.includes(option.tag)
|
||||
? current.filter((language) => language !== option.tag)
|
||||
: [...current, option.tag]
|
||||
|
||||
getStore().set(SPELLCHECK_LANGUAGES_KEY, next)
|
||||
session.defaultSession.setSpellCheckerLanguages(next)
|
||||
},
|
||||
})),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
// Dictionaries picked from the menu win; otherwise follow the system languages on every launch.
|
||||
function spellcheckLanguages() {
|
||||
const available = session.defaultSession.availableSpellCheckerLanguages
|
||||
const stored = getStore().get(SPELLCHECK_LANGUAGES_KEY)
|
||||
const chosen = Array.isArray(stored) ? available.filter((language) => stored.includes(language)) : []
|
||||
|
||||
return chosen.length ? chosen : defaultSpellcheckLanguages(available, app.getPreferredSystemLanguages())
|
||||
}
|
||||
@@ -29,6 +29,16 @@ export function nativeT(key: DesktopNativeKey, params?: Record<string, string |
|
||||
return formatDesktopNativeMessage(bundle.messages[key], params)
|
||||
}
|
||||
|
||||
/** Language tags with their names in the app locale, sorted for a menu. */
|
||||
export function nativeLanguageOptions(tags: readonly string[]) {
|
||||
const locale = DESKTOP_NATIVE_LOCALE_TAGS[bundle.locale]
|
||||
const names = new Intl.DisplayNames([locale], { type: "language" })
|
||||
|
||||
return tags
|
||||
.map((tag) => ({ tag, label: names.of(tag) ?? tag }))
|
||||
.toSorted((a, b) => a.label.localeCompare(b.label, locale))
|
||||
}
|
||||
|
||||
/** The app locale the renderer last pushed. GUI plugin catalogs resolve against it. */
|
||||
export function nativeLocale() {
|
||||
return bundle.locale
|
||||
|
||||
@@ -6,6 +6,10 @@ export const PINCH_ZOOM_ENABLED_KEY = "pinchZoomEnabled"
|
||||
|
||||
export const ZOOM_LEVEL_KEY = "zoomLevel"
|
||||
|
||||
export const SPELLCHECK_ENABLED_KEY = "spellcheckEnabled"
|
||||
|
||||
export const SPELLCHECK_LANGUAGES_KEY = "spellcheckLanguages"
|
||||
|
||||
export const BACKGROUND_COLOR_KEY = "backgroundColor"
|
||||
|
||||
export const WINDOW_IDS_KEY = "windowIds"
|
||||
|
||||
@@ -71,6 +71,8 @@ export type ElectronAPI = {
|
||||
setBackgroundColor(color: string): Promise<void>
|
||||
exportDebugLogs(): Promise<string>
|
||||
setForceFocus(enabled: boolean): Promise<void>
|
||||
getSpellcheckEnabled(): Promise<boolean>
|
||||
setSpellcheckEnabled(enabled: boolean): Promise<void>
|
||||
recordFatalRendererError(error: FatalRendererError): Promise<void>
|
||||
setNativeTranslations(bundle: DesktopNativeBundle): Promise<void>
|
||||
}
|
||||
|
||||
@@ -94,6 +94,8 @@ export const api: ElectronAPI = {
|
||||
setBackgroundColor: (color) => invoke("AppSetBackgroundColor", { color }),
|
||||
exportDebugLogs: () => invoke("AppExportDebugLogs"),
|
||||
setForceFocus: (enabled) => invoke("AppSetForceFocus", { enabled }),
|
||||
getSpellcheckEnabled: () => invoke("AppGetSpellcheckEnabled"),
|
||||
setSpellcheckEnabled: (enabled) => invoke("AppSetSpellcheckEnabled", { enabled }),
|
||||
recordFatalRendererError: (error) => invoke("AppRecordFatalRendererError", { error }),
|
||||
setNativeTranslations: (bundle) => invoke("AppSetNativeTranslations", { value: bundle }),
|
||||
}
|
||||
|
||||
@@ -43,6 +43,8 @@ export function createDesktopPlatform(api: ElectronAPI, windowState: DesktopWind
|
||||
setTrafficLightsPosition: (position) => api.setTrafficLightsPosition(position),
|
||||
getPinchZoomEnabled: () => api.getPinchZoomEnabled(),
|
||||
setPinchZoomEnabled,
|
||||
getSpellcheckEnabled: () => api.getSpellcheckEnabled(),
|
||||
setSpellcheckEnabled: (enabled) => api.setSpellcheckEnabled(enabled),
|
||||
onDragCancel: (callback) => {
|
||||
window.addEventListener(DragCancelEvent, callback)
|
||||
|
||||
|
||||
@@ -54,6 +54,12 @@ export const AppRecordFatalRendererError = Rpc.make("AppRecordFatalRendererError
|
||||
},
|
||||
})
|
||||
|
||||
export const AppGetSpellcheckEnabled = Rpc.make("AppGetSpellcheckEnabled", { success: Schema.Boolean })
|
||||
|
||||
export const AppSetSpellcheckEnabled = Rpc.make("AppSetSpellcheckEnabled", {
|
||||
payload: { enabled: Schema.Boolean },
|
||||
})
|
||||
|
||||
export const AppSetNativeTranslations = Rpc.make("AppSetNativeTranslations", {
|
||||
payload: { value: Schema.Unknown },
|
||||
})
|
||||
@@ -71,6 +77,8 @@ export const AppRpcs = RpcGroup.make(
|
||||
AppSetBackgroundColor,
|
||||
AppExportDebugLogs,
|
||||
AppSetForceFocus,
|
||||
AppGetSpellcheckEnabled,
|
||||
AppSetSpellcheckEnabled,
|
||||
AppRecordFatalRendererError,
|
||||
AppSetNativeTranslations,
|
||||
AppRelaunch,
|
||||
|
||||
Reference in New Issue
Block a user