worklog-reminder

Форк
0
107 строк · 2.5 Кб
1
import { BrowserWindow, screen } from 'electron';
2
import positioner from 'electron-traywindow-positioner';
3
import { execSync } from 'child_process';
4
import { createSettings, KEY_WINDOW_BOUNDS } from '../Settings';
5

6
class MainWindow extends BrowserWindow {
7
  /**
8
   * @param {WindowManager} windowManager
9
   * @param {object} options
10
   */
11
  constructor(windowManager, options) {
12
    super(options);
13
    this.windowManager = windowManager;
14
  }
15

16
  show() {
17
    const allBounds = createSettings().get(KEY_WINDOW_BOUNDS);
18
    const display = this.getCurrentDisplay();
19

20
    const storedBounds = allBounds && allBounds[display.id] ? allBounds[display.id] : null;
21

22
    if (storedBounds) {
23
      this.setBounds(storedBounds);
24
    } else {
25
      this.moveToDefaultPosition();
26
    }
27

28
    super.show();
29
    this.focus();
30
  }
31

32
  moveToDefaultPosition() {
33
    positioner.position(this, this.windowManager.getTray().tray.getBounds());
34
  }
35

36
  setHandlers() {
37
    this.on('blur', () => {
38
      if (!this.webContents.isDevToolsOpened()) {
39
        if (!this.isHideAvailable()) {
40
          this.minimize();
41
        } else {
42
          this.hide();
43
        }
44
      }
45
    });
46

47
    this.on('move', (event) => {
48
      const display = this.getCurrentDisplay();
49
      const stored = createSettings().get(KEY_WINDOW_BOUNDS);
50

51
      const combined = { ...(stored || {}), [display.id]: event.sender.getBounds() };
52

53
      createSettings().set(KEY_WINDOW_BOUNDS, combined);
54
    });
55
  }
56

57
  toggle() {
58
    if (this.isVisible() && !this.isFocused()) {
59
      this.show();
60
      this.focus();
61
    } else if (this.isVisible()) {
62
      if (this.isHideAvailable()) {
63
        this.hide();
64
      } else {
65
        this.minimize();
66
      }
67
    } else {
68
      this.show();
69
    }
70
  }
71

72
  isHideAvailable() {
73
    if (process.platform !== 'linux') {
74
      return true;
75
    }
76

77
    const availableDesktops = [
78
      'x-cinnamon',
79
      'unity',
80
      'kde',
81
      'xfce',
82
    ];
83

84
    let currentDesktop;
85

86
    try {
87
      // nodejs 14 lts
88
      // https://nodejs.org/docs/latest-v14.x/api/child_process.html
89
      currentDesktop = execSync('echo $XDG_CURRENT_DESKTOP').toString().toLowerCase().trim();
90
    } catch (e) {
91
      // todo: send error to monitoring agent
92
    }
93

94
    return availableDesktops.indexOf(currentDesktop) !== -1;
95
  }
96

97
  /**
98
   * @returns {Display}
99
   */
100
  getCurrentDisplay() {
101
    const trayBound = this.windowManager.getTray().tray.getBounds();
102

103
    return screen.getDisplayNearestPoint({ x: trayBound.x, y: trayBound.y });
104
  }
105
}
106

107
export default MainWindow;
108

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.