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
34 changes: 33 additions & 1 deletion src/components/appearance-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ZOOM_LEVELS,
DEFAULT_ZOOM_LEVEL,
type ZoomLevel,
stepZoom,
} from "@/lib/theme-presets"
import {
resolveFontStack,
Expand Down Expand Up @@ -65,7 +66,11 @@ import {
type CustomThemeToken,
} from "@/lib/custom-style"
import { useShortcutSettings } from "@/hooks/use-shortcut-settings"
import { matchShortcutEvent } from "@/lib/keyboard-shortcuts"
import {
isZoomInShortcutEvent,
isZoomOutShortcutEvent,
matchShortcutEvent,
} from "@/lib/keyboard-shortcuts"
import {
DEFAULT_WORKSPACE_BG_ENABLED,
DEFAULT_WORKSPACE_BG_MASK_OPACITY,
Expand Down Expand Up @@ -472,6 +477,8 @@ export function AppearanceProvider({
syncTrafficLightPosition(zoom)
persist(STORAGE_KEY_ZOOM_LEVEL, String(zoom))
}, [])
const zoomLevelRef = useRef(zoomLevel)
zoomLevelRef.current = zoomLevel

const setShowWelcomeQuickActions = useCallback((on: boolean) => {
setShowWelcomeQuickActionsState(on)
Expand Down Expand Up @@ -744,6 +751,7 @@ export function AppearanceProvider({
// 或某个组件吞掉了冒泡,这一路依然能把自定义样式整体停用。
const { shortcuts } = useShortcutSettings()
const toggleCustomStyleShortcut = shortcuts.toggle_custom_style
const zoomResetShortcut = shortcuts.zoom_reset
useEffect(() => {
if (!toggleCustomStyleShortcut) return
const onKeyDown = (event: KeyboardEvent) => {
Expand All @@ -756,6 +764,30 @@ export function AppearanceProvider({
return () => window.removeEventListener("keydown", onKeyDown, true)
}, [toggleCustomStyleShortcut, customStyleSuspended, setCustomStyleSuspended])

// Same rungs as Settings → Window zoom. Capture-phase so the webview
// does not eat Ctrl/Cmd +/- as its own page zoom.
useEffect(() => {
const onKeyDown = (event: KeyboardEvent) => {
if (event.repeat) return
if (isZoomInShortcutEvent(event)) {
event.preventDefault()
setZoomLevel(stepZoom(zoomLevelRef.current, 1))
return
}
if (isZoomOutShortcutEvent(event)) {
event.preventDefault()
setZoomLevel(stepZoom(zoomLevelRef.current, -1))
return
}
if (zoomResetShortcut && matchShortcutEvent(event, zoomResetShortcut)) {
event.preventDefault()
setZoomLevel(DEFAULT_ZOOM_LEVEL)
}
}
window.addEventListener("keydown", onKeyDown, true)
return () => window.removeEventListener("keydown", onKeyDown, true)
}, [setZoomLevel, zoomResetShortcut])

// 跨标签页同步:用户在另一个窗口改了设置时,本窗口实时跟进
useEffect(() => {
const FONT_KEYS = new Set<string>([
Expand Down
12 changes: 12 additions & 0 deletions src/i18n/messages/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@
"toggle_custom_style": {
"title": "إيقاف/استئناف النمط المخصص",
"description": "مخرج طوارئ: يوقف كل الألوان المخصصة وCSS، ويعيد تفعيلها"
},
"zoom_in": {
"title": "Zoom in",
"description": "Step the window zoom up one Settings rung"
},
"zoom_out": {
"title": "Zoom out",
"description": "Step the window zoom down one Settings rung"
},
"zoom_reset": {
"title": "Reset zoom",
"description": "Set the window zoom back to 100%"
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions src/i18n/messages/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@
"toggle_custom_style": {
"title": "Eigenen Stil aussetzen/fortsetzen",
"description": "Notausstieg: schaltet alle eigenen Farben und CSS aus und wieder ein"
},
"zoom_in": {
"title": "Zoom in",
"description": "Step the window zoom up one Settings rung"
},
"zoom_out": {
"title": "Zoom out",
"description": "Step the window zoom down one Settings rung"
},
"zoom_reset": {
"title": "Reset zoom",
"description": "Set the window zoom back to 100%"
}
}
},
Expand Down
14 changes: 13 additions & 1 deletion src/i18n/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
},
"zoomLevel": {
"sectionTitle": "Window zoom",
"sectionDescription": "Scale the entire interface. Applies immediately and persists per device.",
"sectionDescription": "Scale the entire interface. Applies immediately and persists per device. Ctrl + and Ctrl - step the same rungs as this menu (Ctrl 0 resets to 100%).",
"placeholder": "Select zoom level",
"default": "Default",
"current": "Current zoom: {zoom}%"
Expand Down Expand Up @@ -415,6 +415,18 @@
"toggle_custom_style": {
"title": "Suspend/resume custom style",
"description": "Escape hatch: turns all custom colors and CSS off, and back on"
},
"zoom_in": {
"title": "Zoom in",
"description": "Step the window zoom up one Settings rung"
},
"zoom_out": {
"title": "Zoom out",
"description": "Step the window zoom down one Settings rung"
},
"zoom_reset": {
"title": "Reset zoom",
"description": "Set the window zoom back to 100%"
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions src/i18n/messages/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@
"toggle_custom_style": {
"title": "Suspender/reanudar estilo personalizado",
"description": "Vía de escape: desactiva todos los colores y el CSS personalizados, y los vuelve a activar"
},
"zoom_in": {
"title": "Zoom in",
"description": "Step the window zoom up one Settings rung"
},
"zoom_out": {
"title": "Zoom out",
"description": "Step the window zoom down one Settings rung"
},
"zoom_reset": {
"title": "Reset zoom",
"description": "Set the window zoom back to 100%"
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions src/i18n/messages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@
"toggle_custom_style": {
"title": "Suspendre/réactiver le style personnalisé",
"description": "Issue de secours : désactive toutes les couleurs et le CSS personnalisés, puis les réactive"
},
"zoom_in": {
"title": "Zoom in",
"description": "Step the window zoom up one Settings rung"
},
"zoom_out": {
"title": "Zoom out",
"description": "Step the window zoom down one Settings rung"
},
"zoom_reset": {
"title": "Reset zoom",
"description": "Set the window zoom back to 100%"
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions src/i18n/messages/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@
"toggle_custom_style": {
"title": "カスタムスタイルの停止/再開",
"description": "緊急脱出用: カスタム配色と CSS をすべてオフにし、再度押すと元に戻します"
},
"zoom_in": {
"title": "Zoom in",
"description": "Step the window zoom up one Settings rung"
},
"zoom_out": {
"title": "Zoom out",
"description": "Step the window zoom down one Settings rung"
},
"zoom_reset": {
"title": "Reset zoom",
"description": "Set the window zoom back to 100%"
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions src/i18n/messages/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@
"toggle_custom_style": {
"title": "사용자 지정 스타일 중지/재개",
"description": "비상 탈출구: 모든 사용자 지정 색상과 CSS를 끄고, 다시 누르면 되돌립니다"
},
"zoom_in": {
"title": "Zoom in",
"description": "Step the window zoom up one Settings rung"
},
"zoom_out": {
"title": "Zoom out",
"description": "Step the window zoom down one Settings rung"
},
"zoom_reset": {
"title": "Reset zoom",
"description": "Set the window zoom back to 100%"
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions src/i18n/messages/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@
"toggle_custom_style": {
"title": "Suspender/retomar estilo personalizado",
"description": "Saída de emergência: desliga todas as cores e o CSS personalizados e volta a ligá-los"
},
"zoom_in": {
"title": "Zoom in",
"description": "Step the window zoom up one Settings rung"
},
"zoom_out": {
"title": "Zoom out",
"description": "Step the window zoom down one Settings rung"
},
"zoom_reset": {
"title": "Reset zoom",
"description": "Set the window zoom back to 100%"
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions src/i18n/messages/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@
"toggle_custom_style": {
"title": "停用/恢复自定义样式",
"description": "逃生舱:一键关闭全部自定义配色与 CSS,再按一次恢复"
},
"zoom_in": {
"title": "Zoom in",
"description": "Step the window zoom up one Settings rung"
},
"zoom_out": {
"title": "Zoom out",
"description": "Step the window zoom down one Settings rung"
},
"zoom_reset": {
"title": "Reset zoom",
"description": "Set the window zoom back to 100%"
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions src/i18n/messages/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@
"toggle_custom_style": {
"title": "停用/恢復自訂樣式",
"description": "逃生艙:一鍵關閉全部自訂配色與 CSS,再按一次恢復"
},
"zoom_in": {
"title": "Zoom in",
"description": "Step the window zoom up one Settings rung"
},
"zoom_out": {
"title": "Zoom out",
"description": "Step the window zoom down one Settings rung"
},
"zoom_reset": {
"title": "Reset zoom",
"description": "Set the window zoom back to 100%"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/appearance-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const SCRIPT = `
(function() {
try {
var VALID_COLORS = ["neutral","zinc","slate","stone","gray","red","rose","orange","green","blue","yellow","violet"];
var VALID_ZOOMS = [80, 90, 100, 110, 125, 150];
var VALID_ZOOMS = [80, 90, 100, 110, 125, 150, 175, 200, 250, 300];

var storedColor = localStorage.getItem("${STORAGE_KEY_THEME_COLOR}");
var color = VALID_COLORS.indexOf(storedColor) >= 0 ? storedColor : "neutral";
Expand Down
28 changes: 28 additions & 0 deletions src/lib/keyboard-shortcuts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { describe, expect, it } from "vitest"
import {
DEFAULT_SHORTCUTS,
SHORTCUT_DEFINITIONS,
isZoomInShortcutEvent,
isZoomOutShortcutEvent,
matchShortcutEvent,
shortcutFromKeyboardEvent,
} from "./keyboard-shortcuts"
Expand Down Expand Up @@ -117,3 +119,29 @@ describe("alt combinations use event.code", () => {
).toBe(true)
})
})

describe("window zoom shortcuts", () => {
it("registers zoom_in / zoom_out / zoom_reset defaults", () => {
const ids = SHORTCUT_DEFINITIONS.map((definition) => definition.id)
expect(ids).toContain("zoom_in")
expect(ids).toContain("zoom_out")
expect(ids).toContain("zoom_reset")
expect(DEFAULT_SHORTCUTS.zoom_in).toBe("mod+=")
expect(DEFAULT_SHORTCUTS.zoom_out).toBe("mod+-")
expect(DEFAULT_SHORTCUTS.zoom_reset).toBe("mod+0")
})

it("treats Ctrl+= and Ctrl++ as zoom in", () => {
expect(isZoomInShortcutEvent(keyEvent("=", { ctrlKey: true }))).toBe(true)
expect(
isZoomInShortcutEvent(keyEvent("+", { ctrlKey: true, shiftKey: true }))
).toBe(true)
expect(isZoomInShortcutEvent(keyEvent("=", { ctrlKey: true }))).toBe(true)
expect(isZoomInShortcutEvent(keyEvent("k", { ctrlKey: true }))).toBe(false)
})

it("treats Ctrl+- as zoom out", () => {
expect(isZoomOutShortcutEvent(keyEvent("-", { ctrlKey: true }))).toBe(true)
expect(isZoomOutShortcutEvent(keyEvent("=", { ctrlKey: true }))).toBe(false)
})
})
43 changes: 43 additions & 0 deletions src/lib/keyboard-shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export type ShortcutActionId =
| "send_message"
| "newline_in_message"
| "toggle_custom_style"
| "zoom_in"
| "zoom_out"
| "zoom_reset"

export interface ShortcutDefinition {
id: ShortcutActionId
Expand Down Expand Up @@ -69,6 +72,15 @@ export const SHORTCUT_DEFINITIONS: ShortcutDefinition[] = [
{
id: "toggle_custom_style",
},
{
id: "zoom_in",
},
{
id: "zoom_out",
},
{
id: "zoom_reset",
},
]

/** Actions that allow shortcuts without modifier keys (e.g. plain Enter). */
Expand Down Expand Up @@ -98,6 +110,11 @@ export const DEFAULT_SHORTCUTS: ShortcutSettings = {
// 自定义样式的逃生舱:用户把界面改到不可用时,这一路必须仍然按得动,所以选一个
// 三修饰键组合(不会与任何常用操作撞车),并在捕获阶段监听。
toggle_custom_style: "mod+alt+shift+s",
// Same rungs as Settings → Window zoom. `=` is what US keyboards fire for
// Ctrl/+ without Shift; `+` is Shift+= and the numpad.
zoom_in: "mod+=",
zoom_out: "mod+-",
zoom_reset: "mod+0",
}

export const SHORTCUTS_STORAGE_KEY = "settings:shortcuts:v1"
Expand All @@ -115,6 +132,9 @@ const SPECIAL_KEY_ALIASES: Record<string, string> = {
down: "arrowdown",
left: "arrowleft",
right: "arrowright",
add: "+",
subtract: "-",
equal: "=",
}

const KEY_LABELS: Record<string, string> = {
Expand Down Expand Up @@ -306,6 +326,29 @@ export function shortcutFromKeyboardEvent(
return parts.join("+")
}

/** Ctrl/Cmd + or = (with or without Shift). Matches the Settings zoom-in rung. */
export function isZoomInShortcutEvent(event: ShortcutEventLike): boolean {
if (
matchShortcutEvent(event, "mod+=") ||
matchShortcutEvent(event, "mod++")
) {
return true
}
if (!(event.metaKey || event.ctrlKey) || event.altKey) return false
const key = eventKeyToken(event)
if (key === "+" || key === "=") return true
return event.code === "Equal" || event.code === "NumpadAdd"
}

/** Ctrl/Cmd - (with or without Shift). Matches the Settings zoom-out rung. */
export function isZoomOutShortcutEvent(event: ShortcutEventLike): boolean {
if (matchShortcutEvent(event, "mod+-")) return true
if (!(event.metaKey || event.ctrlKey) || event.altKey) return false
const key = eventKeyToken(event)
if (key === "-" || key === "_") return true
return event.code === "Minus" || event.code === "NumpadSubtract"
}

export function matchShortcutEvent(
event: ShortcutEventLike,
shortcut: string
Expand Down
Loading
Loading