PreferencesPrompt.jsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import lodash from 'lodash'
  8. import { Checkbox, Input, Radio, Select, Tabs } from 'antd'
  9. import MyFrame from './MyFrame'
  10. import classnames from 'classnames'
  11. import Agent from '../Agent'
  12. import { version as current_version } from '../../app/version'
  13. import formatVersion from '../../app/libs/formatVersion'
  14. import CodeMirror from 'react-codemirror'
  15. import 'codemirror/mode/shell/shell'
  16. import './PreferencesPrompt.less'
  17. const RadioGroup = Radio.Group
  18. const Option = Select.Option
  19. const TabPane = Tabs.TabPane
  20. const pref_keys = ['after_cmd', 'auto_launch', 'choice_mode', 'hide_at_launch', 'is_dock_icon_hidden', 'user_language', 'send_usage_data']
  21. export default class PreferencesPrompt extends React.Component {
  22. constructor (props) {
  23. super(props)
  24. this.state = {
  25. show: false,
  26. user_language: '',
  27. after_cmd: '',
  28. choice_mode: 'multiple',
  29. auto_launch: false,
  30. hide_at_launch: false,
  31. lang_list: [],
  32. send_usage_data: true,
  33. update_found: false // 发现新版本
  34. }
  35. Agent.pact('getLangList')
  36. .then(lang_list => this.setState({lang_list}))
  37. }
  38. componentDidMount () {
  39. Agent.on('show_preferences', () => {
  40. Agent.pact('getPref')
  41. .then(pref => {
  42. this.setState(Object.assign({}, pref, {show: true}))
  43. console.log(pref)
  44. })
  45. })
  46. Agent.on('update_found', (v) => {
  47. console.log(v)
  48. this.setState({
  49. update_found: true
  50. })
  51. })
  52. }
  53. onOK () {
  54. this.setState({
  55. show: false
  56. }, () => {
  57. let prefs = lodash.pick(this.state, pref_keys)
  58. Agent.pact('setPref', prefs)
  59. .then(() => {
  60. setTimeout(() => {
  61. Agent.pact('relaunch')
  62. }, 200)
  63. })
  64. })
  65. }
  66. onCancel () {
  67. this.setState({
  68. show: false
  69. })
  70. }
  71. getLanguageOptions () {
  72. return this.state.lang_list.map(({key, name}, idx) => {
  73. return (
  74. <Option value={key} key={idx}>{name}</Option>
  75. )
  76. })
  77. }
  78. updateLangKey (v) {
  79. this.setState({user_language: v})
  80. }
  81. updateChoiceMode (v) {
  82. this.setState({
  83. choice_mode: v
  84. })
  85. }
  86. updateAfterCmd (v) {
  87. this.setState({
  88. after_cmd: v
  89. })
  90. }
  91. updateAutoLaunch (v) {
  92. this.setState({
  93. auto_launch: v
  94. })
  95. // todo set auto launch
  96. }
  97. updateMinimizeAtLaunch (v) {
  98. this.setState({
  99. hide_at_launch: v
  100. })
  101. }
  102. prefLanguage () {
  103. let {lang} = this.props
  104. return (
  105. <div className="ln">
  106. <div>{lang.language}</div>
  107. <div
  108. className="inform">{lang.should_restart_after_change_language}</div>
  109. <div>
  110. <Select
  111. value={this.state.user_language || ''}
  112. onChange={v => this.updateLangKey(v)}
  113. style={{minWidth: 120}}
  114. >
  115. {this.getLanguageOptions()}
  116. </Select>
  117. </div>
  118. </div>
  119. )
  120. }
  121. prefChoiceMode () {
  122. let {lang} = this.props
  123. return (
  124. <div className="ln">
  125. <div>{lang.pref_choice_mode}</div>
  126. <div>
  127. <RadioGroup
  128. onChange={e => this.updateChoiceMode(e.target.value)}
  129. value={this.state.choice_mode}
  130. >
  131. <Radio value="single">{lang.pref_choice_mode_single}</Radio>
  132. <Radio value="multiple">{lang.pref_choice_mode_multiple}</Radio>
  133. </RadioGroup>
  134. </div>
  135. </div>
  136. )
  137. }
  138. prefAfterCmd () {
  139. let {lang} = this.props
  140. let options = {
  141. mode: 'shell'
  142. }
  143. return (
  144. <div className="ln">
  145. <div>{lang.pref_after_cmd}</div>
  146. <div>
  147. <div className="inform">{lang.pref_after_cmd_info}</div>
  148. {/*<Input*/}
  149. {/*type="textarea"*/}
  150. {/*rows={8}*/}
  151. {/*defaultValue={this.state.after_cmd}*/}
  152. {/*placeholder={lang.pref_after_cmd_placeholder}*/}
  153. {/*onChange={(e) => this.updateAfterCmd(e.target.value)}*/}
  154. {/*/>*/}
  155. <CodeMirror
  156. className="pref-cm"
  157. value={this.state.after_cmd}
  158. onChange={v => this.updateAfterCmd(v)}
  159. options={options}
  160. />
  161. </div>
  162. </div>
  163. )
  164. }
  165. prefAutoLaunch () {
  166. let {lang} = this.props
  167. return (
  168. <div className="ln">
  169. <Checkbox
  170. value={this.state.auto_launch}
  171. onChange={(e) => this.updateAutoLaunch(e.target.checked)}
  172. >
  173. {lang.auto_launch}
  174. </Checkbox>
  175. </div>
  176. )
  177. }
  178. prefMinimizeAtLaunch () {
  179. let {lang} = this.props
  180. return (
  181. <div className="ln">
  182. <Checkbox
  183. checked={this.state.hide_at_launch}
  184. onChange={(e) => this.updateMinimizeAtLaunch(e.target.checked)}
  185. >
  186. {lang.hide_at_launch}
  187. </Checkbox>
  188. </div>
  189. )
  190. }
  191. prefAdvanced () {
  192. let {lang} = this.props
  193. return (
  194. <div className="ln">
  195. <div>{lang.pref_tab_usage_data_title}</div>
  196. <div className="inform">{lang.pref_tab_usage_data_desc}</div>
  197. <div>
  198. <Checkbox
  199. checked={this.state.send_usage_data}
  200. onChange={(e) => this.setState({send_usage_data: e.target.checked})}
  201. >
  202. {lang.pref_tab_usage_data_label}
  203. </Checkbox>
  204. </div>
  205. </div>
  206. )
  207. }
  208. static openDownloadPage () {
  209. Agent.pact('openUrl', require('../../app/configs').url_download)
  210. }
  211. body () {
  212. let {lang} = this.props
  213. let height = 240
  214. return (
  215. <div ref="body">
  216. {/*<div className="title">{SH_Agent.lang.hosts_title}</div>*/}
  217. {/*<div className="cnt">*/}
  218. {/*</div>*/}
  219. <div
  220. className={classnames('current-version', {'update-found': this.state.update_found})}
  221. >
  222. <a
  223. href="#"
  224. onClick={PreferencesPrompt.openDownloadPage}
  225. >{formatVersion(current_version)}</a>
  226. </div>
  227. <Tabs
  228. defaultActiveKey="1"
  229. tabPosition="left"
  230. style={{minHeight: height}}
  231. >
  232. <TabPane tab={lang.pref_tab_general} key="1" style={{height}}>
  233. {this.prefLanguage()}
  234. {this.prefChoiceMode()}
  235. {/*{this.prefAutoLaunch()}*/}
  236. {this.prefMinimizeAtLaunch()}
  237. </TabPane>
  238. <TabPane tab={lang.pref_tab_custom_cmd} key="2" style={{height}}>
  239. {this.prefAfterCmd()}
  240. </TabPane>
  241. <TabPane tab={lang.pref_tab_advanced} key="3" style={{height}}>
  242. {this.prefAdvanced()}
  243. </TabPane>
  244. </Tabs>
  245. </div>
  246. )
  247. }
  248. render () {
  249. let {lang} = this.props
  250. return (
  251. <MyFrame
  252. show={this.state.show}
  253. title={lang.preferences}
  254. body={this.body()}
  255. onOK={() => this.onOK()}
  256. onCancel={() => this.onCancel()}
  257. cancel_title={lang.cancel}
  258. okText={lang.set_and_relaunch_app}
  259. lang={lang}
  260. />
  261. )
  262. }
  263. }