| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- // Tauri API 封装 - 替代原来的 Electron API
- import { invoke } from '@tauri-apps/api/core'
- import { listen, type UnlistenFn } from '@tauri-apps/api/event'
- import { getCurrentWindow, type PhysicalPosition, type PhysicalSize } from '@tauri-apps/api/window'
- import { Store } from '@tauri-apps/plugin-store'
- // 窗口状态存储
- let windowStore: Store | null = null
- interface WindowState {
- width: number
- height: number
- x: number
- y: number
- maximized: boolean
- }
- async function getWindowStore(): Promise<Store> {
- if (!windowStore) {
- windowStore = await Store.load('window-state.json')
- }
- return windowStore
- }
- import type {
- SoftwareType,
- SoftwareTypeWithAll,
- Platform,
- InstallOptions,
- InstallStatus,
- InstallResult,
- InstalledInfo,
- AllInstalledInfo,
- VersionResult,
- PackageManagerResult,
- GitMirrorType,
- GitMirrorConfig,
- NodejsMirrorType,
- NodejsMirrorConfig,
- InstallHistoryItem,
- LogEntry,
- UpdateResult,
- } from '@shared/types'
- // 存储事件监听器的取消函数
- const listeners: UnlistenFn[] = []
- // 窗口状态保存的防抖定时器
- let saveWindowStateTimer: ReturnType<typeof setTimeout> | null = null
- const SAVE_DEBOUNCE_MS = 500 // 500ms 防抖延迟
- export const tauriAPI = {
- // ==================== 安装相关 ====================
- async install(software: SoftwareTypeWithAll, options: InstallOptions): Promise<void> {
- await invoke('install_software', { software, options })
- },
- async cancelInstall(): Promise<boolean> {
- return await invoke('cancel_install')
- },
- async checkInstalled(software: SoftwareTypeWithAll): Promise<InstalledInfo | AllInstalledInfo> {
- return await invoke('check_installed', { software })
- },
- async uninstall(software: SoftwareType): Promise<boolean> {
- return await invoke('uninstall_software', { software })
- },
- // ==================== 系统检测 ====================
- async checkAdmin(): Promise<boolean> {
- return await invoke('check_admin')
- },
- async checkPackageManager(): Promise<PackageManagerResult> {
- return await invoke('check_package_manager')
- },
- async installPackageManager(manager: string): Promise<{ success: boolean; error?: string }> {
- try {
- await invoke('install_package_manager', { manager })
- return { success: true }
- } catch (error) {
- return { success: false, error: String(error) }
- }
- },
- async getPlatform(): Promise<Platform> {
- return await invoke('get_platform')
- },
- async checkNetwork(): Promise<boolean> {
- return await invoke('check_network')
- },
- // ==================== 版本 ====================
- async getVersions(software: SoftwareType): Promise<VersionResult> {
- return await invoke('get_versions', { software })
- },
- async checkUpdate(software: SoftwareType): Promise<{ hasUpdate: boolean; latestVersion?: string }> {
- return await invoke('check_update', { software })
- },
- // ==================== Git 镜像配置 ====================
- async setGitMirror(mirror: GitMirrorType): Promise<void> {
- await invoke('set_git_mirror', { mirror })
- },
- async getGitMirrorConfig(): Promise<GitMirrorConfig> {
- return await invoke('get_git_mirror_config')
- },
- // ==================== Node.js 镜像配置 ====================
- async setNodejsMirror(mirror: NodejsMirrorType): Promise<void> {
- await invoke('set_nodejs_mirror', { mirror })
- },
- async getNodejsMirrorConfig(): Promise<NodejsMirrorConfig> {
- return await invoke('get_nodejs_mirror_config')
- },
- // ==================== 历史和日志 ====================
- async getInstallHistory(limit?: number): Promise<InstallHistoryItem[]> {
- return await invoke('get_install_history', { limit })
- },
- async getLogs(): Promise<LogEntry[]> {
- return await invoke('get_logs')
- },
- async writeInstallLog(message: string, level?: 'info' | 'warn' | 'error'): Promise<void> {
- await invoke('write_install_log', { message, level })
- },
- async getLogPaths(): Promise<{ appLog: string; installLog: string }> {
- return await invoke('get_log_paths')
- },
- // ==================== 文件夹选择 ====================
- async selectDirectory(defaultPath?: string): Promise<{ canceled: boolean; path: string | null }> {
- return await invoke('select_directory', { defaultPath })
- },
- // ==================== 窗口操作 ====================
- async setWindowTitle(title: string): Promise<void> {
- await invoke('set_window_title', { title })
- },
- async windowMinimize(): Promise<void> {
- await invoke('window_minimize')
- },
- async windowMaximize(): Promise<boolean> {
- return await invoke('window_maximize')
- },
- async windowClose(): Promise<void> {
- await invoke('window_close')
- },
- async windowIsMaximized(): Promise<boolean> {
- return await invoke('window_is_maximized')
- },
- // 保存窗口状态(内部实现)
- async _doSaveWindowState(): Promise<void> {
- try {
- const window = getCurrentWindow()
- const size = await window.innerSize()
- const position = await window.outerPosition()
- const maximized = await window.isMaximized()
- const state: WindowState = {
- width: size.width,
- height: size.height,
- x: position.x,
- y: position.y,
- maximized,
- }
- const store = await getWindowStore()
- await store.set('windowState', state)
- await store.save()
- } catch (error) {
- console.error('Failed to save window state:', error)
- }
- },
- // 保存窗口状态(带防抖)
- saveWindowState(): void {
- if (saveWindowStateTimer) {
- clearTimeout(saveWindowStateTimer)
- }
- saveWindowStateTimer = setTimeout(() => {
- this._doSaveWindowState()
- saveWindowStateTimer = null
- }, SAVE_DEBOUNCE_MS)
- },
- // 立即保存窗口状态(用于关闭前)
- async saveWindowStateImmediate(): Promise<void> {
- if (saveWindowStateTimer) {
- clearTimeout(saveWindowStateTimer)
- saveWindowStateTimer = null
- }
- await this._doSaveWindowState()
- },
- // 开始监听窗口状态变化(resize 和 move)
- startWindowStateListener(): void {
- const window = getCurrentWindow()
- // 监听窗口大小变化
- window.onResized(() => {
- this.saveWindowState()
- }).then((unlisten) => {
- listeners.push(unlisten)
- })
- // 监听窗口位置变化
- window.onMoved(() => {
- this.saveWindowState()
- }).then((unlisten) => {
- listeners.push(unlisten)
- })
- },
- // 恢复窗口状态
- async restoreWindowState(): Promise<void> {
- try {
- const store = await getWindowStore()
- const state = await store.get<WindowState>('windowState')
- if (state) {
- const window = getCurrentWindow()
- // 先设置位置和大小
- if (!state.maximized) {
- await window.setPosition(new PhysicalPosition(state.x, state.y))
- await window.setSize(new PhysicalSize(state.width, state.height))
- }
- // 如果之前是最大化状态,则最大化
- if (state.maximized) {
- await window.maximize()
- }
- }
- } catch (error) {
- console.error('Failed to restore window state:', error)
- }
- },
- // ==================== Claude Code ====================
- async checkClaudeCode(): Promise<{ installed: boolean; version: string | null }> {
- return await invoke('check_claude_code')
- },
- async launchClaudeCode(): Promise<{ success: boolean }> {
- return await invoke('launch_claude_code')
- },
- async installClaudeCode(): Promise<{ success: boolean; error?: string }> {
- return await invoke('install_claude_code')
- },
- // ==================== VS Code Extensions ====================
- async checkVscodeExtension(extensionId: string): Promise<{ installed: boolean; version?: string }> {
- return await invoke('check_vscode_extension', { extensionId })
- },
- async installVscodeExtension(extensionId: string): Promise<{ success: boolean; error?: string }> {
- return await invoke('install_vscode_extension', { extensionId })
- },
- // ==================== 事件监听 ====================
- // 注意:这些方法是同步的,以保持与原 Electron API 的兼容性
- // 内部使用 Promise 但不返回它
- onInstallStatus(callback: (data: InstallStatus) => void): void {
- listen<InstallStatus>('install-status', (event) => {
- callback(event.payload)
- }).then((unlisten) => {
- listeners.push(unlisten)
- })
- },
- onInstallComplete(callback: (data: InstallResult) => void): void {
- listen<InstallResult>('install-complete', (event) => {
- callback(event.payload)
- }).then((unlisten) => {
- listeners.push(unlisten)
- })
- },
- onInstallError(callback: (data: InstallResult) => void): void {
- listen<InstallResult>('install-error', (event) => {
- callback(event.payload)
- }).then((unlisten) => {
- listeners.push(unlisten)
- })
- },
- onNetworkChange(callback: (online: boolean) => void): void {
- listen<boolean>('network-change', (event) => {
- callback(event.payload)
- }).then((unlisten) => {
- listeners.push(unlisten)
- })
- },
- removeAllListeners(): void {
- listeners.forEach((unlisten) => unlisten())
- listeners.length = 0
- },
- // ==================== 自动更新 ====================
- async updaterCheck(): Promise<UpdateResult> {
- return await invoke('updater_check')
- },
- async updaterDownload(): Promise<UpdateResult> {
- return await invoke('updater_download')
- },
- async updaterInstall(): Promise<void> {
- await invoke('updater_install')
- },
- async updaterVersion(): Promise<string> {
- return await invoke('updater_version')
- },
- async updaterIsPortable(): Promise<boolean> {
- return await invoke('updater_is_portable')
- },
- onUpdaterStatus(callback: (data: UpdateResult) => void): void {
- listen<UpdateResult>('updater-status', (event) => {
- callback(event.payload)
- }).then((unlisten) => {
- listeners.push(unlisten)
- })
- },
- }
- // 导出类型以便兼容
- export type TauriAPI = typeof tauriAPI
|