PreferencesPrompt.jsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import R from 'ramda'
  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 = R.pick(pref_keys, this.state)
  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 className="inform">{lang.should_restart_after_change_language}</div>
  108. <div>
  109. <Select
  110. value={this.state.user_language || ''}
  111. onChange={v => this.updateLangKey(v)}
  112. style={{minWidth: 120}}
  113. >
  114. {this.getLanguageOptions()}
  115. </Select>
  116. </div>
  117. </div>
  118. )
  119. }
  120. prefChoiceMode () {
  121. let {lang} = this.props
  122. return (
  123. <div className="ln">
  124. <div>{lang.pref_choice_mode}</div>
  125. <div>
  126. <RadioGroup
  127. onChange={e => this.updateChoiceMode(e.target.value)}
  128. value={this.state.choice_mode}
  129. >
  130. <Radio value="single">{lang.pref_choice_mode_single}</Radio>
  131. <Radio value="multiple">{lang.pref_choice_mode_multiple}</Radio>
  132. </RadioGroup>
  133. </div>
  134. </div>
  135. )
  136. }
  137. prefAfterCmd () {
  138. let {lang} = this.props
  139. let options = {
  140. mode: 'shell'
  141. }
  142. return (
  143. <div className="ln">
  144. <div>{lang.pref_after_cmd}</div>
  145. <div>
  146. <div className="inform">{lang.pref_after_cmd_info}</div>
  147. {/*<Input*/}
  148. {/*type="textarea"*/}
  149. {/*rows={8}*/}
  150. {/*defaultValue={this.state.after_cmd}*/}
  151. {/*placeholder={lang.pref_after_cmd_placeholder}*/}
  152. {/*onChange={(e) => this.updateAfterCmd(e.target.value)}*/}
  153. {/*/>*/}
  154. <CodeMirror
  155. className="pref-cm"
  156. value={this.state.after_cmd}
  157. onChange={v => this.updateAfterCmd(v)}
  158. options={options}
  159. />
  160. </div>
  161. </div>
  162. )
  163. }
  164. prefAutoLaunch () {
  165. let {lang} = this.props
  166. return (
  167. <div className="ln">
  168. <Checkbox
  169. value={this.state.auto_launch}
  170. onChange={(e) => this.updateAutoLaunch(e.target.checked)}
  171. >
  172. {lang.auto_launch}
  173. </Checkbox>
  174. </div>
  175. )
  176. }
  177. prefMinimizeAtLaunch () {
  178. let {lang} = this.props
  179. return (
  180. <div className="ln">
  181. <Checkbox
  182. checked={this.state.hide_at_launch}
  183. onChange={(e) => this.updateMinimizeAtLaunch(e.target.checked)}
  184. >
  185. {lang.hide_at_launch}
  186. </Checkbox>
  187. </div>
  188. )
  189. }
  190. prefAdvanced () {
  191. let {lang} = this.props
  192. return (
  193. <div className="ln">
  194. <div>{lang.pref_tab_usage_data_title}</div>
  195. <div className="inform">{lang.pref_tab_usage_data_desc}</div>
  196. <div>
  197. <Checkbox
  198. checked={this.state.send_usage_data}
  199. onChange={(e) => this.setState({send_usage_data: e.target.checked})}
  200. >
  201. {lang.pref_tab_usage_data_label}
  202. </Checkbox>
  203. </div>
  204. </div>
  205. )
  206. }
  207. static openDownloadPage () {
  208. Agent.pact('openUrl', require('../../app/configs').url_download)
  209. }
  210. body () {
  211. let {lang} = this.props
  212. let height = 240
  213. return (
  214. <div ref="body">
  215. {/*<div className="title">{SH_Agent.lang.hosts_title}</div>*/}
  216. {/*<div className="cnt">*/}
  217. {/*</div>*/}
  218. <div
  219. className={classnames('current-version', {'update-found': this.state.update_found})}
  220. >
  221. <a
  222. href="#"
  223. onClick={PreferencesPrompt.openDownloadPage}
  224. >{formatVersion(current_version)}</a>
  225. </div>
  226. <Tabs
  227. defaultActiveKey="1"
  228. tabPosition="left"
  229. style={{minHeight: height}}
  230. >
  231. <TabPane tab={lang.pref_tab_general} key="1" style={{height}}>
  232. {this.prefLanguage()}
  233. {this.prefChoiceMode()}
  234. {/*{this.prefAutoLaunch()}*/}
  235. {this.prefMinimizeAtLaunch()}
  236. </TabPane>
  237. <TabPane tab={lang.pref_tab_custom_cmd} key="2" style={{height}}>
  238. {this.prefAfterCmd()}
  239. </TabPane>
  240. <TabPane tab={lang.pref_tab_advanced} key="3" style={{height}}>
  241. {this.prefAdvanced()}
  242. </TabPane>
  243. </Tabs>
  244. </div>
  245. )
  246. }
  247. render () {
  248. let {lang} = this.props
  249. return (
  250. <MyFrame
  251. show={this.state.show}
  252. title={lang.preferences}
  253. body={this.body()}
  254. onOK={() => this.onOK()}
  255. onCancel={() => this.onCancel()}
  256. cancel_title={lang.cancel}
  257. okText={lang.set_and_relaunch_app}
  258. lang={lang}
  259. />
  260. )
  261. }
  262. }