From 40410eb10f8c54b3114562bbf8c84225a27c34de Mon Sep 17 00:00:00 2001 From: Dick Choi Date: Fri, 22 Jul 2016 08:39:25 +0900 Subject: [PATCH] hotkey config --- browser/main/global.styl | 6 +- browser/main/lib/ConfigManager.js | 11 ++++ .../main/modals/PreferencesModal/ConfigTab.js | 24 ++++--- .../modals/PreferencesModal/ConfigTab.styl | 8 +++ .../main/modals/PreferencesModal/InfoTab.js | 4 +- lib/hotkey.js | 64 ++----------------- 6 files changed, 43 insertions(+), 74 deletions(-) diff --git a/browser/main/global.styl b/browser/main/global.styl index 1dee7526..2726a551 100644 --- a/browser/main/global.styl +++ b/browser/main/global.styl @@ -37,7 +37,11 @@ button font-size 12px &:focus, &.focus outline none - + &:disabled + cursor not-allowed +input + &:disabled + cursor not-allowed .noSelect noSelect() diff --git a/browser/main/lib/ConfigManager.js b/browser/main/lib/ConfigManager.js index dbc8c59d..b353a043 100644 --- a/browser/main/lib/ConfigManager.js +++ b/browser/main/lib/ConfigManager.js @@ -1,6 +1,8 @@ import _ from 'lodash' const OSX = global.process.platform === 'darwin' +const electron = require('electron') +const { ipcRenderer } = electron const defaultConfig = { zoom: 1, @@ -64,8 +66,17 @@ function set (updates) { let newConfig = Object.assign({}, defaultConfig, currentConfig, updates) if (!validate(newConfig)) throw new Error('INVALID CONFIG') _save(newConfig) + ipcRenderer.send('CONFIG_RENEW', { + config: get(), + silent: false + }) } +ipcRenderer.send('CONFIG_RENEW', { + config: get(), + silent: true +}) + export default { get, set, diff --git a/browser/main/modals/PreferencesModal/ConfigTab.js b/browser/main/modals/PreferencesModal/ConfigTab.js index af789ff4..ade253c4 100644 --- a/browser/main/modals/PreferencesModal/ConfigTab.js +++ b/browser/main/modals/PreferencesModal/ConfigTab.js @@ -25,7 +25,7 @@ class ConfigTab extends React.Component { this.handleSettingDone = () => { this.setState({keymapAlert: { type: 'success', - message: 'Successfully done!' + message: 'Successfully applied!' }}) } this.handleSettingError = (err) => { @@ -43,20 +43,17 @@ class ConfigTab extends React.Component { ipc.removeListener('APP_SETTING_ERROR', this.handleSettingError) } - submitHotKey () { - ipc.send('hotkeyUpdated', this.state.keymap) - } - - submitConfig () { - ipc.send('configUpdated', this.state.config) - } - handleSaveButtonClick (e) { - this.submitHotKey() - } + let newConfig = { + hotkey: this.state.config.hotkey + } - handleConfigSaveButtonClick (e) { - this.submitConfig() + ConfigManager.set(newConfig) + + store.dispatch({ + type: 'SET_UI', + config: newConfig + }) } handleKeyDown (e) { @@ -183,6 +180,7 @@ class ConfigTab extends React.Component { ref='toggleFinder' value={config.hotkey.toggleFinder} type='text' + disabled /> diff --git a/browser/main/modals/PreferencesModal/ConfigTab.styl b/browser/main/modals/PreferencesModal/ConfigTab.styl index b847ba8a..51ed9776 100644 --- a/browser/main/modals/PreferencesModal/ConfigTab.styl +++ b/browser/main/modals/PreferencesModal/ConfigTab.styl @@ -34,6 +34,8 @@ border solid 1px $border-color border-radius 2px padding 0 5px + &:disabled + background-color $ui-input--disabled-backgroundColor .group-checkBoxSection margin-bottom 15px @@ -47,6 +49,12 @@ box-sizing border-box height 40px text-align right + :global + .alert + font-size 12px + line-height 30px + padding 0 5px + float right .group-control-leftButton float left colorDefaultButton() diff --git a/browser/main/modals/PreferencesModal/InfoTab.js b/browser/main/modals/PreferencesModal/InfoTab.js index 001773b2..5980774b 100644 --- a/browser/main/modals/PreferencesModal/InfoTab.js +++ b/browser/main/modals/PreferencesModal/InfoTab.js @@ -2,9 +2,9 @@ import React, { PropTypes } from 'react' import CSSModules from 'browser/lib/CSSModules' import styles from './InfoTab.styl' -const appVersion = global.process.version const electron = require('electron') -const { shell } = electron +const { shell, remote } = electron +const appVersion = remote.app.getVersion() class InfoTab extends React.Component { constructor (props) { diff --git a/lib/hotkey.js b/lib/hotkey.js index 3573174d..063305c4 100644 --- a/lib/hotkey.js +++ b/lib/hotkey.js @@ -1,43 +1,9 @@ const electron = require('electron') -const app = electron.app const ipc = electron.ipcMain const Menu = electron.Menu const globalShortcut = electron.globalShortcut -const jetpack = require('fs-jetpack') const mainWindow = require('./main-window') const nodeIpc = require('@rokt33r/node-ipc') -const _ = require('lodash') - -const OSX = global.process.platform === 'darwin' - -const defaultKeymap = { - toggleFinder: OSX ? 'Cmd + Alt + S' : 'Super + Alt + S', - toggleMain: OSX ? 'Cmd + Alt + L' : 'Super + Alt + E' -} -const keymapFilename = 'keymap.json' - -var userDataPath = app.getPath('userData') - -function getKeymap () { - var userDataPath = app.getPath('userData') - if (jetpack.cwd(userDataPath).exists(keymapFilename)) { - try { - return JSON.parse(jetpack.cwd(userDataPath).read(keymapFilename, 'utf-8')) - } catch (err) {} - } - return {} -} - -function saveKeymap () { - var content - try { - content = JSON.stringify(global.keymap) - } catch (e) { - global.keymap = {} - content = JSON.stringify(global.keymap) - } - jetpack.cwd(userDataPath).file(keymapFilename, { content }) -} function emitToFinder (type, data) { var payload = { @@ -71,31 +37,22 @@ function toggleMain () { } } -// Init -global.keymap = Object.assign({}, defaultKeymap, getKeymap()) - -function registerKey (name, callback) { - if (_.isString(global.keymap[name]) && global.keymap[name].trim().length > 0) { - globalShortcut.register(global.keymap[name], callback) - } -} - -function registerAllKeys (broadcast) { - if (broadcast == null) broadcast = true +ipc.on('CONFIG_RENEW', (e, payload) => { + globalShortcut.unregisterAll() + var { config } = payload var errors = [] try { - registerKey('toggleFinder', toggleFinder) + globalShortcut.register(config.hotkey.toggleFinder, toggleFinder) } catch (err) { errors.push('toggleFinder') } try { - registerKey('toggleMain', toggleMain) + globalShortcut.register(config.hotkey.toggleMain, toggleMain) } catch (err) { errors.push('toggleMain') } - - if (broadcast) { + if (!config.silent) { if (errors.length === 0) { mainWindow.webContents.send('APP_SETTING_DONE', {}) } else { @@ -104,13 +61,4 @@ function registerAllKeys (broadcast) { }) } } -} - -registerAllKeys(false) - -ipc.on('hotkeyUpdated', function (event, newKeymap) { - global.keymap = Object.assign({}, defaultKeymap, global.keymap, newKeymap) - saveKeymap() - globalShortcut.unregisterAll() - registerAllKeys() })