feat(desktop): 接通无边框窗口标题栏控制

This commit is contained in:
2026-09-07 17:48:51 +08:00
parent a63398d89c
commit 4288f90558
9 changed files with 115 additions and 20 deletions
@@ -0,0 +1,47 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
const native = vi.hoisted(() => ({
enabled: false,
minimize: vi.fn(),
toggleMaximize: vi.fn(),
close: vi.fn(),
}))
vi.mock('@tauri-apps/api/core', () => ({ isTauri: () => native.enabled, invoke: vi.fn() }))
vi.mock('@tauri-apps/api/window', () => ({
getCurrentWindow: () => ({
minimize: native.minimize,
toggleMaximize: native.toggleMaximize,
close: native.close,
}),
}))
import { minimizeWindow, requestWindowClose, toggleMaximizeWindow } from './windowControls'
beforeEach(() => {
native.enabled = false
native.minimize.mockReset()
native.toggleMaximize.mockReset()
native.close.mockReset()
})
describe('桌面窗口控制', () => {
it('Web 模式不模拟窗口操作', async () => {
await minimizeWindow()
await toggleMaximizeWindow()
await requestWindowClose()
expect(native.minimize).not.toHaveBeenCalled()
expect(native.toggleMaximize).not.toHaveBeenCalled()
expect(native.close).not.toHaveBeenCalled()
})
it('桌面模式调用当前 Tauri 窗口', async () => {
native.enabled = true
await minimizeWindow()
await toggleMaximizeWindow()
await requestWindowClose()
expect(native.minimize).toHaveBeenCalledOnce()
expect(native.toggleMaximize).toHaveBeenCalledOnce()
expect(native.close).toHaveBeenCalledOnce()
})
})
@@ -0,0 +1,16 @@
/** 桌面窗口操作集中于此边界;Web 页面不会显示或模拟原生窗口行为。 */
import { getCurrentWindow } from '@tauri-apps/api/window'
import { isDesktop } from './desktop'
export async function minimizeWindow() {
if (isDesktop()) await getCurrentWindow().minimize()
}
export async function toggleMaximizeWindow() {
if (isDesktop()) await getCurrentWindow().toggleMaximize()
}
export async function requestWindowClose() {
// close 触发 Rust CloseRequestedHost 会要求前端先保存,再由 lifecycle destroy。
if (isDesktop()) await getCurrentWindow().close()
}