diff options
| author | Andrej Mihajlov <and@codeispoetry.ru> | 2017-03-16 22:23:01 +0000 |
|---|---|---|
| committer | Andrej Mihajlov <and@codeispoetry.ru> | 2017-03-16 22:23:01 +0000 |
| commit | 5d35f427e081a23693b8629a6846aea0f47ca4c7 (patch) | |
| tree | 830c766f0b9f198630c1f553ebe80f51ef9edad1 | |
| parent | 9fa8ea732ab9ebd5677cf60b401b999da69aa165 (diff) | |
| download | mullvadvpn-5d35f427e081a23693b8629a6846aea0f47ca4c7.tar.xz mullvadvpn-5d35f427e081a23693b8629a6846aea0f47ca4c7.zip | |
Refactor code
| -rw-r--r-- | app/lib/tray-animation.js | 2 | ||||
| -rw-r--r-- | app/lib/tray-animator.js | 2 | ||||
| -rw-r--r-- | app/lib/tray-icon-manager.js | 60 | ||||
| -rw-r--r-- | app/lib/tray-icon-provider.js | 38 | ||||
| -rw-r--r-- | app/main.js | 1 | ||||
| -rw-r--r-- | test/tray-animator.spec.js | 4 |
6 files changed, 90 insertions, 17 deletions
diff --git a/app/lib/tray-animation.js b/app/lib/tray-animation.js index 6e9b546e43..7db7a8c8ab 100644 --- a/app/lib/tray-animation.js +++ b/app/lib/tray-animation.js @@ -7,7 +7,7 @@ import { nativeImage } from 'electron'; * @export * @class TrayAnimation */ -export class TrayAnimation { +export default class TrayAnimation { /** * Set animation pace per frame in ms diff --git a/app/lib/tray-animator.js b/app/lib/tray-animator.js index 6d098c4a62..2c45dce37c 100644 --- a/app/lib/tray-animator.js +++ b/app/lib/tray-animator.js @@ -4,7 +4,7 @@ import assert from 'assert'; * Tray icon animator * @class TrayAnimator */ -export class TrayAnimator { +export default class TrayAnimator { /** * Whether animator has started. diff --git a/app/lib/tray-icon-manager.js b/app/lib/tray-icon-manager.js index e72c348add..023f0d4dd8 100644 --- a/app/lib/tray-icon-manager.js +++ b/app/lib/tray-icon-manager.js @@ -1,5 +1,6 @@ import assert from 'assert'; -import { TrayAnimator } from './tray-animator'; +import TrayAnimator from './tray-animator'; +import TrayIconProvider from './tray-icon-provider'; import { TrayIconType } from '../enums'; /** @@ -25,6 +26,8 @@ export default class TrayIconManager { this._iconProvider = iconProvider; this._animator = null; this._iconType = null; + + iconProvider.on(TrayIconProvider.EventType.themeChanged, this._onThemeChange); } /** @@ -37,6 +40,15 @@ export default class TrayIconManager { this._animator = null; } this._iconType = null; + this._iconProvider.removeListener(TrayIconProvider.EventType.themeChanged, this._onThemeChange); + } + + /** + * Event handler for notification when menubar theme is changed. + * @memberOf TrayIconManager + */ + _onThemeChange = () => { + this._updateType(this._iconType, true); } /** @@ -52,24 +64,52 @@ export default class TrayIconManager { * @memberOf TrayIconManager */ set iconType(type) { - let animator; - assert(TrayIconType.isValid(type)); - // no-op if same animator requested if(this._iconType === type) { return; } - + + // do not animate if setting icon for the first time + const skipAnimation = this._iconType === null; + + this._updateType(type, skipAnimation); + } + + /** + * Get animation for iconType + * + * @param {TrayIconType} type + * @returns TrayIconAnimator + * + * @memberOf TrayIconManager + */ + _animationForType(type) { + switch(type) { + case TrayIconType.secured: return this._iconProvider.lockAnimation(); + case TrayIconType.unsecured: return this._iconProvider.unlockAnimation(); + case TrayIconType.securing: return this._iconProvider.spinnerAnimation(); + } + } + + /** + * Update icon animator with new type + * + * @param {TrayIconType} type + * @param {boolean} [skipAnimation=false] whether animation should be skipped + * + * @memberOf TrayIconManager + */ + _updateType(type, skipAnimation = false) { + assert(TrayIconType.isValid(type)); + + let animator = new TrayAnimator(this._tray, this._animationForType(type)); + // destroy existing animator if(this._animator) { this._animator.stop(); this._animator = null; } - // do not animate if setting icon for the first time - const skipAnimation = this._iconType === null; - switch(type) { case TrayIconType.secured: - animator = new TrayAnimator(this._tray, this._iconProvider.lockAnimation()); if(skipAnimation) { animator.advanceToEnd(); } else { @@ -78,7 +118,6 @@ export default class TrayIconManager { break; case TrayIconType.unsecured: - animator = new TrayAnimator(this._tray, this._iconProvider.unlockAnimation()); if(skipAnimation) { animator.advanceToStart(); } else { @@ -87,7 +126,6 @@ export default class TrayIconManager { break; case TrayIconType.securing: - animator = new TrayAnimator(this._tray, this._iconProvider.spinnerAnimation()); animator.start(); break; } diff --git a/app/lib/tray-icon-provider.js b/app/lib/tray-icon-provider.js index cf859852eb..66f926a8c6 100644 --- a/app/lib/tray-icon-provider.js +++ b/app/lib/tray-icon-provider.js @@ -1,6 +1,8 @@ import path from 'path'; +import { EventEmitter } from 'events'; import { systemPreferences } from 'electron'; -import { TrayAnimation } from './tray-animation'; +import TrayAnimation from './tray-animation'; +import Enum from './enum'; const menubarIcons = { base: path.join(path.resolve(__dirname, '..'), 'assets/images/menubar icons'), @@ -20,7 +22,39 @@ const menubarIcons = { * @export * @class TrayIconProvider */ -export default class TrayIconProvider { +export default class TrayIconProvider extends EventEmitter { + + /** + * EventType + * @type {TrayIconProvider.EventType} + * @property {string} themeChanged - event fired when menubar theme is changed + */ + static EventType = new Enum('themeChanged'); + + /** + * Creates an instance of TrayIconProvider. + * + * @memberOf TrayIconProvider + */ + constructor() { + super(); + + this._themeChangeObserver = systemPreferences.subscribeNotification('AppleInterfaceThemeChangedNotification', () => { + this.emit(TrayIconProvider.EventType.themeChanged); + }); + } + + /** + * Destroys TrayIconProvider + * + * @memberOf TrayIconProvider + */ + destroy() { + if(this._themeChangeObserver) { + systemPreferences.unsubscribeNotification(this._themeChangeObserver); + this._themeChangeObserver = null; + } + } /** * Get lock animation diff --git a/app/main.js b/app/main.js index 9f83a32bb9..56cf8b6967 100644 --- a/app/main.js +++ b/app/main.js @@ -10,6 +10,7 @@ const userDataPath = path.join(applicationSupportPath, 'mullvad.vpn-103'); app.setPath('userData', userDataPath); const isDevelopment = (process.env.NODE_ENV === 'development'); + let window = null; let tray = null; let macEventMonitor = null; diff --git a/test/tray-animator.spec.js b/test/tray-animator.spec.js index c6c1769160..c22cdb7a25 100644 --- a/test/tray-animator.spec.js +++ b/test/tray-animator.spec.js @@ -1,6 +1,6 @@ import { expect } from 'chai'; -import { TrayAnimator, } from '../app/lib/tray-animator'; -import { TrayAnimation } from '../app/lib/tray-animation'; +import TrayAnimator from '../app/lib/tray-animator'; +import TrayAnimation from '../app/lib/tray-animation'; import { nativeImage } from 'electron'; describe('lib/tray-animator', function() { |
