From e8ab0c20e7aaef53bcbcf9becbcfe95c4c671f5d Mon Sep 17 00:00:00 2001 From: rhysd Date: Wed, 19 Apr 2017 16:16:55 +0900 Subject: [PATCH] do not reset global shortcut --- main/app.ts | 77 +++++++++++++++++++++++++++++++++----------------- main/window.ts | 32 +-------------------- 2 files changed, 52 insertions(+), 57 deletions(-) diff --git a/main/app.ts b/main/app.ts index 1c526b2..5a49558 100644 --- a/main/app.ts +++ b/main/app.ts @@ -20,32 +20,30 @@ export class App { 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()) { + this.setupTray(); + this.setupHotkey(); + } + + private setupHotkey() { + if (!this.config.hot_key) { + return; + } + + if (this.win.menubar) { + globalShortcut.register(this.config.hot_key, () => { + const mb = this.win.menubar!; + if (mb.window.isFocused()) { log.debug('Toggle window: shown -> hidden'); - if (IS_DARWIN) { - app.hide(); - } else { - win.hide(); - } + mb.hideWindow(); } else { log.debug('Toggle window: hidden -> shown'); - win.show(); + mb.showWindow(); } - }; - - 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'); - } + }); + } else { + globalShortcut.register(this.config.hot_key, this.toggleNormalWindow); } + log.debug('Hot key was set to:', this.config.hot_key); } start() { @@ -55,13 +53,40 @@ export class App { log.debug('Application started', a, url); } + private setupTray() { + if (this.win.menubar) { + // Note: + // If it's menubar window, tray will be put in menubar(). + return; + } + + const icon = trayIcon(this.config.icon_color); + const tray = new Tray(icon); + tray.on('click', this.toggleNormalWindow); + tray.on('double-click', this.toggleNormalWindow); + if (IS_DARWIN) { + tray.setHighlightMode('never'); + } + } + + private toggleNormalWindow = () => { + 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(); + } + } + private onAccountSwitch = (next: Account) => { this.win.close(); - if (this.config.hot_key) { - log.debug('Disable global shortcut for switching account'); - globalShortcut.unregister(this.config.hot_key); - } - Window.create(next, this.config, this.win.menubar) .then(win => { + Window.create(next, this.config, this.win.menubar).then(win => { log.debug('Window was recreated again', next); this.win = win; this.start(); diff --git a/main/window.ts b/main/window.ts index 3b11dfa..060e445 100644 --- a/main/window.ts +++ b/main/window.ts @@ -1,4 +1,4 @@ -import {app, BrowserWindow, globalShortcut, shell, dialog, Menu} from 'electron'; +import {app, BrowserWindow, shell, dialog, Menu} from 'electron'; import windowState = require('electron-window-state'); import * as menubar from 'menubar'; import {Config, Account} from './config'; @@ -116,30 +116,12 @@ function startNormalWindow(account: Account, config: Config): Promise { } state.manage(win); - const toggleWindow = () => { - 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(); - } - }; - win.webContents.on('dom-ready', () => { log.debug('Send config to renderer procress'); win.webContents.send('mstdn:config', config, account); }); win.webContents.once('dom-ready', () => { log.debug('Normal window application was launched'); - if (config.hot_key) { - globalShortcut.register(config.hot_key, toggleWindow); - log.debug('Hot key was set to:', config.hot_key); - } if (IS_DEBUG) { win.webContents.openDevTools({mode: 'detach'}); } @@ -176,18 +158,6 @@ function startMenuBar(account: Account, config: Config, bar: Menubar.MenubarApp }); mb.once('after-create-window', () => { log.debug('Menubar application was launched'); - if (config.hot_key) { - globalShortcut.register(config.hot_key, () => { - if (mb.window.isFocused()) { - log.debug('Toggle window: shown -> hidden'); - mb.hideWindow(); - } else { - log.debug('Toggle window: hidden -> shown'); - mb.showWindow(); - } - }); - log.debug('Hot key was set to:', config.hot_key); - } if (IS_DEBUG) { mb.window.webContents.openDevTools({mode: 'detach'}); }