preferences.js 7.1 KB

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