Skip to content

Commit

Permalink
Fix the "Show Tray Icon" preference (#1215)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
JBudny and sindresorhus authored Feb 5, 2020
1 parent a4a6ddb commit b386b23
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
17 changes: 7 additions & 10 deletions source/menu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import {existsSync, writeFileSync} from 'fs';
import {app, shell, Menu, MenuItemConstructorOptions, BrowserWindow, dialog} from 'electron';
import {app, shell, Menu, MenuItemConstructorOptions, dialog} from 'electron';
import {
is,
appMenu,
Expand All @@ -10,7 +10,7 @@ import {
debugInfo
} from 'electron-util';
import config from './config';
import {sendAction, showRestartDialog} from './util';
import {sendAction, showRestartDialog, getWindow, toggleTrayIcon, toggleLaunchMinimized} from './util';
import {generateSubmenu as generateEmojiSubmenu} from './emoji';
import {toggleMenuBarMode} from './menu-bar-mode';
import {caprineIconPath} from './constants';
Expand Down Expand Up @@ -272,9 +272,7 @@ Press Command/Ctrl+R in Caprine to see your changes.
checked: config.get('menuBarMode'),
click() {
config.set('menuBarMode', !config.get('menuBarMode'));

const [win] = BrowserWindow.getAllWindows();
toggleMenuBarMode(win);
toggleMenuBarMode(getWindow());
}
},
{
Expand Down Expand Up @@ -329,13 +327,13 @@ Press Command/Ctrl+R in Caprine to see your changes.
}
},
{
id: 'showTrayIcon',
label: 'Show Tray Icon',
type: 'checkbox',
enabled: is.linux || is.windows,
enabled: (is.linux || is.windows) && !config.get('launchMinimized'),
checked: config.get('showTrayIcon'),
click() {
config.set('showTrayIcon', !config.get('showTrayIcon'));
sendAction('toggle-tray-icon');
toggleTrayIcon();
}
},
{
Expand All @@ -344,8 +342,7 @@ Press Command/Ctrl+R in Caprine to see your changes.
visible: !is.macos,
checked: config.get('launchMinimized'),
click() {
config.set('launchMinimized', !config.get('launchMinimized'));
sendAction('toggle-tray-icon');
toggleLaunchMinimized(menu);
}
},
{
Expand Down
40 changes: 39 additions & 1 deletion source/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {app, BrowserWindow, dialog} from 'electron';
import {app, BrowserWindow, dialog, Menu} from 'electron';
import {is} from 'electron-util';
import config from './config';
import tray from './tray';

export function getWindow(): BrowserWindow {
const [win] = BrowserWindow.getAllWindows();
Expand Down Expand Up @@ -49,3 +50,40 @@ export function stripTrackingFromUrl(url: string): string {

return url;
}

export const toggleTrayIcon = (): void => {
const showTrayIconState = config.get('showTrayIcon');
config.set('showTrayIcon', !showTrayIconState);

if (showTrayIconState) {
return tray.destroy();
}

return tray.create(getWindow());
};

export const toggleLaunchMinimized = (menu: Menu): void => {
config.set('launchMinimized', !config.get('launchMinimized'));
const showTrayIconItem = menu.getMenuItemById('showTrayIcon');

if (config.get('launchMinimized')) {
if (!config.get('showTrayIcon')) {
toggleTrayIcon();
}

disableMenuItem(showTrayIconItem, true);

dialog.showMessageBox({
type: 'info',
message: 'The “Show Tray Icon” setting is force-enabled while the “Launch Minimized” setting is enabled.',
buttons: ['OK']
});
} else {
showTrayIconItem.enabled = true;
}
};

const disableMenuItem = (menuItem: Electron.MenuItem, checked: boolean): void => {
menuItem.enabled = false;
menuItem.checked = checked;
};

0 comments on commit b386b23

Please sign in to comment.