preferences.jsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import { Checkbox, Input, Radio, Select } from 'antd'
  8. import MyFrame from './frame'
  9. import classnames from 'classnames'
  10. import Agent from '../Agent'
  11. import { version as current_version } from '../../app/version'
  12. import formatVersion from '../../app/libs/formatVersion'
  13. import './preferences.less'
  14. const RadioGroup = Radio.Group
  15. const Option = Select.Option
  16. const pref_keys = ['after_cmd', 'auto_launch', 'choice_mode', 'hide_at_launch', 'is_dock_icon_hidden', 'user_language']
  17. export default class PreferencesPrompt extends React.Component {
  18. constructor (props) {
  19. super(props)
  20. this.state = {
  21. show: false,
  22. user_language: '',
  23. after_cmd: '',
  24. choice_mode: 'multiple',
  25. auto_launch: false,
  26. hide_at_launch: false,
  27. lang_list: [],
  28. update_found: false // 发现新版本
  29. }
  30. Agent.pact('getLangList')
  31. .then(lang_list => this.setState({lang_list}))
  32. }
  33. componentDidMount () {
  34. Agent.on('show_preferences', () => {
  35. Agent.pact('getPref')
  36. .then(pref => {
  37. this.setState(Object.assign({}, pref, {show: true}))
  38. console.log(pref)
  39. })
  40. })
  41. Agent.on('update_found', (v) => {
  42. console.log(v)
  43. this.setState({
  44. update_found: true
  45. })
  46. })
  47. }
  48. onOK () {
  49. this.setState({
  50. show: false
  51. }, () => {
  52. let prefs = {}
  53. let d = this.state
  54. pref_keys.map(k => {
  55. if (d.hasOwnProperty(k)) {
  56. prefs[k] = d[k]
  57. }
  58. })
  59. Agent.pact('setPref', prefs)
  60. .then(() => {
  61. setTimeout(() => {
  62. Agent.pact('relaunch')
  63. }, 200)
  64. })
  65. })
  66. }
  67. onCancel () {
  68. this.setState({
  69. show: false
  70. })
  71. }
  72. getLanguageOptions () {
  73. return this.state.lang_list.map(({key, name}, idx) => {
  74. return (
  75. <Option value={key} key={idx}>{name}</Option>
  76. )
  77. })
  78. }
  79. updateLangKey (v) {
  80. this.setState({user_language: v})
  81. }
  82. updateChoiceMode (v) {
  83. this.setState({
  84. choice_mode: v
  85. })
  86. }
  87. updateAfterCmd (v) {
  88. this.setState({
  89. after_cmd: v
  90. })
  91. }
  92. updateAutoLaunch (v) {
  93. this.setState({
  94. auto_launch: v
  95. })
  96. // todo set auto launch
  97. }
  98. updateMinimizeAtLaunch (v) {
  99. this.setState({
  100. hide_at_launch: v
  101. })
  102. }
  103. prefLanguage () {
  104. let {lang} = this.props
  105. return (
  106. <div className="ln">
  107. <div className="title">{lang.language}</div>
  108. <div className="cnt">
  109. <Select
  110. value={this.state.user_language || ''}
  111. onChange={v => this.updateLangKey(v)}
  112. >
  113. {this.getLanguageOptions()}
  114. </Select>
  115. <div className="inform">{lang.should_restart_after_change_language}</div>
  116. </div>
  117. </div>
  118. )
  119. }
  120. prefChoiceMode () {
  121. let {lang} = this.props
  122. return (
  123. <div className="ln">
  124. <div className="title">{lang.pref_choice_mode}</div>
  125. <div className="cnt">
  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. return (
  140. <div className="ln">
  141. <div className="title">{lang.pref_after_cmd}</div>
  142. <div className="cnt">
  143. <div className="inform">{lang.pref_after_cmd_info}</div>
  144. <Input
  145. type="textarea"
  146. rows={4}
  147. defaultValue={this.state.after_cmd}
  148. placeholder={lang.pref_after_cmd_placeholder}
  149. onChange={(e) => this.updateAfterCmd(e.target.value)}
  150. />
  151. </div>
  152. </div>
  153. )
  154. }
  155. prefAutoLaunch () {
  156. let {lang} = this.props
  157. return (
  158. <div className="ln">
  159. <div className="title">{lang.auto_launch}</div>
  160. <div className="cnt">
  161. <Checkbox
  162. value={this.state.auto_launch}
  163. onChange={(e) => this.updateAutoLaunch(e.target.checked)}
  164. />
  165. </div>
  166. </div>
  167. )
  168. }
  169. prefMinimizeAtLaunch () {
  170. let {lang} = this.props
  171. return (
  172. <div className="ln">
  173. <div className="title">{lang.hide_at_launch}</div>
  174. <div className="cnt">
  175. <Checkbox
  176. checked={this.state.hide_at_launch}
  177. onChange={(e) => this.updateMinimizeAtLaunch(e.target.checked)}
  178. />
  179. </div>
  180. </div>
  181. )
  182. }
  183. static openDownloadPage () {
  184. Agent.pact('openUrl', require('../../app/configs').url_download)
  185. }
  186. body () {
  187. return (
  188. <div ref="body">
  189. {/*<div className="title">{SH_Agent.lang.hosts_title}</div>*/}
  190. {/*<div className="cnt">*/}
  191. {/*</div>*/}
  192. <div
  193. className={classnames('current-version', {'update-found': this.state.update_found})}>
  194. <a href="#"
  195. onClick={PreferencesPrompt.openDownloadPage}>{formatVersion(current_version)}</a>
  196. </div>
  197. {this.prefLanguage()}
  198. {this.prefChoiceMode()}
  199. {this.prefAfterCmd()}
  200. {/*{this.prefAutoLaunch()}*/}
  201. {this.prefMinimizeAtLaunch()}
  202. </div>
  203. )
  204. }
  205. render () {
  206. let {lang} = this.props
  207. return (
  208. <MyFrame
  209. show={this.state.show}
  210. title={lang.preferences}
  211. body={this.body()}
  212. onOK={() => this.onOK()}
  213. onCancel={() => this.onCancel()}
  214. cancel_title={lang.cancel}
  215. okText={lang.set_and_relaunch_app}
  216. lang={lang}
  217. />
  218. )
  219. }
  220. }