mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-11-23 13:41:41 +08:00
feat(ui): add interface density and zoom settings (#9209)
Settings > General > Appearance gets two rows: - Density: Compact (default, exactly the current sizes) or Comfortable (type one step larger, controls 4px taller). Type roles and control heights become CSS variables switched by <html data-density>; the tray panel never sets it and keeps the compact values. - Zoom: 90 / 100 / 110 / 125 %, also Ctrl/Cmd + - 0, via the webview's native setZoom (adds core:webview:allow-set-webview-zoom). Both are frontend-only and stored in localStorage like the theme. main.tsx applies them at the start of bootstrap(), before anything renders; not as an inline script in index.html, because the CSP only allows the existing theme script by hash.
This commit is contained in:
1 parent
65106ce6ca
commit
4e7413c5a8
20 files changed
+409
-23
No files matched your search
@@ -15,6 +15,7 @@
|
||||
"core:window:allow-is-maximized",
|
||||
"core:window:allow-close",
|
||||
"core:window:allow-set-decorations",
|
||||
"core:webview:allow-set-webview-zoom",
|
||||
"process:allow-exit",
|
||||
"process:allow-restart",
|
||||
"dialog:default"
|
||||
|
||||
+19
@@ -57,6 +57,7 @@ import {
|
||||
translatePiProviderMutationError,
|
||||
} from "@/utils/errorUtils";
|
||||
import { isTextEditableTarget } from "@/utils/domUtils";
|
||||
import { setZoom, stepZoom } from "@/lib/interfacePrefs";
|
||||
import { deepClone } from "@/utils/deepClone";
|
||||
import { isLinux, isMac, isWindows } from "@/lib/platform";
|
||||
import {
|
||||
@@ -1015,6 +1016,24 @@ function App() {
|
||||
openSettingsRef.current("general");
|
||||
return;
|
||||
}
|
||||
// 界面缩放:= / +(含小键盘)放大、- 缩小、0 复原,档位见 ZOOM_LEVELS
|
||||
if (mod && !event.altKey) {
|
||||
if (event.key === "=" || event.key === "+") {
|
||||
event.preventDefault();
|
||||
stepZoom(1);
|
||||
return;
|
||||
}
|
||||
if (event.key === "-") {
|
||||
event.preventDefault();
|
||||
stepZoom(-1);
|
||||
return;
|
||||
}
|
||||
if (event.key === "0") {
|
||||
event.preventDefault();
|
||||
setZoom(1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (event.key !== "Escape" || event.defaultPrevented) return;
|
||||
if (document.body.style.overflow === "hidden") return;
|
||||
|
||||
@@ -403,7 +403,7 @@ export function ProviderCard({
|
||||
dragHandleProps?.isDragging && "cursor-grabbing shadow-v7-md",
|
||||
)}
|
||||
>
|
||||
<div className="flex min-h-[58px] items-center gap-3 py-2.5 pe-3 ps-2">
|
||||
<div className="flex min-h-card items-center gap-3 py-card-y pe-3 ps-2">
|
||||
{dragHandleProps ? (
|
||||
<button
|
||||
type="button"
|
||||
|
||||
@@ -11,7 +11,15 @@ import {
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { isLinux } from "@/lib/platform";
|
||||
import { isLinux, isMac } from "@/lib/platform";
|
||||
import {
|
||||
setDensity,
|
||||
setZoom,
|
||||
useInterfacePrefs,
|
||||
ZOOM_LEVELS,
|
||||
type Density,
|
||||
type ZoomLevel,
|
||||
} from "@/lib/interfacePrefs";
|
||||
import { TerminalSelect } from "@/components/settings/TerminalSettings";
|
||||
import {
|
||||
SettingsBlock,
|
||||
@@ -43,6 +51,7 @@ export function GeneralSection({
|
||||
}: GeneralSectionProps) {
|
||||
const { t } = useTranslation();
|
||||
const { theme, setTheme } = useTheme();
|
||||
const { density, zoom } = useInterfacePrefs();
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -58,7 +67,7 @@ export function GeneralSection({
|
||||
}
|
||||
>
|
||||
<SelectTrigger
|
||||
className="h-8 w-[160px]"
|
||||
className="w-[160px]"
|
||||
aria-label={t("settings.language")}
|
||||
>
|
||||
<SelectValue />
|
||||
@@ -128,6 +137,52 @@ export function GeneralSection({
|
||||
/>
|
||||
}
|
||||
/>
|
||||
<SettingsRow
|
||||
label={t("settings.general.density")}
|
||||
help={{
|
||||
title: t("settings.general.density"),
|
||||
body: t("settings.general.densityHelp"),
|
||||
}}
|
||||
control={
|
||||
<SegmentedControl<Density>
|
||||
size="sm"
|
||||
aria-label={t("settings.general.density")}
|
||||
value={density}
|
||||
onValueChange={setDensity}
|
||||
items={[
|
||||
{
|
||||
value: "compact",
|
||||
label: t("settings.general.densityCompact"),
|
||||
},
|
||||
{
|
||||
value: "comfortable",
|
||||
label: t("settings.general.densityComfortable"),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
<SettingsRow
|
||||
label={t("settings.general.zoom")}
|
||||
help={{
|
||||
title: t("settings.general.zoom"),
|
||||
body: t("settings.general.zoomHelp", {
|
||||
shortcut: isMac() ? "⌘" : "Ctrl",
|
||||
}),
|
||||
}}
|
||||
control={
|
||||
<SegmentedControl<`${ZoomLevel}`>
|
||||
size="sm"
|
||||
aria-label={t("settings.general.zoom")}
|
||||
value={`${zoom}`}
|
||||
onValueChange={(value) => setZoom(Number(value) as ZoomLevel)}
|
||||
items={ZOOM_LEVELS.map((level) => ({
|
||||
value: `${level}` as const,
|
||||
label: `${Math.round(level * 100)}%`,
|
||||
}))}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</SettingsCard>
|
||||
</SettingsBlock>
|
||||
|
||||
|
||||
@@ -482,7 +482,7 @@ function AppNavItem({
|
||||
onClick={onSelect}
|
||||
aria-current={selected ? "page" : undefined}
|
||||
className={cn(
|
||||
"mx-2 flex h-7 w-[184px] shrink-0 items-center gap-2 rounded-control px-2 text-start transition-colors hover:bg-subtle",
|
||||
"mx-2 flex h-nav-row w-[184px] shrink-0 items-center gap-2 rounded-control px-2 text-start transition-colors hover:bg-subtle",
|
||||
selected && "bg-selected font-medium hover:bg-selected",
|
||||
)}
|
||||
>
|
||||
@@ -643,7 +643,7 @@ function NavItem({
|
||||
aria-label={trailingLabel ? accessibleName : undefined}
|
||||
aria-current={selected ? "page" : undefined}
|
||||
className={cn(
|
||||
"flex h-7 shrink-0 items-center gap-2 rounded-control px-2 text-start transition-colors hover:bg-subtle",
|
||||
"flex h-nav-row shrink-0 items-center gap-2 rounded-control px-2 text-start transition-colors hover:bg-subtle",
|
||||
compact ? "min-w-0 flex-1" : "mx-2 w-[184px]",
|
||||
selected && "bg-selected font-medium hover:bg-selected",
|
||||
)}
|
||||
@@ -730,7 +730,7 @@ function SettingsDirectory({
|
||||
<button
|
||||
type="button"
|
||||
onClick={onExitSettings}
|
||||
className="mx-2 flex h-7 w-[184px] shrink-0 items-center gap-2 rounded-control px-2 text-start text-fg-2 transition-colors hover:bg-subtle hover:text-fg-1"
|
||||
className="mx-2 flex h-nav-row w-[184px] shrink-0 items-center gap-2 rounded-control px-2 text-start text-fg-2 transition-colors hover:bg-subtle hover:text-fg-1"
|
||||
>
|
||||
<ArrowLeft className="h-4 w-4" strokeWidth={1.5} aria-hidden="true" />
|
||||
{t("nav.back")}
|
||||
|
||||
@@ -41,18 +41,19 @@ const buttonVariants = cva(
|
||||
// 旧尺寸都映射到 v7 的三档(docs/design-system.html「按钮」):
|
||||
// default = regular(32)、sm = compact(28)、icon = 32 见方,lg 只给少数大按钮(36)
|
||||
default:
|
||||
"h-8 px-3.5 transition-[background-color,filter,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
sm: "h-7 px-3 transition-[background-color,filter,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
"h-control px-3.5 transition-[background-color,filter,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
sm: "h-control-sm px-3 transition-[background-color,filter,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
lg: "h-9 px-4 transition-[background-color,filter,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
icon: "h-8 w-8 p-0 transition-[background-color,color,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
// v7:高 28(卡片、对话框里)/ 高 32(页头、模式切换)/ 28 见方的图标按钮
|
||||
icon: "h-control w-control p-0 transition-[background-color,color,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
// v7:compact(卡片、对话框里)/ regular(页头、模式切换)/ 与 compact 同高的图标按钮。
|
||||
// 紧凑密度下是 28 / 32,舒适密度下是 32 / 36(--h-control-sm / --h-control)
|
||||
// 按下缩一下只给「执行动作」的按钮;打开菜单 / 弹层的触发器(aria-haspopup)不缩,弹层自己有进场动画
|
||||
compact:
|
||||
"h-7 rounded-control px-3 text-body font-medium transition-[background-color,filter,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
"h-control-sm rounded-control px-3 text-body font-medium transition-[background-color,filter,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
regular:
|
||||
"h-8 rounded-control px-3.5 text-body font-medium transition-[background-color,filter,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
"h-control rounded-control px-3.5 text-body font-medium transition-[background-color,filter,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
"icon-compact":
|
||||
"h-7 w-7 rounded-control p-0 text-fg-2 hover:text-fg-1 transition-[background-color,color,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
"h-control-sm w-control-sm rounded-control p-0 text-fg-2 hover:text-fg-1 transition-[background-color,color,scale] active:scale-[0.96] [&[aria-haspopup]]:active:scale-100",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
|
||||
@@ -17,7 +17,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(
|
||||
type={type}
|
||||
className={cn(
|
||||
fieldClass,
|
||||
"flex h-8 py-1 file:border-0 file:bg-transparent file:text-body file:font-medium file:text-fg-1",
|
||||
"flex h-control py-1 file:border-0 file:bg-transparent file:text-body file:font-medium file:text-fg-1",
|
||||
className,
|
||||
)}
|
||||
autoComplete="off"
|
||||
|
||||
@@ -50,7 +50,7 @@ export function SegmentedControl<T extends string>({
|
||||
aria-label={rest["aria-label"]}
|
||||
className={cn(
|
||||
"relative inline-flex items-stretch bg-subtle p-[3px]",
|
||||
size === "md" ? "h-9 rounded-[10px]" : "h-7 rounded-[8px]",
|
||||
size === "md" ? "h-9 rounded-[10px]" : "h-control-sm rounded-[8px]",
|
||||
className,
|
||||
)}
|
||||
>
|
||||
|
||||
@@ -19,7 +19,7 @@ const SelectTrigger = React.forwardRef<
|
||||
className={cn(
|
||||
// 和 Input 同一套外观(fieldClass);展开时也保持聚焦态
|
||||
fieldClass,
|
||||
"flex h-8 items-center justify-between whitespace-nowrap text-start data-[placeholder]:text-fg-3 data-[state=open]:border-ring data-[state=open]:ring-[3px] data-[state=open]:ring-ring/20 [&>span]:truncate",
|
||||
"flex h-control items-center justify-between whitespace-nowrap text-start data-[placeholder]:text-fg-3 data-[state=open]:border-ring data-[state=open]:ring-[3px] data-[state=open]:ring-ring/20 [&>span]:truncate",
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
|
||||
@@ -439,6 +439,12 @@
|
||||
"quotaDisplay": "Quota display",
|
||||
"quotaDisplayLeft": "Left",
|
||||
"quotaDisplayUsed": "Used",
|
||||
"density": "Density",
|
||||
"densityHelp": "Comfortable uses larger text and taller controls. Compact keeps the original sizes.",
|
||||
"densityComfortable": "Comfortable",
|
||||
"densityCompact": "Compact",
|
||||
"zoom": "Zoom",
|
||||
"zoomHelp": "You can also press {{shortcut}} + / {{shortcut}} −, and {{shortcut}} 0 to reset.",
|
||||
"quotaDisplayHelp": "Whether each quota window on provider cards, in Accounts and in the tray shows how much is left or how much is used. Balances and Credits always show what is left.",
|
||||
"sidebarAndHeader": "Sidebar and header",
|
||||
"visibleApps": "Apps shown in the sidebar",
|
||||
|
||||
@@ -439,6 +439,12 @@
|
||||
"quotaDisplay": "残量の表示",
|
||||
"quotaDisplayLeft": "残り",
|
||||
"quotaDisplayUsed": "使用済み",
|
||||
"density": "表示密度",
|
||||
"densityHelp": "標準は文字が大きく、操作部品も高くなります。コンパクトは従来のサイズのままです。",
|
||||
"densityComfortable": "標準",
|
||||
"densityCompact": "コンパクト",
|
||||
"zoom": "表示倍率",
|
||||
"zoomHelp": "{{shortcut}} + / {{shortcut}} − でも変更でき、{{shortcut}} 0 で元に戻ります。",
|
||||
"quotaDisplayHelp": "プロバイダーカード、アカウント、トレイで、各枠の残量を「残り」と「使用済み」のどちらで表示するかを選びます。残高と Credits は常に残りを表示します。",
|
||||
"sidebarAndHeader": "サイドバーとヘッダー",
|
||||
"visibleApps": "サイドバーに表示するアプリ",
|
||||
|
||||
@@ -432,6 +432,12 @@
|
||||
"quotaDisplay": "Kota gösterimi",
|
||||
"quotaDisplayLeft": "Kalan",
|
||||
"quotaDisplayUsed": "Kullanılan",
|
||||
"density": "Arayüz yoğunluğu",
|
||||
"densityHelp": "Rahat, daha büyük yazı ve daha yüksek denetimler kullanır. Sıkı, özgün boyutları korur.",
|
||||
"densityComfortable": "Rahat",
|
||||
"densityCompact": "Sıkı",
|
||||
"zoom": "Yakınlaştırma",
|
||||
"zoomHelp": "{{shortcut}} + / {{shortcut}} − ile de değiştirebilir, {{shortcut}} 0 ile sıfırlayabilirsiniz.",
|
||||
"quotaDisplayHelp": "Sağlayıcı kartlarındaki, Hesaplar'daki ve tepsideki her kota penceresinin ne kadar kaldığını veya ne kadar kullanıldığını gösterip göstermediği. Bakiyeler ve Krediler her zaman kalanları gösterir.",
|
||||
"sidebarAndHeader": "Kenar çubuğu ve başlık",
|
||||
"visibleApps": "Kenar çubuğunda gösterilen uygulamalar",
|
||||
|
||||
@@ -439,6 +439,12 @@
|
||||
"quotaDisplay": "額度顯示",
|
||||
"quotaDisplayLeft": "剩餘",
|
||||
"quotaDisplayUsed": "已用",
|
||||
"density": "介面密度",
|
||||
"densityHelp": "舒適的字級更大、控制項更高;緊湊維持原來的尺寸。",
|
||||
"densityComfortable": "舒適",
|
||||
"densityCompact": "緊湊",
|
||||
"zoom": "介面縮放",
|
||||
"zoomHelp": "也可以按 {{shortcut}} + / {{shortcut}} − 調整,{{shortcut}} 0 復原。",
|
||||
"quotaDisplayHelp": "供應商卡片、授權中心和系統匣裡,各檔額度寫還剩多少還是已經用了多少。餘額和 Credits 始終寫剩下的。",
|
||||
"sidebarAndHeader": "側欄與頁首",
|
||||
"visibleApps": "在側欄顯示哪些應用",
|
||||
|
||||
@@ -439,6 +439,12 @@
|
||||
"quotaDisplay": "额度显示",
|
||||
"quotaDisplayLeft": "剩余",
|
||||
"quotaDisplayUsed": "已用",
|
||||
"density": "界面密度",
|
||||
"densityHelp": "舒适的字号更大、控件更高;紧凑保持原来的尺寸。",
|
||||
"densityComfortable": "舒适",
|
||||
"densityCompact": "紧凑",
|
||||
"zoom": "界面缩放",
|
||||
"zoomHelp": "也可以按 {{shortcut}} + / {{shortcut}} − 调,{{shortcut}} 0 复原。",
|
||||
"quotaDisplayHelp": "供应商卡片、授权中心和托盘里,各档额度写还剩多少还是已经用了多少。余额和 Credits 始终写剩下的。",
|
||||
"sidebarAndHeader": "侧栏与页头",
|
||||
"visibleApps": "在侧栏显示哪些应用",
|
||||
|
||||
@@ -377,6 +377,59 @@
|
||||
-webkit-app-region: no-drag;
|
||||
}
|
||||
|
||||
/* 界面密度(设置 › 通用 › 界面密度,src/lib/interfacePrefs.ts)。
|
||||
不带 data-density 或为 compact 时是紧凑,即原来的尺寸(默认;托盘面板等小窗口始终如此);
|
||||
选了舒适时主窗口 <html> 上是 data-density="comfortable",字号整体大一档、控件高 4px。 */
|
||||
:root {
|
||||
--fs-badge: 11px;
|
||||
--lh-badge: 16px;
|
||||
--fs-caption: 12px;
|
||||
--lh-caption: 18px;
|
||||
--fs-body: 13px;
|
||||
--lh-body: 20px;
|
||||
--fs-strong: 14px;
|
||||
--lh-strong: 20px;
|
||||
--fs-section: 15px;
|
||||
--lh-section: 22px;
|
||||
--fs-title: 16px;
|
||||
--lh-title: 24px;
|
||||
--fs-page: 18px;
|
||||
--lh-page: 26px;
|
||||
--fs-xs: 12px;
|
||||
--lh-xs: 16px;
|
||||
|
||||
--h-control: 32px;
|
||||
--h-control-sm: 28px;
|
||||
--h-nav-row: 28px;
|
||||
--h-card: 58px;
|
||||
--card-py: 10px;
|
||||
}
|
||||
|
||||
html[data-density="comfortable"] {
|
||||
--fs-badge: 12px;
|
||||
--lh-badge: 16px;
|
||||
--fs-caption: 13px;
|
||||
--lh-caption: 18px;
|
||||
--fs-body: 14px;
|
||||
--lh-body: 22px;
|
||||
--fs-strong: 15px;
|
||||
--lh-strong: 22px;
|
||||
--fs-section: 16px;
|
||||
--lh-section: 24px;
|
||||
--fs-title: 18px;
|
||||
--lh-title: 26px;
|
||||
--fs-page: 20px;
|
||||
--lh-page: 28px;
|
||||
--fs-xs: 13px;
|
||||
--lh-xs: 18px;
|
||||
|
||||
--h-control: 36px;
|
||||
--h-control-sm: 32px;
|
||||
--h-nav-row: 34px;
|
||||
--h-card: 68px;
|
||||
--card-py: 14px;
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { getCurrentWebview } from "@tauri-apps/api/webview";
|
||||
|
||||
/**
|
||||
* 界面密度与缩放(设置 › 通用 › 外观)。只影响主窗口,存在 localStorage 里,和明暗一样不进后端设置。
|
||||
*
|
||||
* - 密度:写到 <html data-density>,src/index.css 按它换一组字号 / 控件高度变量。
|
||||
* main.tsx 在渲染任何界面之前先调 initializeInterfacePrefs。不放进 index.html 的内联脚本:
|
||||
* tauri.conf.json 的 CSP 只按哈希放行明暗那一段内联脚本,新加的会被拦。
|
||||
* - 缩放:交给 webview 原生缩放(整页等比,弹层定位不受影响);Ctrl / ⌘ + = − 0 也能调。
|
||||
*/
|
||||
|
||||
export type Density = "comfortable" | "compact";
|
||||
|
||||
export const DENSITY_STORAGE_KEY = "cc-switch-density";
|
||||
export const ZOOM_STORAGE_KEY = "cc-switch-zoom";
|
||||
export const DEFAULT_DENSITY: Density = "compact";
|
||||
export const ZOOM_LEVELS = [0.9, 1, 1.1, 1.25] as const;
|
||||
export type ZoomLevel = (typeof ZOOM_LEVELS)[number];
|
||||
|
||||
interface InterfacePrefs {
|
||||
density: Density;
|
||||
zoom: ZoomLevel;
|
||||
}
|
||||
|
||||
function readDensity(): Density {
|
||||
try {
|
||||
const saved = localStorage.getItem(DENSITY_STORAGE_KEY);
|
||||
if (saved === "comfortable" || saved === "compact") return saved;
|
||||
} catch {
|
||||
// 读不到就用默认
|
||||
}
|
||||
return DEFAULT_DENSITY;
|
||||
}
|
||||
|
||||
function readZoom(): ZoomLevel {
|
||||
try {
|
||||
const saved = Number(localStorage.getItem(ZOOM_STORAGE_KEY));
|
||||
const level = ZOOM_LEVELS.find((z) => z === saved);
|
||||
if (level) return level;
|
||||
} catch {
|
||||
// 读不到就用默认
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
let prefs: InterfacePrefs = { density: readDensity(), zoom: readZoom() };
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function emit(next: InterfacePrefs) {
|
||||
prefs = next;
|
||||
listeners.forEach((listener) => listener());
|
||||
}
|
||||
|
||||
function applyZoom(zoom: ZoomLevel) {
|
||||
try {
|
||||
void getCurrentWebview()
|
||||
.setZoom(zoom)
|
||||
.catch(() => {
|
||||
// 不在 Tauri 里(测试、纯浏览器预览)时没有 webview
|
||||
});
|
||||
} catch {
|
||||
// 同上
|
||||
}
|
||||
}
|
||||
|
||||
export function setDensity(density: Density) {
|
||||
document.documentElement.dataset.density = density;
|
||||
try {
|
||||
localStorage.setItem(DENSITY_STORAGE_KEY, density);
|
||||
} catch {
|
||||
// 记不住也不影响这次切换
|
||||
}
|
||||
emit({ ...prefs, density });
|
||||
}
|
||||
|
||||
export function setZoom(zoom: ZoomLevel) {
|
||||
applyZoom(zoom);
|
||||
try {
|
||||
localStorage.setItem(ZOOM_STORAGE_KEY, String(zoom));
|
||||
} catch {
|
||||
// 记不住也不影响这次切换
|
||||
}
|
||||
emit({ ...prefs, zoom });
|
||||
}
|
||||
|
||||
/** 按 ZOOM_LEVELS 的档位放大 / 缩小一档,到头不动。 */
|
||||
export function stepZoom(direction: 1 | -1) {
|
||||
const index = ZOOM_LEVELS.indexOf(prefs.zoom);
|
||||
const next = ZOOM_LEVELS[index + direction];
|
||||
if (next !== undefined) setZoom(next);
|
||||
}
|
||||
|
||||
/** 主窗口启动时调一次:把记住的密度和缩放用上。 */
|
||||
export function initializeInterfacePrefs() {
|
||||
document.documentElement.dataset.density = prefs.density;
|
||||
if (prefs.zoom !== 1) applyZoom(prefs.zoom);
|
||||
}
|
||||
|
||||
function subscribe(listener: () => void) {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
}
|
||||
|
||||
export function useInterfacePrefs(): InterfacePrefs {
|
||||
return useSyncExternalStore(subscribe, () => prefs);
|
||||
}
|
||||
@@ -18,6 +18,11 @@ const twMerge = extendTailwindMerge({
|
||||
],
|
||||
radius: ["control", "panel", "dialog"],
|
||||
shadow: ["v7-sm", "v7-md", "v7-lg"],
|
||||
// 随界面密度变的尺寸:h-control 和调用方传的 h-8 要能互相覆盖
|
||||
spacing: ["control", "control-sm", "nav-row", "card-y"],
|
||||
},
|
||||
classGroups: {
|
||||
"min-h": [{ "min-h": ["card"] }],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
} from "./lib/modelsDevAutoSync";
|
||||
import { initializeWindowActivity } from "@/lib/windowActivity";
|
||||
import { initializeInputModality } from "@/lib/inputModality";
|
||||
import { initializeInterfacePrefs } from "@/lib/interfacePrefs";
|
||||
|
||||
installGlobalErrorHandlers();
|
||||
|
||||
@@ -90,6 +91,9 @@ try {
|
||||
}
|
||||
|
||||
async function bootstrap() {
|
||||
// 界面密度和缩放要在任何界面渲染之前用上(含下面的数据库升级界面),首屏不会先按紧凑尺寸画一遍
|
||||
initializeInterfacePrefs();
|
||||
|
||||
// 启动早期主动查询后端初始化错误,避免事件竞态
|
||||
try {
|
||||
const initError = (await invoke(
|
||||
|
||||
+35
-7
@@ -178,16 +178,44 @@ module.exports = {
|
||||
panel: "10px",
|
||||
dialog: "14px",
|
||||
},
|
||||
// 字号、控件高度随界面密度变(src/index.css 的 --fs-* / --h-*,<html data-density>)
|
||||
fontSize: {
|
||||
// v7 字体角色(AUTHORING.md「颜色与字体」)
|
||||
badge: ["11px", { lineHeight: "16px", fontWeight: "500" }],
|
||||
caption: ["12px", { lineHeight: "18px" }],
|
||||
body: ["13px", { lineHeight: "20px" }],
|
||||
strong: ["14px", { lineHeight: "20px", fontWeight: "500" }],
|
||||
section: ["15px", { lineHeight: "22px", fontWeight: "600" }],
|
||||
title: ["16px", { lineHeight: "24px", fontWeight: "600" }],
|
||||
page: ["18px", { lineHeight: "26px", fontWeight: "600" }],
|
||||
badge: [
|
||||
"var(--fs-badge)",
|
||||
{ lineHeight: "var(--lh-badge)", fontWeight: "500" },
|
||||
],
|
||||
caption: ["var(--fs-caption)", { lineHeight: "var(--lh-caption)" }],
|
||||
body: ["var(--fs-body)", { lineHeight: "var(--lh-body)" }],
|
||||
strong: [
|
||||
"var(--fs-strong)",
|
||||
{ lineHeight: "var(--lh-strong)", fontWeight: "500" },
|
||||
],
|
||||
section: [
|
||||
"var(--fs-section)",
|
||||
{ lineHeight: "var(--lh-section)", fontWeight: "600" },
|
||||
],
|
||||
title: [
|
||||
"var(--fs-title)",
|
||||
{ lineHeight: "var(--lh-title)", fontWeight: "600" },
|
||||
],
|
||||
page: [
|
||||
"var(--fs-page)",
|
||||
{ lineHeight: "var(--lh-page)", fontWeight: "600" },
|
||||
],
|
||||
metric: ["24px", { lineHeight: "32px", fontWeight: "600" }],
|
||||
// 还没迁到字体角色的 text-xs 跟着辅助字放大,免得舒适密度下它比正文小两档
|
||||
xs: ["var(--fs-xs)", { lineHeight: "var(--lh-xs)" }],
|
||||
},
|
||||
spacing: {
|
||||
// 按钮:control = regular,control-sm = compact
|
||||
control: "var(--h-control)",
|
||||
"control-sm": "var(--h-control-sm)",
|
||||
"nav-row": "var(--h-nav-row)",
|
||||
"card-y": "var(--card-py)",
|
||||
},
|
||||
minHeight: {
|
||||
card: "var(--h-card)",
|
||||
},
|
||||
fontFamily: {
|
||||
// 使用与之前版本保持一致的系统字体栈
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const setZoomMock = vi.hoisted(() => vi.fn(() => Promise.resolve()));
|
||||
|
||||
vi.mock("@tauri-apps/api/webview", () => ({
|
||||
getCurrentWebview: () => ({ setZoom: setZoomMock }),
|
||||
}));
|
||||
|
||||
// 模块加载时就读 localStorage,每个用例重新加载一次
|
||||
async function load() {
|
||||
vi.resetModules();
|
||||
return import("@/lib/interfacePrefs");
|
||||
}
|
||||
|
||||
describe("interfacePrefs", () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
delete document.documentElement.dataset.density;
|
||||
setZoomMock.mockClear();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
localStorage.clear();
|
||||
delete document.documentElement.dataset.density;
|
||||
});
|
||||
|
||||
it("defaults to compact density and 100% zoom without touching the webview", async () => {
|
||||
const prefs = await load();
|
||||
prefs.initializeInterfacePrefs();
|
||||
|
||||
expect(document.documentElement.dataset.density).toBe("compact");
|
||||
expect(setZoomMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("applies the stored density and zoom on startup", async () => {
|
||||
localStorage.setItem("cc-switch-density", "comfortable");
|
||||
localStorage.setItem("cc-switch-zoom", "1.25");
|
||||
const prefs = await load();
|
||||
prefs.initializeInterfacePrefs();
|
||||
|
||||
expect(document.documentElement.dataset.density).toBe("comfortable");
|
||||
expect(setZoomMock).toHaveBeenCalledWith(1.25);
|
||||
});
|
||||
|
||||
it("ignores stored values that are not a known density or zoom level", async () => {
|
||||
localStorage.setItem("cc-switch-density", "spacious");
|
||||
localStorage.setItem("cc-switch-zoom", "3");
|
||||
const prefs = await load();
|
||||
prefs.initializeInterfacePrefs();
|
||||
|
||||
expect(document.documentElement.dataset.density).toBe("compact");
|
||||
expect(setZoomMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("persists a density change and applies it to <html>", async () => {
|
||||
const prefs = await load();
|
||||
prefs.setDensity("comfortable");
|
||||
|
||||
expect(document.documentElement.dataset.density).toBe("comfortable");
|
||||
expect(localStorage.getItem("cc-switch-density")).toBe("comfortable");
|
||||
});
|
||||
|
||||
it("steps zoom one level at a time and stops at both ends", async () => {
|
||||
const prefs = await load();
|
||||
|
||||
prefs.stepZoom(1);
|
||||
prefs.stepZoom(1);
|
||||
prefs.stepZoom(1);
|
||||
expect(setZoomMock).toHaveBeenLastCalledWith(1.25);
|
||||
expect(localStorage.getItem("cc-switch-zoom")).toBe("1.25");
|
||||
|
||||
setZoomMock.mockClear();
|
||||
prefs.stepZoom(1);
|
||||
expect(setZoomMock).not.toHaveBeenCalled();
|
||||
|
||||
prefs.setZoom(1);
|
||||
prefs.stepZoom(-1);
|
||||
expect(setZoomMock).toHaveBeenLastCalledWith(0.9);
|
||||
setZoomMock.mockClear();
|
||||
prefs.stepZoom(-1);
|
||||
expect(setZoomMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user