|
| 1 | +import React, { PropTypes } from 'react' |
| 2 | +import CSSModules from 'browser/lib/CSSModules' |
| 3 | +import styles from './ConfigTab.styl' |
| 4 | +import ConfigManager from 'browser/main/lib/ConfigManager' |
| 5 | +import store from 'browser/main/store' |
| 6 | + |
| 7 | +const electron = require('electron') |
| 8 | +const ipc = electron.ipcRenderer |
| 9 | + |
| 10 | +const OSX = global.process.platform === 'darwin' |
| 11 | + |
| 12 | +class HotkeyTagb extends React.Component { |
| 13 | + constructor (props) { |
| 14 | + super(props) |
| 15 | + |
| 16 | + this.state = { |
| 17 | + isHotkeyHintOpen: false, |
| 18 | + config: props.config |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + componentDidMount () { |
| 23 | + this.handleSettingDone = () => { |
| 24 | + this.setState({keymapAlert: { |
| 25 | + type: 'success', |
| 26 | + message: 'Successfully applied!' |
| 27 | + }}) |
| 28 | + } |
| 29 | + this.handleSettingError = (err) => { |
| 30 | + this.setState({keymapAlert: { |
| 31 | + type: 'error', |
| 32 | + message: err.message != null ? err.message : 'Error occurs!' |
| 33 | + }}) |
| 34 | + } |
| 35 | + ipc.addListener('APP_SETTING_DONE', this.handleSettingDone) |
| 36 | + ipc.addListener('APP_SETTING_ERROR', this.handleSettingError) |
| 37 | + } |
| 38 | + |
| 39 | + componentWillUnmount () { |
| 40 | + ipc.removeListener('APP_SETTING_DONE', this.handleSettingDone) |
| 41 | + ipc.removeListener('APP_SETTING_ERROR', this.handleSettingError) |
| 42 | + } |
| 43 | + |
| 44 | + handleSaveButtonClick (e) { |
| 45 | + let newConfig = { |
| 46 | + hotkey: this.state.config.hotkey |
| 47 | + } |
| 48 | + |
| 49 | + ConfigManager.set(newConfig) |
| 50 | + |
| 51 | + store.dispatch({ |
| 52 | + type: 'SET_UI', |
| 53 | + config: newConfig |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + handleHintToggleButtonClick (e) { |
| 58 | + this.setState({ |
| 59 | + isHotkeyHintOpen: !this.state.isHotkeyHintOpen |
| 60 | + }) |
| 61 | + } |
| 62 | + |
| 63 | + handleHotkeyChange (e) { |
| 64 | + let { config } = this.state |
| 65 | + config.hotkey = { |
| 66 | + toggleFinder: this.refs.toggleFinder.value, |
| 67 | + toggleMain: this.refs.toggleMain.value |
| 68 | + } |
| 69 | + this.setState({ |
| 70 | + config |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + render () { |
| 75 | + let keymapAlert = this.state.keymapAlert |
| 76 | + let keymapAlertElement = keymapAlert != null |
| 77 | + ? <p className={`alert ${keymapAlert.type}`}> |
| 78 | + {keymapAlert.message} |
| 79 | + </p> |
| 80 | + : null |
| 81 | + let { config } = this.state |
| 82 | + |
| 83 | + return ( |
| 84 | + <div styleName='root'> |
| 85 | + <div styleName='group'> |
| 86 | + <div styleName='group-header'>Hotkey</div> |
| 87 | + <div styleName='group-section'> |
| 88 | + <div styleName='group-section-label'>Toggle Main</div> |
| 89 | + <div styleName='group-section-control'> |
| 90 | + <input styleName='group-section-control-input' |
| 91 | + onChange={(e) => this.handleHotkeyChange(e)} |
| 92 | + ref='toggleMain' |
| 93 | + value={config.hotkey.toggleMain} |
| 94 | + type='text' |
| 95 | + /> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + <div styleName='group-section'> |
| 99 | + <div styleName='group-section-label'>Toggle Finder(popup)</div> |
| 100 | + <div styleName='group-section-control'> |
| 101 | + <input styleName='group-section-control-input' |
| 102 | + onChange={(e) => this.handleHotkeyChange(e)} |
| 103 | + ref='toggleFinder' |
| 104 | + value={config.hotkey.toggleFinder} |
| 105 | + type='text' |
| 106 | + /> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + <div styleName='group-control'> |
| 110 | + <button styleName='group-control-leftButton' |
| 111 | + onClick={(e) => this.handleHintToggleButtonClick(e)} |
| 112 | + > |
| 113 | + {this.state.isHotkeyHintOpen |
| 114 | + ? 'Hide Hint' |
| 115 | + : 'Show Hint' |
| 116 | + } |
| 117 | + </button> |
| 118 | + <button styleName='group-control-rightButton' |
| 119 | + onClick={(e) => this.handleSaveButtonClick(e)}>Save Hotkey |
| 120 | + </button> |
| 121 | + {keymapAlertElement} |
| 122 | + </div> |
| 123 | + {this.state.isHotkeyHintOpen && |
| 124 | + <div styleName='group-hint'> |
| 125 | + <p>Available Keys</p> |
| 126 | + <ul> |
| 127 | + <li><code>0</code> to <code>9</code></li> |
| 128 | + <li><code>A</code> to <code>Z</code></li> |
| 129 | + <li><code>F1</code> to <code>F24</code></li> |
| 130 | + <li>Punctuations like <code>~</code>, <code>!</code>, <code>@</code>, <code>#</code>, <code>$</code>, etc.</li> |
| 131 | + <li><code>Plus</code></li> |
| 132 | + <li><code>Space</code></li> |
| 133 | + <li><code>Backspace</code></li> |
| 134 | + <li><code>Delete</code></li> |
| 135 | + <li><code>Insert</code></li> |
| 136 | + <li><code>Return</code> (or <code>Enter</code> as alias)</li> |
| 137 | + <li><code>Up</code>, <code>Down</code>, <code>Left</code> and <code>Right</code></li> |
| 138 | + <li><code>Home</code> and <code>End</code></li> |
| 139 | + <li><code>PageUp</code> and <code>PageDown</code></li> |
| 140 | + <li><code>Escape</code> (or <code>Esc</code> for short)</li> |
| 141 | + <li><code>VolumeUp</code>, <code>VolumeDown</code> and <code>VolumeMute</code></li> |
| 142 | + <li><code>MediaNextTrack</code>, <code>MediaPreviousTrack</code>, <code>MediaStop</code> and <code>MediaPlayPause</code></li> |
| 143 | + </ul> |
| 144 | + </div> |
| 145 | + } |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + ) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +HotkeyTab.propTypes = { |
| 153 | + dispatch: PropTypes.func, |
| 154 | +} |
| 155 | + |
| 156 | +export default CSSModules(HotkeyTab, styles) |
0 commit comments