OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('options', function() { | 5 cr.define('options', function() { |
6 | 6 |
7 /** | 7 /** |
8 * The preference is a boolean. | |
9 * @type {string} | |
10 * @const | |
11 */ | |
12 var AUTO_REPEAT_ENABLED_PREF = 'settings.language.xkb_auto_repeat_enabled_r2'; | |
13 | |
14 /** | |
15 * The preference is an integer number of milliseconds to delay before auto- | |
16 * repeating begins. | |
17 * @type {string} | |
18 * @const | |
19 */ | |
20 var AUTO_REPEAT_DELAY_PREF = 'settings.language.xkb_auto_repeat_delay_r2'; | |
21 | |
22 /** | |
23 * The preference is the integer number of milliseconds between each repeat of | |
24 * an auto-repeating key. | |
25 * @type {string} | |
26 * @const | |
27 */ | |
28 var AUTO_REPEAT_INTERVAL_PREF = | |
29 'settings.language.xkb_auto_repeat_interval_r2'; | |
30 | |
31 /** | |
32 * Auto-repeat delays (in ms) for the corresponding slider values, from | |
33 * long to short. The values were chosen to provide a large range while giving | |
34 * several options near the defaults. | |
35 * @type {Array} | |
Dan Beam
2014/07/16 16:59:46
!Array.<number>
michaelpg
2014/07/18 21:33:06
Done.
| |
36 * @const | |
37 */ | |
38 var kAutoRepeatDelays = | |
Dan Beam
2014/07/16 16:59:46
kAutoRepeatDelays => AUTO_REPEAT_DELAYS
michaelpg
2014/07/18 21:33:05
Done.
| |
39 [2000, 1500, 1000, 500, 300, 200, 150]; | |
40 | |
41 /** | |
42 * Auto-repeat intervals (in ms) for the corresponding slider values, from | |
43 * long to short. The slider itself is labeled "rate", the inverse of | |
44 * interval, and goes from slow (long interval) to fast (short interval). | |
45 * @type {Array} | |
Dan Beam
2014/07/16 16:59:46
!Array.<number>
michaelpg
2014/07/18 21:33:06
Done.
| |
46 * @const | |
47 */ | |
48 var kAutoRepeatIntervals = | |
Dan Beam
2014/07/16 16:59:45
AUTO_REPEAT_INTERVALS
michaelpg
2014/07/18 21:33:06
Done.
| |
49 [2000, 1000, 500, 300, 200, 100, 50, 30, 20]; | |
50 | |
51 /** | |
8 * Encapsulated handling of the keyboard overlay. | 52 * Encapsulated handling of the keyboard overlay. |
9 * @constructor | 53 * @constructor |
10 */ | 54 */ |
11 function KeyboardOverlay() { | 55 function KeyboardOverlay() { |
12 options.SettingsDialog.call(this, 'keyboard-overlay', | 56 options.SettingsDialog.call(this, 'keyboard-overlay', |
13 loadTimeData.getString('keyboardOverlayTitle'), | 57 loadTimeData.getString('keyboardOverlayTitle'), |
14 'keyboard-overlay', | 58 'keyboard-overlay', |
15 $('keyboard-confirm'), $('keyboard-cancel')); | 59 $('keyboard-confirm'), $('keyboard-cancel')); |
16 } | 60 } |
17 | 61 |
18 cr.addSingletonGetter(KeyboardOverlay); | 62 cr.addSingletonGetter(KeyboardOverlay); |
19 | 63 |
20 KeyboardOverlay.prototype = { | 64 KeyboardOverlay.prototype = { |
21 __proto__: options.SettingsDialog.prototype, | 65 __proto__: options.SettingsDialog.prototype, |
22 | 66 |
23 /** | 67 /** |
68 * Keeps track of whether the user has changed the auto-repeat delay slider. | |
69 * Only update the pref if the slider is dirty, so we don't override a value | |
70 * that the slider doesn't support. | |
71 * @type {boolean} | |
72 * @private | |
73 */ | |
74 autoRepeatDelayChanged_: false, | |
75 | |
76 /** | |
77 * Keeps track of whether the user has changed the auto-repeat interval | |
78 * slider. See |autoRepeatDelayChanged_|. | |
79 * @type {boolean} | |
80 * @private | |
81 */ | |
82 autoRepeatIntervalChanged_: false, | |
83 | |
84 /** | |
24 * Initializes the page. This method is called in initialize. | 85 * Initializes the page. This method is called in initialize. |
25 */ | 86 */ |
26 initializePage: function() { | 87 initializePage: function() { |
27 options.SettingsDialog.prototype.initializePage.call(this); | 88 options.SettingsDialog.prototype.initializePage.call(this); |
28 | 89 |
90 $('auto-repeat-delay-range').max = kAutoRepeatDelays.length - 1; | |
91 $('auto-repeat-interval-range').max = kAutoRepeatIntervals.length - 1; | |
92 | |
93 $('auto-repeat-delay-range').onchange = | |
94 this.handleAutoRepeatDelaySliderChange_.bind(this); | |
95 $('auto-repeat-interval-range').onchange = | |
96 this.handleAutoRepeatIntervalSliderChange_.bind(this); | |
97 | |
29 $('languages-and-input-settings').onclick = function(e) { | 98 $('languages-and-input-settings').onclick = function(e) { |
30 OptionsPage.navigateToPage('languages'); | 99 OptionsPage.navigateToPage('languages'); |
31 chrome.send('coreOptionsUserMetricsAction', | 100 chrome.send('coreOptionsUserMetricsAction', |
32 ['Options_KeyboardShowLanguageSettings']); | 101 ['Options_KeyboardShowLanguageSettings']); |
33 }; | 102 }; |
34 | 103 |
35 $('keyboard-shortcuts').onclick = function(e) { | 104 $('keyboard-shortcuts').onclick = function(e) { |
36 chrome.send('showKeyboardShortcuts'); | 105 chrome.send('showKeyboardShortcuts'); |
37 chrome.send('coreOptionsUserMetricsAction', | 106 chrome.send('coreOptionsUserMetricsAction', |
38 ['Options_KeyboardShowKeyboardShortcuts']); | 107 ['Options_KeyboardShowKeyboardShortcuts']); |
39 }; | 108 }; |
109 | |
110 Preferences.getInstance().addEventListener( | |
111 AUTO_REPEAT_ENABLED_PREF, | |
112 this.handleAutoRepeatEnabledPrefChange_.bind(this)); | |
113 Preferences.getInstance().addEventListener( | |
114 AUTO_REPEAT_DELAY_PREF, | |
115 this.handleAutoRepeatDelayPrefChange_.bind(this)); | |
116 Preferences.getInstance().addEventListener( | |
117 AUTO_REPEAT_INTERVAL_PREF, | |
118 this.handleAutoRepeatIntervalPrefChange_.bind(this)); | |
40 }, | 119 }, |
41 | 120 |
42 /** | 121 /** |
122 * Saves the dialog prefs and sets the auto-repeat prefs. | |
123 * @override | |
124 */ | |
125 handleConfirm: function() { | |
126 options.SettingsDialog.prototype.handleConfirm.call(this); | |
Dan Beam
2014/07/16 16:59:45
nit: \n
michaelpg
2014/07/18 21:33:06
Removed code.
| |
127 if (!$('enable-auto-repeat').checked) | |
128 return; | |
Dan Beam
2014/07/16 16:59:45
nit: \n
michaelpg
2014/07/18 21:33:06
Removed code.
| |
129 if (this.autoRepeatDelayChanged_) { | |
Dan Beam
2014/07/16 16:59:45
can we just remove this.*Changed_ and send the pre
michaelpg
2014/07/18 21:33:05
No, but now we're using PrefRange which works the
| |
130 // Use the slider's position as the index in the array of delay values. | |
131 Preferences.setIntegerPref( | |
132 AUTO_REPEAT_DELAY_PREF, | |
133 kAutoRepeatDelays[$('auto-repeat-delay-range').value], | |
134 true, | |
135 'Options_KeyboardAutoRepeat_Delay'); | |
136 } | |
Dan Beam
2014/07/16 16:59:46
nit: \n
michaelpg
2014/07/18 21:33:05
Removed code.
| |
137 if (this.autoRepeatIntervalChanged_) { | |
138 Preferences.setIntegerPref( | |
139 AUTO_REPEAT_INTERVAL_PREF, | |
140 kAutoRepeatIntervals[$('auto-repeat-interval-range').value], | |
141 true, | |
142 'Options_KeyboardAutoRepeat_Interval'); | |
143 } | |
144 }, | |
145 | |
146 /** | |
147 * Handles auto-repeat enabled pref change. | |
148 * @param {Event} e Change event. | |
149 * @private | |
150 */ | |
151 handleAutoRepeatEnabledPrefChange_: function(e) { | |
152 var section = $('auto-repeat-settings-section'); | |
153 if (e.value.value) | |
154 section.classList.remove('disabled'); | |
155 else | |
156 section.classList.add('disabled'); | |
Dan Beam
2014/07/16 16:59:45
section.classList.toggle('disabled', e.value.value
michaelpg
2014/07/18 21:33:06
Done.
| |
157 | |
158 $('auto-repeat-delay-range').disabled = | |
159 $('auto-repeat-interval-range').disabled = !e.value.value; | |
160 }, | |
161 | |
162 /** | |
163 * Handles auto-repeat delay pref change. | |
164 * @param {Event} e Change event. | |
165 * @private | |
166 */ | |
167 handleAutoRepeatDelayPrefChange_: function(e) { | |
168 var index = this.indexOfClosestValue_(e.value.value, kAutoRepeatDelays); | |
169 if (index != -1) | |
Dan Beam
2014/07/16 16:59:46
can this be an assert() instead?
michaelpg
2014/07/18 21:33:05
Done.
| |
170 $('auto-repeat-delay-range').value = index; | |
171 }, | |
172 | |
173 /** | |
174 * Handles auto-repeat interval pref change. | |
175 * @param {Event} e Change event. | |
176 * @private | |
177 */ | |
178 handleAutoRepeatIntervalPrefChange_: function(e) { | |
179 var index = | |
180 this.indexOfClosestValue_(e.value.value, kAutoRepeatIntervals); | |
181 if (index != -1) | |
Dan Beam
2014/07/16 16:59:46
same
michaelpg
2014/07/18 21:33:06
Done.
| |
182 $('auto-repeat-interval-range').value = index; | |
183 }, | |
184 | |
185 /** | |
186 * Handles the auto-repeat delay slider's change event. | |
187 * @param {Event} e Change event. | |
188 * @private | |
189 */ | |
190 handleAutoRepeatDelaySliderChange_: function(e) { | |
191 this.autoRepeatDelayChanged_ = true; | |
192 }, | |
193 | |
194 /** | |
195 * Handles the auto-repeat interval slider's change event. | |
196 * @param {Event} e Change event. | |
197 * @private | |
198 */ | |
199 handleAutoRepeatIntervalSliderChange_: function(e) { | |
200 this.autoRepeatIntervalChanged_ = true; | |
201 }, | |
202 | |
203 /** | |
43 * Show/hide the caps lock remapping section. | 204 * Show/hide the caps lock remapping section. |
44 * @private | 205 * @private |
45 */ | 206 */ |
46 showCapsLockOptions_: function(show) { | 207 showCapsLockOptions_: function(show) { |
47 $('caps-lock-remapping-section').hidden = !show; | 208 $('caps-lock-remapping-section').hidden = !show; |
48 }, | 209 }, |
49 | 210 |
50 /** | 211 /** |
51 * Show/hide the diamond key remapping section. | 212 * Show/hide the diamond key remapping section. |
52 * @private | 213 * @private |
53 */ | 214 */ |
54 showDiamondKeyOptions_: function(show) { | 215 showDiamondKeyOptions_: function(show) { |
55 $('diamond-key-remapping-section').hidden = !show; | 216 $('diamond-key-remapping-section').hidden = !show; |
56 }, | 217 }, |
218 | |
219 /** | |
220 * Finds the closest value to |target| in |arr| and returns its index. | |
221 * @param {number} target The value to find. | |
Dan Beam
2014/07/16 16:59:45
nit: s/target/value
michaelpg
2014/07/18 21:33:05
Done.
| |
222 * @param {Array} arr The array to search. | |
Dan Beam
2014/07/16 16:59:45
nit: s/arr/values
Dan Beam
2014/07/16 16:59:45
!Array.<number>
michaelpg
2014/07/18 21:33:06
Done and done.
| |
223 * @return {number} The index of the closest value, or -1 if |arr| has no | |
224 * (finite) values. | |
225 * @private | |
226 */ | |
227 indexOfClosestValue_: function(target, arr) { | |
228 var index = arr.indexOf(target); | |
229 if (index != -1) | |
230 return index; | |
231 var closestValue = Infinity; | |
232 for (var i = 0; i < arr.length; i++) { | |
233 if (Math.abs(arr[i] - target) < | |
234 Math.abs(closestValue - target)) { | |
235 closestValue = arr[i]; | |
236 index = i; | |
237 } | |
238 } | |
239 return index; | |
240 }, | |
57 }; | 241 }; |
58 | 242 |
59 // Forward public APIs to private implementations. | 243 // Forward public APIs to private implementations. |
60 [ | 244 [ |
61 'showCapsLockOptions', | 245 'showCapsLockOptions', |
62 'showDiamondKeyOptions', | 246 'showDiamondKeyOptions', |
63 ].forEach(function(name) { | 247 ].forEach(function(name) { |
64 KeyboardOverlay[name] = function() { | 248 KeyboardOverlay[name] = function() { |
65 var instance = KeyboardOverlay.getInstance(); | 249 var instance = KeyboardOverlay.getInstance(); |
66 return instance[name + '_'].apply(instance, arguments); | 250 return instance[name + '_'].apply(instance, arguments); |
67 }; | 251 }; |
68 }); | 252 }); |
69 | 253 |
70 // Export | 254 // Export |
71 return { | 255 return { |
72 KeyboardOverlay: KeyboardOverlay | 256 KeyboardOverlay: KeyboardOverlay |
73 }; | 257 }; |
74 }); | 258 }); |
OLD | NEW |