diff --git a/main/account_switcher.ts b/main/account_switcher.ts index f3f62e6..b5d5d23 100644 --- a/main/account_switcher.ts +++ b/main/account_switcher.ts @@ -44,9 +44,11 @@ export default class AccountSwitcher extends EventEmitter { }); const menu = Menu.getApplicationMenu(); - // Insert item before 'Help' - menu.insert(menu.items.length - 1, item); - Menu.setApplicationMenu(menu); + if (menu !== null) { + // Insert item before 'Help' + menu.insert(menu.items.length - 1, item); + Menu.setApplicationMenu(menu); + } } switchTo(account: Account) { diff --git a/main/app.ts b/main/app.ts index c413cb5..98c7fbf 100644 --- a/main/app.ts +++ b/main/app.ts @@ -1,5 +1,5 @@ import * as path from 'path'; -import {app, Menu, globalShortcut} from 'electron'; +import {app, Menu, globalShortcut, Tray} from 'electron'; import log from './log'; import {Config, Account} from './config'; import AccountSwitcher from './account_switcher'; @@ -9,6 +9,12 @@ import Window from './window'; const IS_DARWIN = process.platform === 'darwin'; const APP_ICON = path.join(__dirname, '..', 'resources', 'icon', 'icon.png'); +function trayIcon(color: string) { + return path.join(__dirname, '..', 'resources', 'icon', `tray-icon-${ + color === 'white' ? 'white' : 'black' + }@2x.png`); +} + export class App { private switcher: AccountSwitcher; @@ -22,6 +28,33 @@ export class App { Menu.setApplicationMenu(defaultMenu()); this.switcher = new AccountSwitcher(this.config.accounts); this.switcher.on('switch', this.onAccountSwitch); + + if (!win.menubar) { + // Note: + // If it's menubar window, tray will be put in menubar(). + const toggleWindow = () => { + const win = this.win.browser; + if (win.isFocused()) { + log.debug('Toggle window: shown -> hidden'); + if (IS_DARWIN) { + app.hide(); + } else { + win.hide(); + } + } else { + log.debug('Toggle window: hidden -> shown'); + win.show(); + } + }; + + const icon = trayIcon(config.icon_color); + const tray = new Tray(icon); + tray.on('click', toggleWindow); + tray.on('double-click', toggleWindow); + if (IS_DARWIN) { + tray.setHighlightMode('never'); + } + } } start() { diff --git a/main/window.ts b/main/window.ts index 0e9178b..418f634 100644 --- a/main/window.ts +++ b/main/window.ts @@ -1,5 +1,5 @@ import * as path from 'path'; -import {app, BrowserWindow, globalShortcut, Tray, shell, dialog, Menu} from 'electron'; +import {app, BrowserWindow, globalShortcut, shell, dialog, Menu} from 'electron'; import windowState = require('electron-window-state'); import * as menubar from 'menubar'; import {Config, Account} from './config'; @@ -156,14 +156,6 @@ function startNormalWindow(account: Account, config: Config): Promise { } }); - const normalIcon = trayIcon(config.icon_color); - const tray = new Tray(normalIcon); - tray.on('click', toggleWindow); - tray.on('double-click', toggleWindow); - if (IS_DARWIN) { - tray.setHighlightMode('never'); - } - resolve(new Window(win, state, account, null)); }); }