summaryrefslogtreecommitdiffhomepage
path: root/app/window-controller.js
blob: 849da007cf53d75ac65a7b921eefca63e28c38dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// @flow

import electron, { screen } from 'electron';
import type { BrowserWindow, Tray, Display } from 'electron';

export default class WindowController {
  _window: BrowserWindow;
  _tray: Tray;
  _isWindowReady = false;

  get window(): BrowserWindow {
    return this._window;
  }

  constructor(window: BrowserWindow, tray: Tray) {
    this._window = window;
    this._tray = tray;

    this._installDisplayMetricsHandler();
    this._installWindowReadyHandlers();
  }

  show(whenReady: boolean = true) {
    if (whenReady) {
      this._executeWhenWindowIsReady(() => this._showImmediately());
    } else {
      this._showImmediately();
    }
  }

  hide() {
    this._window.hide();
  }

  toggle() {
    if (this._window.isVisible()) {
      this.hide();
    } else {
      this.show();
    }
  }

  _showImmediately() {
    const window = this._window;

    this._updatePosition();

    window.show();
    window.focus();
  }

  _updatePosition() {
    const { x, y } = this._getWindowPosition();
    this._window.setPosition(x, y, false);
  }

  _getTrayPlacement() {
    switch (process.platform) {
      case 'darwin':
        // macOS has menubar always placed at the top
        return 'top';

      case 'win32': {
        // taskbar occupies some part of the screen excluded from work area
        const primaryDisplay = electron.screen.getPrimaryDisplay();
        const displaySize = primaryDisplay.size;
        const workArea = primaryDisplay.workArea;

        if (workArea.width < displaySize.width) {
          return workArea.x > 0 ? 'left' : 'right';
        } else if (workArea.height < displaySize.height) {
          return workArea.y > 0 ? 'top' : 'bottom';
        } else {
          return 'none';
        }
      }

      default:
        return 'none';
    }
  }

  _getWindowPosition(): { x: number, y: number } {
    const windowBounds = this._window.getBounds();
    const trayBounds = this._tray.getBounds();

    const primaryDisplay = electron.screen.getPrimaryDisplay();
    const workArea = primaryDisplay.workArea;
    const placement = this._getTrayPlacement();
    const maxX = workArea.x + workArea.width - windowBounds.width;
    const maxY = workArea.y + workArea.height - windowBounds.height;

    let x = 0,
      y = 0;
    switch (placement) {
      case 'top':
        x = trayBounds.x + (trayBounds.width - windowBounds.width) * 0.5;
        y = workArea.y;
        break;

      case 'bottom':
        x = trayBounds.x + (trayBounds.width - windowBounds.width) * 0.5;
        y = workArea.y + workArea.height - windowBounds.height;
        break;

      case 'left':
        x = workArea.x;
        y = trayBounds.y + (trayBounds.height - windowBounds.height) * 0.5;
        break;

      case 'right':
        x = workArea.width - windowBounds.width;
        y = trayBounds.y + (trayBounds.height - windowBounds.height) * 0.5;
        break;

      case 'none':
        x = workArea.x + (workArea.width - windowBounds.width) * 0.5;
        y = workArea.y + (workArea.height - windowBounds.height) * 0.5;
        break;
    }

    x = Math.min(Math.max(x, workArea.x), maxX);
    y = Math.min(Math.max(y, workArea.y), maxY);

    return {
      x: Math.round(x),
      y: Math.round(y),
    };
  }

  // Installs display event handlers to update the window position on any changes in the display or workarea dimensions.
  _installDisplayMetricsHandler() {
    screen.addListener('display-metrics-changed', this._onDisplayMetricsChanged);
    this._window.once('closed', () => {
      screen.removeListener('display-metrics-changed', this._onDisplayMetricsChanged);
    });
  }

  _onDisplayMetricsChanged = (_event: any, _display: Display, changedMetrics: Array<string>) => {
    if (changedMetrics.includes('workArea') && this._window.isVisible()) {
      this._updatePosition();
    }
  };

  _installWindowReadyHandlers() {
    this._window.once('ready-to-show', () => {
      this._isWindowReady = true;
    });
  }

  _executeWhenWindowIsReady(closure: () => any) {
    if (this._isWindowReady) {
      closure();
    } else {
      this._window.once('ready-to-show', () => {
        closure();
      });
    }
  }
}