diff --git a/apps/desktop/e2e/specs/settings.e2e.ts b/apps/desktop/e2e/specs/settings.e2e.ts index 937dffb..16cec65 100644 --- a/apps/desktop/e2e/specs/settings.e2e.ts +++ b/apps/desktop/e2e/specs/settings.e2e.ts @@ -60,6 +60,15 @@ describe('settings', () => { await page.emulateMedia({ colorScheme: null }) }) + it('offers 界面大小, 自动 by default, and saves a fixed size', async () => { + const page = await app.open({ route: '/settings/general' }) + const picker = page.getByRole('group', { name: '界面大小' }) + expect(await picker.getByRole('button', { name: '自动' }).getAttribute('aria-pressed')).toBe('true') + await picker.getByRole('button', { name: '125%' }).click() + await expect.poll(() => app.calls('settings:update').then((calls) => calls.at(-1))).toEqual({ uiScale: '125' }) + await expect.poll(() => picker.getByRole('button', { name: '125%' }).getAttribute('aria-pressed')).toBe('true') + }) + it('turning 训练 on adds it to the rail; off removes it', async () => { const page = await app.open({ route: '/settings/features' }) const rail = page.getByRole('navigation', { name: '主导航' }) diff --git a/apps/desktop/src/main/ipc.ts b/apps/desktop/src/main/ipc.ts index dbb69d7..b91f99e 100644 --- a/apps/desktop/src/main/ipc.ts +++ b/apps/desktop/src/main/ipc.ts @@ -92,7 +92,7 @@ import { codedError, errorCode } from './lib/codedError' import { getEngineFeatures } from './services/engineFeatures' import { importImageBytes, importImageFile, pruneProjectMasks, saveMask } from './services/studioAssets' import { addControlAdapter, listControlAdapters, removeControlAdapter } from './services/controlAdapters' -import { applyThemeSource, controlWindow, openWindow } from './windows' +import { applyThemeSource, applyUiScale, controlWindow, openWindow } from './windows' import { getGpuInventory, listDevices, refreshGpuInventory } from './services/devices' import { getOnboardingStatus } from './services/onboarding' import { @@ -550,6 +550,7 @@ export function registerIpc(): void { // A GPU switched to another engine frees its old moss-server's memory now, not at its next job. releaseAfterSettingsChange(before, settings) if (before.theme !== settings.theme) applyThemeSource(settings.theme) + if (before.uiScale !== settings.uiScale) applyUiScale(settings.uiScale) if (!before.engineAutoUpdate && settings.engineAutoUpdate) clearEngineAutoUpdateHold() invalidateEngineReadiness() ensureDirs(settings.modelsRoot, settings.modelDirectories) diff --git a/apps/desktop/src/main/services/settings.ts b/apps/desktop/src/main/services/settings.ts index 7fd8950..1273b95 100644 --- a/apps/desktop/src/main/services/settings.ts +++ b/apps/desktop/src/main/services/settings.ts @@ -9,6 +9,7 @@ import { isInside, paths, setEngineRoot } from '../lib/paths' export const DEFAULT_SETTINGS: Settings = { theme: 'system', uiAnimation: 'system', + uiScale: 'auto', language: 'zh-CN', networkMode: 'auto', gitMirror: null, diff --git a/apps/desktop/src/main/windows.ts b/apps/desktop/src/main/windows.ts index f6fd706..59c3a1a 100644 --- a/apps/desktop/src/main/windows.ts +++ b/apps/desktop/src/main/windows.ts @@ -1,7 +1,9 @@ import { app, BrowserWindow, nativeTheme, shell } from 'electron' import { join } from 'node:path' import { Events } from '@shared/channels' +import { uiZoomFactor, type UiScale } from '@shared/uiScale' import { applyAppDetails } from './install/identity' +import { getSettings } from './services/settings' export type WindowKind = 'studio' @@ -38,11 +40,41 @@ nativeTheme.on('updated', () => { if (win.isDestroyed()) continue win.setBackgroundColor(colors.background) if (process.platform === 'win32') { - win.setTitleBarOverlay({ color: '#00000000', symbolColor: colors.symbol, height: TITLEBAR_HEIGHT }) + win.setTitleBarOverlay({ color: '#00000000', symbolColor: colors.symbol, height: titleBarHeight(win) }) } } }) +/** 界面大小, read from settings when the first window opens. */ +let uiScale: UiScale | null = null +const zooms = new WeakMap() + +/** The renderer's title bar is 40 CSS px; zoomed, it is taller on screen, and the native buttons must sit in it. */ +function titleBarHeight(win: BrowserWindow): number { + return Math.round(TITLEBAR_HEIGHT * (zooms.get(win) ?? 1)) +} + +/** Zooms a window for 界面大小 at its present size; a resize may change it (自动). */ +function zoomToSize(win: BrowserWindow): void { + if (win.isDestroyed()) return + uiScale ??= getSettings().uiScale + const factor = uiZoomFactor(uiScale, win.getContentBounds()) + if (zooms.get(win) === factor) return + zooms.set(win, factor) + win.webContents.setZoomFactor(factor) + if (process.platform === 'win32') { + win.setTitleBarOverlay({ color: '#00000000', symbolColor: currentChrome().symbol, height: titleBarHeight(win) }) + } + // The traffic lights are 14 px tall; keep them centred in the taller bar. + if (process.platform === 'darwin') win.setWindowButtonPosition({ x: 16, y: Math.round(titleBarHeight(win) / 2 - 7) }) +} + +/** 界面大小 changed in 设置: every open window takes it now. */ +export function applyUiScale(scale: UiScale): void { + uiScale = scale + for (const win of windows.values()) zoomToSize(win) +} + export function createAppWindow(kind: WindowKind, show = true): BrowserWindow { const existing = windows.get(kind) if (existing && !existing.isDestroyed()) { @@ -122,6 +154,13 @@ export function createAppWindow(kind: WindowKind, show = true): BrowserWindow { void win.loadFile(join(__dirname, '../renderer/index.html'), { hash: meta.route }) } + win.on('resize', () => zoomToSize(win)) + // A page load starts at the default zoom. + win.webContents.on('did-finish-load', () => { + zooms.delete(win) + zoomToSize(win) + }) + const sendMaximized = (): void => { if (!win.isDestroyed()) win.webContents.send(Events.windowState, { maximized: win.isMaximized() }) } diff --git a/apps/desktop/src/renderer/src/pages/settings/GeneralSection.tsx b/apps/desktop/src/renderer/src/pages/settings/GeneralSection.tsx index 13ad22b..b72b249 100644 --- a/apps/desktop/src/renderer/src/pages/settings/GeneralSection.tsx +++ b/apps/desktop/src/renderer/src/pages/settings/GeneralSection.tsx @@ -35,6 +35,20 @@ export function GeneralSection(): React.JSX.Element { ]} /> + + + label="界面大小" + value={settings.uiScale} + onChange={(uiScale) => void save({ uiScale })} + options={[ + { value: 'auto', label: '自动' }, + { value: '100', label: '100%' }, + { value: '110', label: '110%' }, + { value: '125', label: '125%' }, + { value: '150', label: '150%' }, + ]} + /> + 简体中文 diff --git a/apps/desktop/src/shared/schemas.ts b/apps/desktop/src/shared/schemas.ts index 6638cd4..17d5e78 100644 --- a/apps/desktop/src/shared/schemas.ts +++ b/apps/desktop/src/shared/schemas.ts @@ -1,3 +1,4 @@ +import { UI_SCALES } from './uiScale' import { z } from 'zod' import { agentCardAnswerSchema } from './agent/cards' import { UPDATE_CHANNELS } from './install' @@ -324,6 +325,7 @@ export const schemas = { .object({ theme: z.enum(['system', 'light', 'dark']), uiAnimation: z.enum(['system', 'on', 'off']), + uiScale: z.enum(UI_SCALES), language: z.enum(['zh-CN', 'en']), networkMode: z.enum(['auto', 'direct', 'mirror']), gitMirror: z.string().max(2048).nullable(), diff --git a/apps/desktop/src/shared/types.ts b/apps/desktop/src/shared/types.ts index 878970c..1ab9dd3 100644 --- a/apps/desktop/src/shared/types.ts +++ b/apps/desktop/src/shared/types.ts @@ -1,3 +1,4 @@ +import type { UiScale } from './uiScale' import type { EngineMachine, EnginePlan } from './engineChoice' import type { GbcLanguage } from './gbc' @@ -145,6 +146,8 @@ export interface ModelScanResult { export interface Settings { theme: 'system' | 'light' | 'dark' uiAnimation: 'system' | 'on' | 'off' + /** 界面大小: 自动 grows text and controls with the window (see uiScale.ts). */ + uiScale: UiScale language: 'zh-CN' | 'en' networkMode: 'auto' | 'direct' | 'mirror' gitMirror: string | null diff --git a/apps/desktop/src/shared/uiScale.ts b/apps/desktop/src/shared/uiScale.ts new file mode 100644 index 0000000..aacfd19 --- /dev/null +++ b/apps/desktop/src/shared/uiScale.ts @@ -0,0 +1,27 @@ +/** + * 界面大小: how large text and controls are drawn. 自动 grows them with the + * window from its default size, so a maximized window on a large screen is + * not a sea of small text; a fixed size keeps them there. Main applies the + * result as the window's zoom factor. No Node or DOM imports. + */ + +export const UI_SCALES = ['auto', '100', '110', '125', '150'] as const +export type UiScale = (typeof UI_SCALES)[number] + +/** The window size the layout is drawn for at 100 % (the window's default size). */ +const BASE = { width: 1600, height: 1000 } +/** The smallest layout the pages hold (the window's minimum size): zoom never squeezes the page below it. */ +const MIN_LAYOUT = { width: 1024, height: 640 } +const AUTO_MAX = 1.5 +/** Zoom moves in steps, so resizing a window does not lay the page out again at every pixel. */ +const STEP = 0.05 + +/** The zoom factor for a window whose content area is `size` (in DIPs). */ +export function uiZoomFactor(scale: UiScale, size: { width: number; height: number }): number { + const wanted = + scale === 'auto' ? Math.min(AUTO_MAX, Math.max(1, Math.min(size.width / BASE.width, size.height / BASE.height))) : Number(scale) / 100 + // The page must still have its minimum size in CSS pixels; a window smaller than that stays at 100 %. + const room = Math.min(size.width / MIN_LAYOUT.width, size.height / MIN_LAYOUT.height) + const factor = Math.max(1, Math.min(wanted, room)) + return Math.round(Math.floor(factor / STEP + 1e-9) * STEP * 100) / 100 +} diff --git a/apps/desktop/test/mocks/backend/fixtures.ts b/apps/desktop/test/mocks/backend/fixtures.ts index 12dbd60..6278e81 100644 --- a/apps/desktop/test/mocks/backend/fixtures.ts +++ b/apps/desktop/test/mocks/backend/fixtures.ts @@ -55,6 +55,7 @@ export function settingsFixture(patch: Partial = {}): Settings { return { theme: 'system', uiAnimation: 'system', + uiScale: 'auto', language: 'zh-CN', networkMode: 'auto', gitMirror: null, diff --git a/apps/desktop/test/shared/uiScale.test.ts b/apps/desktop/test/shared/uiScale.test.ts new file mode 100644 index 0000000..af402d6 --- /dev/null +++ b/apps/desktop/test/shared/uiScale.test.ts @@ -0,0 +1,31 @@ +import { describe, expect, it } from 'vitest' +import { uiZoomFactor } from '@shared/uiScale' + +describe('shared/uiScale', () => { + it('draws the default window at 100 % and grows with it when 自动', () => { + expect(uiZoomFactor('auto', { width: 1600, height: 1000 })).toBe(1) + // A maximized window on a 1440p screen: both sides grew by at least 40 %. + expect(uiZoomFactor('auto', { width: 2560, height: 1400 })).toBe(1.4) + // The shorter side decides: a wide but low window does not grow. + expect(uiZoomFactor('auto', { width: 3000, height: 1000 })).toBe(1) + // No more than 150 %. + expect(uiZoomFactor('auto', { width: 3840, height: 2100 })).toBe(1.5) + }) + + it('never shrinks below 100 %, even in a small window', () => { + expect(uiZoomFactor('auto', { width: 1100, height: 700 })).toBe(1) + expect(uiZoomFactor('110', { width: 900, height: 600 })).toBe(1) + }) + + it('keeps a fixed size unless the page would fall below its minimum layout', () => { + expect(uiZoomFactor('125', { width: 1600, height: 1000 })).toBe(1.25) + expect(uiZoomFactor('150', { width: 1600, height: 1000 })).toBe(1.5) + // 1280×800 at 150 % would leave a 853 px page; it stops at what fits (1280 / 1024 = 1.25). + expect(uiZoomFactor('150', { width: 1280, height: 800 })).toBe(1.25) + }) + + it('moves in steps of 5 %, so resizing does not lay the page out at every pixel', () => { + expect(uiZoomFactor('auto', { width: 1700, height: 1063 })).toBe(1.05) + expect(uiZoomFactor('auto', { width: 1710, height: 1069 })).toBe(1.05) + }) +}) diff --git a/docs/how-to/setup.md b/docs/how-to/setup.md index cee2d75..7942ab1 100644 --- a/docs/how-to/setup.md +++ b/docs/how-to/setup.md @@ -103,7 +103,7 @@ CUDA 引擎对 NVIDIA 驱动的最低要求:CUDA 13 需要 Windows 580.88 或 | 分区 | 能改什么 | | --- | --- | -| 「通用」 | 「主题」(浅色、深色、跟随系统)、「动效」、「语言」 | +| 「通用」 | 「主题」(浅色、深色、跟随系统)、「动效」、「界面大小」(自动随窗口放大,或固定 100%–150%)、「语言」 | | 「设备与引擎」 | 「显卡」:多块显卡时「设为默认」或「改为自动」,右上角可「重新检测显卡」。「引擎」:「安装引擎」「检查更新」「更新」,以及「自动更新」开关。「远程主机」。「高级」:「显存模式」「性能档位」「连跑时重新加载引擎」「每块显卡的引擎」「渠道」「版本」「添加自定义引擎」「引擎目录」 | | 「存储」 | 「数据文件夹」「模型文件夹」「作品来源」 | | 「网络」 | Hugging Face「访问令牌(Token)」;「高级」里的「下载方式」(自动、直连、镜像)、「Hugging Face 端点」、「同时下载的文件数」、「GitHub 令牌」 | diff --git a/docs/reference/architecture.md b/docs/reference/architecture.md index 90af968..8674d95 100644 --- a/docs/reference/architecture.md +++ b/docs/reference/architecture.md @@ -30,7 +30,7 @@ The build (`apps/desktop/electron.vite.config.ts`) has two main entries, `index` | --- | --- | --- | --- | | `studio` | `/studio` | 1600 × 1000, minimum 1024 × 640 | MossStudio | -The studio window opens at start. The separate trainer window is retired ([ADR-0015](/docs/decisions/0015-training-in-the-main-window.md)); `window:open` with `kind: 'trainer'` opens `/training` in the studio window. On Linux the window is frameless and the renderer draws the title bar (`components/shell/Titlebar.tsx`, `window:control`, `event:window-state`); Windows uses a hidden title bar with an overlay, macOS `hiddenInset`. Two more windows exist outside the app proper: the setup and uninstall windows (`createInstallWindow` in `main/install/app.ts`) load the same renderer at `/install` and `/uninstall`, which `App.tsx` renders standalone without settings, models or jobs. +The studio window opens at start. The separate trainer window is retired ([ADR-0015](/docs/decisions/0015-training-in-the-main-window.md)); `window:open` with `kind: 'trainer'` opens `/training` in the studio window. On Linux the window is frameless and the renderer draws the title bar (`components/shell/Titlebar.tsx`, `window:control`, `event:window-state`); Windows uses a hidden title bar with an overlay, macOS `hiddenInset`. 界面大小 (`Settings.uiScale`, `shared/uiScale.ts`) is the window's zoom factor: 自动 grows it with the window from 1600 × 1000 up to 150 %, a fixed size keeps it, and neither lets the page fall below its 1024 × 640 layout. `main/windows.ts` applies it on every resize and when the setting changes, and moves the native title bar parts (Windows overlay height, macOS traffic lights) to the zoomed 40 px bar. Two more windows exist outside the app proper: the setup and uninstall windows (`createInstallWindow` in `main/install/app.ts`) load the same renderer at `/install` and `/uninstall`, which `App.tsx` renders standalone without settings, models or jobs. How the process was started decides paths and behaviour (`RunMode` in `main/install/runContext.ts`):