Skip to content

Commit

Permalink
-Refactor more tray code into TrayManager
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisknepper committed Jul 17, 2018
1 parent e9bcf0b commit 77b38e9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 48 deletions.
42 changes: 3 additions & 39 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { devMenuTemplate } from './menu/dev_menu_template';
import { settingsMenu } from './menu/settings_menu_template';
import { helpMenuTemplate } from './menu/help_menu_template';
import createWindow from './helpers/window';
import TrayManager from './helpers/tray/tray_manager';
import settings from 'electron-settings';
import { IS_MAC, IS_WINDOWS, IS_LINUX, IS_DEV } from './constants';
import { IS_MAC, IS_WINDOWS, IS_LINUX, IS_DEV, SETTING_TRAY_ENABLED } from './constants';

// Special module holding environment variables which you declared
// in config/env_xxx.json file.
import env from 'env';
import TrayManager from './helpers/tray/tray_manager';

let mainWindow = null;

Expand Down Expand Up @@ -69,43 +69,7 @@ if (isSecondInstance) {
// TODO: Create a preference manager which handles all of these
const autoHideMenuBar = settings.get('autoHideMenuPref', false);
const startInTray = settings.get('startInTrayPref', false);
settings.watch('trayEnabledPref', (newValue, oldValue) => {
trayManager.enabled = newValue;
if (newValue) {
if (!IS_MAC) {
// Must get a live reference to the menu item when updating their properties from outside of them.
let liveStartInTrayMenuItemRef = Menu.getApplicationMenu().getMenuItemById('startInTrayMenuItem');
liveStartInTrayMenuItemRef.enabled = true;
}
if (!trayManager.tray) {
trayManager.startIfEnabled();
}
}
if (!newValue) {
if (trayManager.tray) {
trayManager.destroy();
if ((!IS_MAC) && mainWindow) {
if (!mainWindow.isVisible()) {
mainWindow.show();
}
}
}
if (!IS_MAC) {
// If the app has no tray icon, it can be difficult or impossible to re-gain access to the window, so disallow
// starting hidden, except on Mac, where the app window can still be un-hidden via the dock.
settings.set('startInTrayPref', false);
let liveStartInTrayMenuItemRef = Menu.getApplicationMenu().getMenuItemById('startInTrayMenuItem');
liveStartInTrayMenuItemRef.enabled = false;
liveStartInTrayMenuItemRef.checked = false;
}
if (IS_LINUX) {
// On Linux, the call to tray.destroy doesn't seem to work, causing multiple instances of the tray icon.
// Work around this by quickly restarting the app.
app.relaunch();
app.exit(0);
}
}
});
settings.watch(SETTING_TRAY_ENABLED, trayManager.handleTrayEnabledToggle);

if (!IS_MAC) {
// Sets checked status based on user prefs
Expand Down
6 changes: 5 additions & 1 deletion src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ const IS_LINUX = (osName === 'linux');
// Environment
const IS_DEV = (env.name === 'development');

// Settings
const SETTING_TRAY_ENABLED = 'trayEnabledPref';

export {
osName,
osNameFriendly,
IS_WINDOWS,
IS_MAC,
IS_LINUX,
IS_DEV
IS_DEV,
SETTING_TRAY_ENABLED
};
50 changes: 45 additions & 5 deletions src/helpers/tray/tray_manager.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import path from 'path';
import { app, Tray, Menu } from 'electron';
import { trayMenuTemplate } from '../../menu/tray_menu_template';
import { IS_MAC, IS_LINUX, IS_WINDOWS } from '../../constants';
import { IS_MAC, IS_LINUX, IS_WINDOWS, SETTING_TRAY_ENABLED } from '../../constants';
import settings from 'electron-settings';

export default class TrayManager {
constructor() {
// Must declare reference to instance of Tray as a variable, not a const, or bad/weird things happen!
this._tray = null;
// Enable tray/menu bar icon by default except on Linux -- the system having a tray is less of a guarantee on Linux.
this._enabled = settings.get('trayEnabledPref', (!IS_LINUX));
this._iconPath = this._enabled ? this.setTrayIconPath() : null;
this._enabled = settings.get(SETTING_TRAY_ENABLED, (!IS_LINUX));
this._iconPath = this.setTrayIconPath();

this.handleTrayEnabledToggle = this.handleTrayEnabledToggle.bind(this);
}

get tray() {
Expand Down Expand Up @@ -78,10 +80,10 @@ export default class TrayManager {
}

showMinimizeToTrayWarning() {
if (IS_WINDOWS && trayManager.enabled) {
if (IS_WINDOWS && this.enabled) {
const seenMinimizeToTrayWarning = settings.get('seenMinimizeToTrayWarningPref', false);
if (!seenMinimizeToTrayWarning) {
trayManager.tray.displayBalloon({
this.tray.displayBalloon({
title: 'Android Messages',
content: 'Android Messages is still running in the background. To close it, use the File menu or right-click on the tray icon.'
});
Expand All @@ -90,4 +92,42 @@ export default class TrayManager {
}
}

handleTrayEnabledToggle(newValue, oldValue) {
this.enabled = newValue;
if (newValue) {
if (!IS_MAC) {
// Must get a live reference to the menu item when updating their properties from outside of them.
let liveStartInTrayMenuItemRef = Menu.getApplicationMenu().getMenuItemById('startInTrayMenuItem');
liveStartInTrayMenuItemRef.enabled = true;
}
if (!this.tray) {
this.startIfEnabled();
}
}
if (!newValue) {
if (this.tray) {
this.destroy();
if ((!IS_MAC) && mainWindow) {
if (!mainWindow.isVisible()) {
mainWindow.show();
}
}
}
if (!IS_MAC) {
// If the app has no tray icon, it can be difficult or impossible to re-gain access to the window, so disallow
// starting hidden, except on Mac, where the app window can still be un-hidden via the dock.
settings.set('startInTrayPref', false);
let liveStartInTrayMenuItemRef = Menu.getApplicationMenu().getMenuItemById('startInTrayMenuItem');
liveStartInTrayMenuItemRef.enabled = false;
liveStartInTrayMenuItemRef.checked = false;
}
if (IS_LINUX) {
// On Linux, the call to tray.destroy doesn't seem to work, causing multiple instances of the tray icon.
// Work around this by quickly restarting the app.
app.relaunch();
app.exit(0);
}
}
}

}
6 changes: 3 additions & 3 deletions src/menu/settings_menu_template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import settings from "electron-settings";
import { IS_MAC } from '../constants';
import { IS_MAC, SETTING_TRAY_ENABLED } from '../constants';

export const settingsMenu = {
label: IS_MAC ? 'Preferences' : 'Settings',
Expand Down Expand Up @@ -31,8 +31,8 @@ export const settingsMenu = {
label: IS_MAC ? 'Enable Menu Bar Icon' : 'Enable Tray Icon',
type: 'checkbox',
click: (item) => {
const trayEnabledPref = !settings.get('trayEnabledPref');
settings.set('trayEnabledPref', trayEnabledPref);
const trayEnabledPref = !settings.get(SETTING_TRAY_ENABLED);
settings.set(SETTING_TRAY_ENABLED, trayEnabledPref);
item.checked = trayEnabledPref;
}
}
Expand Down

0 comments on commit 77b38e9

Please sign in to comment.