summaryrefslogtreecommitdiffhomepage
path: root/desktop/packages/mullvad-vpn/src/main/user-interface.ts
blob: 96af5ca7e4d7ae46c7d0d5c4d8e8f41a1ed30dfa (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
import { exec, spawn } from 'child_process';
import { app, BrowserWindow, dialog, Menu, nativeImage, screen, Tray } from 'electron';
import path from 'path';
import { sprintf } from 'sprintf-js';
import { promisify } from 'util';

import { connectEnabled, disconnectEnabled, reconnectEnabled } from '../shared/connect-helper';
import { IAccountData, ILocation, TunnelState } from '../shared/daemon-rpc-types';
import { messages, relayLocations } from '../shared/gettext';
import log from '../shared/logging';
import { Scheduler } from '../shared/scheduler';
import { CommandLineOptions } from './command-line-options';
import { DaemonRpc } from './daemon-rpc';
import {
  changeIpcWebContents,
  IpcMainEventChannel,
  unsetIpcWebContents,
} from './ipc-event-channel';
import { WebContentsConsoleInput } from './logging';
import { isMacOs11OrNewer } from './platform-version';
import { resolveBin } from './proc';
import TrayIconController, { TrayIconType } from './tray-icon-controller';
import WindowController, { WindowControllerDelegate } from './window-controller';

const execAsync = promisify(exec);

export interface UserInterfaceDelegate {
  dismissActiveNotifications(): void;
  updateAccountData(): void;
  connectTunnel(): void;
  reconnectTunnel(): void;
  disconnectTunnel(): void;
  disconnectAndQuit(): void;
  isUnpinnedWindow(): boolean;
  isLoggedIn(): boolean;
  getAccountData(): IAccountData | undefined;
  getTunnelState(): TunnelState;
  getVersionInfo(): Promise<void>;
}

export default class UserInterface implements WindowControllerDelegate {
  private windowController: WindowController;

  private tray: Tray;
  private trayIconController?: TrayIconController;

  // True while file pickers are displayed which is used to decide if the Browser window should be
  // hidden when losing focus.
  private browsingFiles = false;

  private blurNavigationResetScheduler = new Scheduler();
  private backgroundThrottleScheduler = new Scheduler();

  public constructor(
    private delegate: UserInterfaceDelegate,
    private daemonRpc: DaemonRpc,
    private sandboxDisabled: boolean,
    private navigationResetDisabled: boolean,
  ) {
    const window = this.createWindow();

    this.windowController = this.createWindowController(window);
    this.tray = this.createTray();
  }

  public registerIpcListeners() {
    IpcMainEventChannel.daemon.handleTryStart(() => {
      IpcMainEventChannel.daemon.notifyTryStartEvent?.('start-requested');

      try {
        const SETUP_PATH = `"\\"${resolveBin('mullvad-setup')}\\""`;
        const SYSTEM_ROOT_PATH = process.env.SYSTEMROOT || process.env.windir || 'C:\\Windows';
        const PWSH_PATH = `${SYSTEM_ROOT_PATH}\\System32\\WindowsPowershell\\v1.0\\powershell.exe`;

        const child = spawn(
          PWSH_PATH,
          [
            '-Command',
            'Start-Process',
            SETUP_PATH,
            'start-service',
            '-Verb',
            'RunAs',
            '-WindowStyle',
            'Hidden',
            '-Wait',
          ],
          {
            detached: false,
            stdio: 'ignore',
            windowsVerbatimArguments: true,
          },
        );
        child.once('error', (error) => {
          log.error(`"mullvad-setup.exe start-service" failed: ${error.message}`);
          IpcMainEventChannel.daemon.notifyTryStartEvent?.('stopped');
        });

        child.once('exit', (code) => {
          if (code !== 0) {
            log.error(
              `"mullvad-setup.exe start-service" exited unexpectedly with exit code: ${code}`,
            );
            IpcMainEventChannel.daemon.notifyTryStartEvent?.('stopped');
          } else {
            log.info('"mullvad-setup.exe start-service" succeeded');
            // 'running' is set from onDaemonConnected event handler
          }
        });
      } catch (e) {
        const error = e as Error;
        log.error(`Failed to run "mullvad-setup.exe start-service". Error: ${error.message}`);
        IpcMainEventChannel.daemon.notifyTryStartEvent?.('stopped');
      }
    });

    IpcMainEventChannel.app.handleShowOpenDialog(async (options) => {
      this.browsingFiles = true;
      const response = await dialog.showOpenDialog({
        defaultPath: app.getPath('home'),
        ...options,
      });
      this.browsingFiles = false;
      this.showWindow();
      return response;
    });

    IpcMainEventChannel.app.handleShowLaunchDaemonSettings(async () => {
      try {
        await execAsync('open x-apple.systempreferences:com.apple.LoginItems-Settings.extension');
      } catch (error) {
        log.error(`Failed to open launch daemon settings: ${error}`);
      }
    });

    IpcMainEventChannel.app.handleShowFullDiskAccessSettings(async () => {
      try {
        await execAsync(
          'open "x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles"',
        );
      } catch (error) {
        log.error(`Failed to open Full Disk Access settings: ${error}`);
      }
    });
  }

  public createTrayIconController(tunnelState: TunnelState, monochromaticIcon: boolean) {
    const iconType = this.trayIconType(tunnelState);
    this.trayIconController = new TrayIconController(this.tray, iconType, monochromaticIcon, false);
  }

  public async initializeWindow(isLoggedIn: boolean, tunnelState: TunnelState) {
    if (!this.windowController.window) {
      throw new Error('No window available in initializeWindow');
    }

    const window = this.windowController.window;

    // Make sure the IPC wrapper always has the latest webcontents if any
    window.webContents.on('destroyed', unsetIpcWebContents);
    changeIpcWebContents(window.webContents);

    this.registerWindowListener();
    this.addContextMenu();

    if (process.env.NODE_ENV === 'development') {
      await this.installDevTools();

      if (!CommandLineOptions.disableDevtoolsOpen.match) {
        // The devtools doesn't open on Windows if openDevTools is called without a delay here.
        window.once('ready-to-show', () => window.webContents.openDevTools({ mode: 'detach' }));
      }

      if (CommandLineOptions.forwardRendererLog.match) {
        log.addInput(new WebContentsConsoleInput(window.webContents));
      }
    }

    switch (process.platform) {
      case 'win32':
        this.installWindowsMenubarAppWindowHandlers();
        break;
      case 'darwin':
        this.installMacOsMenubarAppWindowHandlers();
        this.setMacOsAppMenu();
        break;
      case 'linux':
        this.setTrayContextMenu(isLoggedIn, tunnelState);
        this.setLinuxAppMenu();
        window.setMenuBarVisibility(false);
        break;
    }

    this.installWindowCloseHandler();
    this.installTrayClickHandlers();

    try {
      if (process.env.NODE_ENV === 'development' && process.env.VITE_DEV_SERVER_URL) {
        await window.loadURL(process.env.VITE_DEV_SERVER_URL);
      } else {
        await window.loadFile(path.join(__dirname, 'index.html'));
      }
    } catch (e) {
      const error = e as Error;
      log.error(`Failed to load index file: ${error.message}`);
    }

    // disable pinch to zoom
    if (this.windowController.webContents) {
      void this.windowController.webContents.setVisualZoomLevelLimits(1, 1);
    }
  }

  public updateTray = (isLoggedIn: boolean, tunnelState: TunnelState) => {
    this.updateTrayIcon(tunnelState);
    this.setTrayContextMenu(isLoggedIn, tunnelState);
    this.setTrayTooltip(tunnelState);
  };

  public async recreateWindow(isLoggedIn: boolean, tunnelState: TunnelState): Promise<void> {
    if (this.tray) {
      this.tray.removeAllListeners();
      // Prevent the IPC webcontents reference to be reset when replacing window. Resetting wouldn't
      // work since the old webContents is destroyed after the IPC wrapper has been updated with the
      // new one.
      this.windowController.webContents?.removeListener('destroyed', unsetIpcWebContents);
      // Remove window close handler that calls `preventDefault` when closed.
      this.windowController.window?.removeListener('close', this.windowCloseHandler);

      const window = this.createWindow();
      changeIpcWebContents(window.webContents);

      this.windowController.close();
      this.windowController = new WindowController(this, window);

      await this.initializeWindow(isLoggedIn, tunnelState);
      this.windowController.show();
    }
  }

  public reloadWindow = () => this.windowController.window?.reload();
  public isWindowVisible = () => this.windowController.isVisible();
  public showWindow = () => this.windowController.show();
  public updateTrayTheme = () => this.trayIconController?.updateTheme() ?? Promise.resolve();
  public setMonochromaticIcon = (value: boolean) =>
    this.trayIconController?.setMonochromaticIcon(value);
  public showNotificationIcon = (value: boolean, reason?: string) =>
    this.trayIconController?.showNotificationIcon(value, reason);
  public setWindowIcon = (icon: string) => this.windowController.window?.setIcon(icon);

  public updateTrayIcon(tunnelState: TunnelState) {
    const type = this.trayIconType(tunnelState);
    this.trayIconController?.animateToIcon(type);
  }

  public dispose = () => {
    this.tray.removeAllListeners();
    this.windowController.window?.removeAllListeners();

    // The window is not closable on macOS to be able to hide the titlebar and workaround
    // a shadow bug rendered above the invisible title bar. This also prevents the window from
    // closing normally, even programmatically. Therefore re-enable the close button just before
    // quitting the app.
    // Github issue: https://github.com/electron/electron/issues/15008
    if (process.platform === 'darwin' && this.windowController.window) {
      this.windowController.window.closable = true;
    }

    this.windowController.close();
    this.trayIconController?.dispose();
  };

  private createTray(): Tray {
    const tray = new Tray(nativeImage.createEmpty());
    tray.setToolTip('Mullvad VPN');

    // disable double click on tray icon since it causes weird delay
    tray.setIgnoreDoubleClickEvents(true);

    return tray;
  }

  private createWindow(): BrowserWindow {
    const unpinnedWindow = this.delegate.isUnpinnedWindow();
    const { width, height } = WindowController.getContentSize(unpinnedWindow);

    const options: Electron.BrowserWindowConstructorOptions = {
      useContentSize: true,
      width,
      height,
      resizable: false,
      maximizable: false,
      fullscreenable: false,
      show: false,
      frame: unpinnedWindow,
      webPreferences: {
        preload: path.join(__dirname, 'preload.js'),
        nodeIntegration: false,
        nodeIntegrationInWorker: false,
        nodeIntegrationInSubFrames: false,
        sandbox: !this.sandboxDisabled,
        contextIsolation: true,
        spellcheck: false,
        devTools: process.env.NODE_ENV === 'development',
      },
    };

    switch (process.platform) {
      case 'darwin': {
        // setup window flags to mimic popover on macOS
        const appWindow = new BrowserWindow({
          ...options,
          titleBarStyle: unpinnedWindow ? 'default' : 'customButtonsOnHover',
          minimizable: unpinnedWindow,
          closable: unpinnedWindow,
          transparent: !unpinnedWindow,
          hiddenInMissionControl: !unpinnedWindow,
        });

        // make the window visible on all workspaces and prevent the icon from showing in the dock
        // and app switcher.
        if (unpinnedWindow) {
          void app.dock?.show();
        } else {
          appWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
          app.dock?.hide();
        }

        return appWindow;
      }

      case 'win32': {
        // setup window flags to mimic an overlay window
        const appWindow = new BrowserWindow({
          ...options,
          // Due to a bug in Electron the app is sometimes placed behind other apps when opened.
          // Setting alwaysOnTop to true ensures that the app is placed on top. Electron issue:
          // https://github.com/electron/electron/issues/25915
          alwaysOnTop: !unpinnedWindow,
          skipTaskbar: !unpinnedWindow,
          // Workaround for sub-pixel anti-aliasing
          // https://github.com/electron/electron/blob/main/docs/faq.md#the-font-looks-blurry-what-is-this-and-what-can-i-do
          backgroundColor: '#fff',
        });
        const WM_DEVICECHANGE = 0x0219;
        const DBT_DEVICEARRIVAL = 0x8000n;
        const DBT_DEVICEREMOVECOMPLETE = 0x8004n;
        appWindow.hookWindowMessage(WM_DEVICECHANGE, (wParam) => {
          const wParamL = wParam.readBigInt64LE(0);
          if (wParamL != DBT_DEVICEARRIVAL && wParamL != DBT_DEVICEREMOVECOMPLETE) {
            return;
          }
          this.daemonRpc
            .checkVolumes()
            .catch((error) =>
              log.error(`Unable to notify daemon of device event: ${error.message}`),
            );
        });

        appWindow.removeMenu();

        return appWindow;
      }

      default:
        return new BrowserWindow(options);
    }
  }

  private createWindowController(window: BrowserWindow) {
    return new WindowController(this, window);
  }

  private registerWindowListener() {
    this.windowController.window?.on('focus', () => {
      IpcMainEventChannel.window.notifyFocus?.(true);

      this.blurNavigationResetScheduler.cancel();

      // cancel notifications when window appears
      this.delegate.dismissActiveNotifications();

      this.delegate.updateAccountData();

      void this.delegate.getVersionInfo();
    });

    this.windowController.window?.on('blur', () => {
      IpcMainEventChannel.window.notifyFocus?.(false);
    });

    // Use hide instead of blur to prevent the navigation reset from happening when bluring an
    // unpinned window.
    this.windowController.window?.on('hide', () => {
      if (process.env.NODE_ENV !== 'development' || !this.navigationResetDisabled) {
        this.blurNavigationResetScheduler.schedule(() => {
          this.windowController.webContents?.setBackgroundThrottling(false);
          IpcMainEventChannel.navigation.notifyReset?.();

          this.backgroundThrottleScheduler.schedule(() => {
            this.windowController.webContents?.setBackgroundThrottling(true);
          }, 1_000);
        }, 120_000);
      }
    });
  }

  private setTrayContextMenu(isLoggedIn: boolean, tunnelState: TunnelState) {
    if (process.platform === 'linux') {
      this.tray.setContextMenu(
        this.createContextMenu(this.daemonRpc.isConnected, isLoggedIn, tunnelState),
      );
    }
  }

  private setTrayTooltip(tunnelState: TunnelState) {
    const tooltip = this.createTooltipText(this.daemonRpc.isConnected, tunnelState);
    this.tray?.setToolTip(tooltip);
  }

  private addContextMenu() {
    const menuTemplate: Electron.MenuItemConstructorOptions[] = [
      { role: 'cut' },
      { role: 'copy' },
      { role: 'paste' },
      { type: 'separator' },
      { role: 'selectAll' },
    ];

    // add inspect element on right click menu
    this.windowController.window?.webContents.on(
      'context-menu',
      (_e: Electron.Event, props: { x: number; y: number; isEditable: boolean }) => {
        const inspectTemplate = [
          {
            label: 'Inspect element',
            click: () => {
              this.windowController.window?.webContents.openDevTools({ mode: 'detach' });
              this.windowController.window?.webContents.inspectElement(props.x, props.y);
            },
          },
        ];

        if (props.isEditable) {
          // mixin 'inspect element' into standard menu when in development mode
          if (process.env.NODE_ENV === 'development') {
            const inputMenu: Electron.MenuItemConstructorOptions[] = [
              { type: 'separator' },
              ...inspectTemplate,
            ];

            Menu.buildFromTemplate(inputMenu).popup({ window: this.windowController.window });
          } else {
            Menu.buildFromTemplate(menuTemplate).popup({ window: this.windowController.window });
          }
        } else if (process.env.NODE_ENV === 'development') {
          // display inspect element for all non-editable
          // elements when in development mode
          Menu.buildFromTemplate(inspectTemplate).popup({ window: this.windowController.window });
        }
      },
    );
  }

  private async installDevTools() {
    const {
      default: installer,
      REACT_DEVELOPER_TOOLS,
      REDUX_DEVTOOLS,
    } = await import('electron-devtools-installer');
    const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
    const options = { forceDownload, loadExtensionOptions: { allowFileAccess: true } };
    try {
      await installer(REACT_DEVELOPER_TOOLS, options);
      await installer(REDUX_DEVTOOLS, options);
    } catch (e) {
      const error = e as Error;
      log.info(`Error installing extension: ${error.message}`);
    }
  }

  // On macOS, hotkeys are bound to the app menu and won't work if it's not set,
  // even though the app menu itself is not visible because the app does not appear in the dock.
  private setMacOsAppMenu() {
    const mullvadVpnSubmenu: Electron.MenuItemConstructorOptions[] = [];
    if (process.env.NODE_ENV === 'development') {
      mullvadVpnSubmenu.unshift({ role: 'quit' }, { role: 'reload' }, { role: 'forceReload' });
    }

    const template: Electron.MenuItemConstructorOptions[] = [
      {
        label: 'Mullvad VPN',
        submenu: mullvadVpnSubmenu,
      },
      {
        label: 'Edit',
        submenu: [
          { role: 'cut' },
          { role: 'copy' },
          { role: 'paste' },
          { type: 'separator' },
          { role: 'selectAll' },
        ],
      },
    ];
    Menu.setApplicationMenu(Menu.buildFromTemplate(template));
  }

  private setLinuxAppMenu() {
    const template: Electron.MenuItemConstructorOptions[] = [
      {
        label: 'Mullvad VPN',
        submenu: [{ role: 'quit' }],
      },
    ];
    Menu.setApplicationMenu(Menu.buildFromTemplate(template));
  }

  private installWindowsMenubarAppWindowHandlers() {
    if (this.delegate.isUnpinnedWindow()) {
      return;
    }

    this.windowController.window?.on('blur', () => {
      // Detect if blur happened when user had a cursor above the tray icon.
      const trayBounds = this.tray.getBounds();
      const cursorPos = screen.getCursorScreenPoint();
      const isCursorInside =
        cursorPos.x >= trayBounds.x &&
        cursorPos.y >= trayBounds.y &&
        cursorPos.x <= trayBounds.x + trayBounds.width &&
        cursorPos.y <= trayBounds.y + trayBounds.height;
      if (!isCursorInside && !this.browsingFiles) {
        this.windowController.hide();
      }
    });
  }

  // setup NSEvent forwarder to fix inconsistent window.blur on macOS
  // see https://github.com/electron/electron/issues/8689
  private installMacOsMenubarAppWindowHandlers() {
    if (this.delegate.isUnpinnedWindow()) {
      return;
    }

    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const nseventforwarder = require('nseventforwarder');
    let nseventforwarderStop: ReturnType<typeof nseventforwarder.start>;

    this.windowController.window?.on(
      'show',
      () => (nseventforwarderStop = nseventforwarder.start(() => this.windowController.hide())),
    );
    this.windowController.window?.on('hide', () => nseventforwarderStop?.());
    this.windowController.window?.on('blur', () => {
      // Make sure to hide the menubar window when other program captures the focus.
      // But avoid doing that when dev tools capture the focus to make it possible to inspect the UI
      if (
        this.windowController.window?.isVisible() &&
        !this.windowController.window?.webContents.isDevToolsFocused()
      ) {
        this.windowController.hide();
      }
    });
  }

  private installWindowCloseHandler() {
    if (!this.delegate.isUnpinnedWindow()) {
      return;
    }

    this.windowController.window?.on('close', this.windowCloseHandler);
  }

  private windowCloseHandler = (closeEvent: Electron.Event) => {
    closeEvent.preventDefault();
    this.windowController.hide();
  };

  private installTrayClickHandlers() {
    switch (process.platform) {
      case 'win32':
        if (this.delegate.isUnpinnedWindow()) {
          // This needs to be executed on click since if it is added to the tray icon it will be
          // displayed on left click as well.
          this.tray?.on('right-click', () =>
            this.popUpContextMenu(this.delegate.isLoggedIn(), this.delegate.getTunnelState()),
          );
          this.tray?.on('click', () => this.windowController.show());
        } else {
          this.tray?.on('right-click', () => this.windowController.hide());
          this.tray?.on('click', () => this.windowController.toggle());
        }
        break;
      case 'darwin':
        this.tray?.on('right-click', () => this.windowController.hide());
        this.tray?.on('click', (event) => {
          if (event.metaKey) {
            setImmediate(() => this.windowController.updatePosition());
          } else {
            if (isMacOs11OrNewer() && !this.windowController.isVisible()) {
              // This is a workaround for this Electron issue, when it's resolved
              // `this.windowController.toggle()` should do the trick on all platforms:
              // https://github.com/electron/electron/issues/28776
              const contextMenu = Menu.buildFromTemplate([]);
              contextMenu.on('menu-will-show', () => this.windowController.show());
              this.tray?.popUpContextMenu(contextMenu);
            } else {
              this.windowController.toggle();
            }
          }
        });
        break;
      case 'linux':
        this.tray?.on('click', () => this.windowController.show());
        break;
    }
  }

  private popUpContextMenu(isLoggedIn: boolean, tunnelState: TunnelState) {
    this.tray.popUpContextMenu(
      this.createContextMenu(this.daemonRpc.isConnected, isLoggedIn, tunnelState),
    );
  }

  private createTooltipText(connectedToDaemon: boolean, tunnelState: TunnelState): string {
    if (!connectedToDaemon) {
      return messages.pgettext('tray-icon-context-menu', 'Disconnected from system service');
    }

    switch (tunnelState.state) {
      case 'disconnected':
        return messages.gettext('Disconnected');
      case 'disconnecting':
        return messages.gettext('Disconnecting');
      case 'connecting': {
        const location = this.createLocationString(tunnelState.details?.location);
        return location
          ? sprintf(messages.pgettext('tray-icon-tooltip', 'Connecting. %(location)s'), {
              location,
            })
          : messages.gettext('Connecting');
      }
      case 'connected': {
        const location = this.createLocationString(tunnelState.details.location);
        return location
          ? sprintf(messages.pgettext('tray-icon-tooltip', 'Connected. %(location)s'), {
              location,
            })
          : messages.gettext('Connected');
      }
    }

    return 'Mullvad VPN';
  }

  private createLocationString(location?: ILocation): string | undefined {
    if (location === undefined) {
      return undefined;
    }

    const country = relayLocations.gettext(location.country);
    return location.city
      ? sprintf(messages.pgettext('tray-icon-tooltip', '%(city)s, %(country)s'), {
          city: relayLocations.gettext(location.city),
          country,
        })
      : country;
  }

  private createContextMenu(
    connectedToDaemon: boolean,
    loggedIn: boolean,
    tunnelState: TunnelState,
  ) {
    const template: Electron.MenuItemConstructorOptions[] = [
      {
        label: sprintf(messages.pgettext('tray-icon-context-menu', 'Open %(mullvadVpn)s'), {
          mullvadVpn: 'Mullvad VPN',
        }),
        click: () => this.windowController.show(),
      },
      { type: 'separator' },
      {
        id: 'connect',
        label: messages.gettext('Connect'),
        enabled: connectEnabled(connectedToDaemon, loggedIn, tunnelState.state),
        click: this.delegate.connectTunnel,
      },
      {
        id: 'reconnect',
        label: messages.gettext('Reconnect'),
        enabled: reconnectEnabled(connectedToDaemon, loggedIn, tunnelState.state),
        click: this.delegate.reconnectTunnel,
      },
      {
        id: 'disconnect',
        label: messages.gettext('Disconnect'),
        enabled: disconnectEnabled(connectedToDaemon, tunnelState.state),
        click: this.delegate.disconnectTunnel,
      },
      { type: 'separator' },
      {
        id: 'disconnect',
        label:
          tunnelState.state === 'disconnected'
            ? messages.gettext('Quit')
            : this.escapeContextMenuLabel(messages.gettext('Disconnect & quit')),
        click: this.delegate.disconnectAndQuit,
      },
    ];

    return Menu.buildFromTemplate(template);
  }

  private escapeContextMenuLabel(label: string): string {
    return label.replace('&', '&&');
  }

  private trayIconType(tunnelState: TunnelState): TrayIconType {
    switch (tunnelState.state) {
      case 'connected':
        return 'secured';

      case 'connecting':
        return 'securing';

      case 'error':
        if (!tunnelState.details.blockingError) {
          return 'securing';
        } else {
          return 'unsecured';
        }
      case 'disconnecting':
        return 'securing';

      case 'disconnected':
        if (tunnelState.lockedDown) {
          return 'securing';
        } else {
          return 'unsecured';
        }
    }
  }

  /* eslint-disable @typescript-eslint/member-ordering */
  // WindowControllerDelegate
  public getTrayBounds = () => this.tray.getBounds();
  public isUnpinnedWindow = () => this.delegate.isUnpinnedWindow();
  /* eslint-enable @typescript-eslint/member-ordering */
}