Skip to content
Merged
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
9 changes: 9 additions & 0 deletions apps/desktop/e2e/specs/settings.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '主导航' })
Expand Down
3 changes: 2 additions & 1 deletion apps/desktop/src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
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 {
Expand Down Expand Up @@ -167,7 +167,7 @@
}))
}

function handle<T>(channel: string, schema: ZodType | null, fn: (input: any, event: Electron.IpcMainInvokeEvent) => Promise<T> | T): void {

Check warning on line 170 in apps/desktop/src/main/ipc.ts

View workflow job for this annotation

GitHub Actions / Typecheck, unit tests, launcher tests, E2E (Linux)

Unexpected any. Specify a different type
ipcMain.handle(channel, async (event, payload: unknown) =>
toResult(async () => {
const input = schema ? schema.parse(payload ?? {}) : payload
Expand Down Expand Up @@ -550,6 +550,7 @@
// 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)
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/main/services/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 40 additions & 1 deletion apps/desktop/src/main/windows.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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<BrowserWindow, number>()

/** 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()) {
Expand Down Expand Up @@ -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() })
}
Expand Down
14 changes: 14 additions & 0 deletions apps/desktop/src/renderer/src/pages/settings/GeneralSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ export function GeneralSection(): React.JSX.Element {
]}
/>
</Row>
<Row title="界面大小" description="自动会随窗口放大文字和按钮。">
<Segmented<Settings['uiScale']>
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%' },
]}
/>
</Row>
<Row title="语言" description="目前只提供简体中文界面。">
<span className="text-ui text-ink-2">简体中文</span>
</Row>
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/shared/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UI_SCALES } from './uiScale'
import { z } from 'zod'
import { agentCardAnswerSchema } from './agent/cards'
import { UPDATE_CHANNELS } from './install'
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions apps/desktop/src/shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { UiScale } from './uiScale'
import type { EngineMachine, EnginePlan } from './engineChoice'
import type { GbcLanguage } from './gbc'

Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions apps/desktop/src/shared/uiScale.ts
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions apps/desktop/test/mocks/backend/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function settingsFixture(patch: Partial<Settings> = {}): Settings {
return {
theme: 'system',
uiAnimation: 'system',
uiScale: 'auto',
language: 'zh-CN',
networkMode: 'auto',
gitMirror: null,
Expand Down
31 changes: 31 additions & 0 deletions apps/desktop/test/shared/uiScale.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
2 changes: 1 addition & 1 deletion docs/how-to/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ CUDA 引擎对 NVIDIA 驱动的最低要求:CUDA 13 需要 Windows 580.88 或

| 分区 | 能改什么 |
| --- | --- |
| 「通用」 | 「主题」(浅色、深色、跟随系统)、「动效」、「语言」 |
| 「通用」 | 「主题」(浅色、深色、跟随系统)、「动效」、「界面大小」(自动随窗口放大,或固定 100%–150%)、「语言」 |
| 「设备与引擎」 | 「显卡」:多块显卡时「设为默认」或「改为自动」,右上角可「重新检测显卡」。「引擎」:「安装引擎」「检查更新」「更新」,以及「自动更新」开关。「远程主机」。「高级」:「显存模式」「性能档位」「连跑时重新加载引擎」「每块显卡的引擎」「渠道」「版本」「添加自定义引擎」「引擎目录」 |
| 「存储」 | 「数据文件夹」「模型文件夹」「作品来源」 |
| 「网络」 | Hugging Face「访问令牌(Token)」;「高级」里的「下载方式」(自动、直连、镜像)、「Hugging Face 端点」、「同时下载的文件数」、「GitHub 令牌」 |
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`):

Expand Down
Loading