Skip to content

Commit

Permalink
-Implement tray shortcut toggle (single vs double-click, Fixes #64)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisknepper committed Jul 25, 2018
1 parent 059f5a8 commit baf7ddb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 20 deletions.
8 changes: 7 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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, SETTING_TRAY_ENABLED, EVENT_WEBVIEW_NOTIFICATION, EVENT_NOTIFICATION_REFLECT_READY } from './constants';
import { IS_MAC, IS_WINDOWS, IS_LINUX, IS_DEV, SETTING_TRAY_ENABLED, SETTING_TRAY_CLICK_SHORTCUT, EVENT_WEBVIEW_NOTIFICATION, EVENT_NOTIFICATION_REFLECT_READY } from './constants';

// Special module holding environment variables which you declared
// in config/env_xxx.json file.
Expand Down Expand Up @@ -74,6 +74,7 @@ if (isSecondInstance) {
const autoHideMenuBar = settings.get('autoHideMenuPref', false);
const startInTray = settings.get('startInTrayPref', false);
settings.watch(SETTING_TRAY_ENABLED, trayManager.handleTrayEnabledToggle);
settings.watch(SETTING_TRAY_CLICK_SHORTCUT, trayManager.handleTrayClickShortcutToggle);

if (IS_MAC) {
app.on('activate', () => {
Expand All @@ -90,6 +91,11 @@ if (isSecondInstance) {
settingsMenu.submenu[2].checked = startInTray;
settingsMenu.submenu[1].checked = trayManager.enabled;

if (IS_WINDOWS) {
settingsMenu.submenu[3].submenu[0].checked = (trayManager.clickShortcut === 'double-click');
settingsMenu.submenu[3].submenu[1].checked = (trayManager.clickShortcut === 'click');
}

setApplicationMenu();

autoUpdater.checkForUpdatesAndNotify();
Expand Down
2 changes: 2 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const IS_DEV = (env.name === 'development');

// Settings
const SETTING_TRAY_ENABLED = 'trayEnabledPref';
const SETTING_TRAY_CLICK_SHORTCUT = 'trayClickShortcut';

// Events
const EVENT_WEBVIEW_NOTIFICATION = 'messages-webview-notification';
Expand All @@ -31,6 +32,7 @@ export {
IS_LINUX,
IS_DEV,
SETTING_TRAY_ENABLED,
SETTING_TRAY_CLICK_SHORTCUT,
EVENT_WEBVIEW_NOTIFICATION,
EVENT_NOTIFICATION_REFLECT_READY
};
58 changes: 40 additions & 18 deletions src/helpers/tray/tray_manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import { app, Tray, Menu } from 'electron';
import { trayMenuTemplate } from '../../menu/tray_menu_template';
import { IS_MAC, IS_LINUX, IS_WINDOWS, SETTING_TRAY_ENABLED } from '../../constants';
import { IS_MAC, IS_LINUX, IS_WINDOWS, SETTING_TRAY_ENABLED, SETTING_TRAY_CLICK_SHORTCUT } from '../../constants';
import settings from 'electron-settings';

// TODO: Make this static
Expand All @@ -14,8 +14,10 @@ export default class TrayManager {
this._iconPath = this.setTrayIconPath();
this._overlayIconPath = this.setOverlayIconPath();
this._overlayVisible = false;
this._clickShortcut = settings.get(SETTING_TRAY_CLICK_SHORTCUT, 'double-click');

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

get tray() {
Expand Down Expand Up @@ -50,6 +52,14 @@ export default class TrayManager {
this._overlayVisible = visible;
}

get clickShortcut() {
return this._clickShortcut;
}

set clickShortcut(shortcut) {
this._clickShortcut = shortcut;
}

setTrayIconPath() {
if (IS_WINDOWS) {
// Re-use regular app .ico for the tray icon on Windows.
Expand All @@ -75,25 +85,31 @@ export default class TrayManager {
this.tray = new Tray(this.trayIconPath);
let trayContextMenu = Menu.buildFromTemplate(trayMenuTemplate);
this.tray.setContextMenu(trayContextMenu);
this.setupEventListeners();
}
}

if (IS_WINDOWS) {
this.tray.on('double-click', (event) => {
event.preventDefault();
if (app.mainWindow) {
app.mainWindow.show();
}
});
}
setupEventListeners() {
if (IS_WINDOWS) {
this.tray.on(this.clickShortcut, this.handleTrayClick);
}

// This actually has no effect. Electron docs say that click event is ignored on Linux for
// AppIndicator tray, but I can't find a way to not use AppIndicator for Linux tray.
if (IS_LINUX) {
this.tray.on('click', () => {
if (app.mainWindow) {
app.mainWindow.show();
}
});
}
// This actually has no effect. Electron docs say that click event is ignored on Linux for
// AppIndicator tray, but I can't find a way to not use AppIndicator for Linux tray.
if (IS_LINUX) {
this.tray.on('click', this.handleTrayClick);
}
}

destroyEventListeners() {
this.tray.removeListener('click', this.handleTrayClick);
this.tray.removeListener('double-click', this.handleTrayClick);
}

handleTrayClick(event) {
event.preventDefault();
if (app.mainWindow) {
app.mainWindow.show();
}
}

Expand Down Expand Up @@ -153,6 +169,12 @@ export default class TrayManager {
}
}

handleTrayClickShortcutToggle(newValue, oldValue) {
this.clickShortcut = newValue;
this.destroyEventListeners();
this.setupEventListeners();
}

toggleOverlay(toggle) {
if (IS_WINDOWS && this.tray && toggle !== this.overlayVisible) {
if (toggle) {
Expand Down
30 changes: 29 additions & 1 deletion src/menu/settings_menu_template.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dialog } from 'electron';
import settings from "electron-settings";
import { IS_MAC, SETTING_TRAY_ENABLED, IS_LINUX } from '../constants';
import { IS_LINUX, IS_MAC, IS_WINDOWS, SETTING_TRAY_ENABLED, SETTING_TRAY_CLICK_SHORTCUT } from '../constants';

export const settingsMenu = {
label: IS_MAC ? 'Preferences' : 'Settings',
Expand Down Expand Up @@ -55,3 +55,31 @@ export const settingsMenu = {
}
]
};

// Electron doesn't seem to support the visible property for submenus, so push it instead of hiding it in non-Windows
// See: https://github.com/electron/electron/issues/8703
if (IS_WINDOWS) {
settingsMenu.submenu.push(
{
label: 'Open from Tray On...',
submenu: [
{
label: 'Double-click',
type: 'radio',
click: (item) => {
settings.set(SETTING_TRAY_CLICK_SHORTCUT, 'double-click');
item.checked = true;
}
},
{
label: 'Single-click',
type: 'radio',
click: (item) => {
settings.set(SETTING_TRAY_CLICK_SHORTCUT, 'click');
item.checked = true;
}
}
]
}
);
}

0 comments on commit baf7ddb

Please sign in to comment.