import path from 'path' import { SETTING_WINDOW_URL, TRAY_WINDOW_URL, MINI_WINDOW_URL, RENAME_WINDOW_URL, TOOLBOX_WINDOW_URL } from './constants' import { IStartupMode, IWindowList } from '#/types/enum' import bus from '@core/bus' import { CREATE_APP_MENU } from '@core/bus/constants' import { TOGGLE_SHORTKEY_MODIFIED_MODE, WINDOW_STATE_CHANGED } from '#/events/constants' import { app } from 'electron' import { T } from '~/main/i18n' import { isLinux, isWindows } from '~/universal/utils/common' import { getStaticPath } from '#/utils/staticPath' import picgo from '@core/picgo' import { getMainWindowState, saveMainWindowState } from './windowState' import { isDev } from '~/main/utils/env' // import { URLSearchParams } from 'url' const windowList = new Map() const defaultWebPreferences = { preload: path.join(__dirname, '../preload/index.js'), nodeIntegration: false, contextIsolation: true, nodeIntegrationInWorker: false, backgroundThrottling: false } const handleWindowParams = (windowURL: string) => { // const [baseURL, hash = ''] = windowURL.split('#') // const search = new URLSearchParams() // const lang = i18n.getLanguage() // search.append('lang', lang) // return `${baseURL}?${search.toString()}#${hash}` return windowURL } export const isWindowShouldShowOnStartup = (currentWindow: IWindowList) => { const startupMode = picgo.getConfig('settings.startupMode') || (isLinux ? IStartupMode.SHOW_MINI_WINDOW : isWindows ? IStartupMode.SHOW_MAIN_WINDOW : IStartupMode.HIDE) switch (currentWindow) { case IWindowList.MINI_WINDOW: { return startupMode === IStartupMode.SHOW_MINI_WINDOW } case IWindowList.SETTING_WINDOW: { return startupMode === IStartupMode.SHOW_MAIN_WINDOW } default: { return false } } } windowList.set(IWindowList.TRAY_WINDOW, { isValid: process.platform !== 'linux', multiple: false, options () { return { height: 350, width: 196, // 196 show: false, frame: false, fullscreenable: false, resizable: false, transparent: false, backgroundColor: '#111827', webPreferences: { ...defaultWebPreferences, webSecurity: false } } }, callback (window) { window.loadURL(handleWindowParams(TRAY_WINDOW_URL)) window.on('blur', () => { window.hide() }) } }) windowList.set(IWindowList.SETTING_WINDOW, { isValid: true, multiple: false, options () { const showDockIcon = picgo.getConfig('settings.showDockIcon') !== false const mainWindowState = getMainWindowState() const options: IBrowserWindowOptions = { height: mainWindowState.height, width: mainWindowState.width, minHeight: 450, minWidth: 800, show: false, frame: false, center: true, fullscreenable: false, resizable: true, title: 'PicGo', transparent: false, backgroundColor: '#0f172a', skipTaskbar: !showDockIcon, webPreferences: { ...defaultWebPreferences, webSecurity: false } } if (process.platform !== 'darwin') { options.show = false options.frame = false options.icon = getStaticPath('logo.png') options.skipTaskbar = false } return options }, callback (window, windowManager) { window.loadURL(handleWindowParams(SETTING_WINDOW_URL)) if (isDev) { window.webContents.openDevTools({ mode: 'detach' }) } window.on('maximize', () => { window.webContents.send(WINDOW_STATE_CHANGED, { isMaximized: true }) }) window.on('unmaximize', () => { window.webContents.send(WINDOW_STATE_CHANGED, { isMaximized: false }) }) window.on('close', () => { const nextBounds = window.isMaximized() ? window.getNormalBounds() : window.getBounds() saveMainWindowState({ width: nextBounds.width, height: nextBounds.height, isMaximized: window.isMaximized() }) }) window.on('closed', () => { bus.emit(TOGGLE_SHORTKEY_MODIFIED_MODE, false) if (process.platform === 'linux') { process.nextTick(() => { app.quit() }) } }) if (getMainWindowState().isMaximized) { window.maximize() } bus.emit(CREATE_APP_MENU) windowManager.create(IWindowList.MINI_WINDOW) } }) windowList.set(IWindowList.MINI_WINDOW, { isValid: process.platform !== 'darwin', multiple: false, options () { const obj: IBrowserWindowOptions = { height: 64, width: 64, show: false, frame: false, fullscreenable: false, skipTaskbar: true, resizable: false, transparent: true, icon: getStaticPath('logo.png'), webPreferences: { ...defaultWebPreferences } } if (picgo.getConfig('settings.miniWindowOnTop')) { obj.alwaysOnTop = true } return obj }, callback (window) { window.loadURL(handleWindowParams(MINI_WINDOW_URL)) } }) windowList.set(IWindowList.RENAME_WINDOW, { isValid: true, multiple: true, options () { const options: IBrowserWindowOptions = { height: 175, width: 300, show: true, fullscreenable: false, resizable: false, backgroundColor: '#0f172a', webPreferences: { ...defaultWebPreferences } } if (process.platform !== 'darwin') { options.show = true options.autoHideMenuBar = true } return options }, async callback (window, windowManager) { window.loadURL(handleWindowParams(RENAME_WINDOW_URL)) const currentWindow = windowManager.getAvailableWindow() if (currentWindow && currentWindow.isVisible()) { // bounds: { x: 821, y: 75, width: 800, height: 450 } const bounds = currentWindow.getBounds() const positionX = bounds.x + bounds.width / 2 - 150 let positionY // if is the settingWindow if (bounds.height > 400) { positionY = bounds.y + bounds.height / 2 - 88 } else { // if is the miniWindow positionY = bounds.y + bounds.height / 2 } window.setPosition(positionX, positionY, false) } } }) windowList.set(IWindowList.TOOLBOX_WINDOW, { isValid: true, multiple: false, options () { const options: IBrowserWindowOptions = { height: 480, width: 800, show: false, frame: true, center: true, fullscreenable: false, resizable: false, backgroundColor: '#0f172a', title: `PicGo-${T('TOOLBOX')}`, icon: getStaticPath('logo.png'), webPreferences: { ...defaultWebPreferences, webSecurity: false } } if (process.platform !== 'darwin') { options.autoHideMenuBar = true } return options }, async callback (window, windowManager) { window.loadURL(TOOLBOX_WINDOW_URL) const currentWindow = windowManager.getAvailableWindow() if (currentWindow && currentWindow.isVisible()) { // bounds: { x: 821, y: 75, width: 800, height: 450 } const bounds = currentWindow.getBounds() const positionX = Math.round(bounds.x + bounds.width / 2 - 400) let positionY // if is the settingWindow if (bounds.height > 400) { positionY = Math.round(bounds.y + bounds.height / 2 - 240) } else { // if is the miniWindow positionY = Math.round(bounds.y + bounds.height / 2) } window.setPosition(positionX, positionY, false) } } }) export default windowList