| import { BrowserWindow, app, shell } from 'electron' |
| import { quickAskWindowConfig } from './quickAskWindowConfig' |
| import { mainWindowConfig } from './mainWindowConfig' |
| import { getAppConfigurations, AppEvent } from '@janhq/core/node' |
| import { getBounds, saveBounds } from '../utils/setup' |
|
|
| |
| |
| |
| |
| let isAppQuitting = false |
|
|
| class WindowManager { |
| public mainWindow?: BrowserWindow |
| private _quickAskWindow: BrowserWindow | undefined = undefined |
| private _quickAskWindowVisible = false |
| private _mainWindowVisible = false |
|
|
| private deeplink: string | undefined |
| |
| |
| |
| |
| async createMainWindow(preloadPath: string, startUrl: string) { |
| const bounds = await getBounds() |
|
|
| this.mainWindow = new BrowserWindow({ |
| ...mainWindowConfig, |
| width: bounds.width, |
| height: bounds.height, |
| x: bounds.x, |
| y: bounds.y, |
| webPreferences: { |
| nodeIntegration: true, |
| preload: preloadPath, |
| webSecurity: false, |
| }, |
| }) |
|
|
| if (process.platform === 'win32' || process.platform === 'linux') { |
| |
| |
| |
| const commandLine = process.argv.slice(1) |
| if (commandLine.length > 0) { |
| const url = commandLine[0] |
| this.sendMainAppDeepLink(url) |
| } |
| } |
|
|
| this.mainWindow.on('resized', () => { |
| saveBounds(this.mainWindow?.getBounds()) |
| }) |
|
|
| this.mainWindow.on('moved', () => { |
| saveBounds(this.mainWindow?.getBounds()) |
| }) |
|
|
| |
| this.mainWindow.loadURL(startUrl) |
|
|
| |
| this.mainWindow.webContents.setWindowOpenHandler(({ url }) => { |
| shell.openExternal(url) |
| return { action: 'deny' } |
| }) |
|
|
| app.on('before-quit', function () { |
| isAppQuitting = true |
| }) |
|
|
| windowManager.mainWindow?.on('close', function (evt) { |
| |
| if (!getAppConfigurations().quick_ask) return |
|
|
| if (!isAppQuitting) { |
| evt.preventDefault() |
| windowManager.hideMainWindow() |
| } |
| }) |
| } |
|
|
| createQuickAskWindow(preloadPath: string, startUrl: string): void { |
| this._quickAskWindow = new BrowserWindow({ |
| ...quickAskWindowConfig, |
| webPreferences: { |
| nodeIntegration: true, |
| preload: preloadPath, |
| webSecurity: false, |
| }, |
| }) |
|
|
| this._quickAskWindow.loadURL(startUrl) |
| this._quickAskWindow.on('blur', () => { |
| this.hideQuickAskWindow() |
| }) |
| } |
|
|
| isMainWindowVisible(): boolean { |
| return this._mainWindowVisible |
| } |
|
|
| hideMainWindow(): void { |
| this.mainWindow?.hide() |
| this._mainWindowVisible = false |
| } |
|
|
| showMainWindow(): void { |
| this.mainWindow?.show() |
| this._mainWindowVisible = true |
| } |
|
|
| hideQuickAskWindow(): void { |
| this._quickAskWindow?.hide() |
| this._quickAskWindowVisible = false |
| } |
|
|
| showQuickAskWindow(): void { |
| this._quickAskWindow?.show() |
| this._quickAskWindowVisible = true |
| } |
|
|
| closeQuickAskWindow(): void { |
| if (this._quickAskWindow?.isDestroyed()) return |
| this._quickAskWindow?.close() |
| this._quickAskWindow?.destroy() |
| this._quickAskWindow = undefined |
| this._quickAskWindowVisible = false |
| } |
|
|
| isQuickAskWindowVisible(): boolean { |
| return this._quickAskWindowVisible |
| } |
|
|
| isQuickAskWindowDestroyed(): boolean { |
| return this._quickAskWindow?.isDestroyed() ?? true |
| } |
|
|
| expandQuickAskWindow(heightOffset: number): void { |
| const width = quickAskWindowConfig.width! |
| const height = quickAskWindowConfig.height! + heightOffset |
| this._quickAskWindow?.setMinimumSize(width, height) |
| this._quickAskWindow?.setSize(width, height, true) |
| } |
|
|
| sendQuickAskSelectedText(selectedText: string): void { |
| this._quickAskWindow?.webContents.send( |
| AppEvent.onSelectedText, |
| selectedText |
| ) |
| } |
|
|
| |
| |
| |
| sendMainAppDeepLink(url: string): void { |
| this.deeplink = url |
| const interval = setInterval(() => { |
| if (!this.deeplink) clearInterval(interval) |
| const mainWindow = this.mainWindow |
| if (mainWindow) { |
| mainWindow.webContents.send(AppEvent.onDeepLink, this.deeplink) |
| if (mainWindow.isMinimized()) mainWindow.restore() |
| mainWindow.focus() |
| } |
| }, 500) |
| } |
|
|
| cleanUp(): void { |
| if (!this.mainWindow?.isDestroyed()) { |
| this.mainWindow?.close() |
| this.mainWindow?.destroy() |
| this.mainWindow = undefined |
| this._mainWindowVisible = false |
| } |
| if (!this._quickAskWindow?.isDestroyed()) { |
| this._quickAskWindow?.close() |
| this._quickAskWindow?.destroy() |
| this._quickAskWindow = undefined |
| this._quickAskWindowVisible = false |
| } |
| } |
|
|
| |
| |
| |
| ackDeepLink() { |
| this.deeplink = undefined |
| } |
| } |
|
|
| export const windowManager = new WindowManager() |
|
|