preferences.js 6.0 KB

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