worklog-reminder
107 строк · 2.5 Кб
1import { BrowserWindow, screen } from 'electron';2import positioner from 'electron-traywindow-positioner';3import { execSync } from 'child_process';4import { createSettings, KEY_WINDOW_BOUNDS } from '../Settings';5
6class MainWindow extends BrowserWindow {7/**8* @param {WindowManager} windowManager
9* @param {object} options
10*/
11constructor(windowManager, options) {12super(options);13this.windowManager = windowManager;14}15
16show() {17const allBounds = createSettings().get(KEY_WINDOW_BOUNDS);18const display = this.getCurrentDisplay();19
20const storedBounds = allBounds && allBounds[display.id] ? allBounds[display.id] : null;21
22if (storedBounds) {23this.setBounds(storedBounds);24} else {25this.moveToDefaultPosition();26}27
28super.show();29this.focus();30}31
32moveToDefaultPosition() {33positioner.position(this, this.windowManager.getTray().tray.getBounds());34}35
36setHandlers() {37this.on('blur', () => {38if (!this.webContents.isDevToolsOpened()) {39if (!this.isHideAvailable()) {40this.minimize();41} else {42this.hide();43}44}45});46
47this.on('move', (event) => {48const display = this.getCurrentDisplay();49const stored = createSettings().get(KEY_WINDOW_BOUNDS);50
51const combined = { ...(stored || {}), [display.id]: event.sender.getBounds() };52
53createSettings().set(KEY_WINDOW_BOUNDS, combined);54});55}56
57toggle() {58if (this.isVisible() && !this.isFocused()) {59this.show();60this.focus();61} else if (this.isVisible()) {62if (this.isHideAvailable()) {63this.hide();64} else {65this.minimize();66}67} else {68this.show();69}70}71
72isHideAvailable() {73if (process.platform !== 'linux') {74return true;75}76
77const availableDesktops = [78'x-cinnamon',79'unity',80'kde',81'xfce',82];83
84let currentDesktop;85
86try {87// nodejs 14 lts88// https://nodejs.org/docs/latest-v14.x/api/child_process.html89currentDesktop = execSync('echo $XDG_CURRENT_DESKTOP').toString().toLowerCase().trim();90} catch (e) {91// todo: send error to monitoring agent92}93
94return availableDesktops.indexOf(currentDesktop) !== -1;95}96
97/**98* @returns {Display}
99*/
100getCurrentDisplay() {101const trayBound = this.windowManager.getTray().tray.getBounds();102
103return screen.getDisplayNearestPoint({ x: trayBound.x, y: trayBound.y });104}105}
106
107export default MainWindow;108