diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-12-18 14:08:42 -0200 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-12-18 14:23:41 -0200 |
| commit | 518cf6429ea57ee7b2b1748c6ee1b8f16d96dc4e (patch) | |
| tree | dfa1a1e69d32bf3a000cf7d65e0f7fe32360f662 | |
| parent | 717af9f442ac1a1bc419e3ad4d8d71a0764ba610 (diff) | |
| download | mullvadvpn-518cf6429ea57ee7b2b1748c6ee1b8f16d96dc4e.tar.xz mullvadvpn-518cf6429ea57ee7b2b1748c6ee1b8f16d96dc4e.zip | |
Allow changing GUI settings through IPC
| -rw-r--r-- | gui/packages/desktop/src/main/gui-settings.js | 20 | ||||
| -rw-r--r-- | gui/packages/desktop/src/main/index.js | 3 | ||||
| -rw-r--r-- | gui/packages/desktop/src/shared/ipc-event-channel.js | 26 |
3 files changed, 49 insertions, 0 deletions
diff --git a/gui/packages/desktop/src/main/gui-settings.js b/gui/packages/desktop/src/main/gui-settings.js index 633b172244..2fe3b0178e 100644 --- a/gui/packages/desktop/src/main/gui-settings.js +++ b/gui/packages/desktop/src/main/gui-settings.js @@ -5,6 +5,7 @@ import log from 'electron-log'; import path from 'path'; import { app } from 'electron'; +import IpcEventChannel from '../shared/ipc-event-channel'; import type { GuiSettingsState } from '../shared/gui-settings-state'; export default class GuiSettings { @@ -12,6 +13,8 @@ export default class GuiSettings { startMinimized: false, }; + _notify: ?(GuiSettingsState) => void; + load() { try { const settingsFile = this._filePath(); @@ -42,7 +45,24 @@ export default class GuiSettings { return this._state.startMinimized; } + registerIpcHandlers(ipcEventChannel: IpcEventChannel) { + this._notify = ipcEventChannel.guiSettings.notify; + + ipcEventChannel.guiSettings.handleStartMinimized((startMinimized: boolean) => { + this._state.startMinimized = startMinimized; + this._settingsChanged(); + }); + } + _filePath() { return path.join(app.getPath('userData'), 'gui_settings.json'); } + + _settingsChanged() { + this.store(); + + if (this._notify) { + this._notify(this._state); + } + } } diff --git a/gui/packages/desktop/src/main/index.js b/gui/packages/desktop/src/main/index.js index 1c25a5c382..83d2c3e699 100644 --- a/gui/packages/desktop/src/main/index.js +++ b/gui/packages/desktop/src/main/index.js @@ -287,6 +287,8 @@ const ApplicationMain = { this._trayIconController = trayIconController; this._ipcEventChannel = new IpcEventChannel(window.webContents); + this._guiSettings.registerIpcHandlers(this._ipcEventChannel); + if (process.env.NODE_ENV === 'development') { await this._installDevTools(); window.openDevTools({ mode: 'detach' }); @@ -749,6 +751,7 @@ const ApplicationMain = { relays: this._relays, currentVersion: this._currentVersion, upgradeVersion: this._upgradeVersion, + guiSettings: this._guiSettings.state, }; }); diff --git a/gui/packages/desktop/src/shared/ipc-event-channel.js b/gui/packages/desktop/src/shared/ipc-event-channel.js index 7b55c35ee6..fa5e92783c 100644 --- a/gui/packages/desktop/src/shared/ipc-event-channel.js +++ b/gui/packages/desktop/src/shared/ipc-event-channel.js @@ -27,6 +27,14 @@ interface Receiver<T> { listen<T>(fn: (T) => void): void; } +interface GuiSettingsMethods { + setStartMinimized: (boolean) => void; +} + +interface GuiSettingsHandlers { + handleStartMinimized: ((boolean) => void) => void; +} + /// Events names const DAEMON_CONNECTED = 'daemon-connected'; @@ -39,6 +47,8 @@ const CURRENT_VERSION_CHANGED = 'current-version-changed'; const UPGRADE_VERSION_CHANGED = 'upgrade-version-changed'; const GUI_SETTINGS_CHANGED = 'gui-settings-changed'; +const SET_START_MINIMIZED = 'set-start-minimized'; + /// Typed IPC event channel /// /// Static methods are meant to be provide the way to send the events from a renderer process, while @@ -147,11 +157,13 @@ export default class IpcEventChannel { static guiSettings: Receiver<GuiSettingsState> & GuiSettingsMethods = { listen: listen(GUI_SETTINGS_CHANGED), + setStartMinimized: set(SET_START_MINIMIZED), }; get guiSettings(): Sender<GuiSettingsState> & GuiSettingsHandlers { return { notify: sender(this._webContents, GUI_SETTINGS_CHANGED), + handleStartMinimized: handler(SET_START_MINIMIZED), }; } } @@ -162,8 +174,22 @@ function listen<T>(event: string): ((T) => void) => void { }; } +function set<T>(event: string): (T) => void { + return function(newValue: T) { + ipcRenderer.send(event, newValue); + }; +} + function sender<T>(webContents: WebContents, event: string): (T) => void { return function(newState: T) { webContents.send(event, newState); }; } + +function handler<T>(event: string): ((T) => void) => void { + return function(handlerFn: (T) => void) { + ipcMain.on(event, (_, newValue: T) => { + handlerFn(newValue); + }); + }; +} |
