|
| 1 | +import { CommandBarWindow } from '../windows' |
| 2 | +import { IPC_EVENTS } from '@shared/constants' |
| 3 | +import { Log } from '@shared/utils/logger' |
| 4 | +import { screen } from 'electron' |
| 5 | + |
| 6 | +export class CommandBarController { |
| 7 | + static instance: CommandBarController |
| 8 | + window: CommandBarWindow |
| 9 | + private isVisible: boolean = false |
| 10 | + |
| 11 | + constructor() { |
| 12 | + CommandBarController.instance = this |
| 13 | + this.window = new CommandBarWindow() |
| 14 | + this.setupBlurListener() |
| 15 | + } |
| 16 | + |
| 17 | + private setupBlurListener() { |
| 18 | + this.window.addOnBuildListener(() => { |
| 19 | + const window = this.window.getWindow() |
| 20 | + if (window) { |
| 21 | + window.on('blur', () => { |
| 22 | + this.hide() |
| 23 | + }) |
| 24 | + } |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + show() { |
| 29 | + try { |
| 30 | + const window = this.window.getWindow() |
| 31 | + if (window && !this.isVisible) { |
| 32 | + const cursorPoint = screen.getCursorScreenPoint() |
| 33 | + const currentDisplay = screen.getDisplayNearestPoint(cursorPoint) |
| 34 | + const { x, y, width, height } = currentDisplay.workArea |
| 35 | + const windowBounds = window.getBounds() |
| 36 | + |
| 37 | + const centerX = x + Math.round((width - windowBounds.width) / 2) |
| 38 | + const centerY = y + Math.round(height * 0.3) |
| 39 | + |
| 40 | + window.setBounds({ x: centerX, y: centerY }) |
| 41 | + window.show() |
| 42 | + window.setAlwaysOnTop(true, 'screen-saver') |
| 43 | + window.focus() |
| 44 | + this.isVisible = true |
| 45 | + this.window.emit(IPC_EVENTS.SHOW_COMMAND_BAR) |
| 46 | + } |
| 47 | + } catch (e) { |
| 48 | + Log.warning('error during showing CommandBarWindow:', e) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + hide() { |
| 53 | + try { |
| 54 | + const window = this.window.getWindow() |
| 55 | + if (window && this.isVisible) { |
| 56 | + window.hide() |
| 57 | + this.isVisible = false |
| 58 | + this.window.emit(IPC_EVENTS.HIDE_COMMAND_BAR) |
| 59 | + } |
| 60 | + } catch (e) { |
| 61 | + Log.warning('error during hiding CommandBarWindow:', e) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + toggle() { |
| 66 | + if (this.isVisible) { |
| 67 | + this.hide() |
| 68 | + } else { |
| 69 | + this.show() |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + isOpen(): boolean { |
| 74 | + return this.isVisible |
| 75 | + } |
| 76 | + |
| 77 | + async safeQuit() { |
| 78 | + await this.window.quit(true) |
| 79 | + } |
| 80 | +} |
0 commit comments