/
githubmirror
/
marktext
Обзор
Документация
Войти
/
githubmirror
/
marktext
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/desktop/src/main/windows/setting.ts
130 строк
4 KB
Ran Luo
refactor(desktop): remove remaining no-explicit-any suppressions (#4548)
17 июн 2026, 13:24
Не верифицирован
17 июн 2026, 13:24
2482e00
Код
Авторство
О чём код?
import path from 'path' import { BrowserWindow, ipcMain } from 'electron' import type { BrowserWindowConstructorOptions } from 'electron' import { electronLocalshortcut } from '@hfelix/electron-localshortcut' import BaseWindow, { WindowLifecycle, WindowType, type EnvLike, type PreferenceLike } from './base' import type Accessor from '../app/accessor' import { centerWindowOptions } from './utils' import { TITLE_BAR_HEIGHT, preferencesWinOptions, isLinux, isOsx } from '../config' import log from 'electron-log' class SettingWindow extends BaseWindow { /** * @param accessor The application accessor for application instances. */ constructor(accessor: Accessor) { super(accessor) this.type = WindowType.SETTINGS } /** * Creates a new setting window. * * @param category The settings category tab name. */ createWindow(category: string | null = null): BrowserWindow { const { menu: appMenu, env, keybindings, preferences } = this._accessor const winOptions: BrowserWindowConstructorOptions = Object.assign({}, preferencesWinOptions) centerWindowOptions( winOptions as BrowserWindowConstructorOptions & { width: number height: number x?: number y?: number } ) if (isLinux) { winOptions.icon = path.join( (global as unknown as { __static: string }).__static, 'logo-96px.png' ) } // WORKAROUND: Electron has issues with different DPI per monitor when // setting a fixed window size. winOptions.resizable = true // Enable native or custom/frameless window and titlebar const { titleBarStyle, theme } = preferences.getAll() if (!isOsx) { winOptions.titleBarStyle = 'default' if (titleBarStyle === 'native') { winOptions.frame = true } } winOptions.backgroundColor = this._getPreferredBackgroundColor(theme) let win: BrowserWindow | null = (this.browserWindow = new BrowserWindow(winOptions)) win.webContents.on('did-fail-load', (_event, code, desc, url) => { log.error(`did-fail-load ${code} ${desc} @ ${url}`) }) win.webContents.on('render-process-gone', (_event, details) => { log.error(`render-process-gone: ${details.reason} (${details.exitCode})`) }) this.id = win.id // Create a menu for the current window appMenu.addSettingMenu(win) win.once('ready-to-show', () => { this.lifecycle = WindowLifecycle.READY this.emit('window-ready') }) win.on('focus', () => { this.emit('window-focus') win!.webContents.send('mt::window-active-status', { status: true }) }) // Lost focus win.on('blur', () => { this.emit('window-blur') win!.webContents.send('mt::window-active-status', { status: false }) }) win.on('close', (event) => { this.emit('window-close') event.preventDefault() ipcMain.emit('window-close-by-id', win!.id) }) // The window is now destroyed. win.on('closed', () => { this.emit('window-closed') // Free window reference win = null }) this.lifecycle = WindowLifecycle.LOADING win.loadURL(this._buildUrlString(this.id, env, preferences, category)) win.setSheetOffset(TITLE_BAR_HEIGHT) const devToolsAccelerator = keybindings.getAccelerator('view.toggle-dev-tools') if (env.debug && devToolsAccelerator) { electronLocalshortcut.register(win, devToolsAccelerator, () => { win!.webContents.toggleDevTools() }) } return win } protected override _buildUrlString( windowId: number | null, env: EnvLike, userPreference: PreferenceLike, category?: string | null ): string { const url = this._buildUrlWithSettings(windowId, env, userPreference) if (category) { // Overwrite type to add category name url.searchParams.set('type', `${WindowType.SETTINGS}/${category}`) } return url.toString() } } export default SettingWindow