Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: chrome/browser/resources/settings/device_page/display.js

Issue 2587913007: MD Settings: cr-slider: Make display consistent and clean up. (Closed)
Patch Set: Update tests Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 /** 5 /**
6 * @fileoverview 6 * @fileoverview
7 * 'settings-display' is the settings subpage for display settings. 7 * 'settings-display' is the settings subpage for display settings.
8 */ 8 */
9 9
10 cr.define('settings.display', function() { 10 cr.define('settings.display', function() {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 46
47 /** @type {!chrome.system.display.DisplayUnitInfo|undefined} */ 47 /** @type {!chrome.system.display.DisplayUnitInfo|undefined} */
48 selectedDisplay: {type: Object, observer: 'selectedDisplayChanged_'}, 48 selectedDisplay: {type: Object, observer: 'selectedDisplayChanged_'},
49 49
50 /** Id passed to the overscan dialog. */ 50 /** Id passed to the overscan dialog. */
51 overscanDisplayId: { 51 overscanDisplayId: {
52 type: String, 52 type: String,
53 notify: true, 53 notify: true,
54 }, 54 },
55 55
56 /** Maximum mode index value for slider. */ 56 /** @private {!Array<number>} Mode index values for slider. */
57 maxModeIndex_: {type: Number, value: 0}, 57 modeValues_: Array,
58 58
59 /** Selected mode index value for slider. */ 59 /** @private Selected mode index value for slider. */
60 selectedModeIndex_: {type: Number}, 60 selectedModeIndex_: Number,
61 },
61 62
62 /** Immediate selected mode index value for slider. */ 63 /** @private {number} Selected mode index received from chrome. */
63 immediateSelectedModeIndex_: {type: Number, value: 0} 64 currentSelectedModeIndex_: -1,
64 },
65 65
66 /** 66 /**
67 * Listener for chrome.system.display.onDisplayChanged events. 67 * Listener for chrome.system.display.onDisplayChanged events.
68 * @type {function(void)|undefined} 68 * @type {function(void)|undefined}
69 * @private 69 * @private
70 */ 70 */
71 displayChangedListener_: undefined, 71 displayChangedListener_: undefined,
72 72
73 /** @override */ 73 /** @override */
74 attached: function() { 74 attached: function() {
75 this.displayChangedListener_ = this.getDisplayInfo_.bind(this); 75 this.displayChangedListener_ = this.getDisplayInfo_.bind(this);
76 settings.display.systemDisplayApi.onDisplayChanged.addListener( 76 settings.display.systemDisplayApi.onDisplayChanged.addListener(
77 this.displayChangedListener_); 77 this.displayChangedListener_);
78 this.getDisplayInfo_(); 78 this.getDisplayInfo_();
79 }, 79 },
80 80
81 /** @override */ 81 /** @override */
82 detached: function() { 82 detached: function() {
83 if (this.displayChangedListener_) { 83 if (this.displayChangedListener_) {
84 settings.display.systemDisplayApi.onDisplayChanged.removeListener( 84 settings.display.systemDisplayApi.onDisplayChanged.removeListener(
85 this.displayChangedListener_); 85 this.displayChangedListener_);
86 } 86 }
87 this.currentSelectedModeIndex_ = -1;
87 }, 88 },
88 89
89 /** 90 /**
90 * Shows or hides the overscan dialog. 91 * Shows or hides the overscan dialog.
91 * @param {boolean} showOverscan 92 * @param {boolean} showOverscan
92 * @private 93 * @private
93 */ 94 */
94 showOverscanDialog_: function(showOverscan) { 95 showOverscanDialog_: function(showOverscan) {
95 if (showOverscan) { 96 if (showOverscan) {
96 this.$.displayOverscan.open(); 97 this.$.displayOverscan.open();
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 getSelectedModeIndex_: function(selectedDisplay) { 144 getSelectedModeIndex_: function(selectedDisplay) {
144 for (var i = 0; i < selectedDisplay.modes.length; ++i) { 145 for (var i = 0; i < selectedDisplay.modes.length; ++i) {
145 if (selectedDisplay.modes[i].isSelected) 146 if (selectedDisplay.modes[i].isSelected)
146 return i; 147 return i;
147 } 148 }
148 return 0; 149 return 0;
149 }, 150 },
150 151
151 /** @private */ 152 /** @private */
152 selectedDisplayChanged_: function() { 153 selectedDisplayChanged_: function() {
153 // Set maxModeIndex first so that the slider updates correctly. 154 // Set |modeValues_| before |selectedModeIndex_| so that the slider updates
154 if (this.selectedDisplay.modes.length == 0) { 155 // correctly.
155 this.maxModeIndex_ = 0; 156 let numModes = this.selectedDisplay.modes.length;
157 if (numModes == 0) {
158 this.modeValues_ = [];
156 this.selectedModeIndex_ = 0; 159 this.selectedModeIndex_ = 0;
160 this.currentSelectedModeIndex_ = 0;
157 return; 161 return;
158 } 162 }
159 this.maxModeIndex_ = this.selectedDisplay.modes.length - 1; 163 this.modeValues_ = Array.from(Array(numModes).keys());
160 this.selectedModeIndex_ = this.getSelectedModeIndex_(this.selectedDisplay); 164 this.selectedModeIndex_ = this.getSelectedModeIndex_(this.selectedDisplay);
161 this.immediateSelectedModeIndex_ = this.selectedModeIndex_; 165 this.currentSelectedModeIndex_ = this.selectedModeIndex_;
162 }, 166 },
163 167
164 /** 168 /**
165 * @param {!Array<!chrome.system.display.DisplayUnitInfo>} displays 169 * @param {!Array<!chrome.system.display.DisplayUnitInfo>} displays
166 * @return {boolean} 170 * @return {boolean}
167 * @private 171 * @private
168 */ 172 */
169 showDisplayTabMenu_: function(displays) { 173 showDisplayTabMenu_: function(displays) {
170 return displays.length > 1; 174 return displays.length > 1;
171 }, 175 },
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 */ 245 */
242 enableSetResolution_: function(selectedDisplay) { 246 enableSetResolution_: function(selectedDisplay) {
243 return selectedDisplay.modes.length > 1; 247 return selectedDisplay.modes.length > 1;
244 }, 248 },
245 249
246 /** 250 /**
247 * @return {string} 251 * @return {string}
248 * @private 252 * @private
249 */ 253 */
250 getResolutionText_: function() { 254 getResolutionText_: function() {
251 if (this.selectedDisplay.modes.length == 0) { 255 if (this.selectedDisplay.modes.length == 0 ||
252 var widthStr = this.selectedDisplay.bounds.width.toString(); 256 this.currentSelectedModeIndex_ == -1) {
253 var heightStr = this.selectedDisplay.bounds.height.toString(); 257 // If currentSelectedModeIndex_ == -1, selectedDisplay and
258 // selectedModeIndex_ are not in sync.
259 let widthStr = this.selectedDisplay.bounds.width.toString();
260 let heightStr = this.selectedDisplay.bounds.height.toString();
254 return this.i18n('displayResolutionText', widthStr, heightStr); 261 return this.i18n('displayResolutionText', widthStr, heightStr);
255 } 262 }
256 // immediateSelectedModeIndex_ is bound to paper-slider.immediate-value 263 let mode = this.selectedDisplay.modes[this.selectedModeIndex_];
257 // which may not be valid, or may not have updated yet when this is called. 264 let best =
258 if (isNaN(this.immediateSelectedModeIndex_) ||
259 this.immediateSelectedModeIndex_ >= this.selectedDisplay.modes.length) {
260 this.immediateSelectedModeIndex_ =
261 this.getSelectedModeIndex_(this.selectedDisplay);
262 }
263 var mode = this.selectedDisplay.modes[this.immediateSelectedModeIndex_];
264 var best =
265 this.selectedDisplay.isInternal ? mode.uiScale == 1.0 : mode.isNative; 265 this.selectedDisplay.isInternal ? mode.uiScale == 1.0 : mode.isNative;
266 var widthStr = mode.width.toString(); 266 let widthStr = mode.width.toString();
267 var heightStr = mode.height.toString(); 267 let heightStr = mode.height.toString();
268 if (best) 268 if (best)
269 return this.i18n('displayResolutionTextBest', widthStr, heightStr); 269 return this.i18n('displayResolutionTextBest', widthStr, heightStr);
270 else if (mode.isNative) 270 else if (mode.isNative)
271 return this.i18n('displayResolutionTextNative', widthStr, heightStr); 271 return this.i18n('displayResolutionTextNative', widthStr, heightStr);
272 return this.i18n('displayResolutionText', widthStr, heightStr); 272 return this.i18n('displayResolutionText', widthStr, heightStr);
273 }, 273 },
274 274
275 /** 275 /**
276 * @param {!{detail: string}} e |e.detail| is the id of the selected display. 276 * @param {!{detail: string}} e |e.detail| is the id of the selected display.
277 * @private 277 * @private
278 */ 278 */
279 onSelectDisplay_: function(e) { 279 onSelectDisplay_: function(e) {
280 var id = e.detail; 280 var id = e.detail;
281 for (let display of this.displays) { 281 for (let display of this.displays) {
282 if (id != display.id) 282 if (id != display.id)
283 continue; 283 continue;
284 this.currentSelectedModeIndex_ = -1;
284 this.selectedDisplay = display; 285 this.selectedDisplay = display;
285 } 286 }
286 }, 287 },
287 288
288 /** 289 /**
289 * Handles event when a display tab is selected. 290 * Handles event when a display tab is selected.
290 * @param {!{detail: !{item: !{displayId: string}}}} e 291 * @param {!{detail: !{item: !{displayId: string}}}} e
291 * @private 292 * @private
292 */ 293 */
293 onSelectDisplayTab_: function(e) { 294 onSelectDisplayTab_: function(e) {
(...skipping 16 matching lines...) Expand all
310 311
311 /** @type {!chrome.system.display.DisplayProperties} */ var properties = { 312 /** @type {!chrome.system.display.DisplayProperties} */ var properties = {
312 isPrimary: true 313 isPrimary: true
313 }; 314 };
314 settings.display.systemDisplayApi.setDisplayProperties( 315 settings.display.systemDisplayApi.setDisplayProperties(
315 this.selectedDisplay.id, properties, 316 this.selectedDisplay.id, properties,
316 this.setPropertiesCallback_.bind(this)); 317 this.setPropertiesCallback_.bind(this));
317 }, 318 },
318 319
319 /** 320 /**
320 * @param {!{target: !PaperSliderElement}} e 321 * Triggered when the 'change' event for the selected mode slider is
322 * triggered. This only occurs when the value is comitted (i.e. not while
323 * the slider is being dragged).
321 * @private 324 * @private
322 */ 325 */
323 onChangeMode_: function(e) { 326 onSelectedModeChange_: function() {
324 var curIndex = this.selectedModeIndex_; 327 if (this.currentSelectedModeIndex_ == -1 ||
325 var newIndex = parseInt(e.target.value, 10); 328 this.currentSelectedModeIndex_ == this.selectedModeIndex_) {
326 if (newIndex == curIndex) 329 // Don't change the selected display mode until we have received an update
330 // from Chrome and the mode differs from the current mode.
327 return; 331 return;
328 assert(newIndex >= 0); 332 }
329 assert(newIndex < this.selectedDisplay.modes.length);
330 /** @type {!chrome.system.display.DisplayProperties} */ var properties = { 333 /** @type {!chrome.system.display.DisplayProperties} */ var properties = {
331 displayMode: this.selectedDisplay.modes[newIndex] 334 displayMode: this.selectedDisplay.modes[this.selectedModeIndex_]
332 }; 335 };
333 settings.display.systemDisplayApi.setDisplayProperties( 336 settings.display.systemDisplayApi.setDisplayProperties(
334 this.selectedDisplay.id, properties, 337 this.selectedDisplay.id, properties,
335 this.setPropertiesCallback_.bind(this)); 338 this.setPropertiesCallback_.bind(this));
336 }, 339 },
337 340
338 /** 341 /**
339 * @param {!Event} event 342 * @param {!Event} event
340 * @private 343 * @private
341 */ 344 */
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 displayIds += display.id; 394 displayIds += display.id;
392 if (display.isPrimary && !primaryDisplay) 395 if (display.isPrimary && !primaryDisplay)
393 primaryDisplay = display; 396 primaryDisplay = display;
394 if (this.selectedDisplay && display.id == this.selectedDisplay.id) 397 if (this.selectedDisplay && display.id == this.selectedDisplay.id)
395 selectedDisplay = display; 398 selectedDisplay = display;
396 } 399 }
397 this.displayIds = displayIds; 400 this.displayIds = displayIds;
398 this.primaryDisplayId = (primaryDisplay && primaryDisplay.id) || ''; 401 this.primaryDisplayId = (primaryDisplay && primaryDisplay.id) || '';
399 this.selectedDisplay = selectedDisplay || primaryDisplay || 402 this.selectedDisplay = selectedDisplay || primaryDisplay ||
400 (this.displays && this.displays[0]); 403 (this.displays && this.displays[0]);
404 // Save the selected mode index received from Chrome so that we do not
405 // send an unnecessary setDisplayProperties call (which would log an error).
406 this.currentSelectedModeIndex_ =
407 this.getSelectedModeIndex_(this.selectedDisplay);
408
401 this.$.displayLayout.updateDisplays(this.displays, this.layouts); 409 this.$.displayLayout.updateDisplays(this.displays, this.layouts);
402 }, 410 },
403 411
404 /** @private */ 412 /** @private */
405 setPropertiesCallback_: function() { 413 setPropertiesCallback_: function() {
406 if (chrome.runtime.lastError) { 414 if (chrome.runtime.lastError) {
407 console.error( 415 console.error(
408 'setDisplayProperties Error: ' + chrome.runtime.lastError.message); 416 'setDisplayProperties Error: ' + chrome.runtime.lastError.message);
409 } 417 }
410 }, 418 },
411 }); 419 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698