blob: cbd4285f9ee767cf63a59a86464ac83bf6bd5c10 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4/**
5 * @unrestricted
6 */
7Emulation.DeviceModeToolbar = class {
8 /**
9 * @param {!Emulation.DeviceModeModel} model
10 * @param {!Common.Setting} showMediaInspectorSetting
11 * @param {!Common.Setting} showRulersSetting
12 */
13 constructor(model, showMediaInspectorSetting, showRulersSetting) {
14 this._model = model;
15 this._showMediaInspectorSetting = showMediaInspectorSetting;
16 this._showRulersSetting = showRulersSetting;
17
18 this._deviceOutlineSetting = this._model.deviceOutlineSetting();
19 this._showDeviceScaleFactorSetting = Common.settings.createSetting('emulation.showDeviceScaleFactor', false);
20 this._showDeviceScaleFactorSetting.addChangeListener(this._updateDeviceScaleFactorVisibility, this);
21
22 this._showUserAgentTypeSetting = Common.settings.createSetting('emulation.showUserAgentType', false);
23 this._showUserAgentTypeSetting.addChangeListener(this._updateUserAgentTypeVisibility, this);
24
25 this._autoAdjustScaleSetting = Common.settings.createSetting('emulation.autoAdjustScale', true);
26
27 /** @type {!Map<!Emulation.EmulatedDevice, !Emulation.EmulatedDevice.Mode>} */
28 this._lastMode = new Map();
29
30 this._element = createElementWithClass('div', 'device-mode-toolbar');
31
32 const leftContainer = this._element.createChild('div', 'device-mode-toolbar-spacer');
33 leftContainer.createChild('div', 'device-mode-toolbar-spacer');
34 const leftToolbar = new UI.Toolbar('', leftContainer);
35 leftToolbar.makeWrappable();
36 this._fillLeftToolbar(leftToolbar);
37
38 const mainToolbar = new UI.Toolbar('', this._element);
39 mainToolbar.makeWrappable();
40 this._fillMainToolbar(mainToolbar);
41
42 const rightContainer = this._element.createChild('div', 'device-mode-toolbar-spacer');
43 const rightToolbar = new UI.Toolbar('device-mode-toolbar-fixed-size', rightContainer);
44 rightToolbar.makeWrappable();
45 this._fillRightToolbar(rightToolbar);
46 const modeToolbar = new UI.Toolbar('device-mode-toolbar-fixed-size', rightContainer);
47 modeToolbar.makeWrappable();
48 this._fillModeToolbar(modeToolbar);
49 rightContainer.createChild('div', 'device-mode-toolbar-spacer');
50 const optionsToolbar = new UI.Toolbar('device-mode-toolbar-options', rightContainer);
51 optionsToolbar.makeWrappable(true);
52 this._fillOptionsToolbar(optionsToolbar);
53
54 this._emulatedDevicesList = Emulation.EmulatedDevicesList.instance();
55 this._emulatedDevicesList.addEventListener(
56 Emulation.EmulatedDevicesList.Events.CustomDevicesUpdated, this._deviceListChanged, this);
57 this._emulatedDevicesList.addEventListener(
58 Emulation.EmulatedDevicesList.Events.StandardDevicesUpdated, this._deviceListChanged, this);
59
60 this._persistenceSetting =
61 Common.settings.createSetting('emulation.deviceModeValue', {device: '', orientation: '', mode: ''});
62
63 this._model.toolbarControlsEnabledSetting().addChangeListener(updateToolbarsEnabled);
64 updateToolbarsEnabled();
65
66 function updateToolbarsEnabled() {
67 const enabled = model.toolbarControlsEnabledSetting().get();
68 leftToolbar.setEnabled(enabled);
69 mainToolbar.setEnabled(enabled);
70 rightToolbar.setEnabled(enabled);
71 modeToolbar.setEnabled(enabled);
72 optionsToolbar.setEnabled(enabled);
73 }
74 }
75
76 /**
77 * @param {!UI.Toolbar} toolbar
78 */
79 _fillLeftToolbar(toolbar) {
80 toolbar.appendToolbarItem(
81 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-toolbar-element')));
82 this._deviceSelectItem = new UI.ToolbarMenuButton(this._appendDeviceMenuItems.bind(this));
83 this._deviceSelectItem.setGlyph('');
84 this._deviceSelectItem.turnIntoSelect(95);
85 this._deviceSelectItem.setDarkText();
86 toolbar.appendToolbarItem(this._deviceSelectItem);
87 }
88
89 /**
90 * @param {!UI.Toolbar} toolbar
91 */
92 _fillMainToolbar(toolbar) {
93 const widthInput = UI.createInput('device-mode-size-input', 'text');
94 widthInput.maxLength = 4;
95 widthInput.title = Common.UIString('Width');
96 this._updateWidthInput =
97 UI.bindInput(widthInput, this._applyWidth.bind(this), Emulation.DeviceModeModel.deviceSizeValidator, true);
98 this._widthInput = widthInput;
99 this._widthItem = this._wrapToolbarItem(widthInput);
100 toolbar.appendToolbarItem(this._widthItem);
101
102 const xElement = createElementWithClass('div', 'device-mode-x');
103 xElement.textContent = '\u00D7';
104 this._xItem = this._wrapToolbarItem(xElement);
105 toolbar.appendToolbarItem(this._xItem);
106
107 const heightInput = UI.createInput('device-mode-size-input', 'text');
108 heightInput.maxLength = 4;
109 heightInput.title = Common.UIString('Height (leave empty for full)');
110 this._updateHeightInput = UI.bindInput(heightInput, this._applyHeight.bind(this), validateHeight, true);
111 this._heightInput = heightInput;
112 this._heightItem = this._wrapToolbarItem(heightInput);
113 toolbar.appendToolbarItem(this._heightItem);
114
115 /**
116 * @param {string} value
117 * @return {boolean}
118 */
119 function validateHeight(value) {
120 return !value || Emulation.DeviceModeModel.deviceSizeValidator(value);
121 }
122 }
123
124 /**
125 * @param {string} value
126 */
127 _applyWidth(value) {
128 const width = value ? Number(value) : 0;
129 this._model.setWidthAndScaleToFit(width);
130 }
131
132 /**
133 * @param {string} value
134 */
135 _applyHeight(value) {
136 const height = value ? Number(value) : 0;
137 this._model.setHeightAndScaleToFit(height);
138 }
139
140 /**
141 * @param {!UI.Toolbar} toolbar
142 */
143 _fillRightToolbar(toolbar) {
144 toolbar.appendToolbarItem(
145 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-toolbar-element')));
146 this._scaleItem = new UI.ToolbarMenuButton(this._appendScaleMenuItems.bind(this));
147 this._scaleItem.setTitle(Common.UIString('Zoom'));
148 this._scaleItem.setGlyph('');
149 this._scaleItem.turnIntoSelect();
150 this._scaleItem.setDarkText();
151 toolbar.appendToolbarItem(this._scaleItem);
152
153 toolbar.appendToolbarItem(
154 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-toolbar-element')));
155 this._deviceScaleItem = new UI.ToolbarMenuButton(this._appendDeviceScaleMenuItems.bind(this));
156 this._deviceScaleItem.setVisible(this._showDeviceScaleFactorSetting.get());
157 this._deviceScaleItem.setTitle(Common.UIString('Device pixel ratio'));
158 this._deviceScaleItem.setGlyph('');
159 this._deviceScaleItem.turnIntoSelect();
160 this._deviceScaleItem.setDarkText();
161 toolbar.appendToolbarItem(this._deviceScaleItem);
162
163 toolbar.appendToolbarItem(
164 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-toolbar-element')));
165 this._uaItem = new UI.ToolbarMenuButton(this._appendUserAgentMenuItems.bind(this));
166 this._uaItem.setVisible(this._showUserAgentTypeSetting.get());
167 this._uaItem.setTitle(Common.UIString('Device type'));
168 this._uaItem.setGlyph('');
169 this._uaItem.turnIntoSelect();
170 this._uaItem.setDarkText();
171 toolbar.appendToolbarItem(this._uaItem);
172
173 this._throttlingConditionsItem = MobileThrottling.throttlingManager().createMobileThrottlingButton();
174 toolbar.appendToolbarItem(this._throttlingConditionsItem);
175 }
176
177 /**
178 * @param {!UI.Toolbar} toolbar
179 */
180 _fillModeToolbar(toolbar) {
181 toolbar.appendToolbarItem(
182 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-toolbar-element')));
183 this._modeButton = new UI.ToolbarButton('', 'largeicon-rotate-screen');
184 this._modeButton.addEventListener(UI.ToolbarButton.Events.Click, this._modeMenuClicked, this);
185 toolbar.appendToolbarItem(this._modeButton);
186 }
187
188 /**
189 * @param {!UI.Toolbar} toolbar
190 */
191 _fillOptionsToolbar(toolbar) {
192 const moreOptionsButton = new UI.ToolbarMenuButton(this._appendOptionsMenuItems.bind(this));
193 moreOptionsButton.setTitle(Common.UIString('More options'));
194 toolbar.appendToolbarItem(moreOptionsButton);
195
196 toolbar.appendToolbarItem(
197 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-toolbar-element')));
198 }
199
200 /**
201 * @param {!UI.ContextMenu} contextMenu
202 */
203 _appendScaleMenuItems(contextMenu) {
204 if (this._model.type() === Emulation.DeviceModeModel.Type.Device) {
205 contextMenu.footerSection().appendItem(
206 Common.UIString('Fit to window (%.0f%%)', this._model.fitScale() * 100),
207 this._onScaleMenuChanged.bind(this, this._model.fitScale()), false);
208 }
209 contextMenu.footerSection().appendCheckboxItem(
210 ls`Auto-adjust zoom`, this._onAutoAdjustScaleChanged.bind(this), this._autoAdjustScaleSetting.get());
211 const boundAppendScaleItem = appendScaleItem.bind(this);
212 boundAppendScaleItem(Common.UIString('50%'), 0.5);
213 boundAppendScaleItem(Common.UIString('75%'), 0.75);
214 boundAppendScaleItem(Common.UIString('100%'), 1);
215 boundAppendScaleItem(Common.UIString('125%'), 1.25);
216 boundAppendScaleItem(Common.UIString('150%'), 1.5);
217
218 /**
219 * @param {string} title
220 * @param {number} value
221 * @this {!Emulation.DeviceModeToolbar}
222 */
223 function appendScaleItem(title, value) {
224 contextMenu.defaultSection().appendCheckboxItem(
225 title, this._onScaleMenuChanged.bind(this, value), this._model.scaleSetting().get() === value, false);
226 }
227 }
228
229 /**
230 * @param {number} value
231 */
232 _onScaleMenuChanged(value) {
233 this._model.scaleSetting().set(value);
234 }
235
236 _onAutoAdjustScaleChanged() {
237 this._autoAdjustScaleSetting.set(!this._autoAdjustScaleSetting.get());
238 }
239
240 /**
241 * @param {!UI.ContextMenu} contextMenu
242 */
243 _appendDeviceScaleMenuItems(contextMenu) {
244 const deviceScaleFactorSetting = this._model.deviceScaleFactorSetting();
245 const defaultValue = this._model.uaSetting().get() === Emulation.DeviceModeModel.UA.Mobile ||
246 this._model.uaSetting().get() === Emulation.DeviceModeModel.UA.MobileNoTouch ?
247 Emulation.DeviceModeModel.defaultMobileScaleFactor :
248 window.devicePixelRatio;
249 appendDeviceScaleFactorItem(contextMenu.headerSection(), Common.UIString('Default: %.1f', defaultValue), 0);
250 appendDeviceScaleFactorItem(contextMenu.defaultSection(), Common.UIString('1'), 1);
251 appendDeviceScaleFactorItem(contextMenu.defaultSection(), Common.UIString('2'), 2);
252 appendDeviceScaleFactorItem(contextMenu.defaultSection(), Common.UIString('3'), 3);
253
254 /**
255 * @param {!UI.ContextMenuSection} section
256 * @param {string} title
257 * @param {number} value
258 */
259 function appendDeviceScaleFactorItem(section, title, value) {
260 section.appendCheckboxItem(
261 title, deviceScaleFactorSetting.set.bind(deviceScaleFactorSetting, value),
262 deviceScaleFactorSetting.get() === value);
263 }
264 }
265
266 /**
267 * @param {!UI.ContextMenu} contextMenu
268 */
269 _appendUserAgentMenuItems(contextMenu) {
270 const uaSetting = this._model.uaSetting();
271 appendUAItem(Emulation.DeviceModeModel.UA.Mobile, Emulation.DeviceModeModel.UA.Mobile);
272 appendUAItem(Emulation.DeviceModeModel.UA.MobileNoTouch, Emulation.DeviceModeModel.UA.MobileNoTouch);
273 appendUAItem(Emulation.DeviceModeModel.UA.Desktop, Emulation.DeviceModeModel.UA.Desktop);
274 appendUAItem(Emulation.DeviceModeModel.UA.DesktopTouch, Emulation.DeviceModeModel.UA.DesktopTouch);
275
276 /**
277 * @param {string} title
278 * @param {!Emulation.DeviceModeModel.UA} value
279 */
280 function appendUAItem(title, value) {
281 contextMenu.defaultSection().appendCheckboxItem(
282 title, uaSetting.set.bind(uaSetting, value), uaSetting.get() === value);
283 }
284 }
285
286 /**
287 * @param {!UI.ContextMenu} contextMenu
288 */
289 _appendOptionsMenuItems(contextMenu) {
290 const model = this._model;
291 appendToggleItem(
292 contextMenu.headerSection(), this._deviceOutlineSetting, Common.UIString('Hide device frame'),
293 Common.UIString('Show device frame'), model.type() !== Emulation.DeviceModeModel.Type.Device);
294 appendToggleItem(
295 contextMenu.headerSection(), this._showMediaInspectorSetting, Common.UIString('Hide media queries'),
296 Common.UIString('Show media queries'));
297 appendToggleItem(
298 contextMenu.headerSection(), this._showRulersSetting, Common.UIString('Hide rulers'),
299 Common.UIString('Show rulers'));
300 appendToggleItem(
301 contextMenu.defaultSection(), this._showDeviceScaleFactorSetting, Common.UIString('Remove device pixel ratio'),
302 Common.UIString('Add device pixel ratio'));
303 appendToggleItem(
304 contextMenu.defaultSection(), this._showUserAgentTypeSetting, Common.UIString('Remove device type'),
305 Common.UIString('Add device type'));
306 contextMenu.appendItemsAtLocation('deviceModeMenu');
307 contextMenu.footerSection().appendItem(Common.UIString('Reset to defaults'), this._reset.bind(this));
308
309 /**
310 * @param {!UI.ContextMenuSection} section
311 * @param {!Common.Setting} setting
312 * @param {string} title1
313 * @param {string} title2
314 * @param {boolean=} disabled
315 */
316 function appendToggleItem(section, setting, title1, title2, disabled) {
317 if (typeof disabled === 'undefined')
318 disabled = model.type() === Emulation.DeviceModeModel.Type.None;
319 section.appendItem(setting.get() ? title1 : title2, setting.set.bind(setting, !setting.get()), disabled);
320 }
321 }
322
323 _reset() {
324 this._deviceOutlineSetting.set(false);
325 this._showDeviceScaleFactorSetting.set(false);
326 this._showUserAgentTypeSetting.set(false);
327 this._showMediaInspectorSetting.set(false);
328 this._showRulersSetting.set(false);
329 this._model.reset();
330 }
331
332 /**
333 * @param {!Element} element
334 * @return {!UI.ToolbarItem}
335 */
336 _wrapToolbarItem(element) {
337 const container = createElement('div');
338 const shadowRoot = UI.createShadowRootWithCoreStyles(container, 'emulation/deviceModeToolbar.css');
339 shadowRoot.appendChild(element);
340 return new UI.ToolbarItem(container);
341 }
342
343 /**
344 * @param {!Emulation.EmulatedDevice} device
345 */
346 _emulateDevice(device) {
347 const scale = this._autoAdjustScaleSetting.get() ? undefined : this._model.scaleSetting().get();
348 this._model.emulate(
349 Emulation.DeviceModeModel.Type.Device, device, this._lastMode.get(device) || device.modes[0], scale);
350 }
351
352 _switchToResponsive() {
353 this._model.emulate(Emulation.DeviceModeModel.Type.Responsive, null, null);
354 }
355
356 /**
357 * @param {!Array<!Emulation.EmulatedDevice>} devices
358 * @return {!Array<!Emulation.EmulatedDevice>}
359 */
360 _filterDevices(devices) {
361 devices = devices.filter(function(d) {
362 return d.show();
363 });
364 devices.sort(Emulation.EmulatedDevice.deviceComparator);
365 return devices;
366 }
367
368 /**
369 * @return {!Array<!Emulation.EmulatedDevice>}
370 */
371 _standardDevices() {
372 return this._filterDevices(this._emulatedDevicesList.standard());
373 }
374
375 /**
376 * @return {!Array<!Emulation.EmulatedDevice>}
377 */
378 _customDevices() {
379 return this._filterDevices(this._emulatedDevicesList.custom());
380 }
381
382 /**
383 * @return {!Array<!Emulation.EmulatedDevice>}
384 */
385 _allDevices() {
386 return this._standardDevices().concat(this._customDevices());
387 }
388
389 /**
390 * @param {!UI.ContextMenu} contextMenu
391 */
392 _appendDeviceMenuItems(contextMenu) {
393 contextMenu.headerSection().appendCheckboxItem(
394 Common.UIString('Responsive'), this._switchToResponsive.bind(this),
395 this._model.type() === Emulation.DeviceModeModel.Type.Responsive, false);
396 appendGroup.call(this, this._standardDevices());
397 appendGroup.call(this, this._customDevices());
398 contextMenu.footerSection().appendItem(
399 Common.UIString('Edit\u2026'), this._emulatedDevicesList.revealCustomSetting.bind(this._emulatedDevicesList),
400 false);
401
402 /**
403 * @param {!Array<!Emulation.EmulatedDevice>} devices
404 * @this {Emulation.DeviceModeToolbar}
405 */
406 function appendGroup(devices) {
407 if (!devices.length)
408 return;
409 const section = contextMenu.section();
410 for (const device of devices) {
411 section.appendCheckboxItem(
412 device.title, this._emulateDevice.bind(this, device), this._model.device() === device, false);
413 }
414 }
415 }
416
417 /**
418 * @this {Emulation.DeviceModeToolbar}
419 */
420 _deviceListChanged() {
421 const device = this._model.device();
422 if (!device)
423 return;
424
425 const devices = this._allDevices();
426 if (devices.indexOf(device) === -1) {
427 if (devices.length)
428 this._emulateDevice(devices[0]);
429 else
430 this._model.emulate(Emulation.DeviceModeModel.Type.Responsive, null, null);
431 }
432 }
433
434 _updateDeviceScaleFactorVisibility() {
435 this._deviceScaleItem.setVisible(this._showDeviceScaleFactorSetting.get());
436 }
437
438 _updateUserAgentTypeVisibility() {
439 this._uaItem.setVisible(this._showUserAgentTypeSetting.get());
440 }
441
442 /**
443 * @param {!Common.Event} event
444 */
445 _modeMenuClicked(event) {
446 const device = this._model.device();
447 const model = this._model;
448 const autoAdjustScaleSetting = this._autoAdjustScaleSetting;
449
450 if (model.type() === Emulation.DeviceModeModel.Type.Responsive) {
451 const appliedSize = model.appliedDeviceSize();
452 if (autoAdjustScaleSetting.get()) {
453 model.setSizeAndScaleToFit(appliedSize.height, appliedSize.width);
454 } else {
455 model.setWidth(appliedSize.height);
456 model.setHeight(appliedSize.width);
457 }
458 return;
459 }
460
461 if (device.modes.length === 2 && device.modes[0].orientation !== device.modes[1].orientation) {
462 const scale = autoAdjustScaleSetting.get() ? undefined : model.scaleSetting().get();
463 model.emulate(
464 model.type(), model.device(), model.mode() === device.modes[0] ? device.modes[1] : device.modes[0], scale);
465 return;
466 }
467
468 const contextMenu = new UI.ContextMenu(
469 /** @type {!Event} */ (event.data), false, this._modeButton.element.totalOffsetLeft(),
470 this._modeButton.element.totalOffsetTop() + this._modeButton.element.offsetHeight);
471 addOrientation(Emulation.EmulatedDevice.Vertical, Common.UIString('Portrait'));
472 addOrientation(Emulation.EmulatedDevice.Horizontal, Common.UIString('Landscape'));
473 contextMenu.show();
474
475 /**
476 * @param {string} orientation
477 * @param {string} title
478 */
479 function addOrientation(orientation, title) {
480 const modes = device.modesForOrientation(orientation);
481 if (!modes.length)
482 return;
483 if (modes.length === 1) {
484 addMode(modes[0], title);
485 } else {
486 for (let index = 0; index < modes.length; index++)
487 addMode(modes[index], title + ' \u2013 ' + modes[index].title);
488 }
489 }
490
491 /**
492 * @param {!Emulation.EmulatedDevice.Mode} mode
493 * @param {string} title
494 */
495 function addMode(mode, title) {
496 contextMenu.defaultSection().appendCheckboxItem(title, applyMode.bind(null, mode), model.mode() === mode, false);
497 }
498
499 /**
500 * @param {!Emulation.EmulatedDevice.Mode} mode
501 */
502 function applyMode(mode) {
503 const scale = autoAdjustScaleSetting.get() ? undefined : model.scaleSetting().get();
504 model.emulate(model.type(), model.device(), mode, scale);
505 }
506 }
507
508 /**
509 * @return {!Element}
510 */
511 element() {
512 return this._element;
513 }
514
515 update() {
516 if (this._model.type() !== this._cachedModelType) {
517 this._cachedModelType = this._model.type();
518 this._widthInput.disabled = this._model.type() !== Emulation.DeviceModeModel.Type.Responsive;
519 this._heightInput.disabled = this._model.type() !== Emulation.DeviceModeModel.Type.Responsive;
520 this._deviceScaleItem.setEnabled(this._model.type() === Emulation.DeviceModeModel.Type.Responsive);
521 this._uaItem.setEnabled(this._model.type() === Emulation.DeviceModeModel.Type.Responsive);
Erik Luo60d3b222018-05-14 20:18:44522 if (this._model.type() === Emulation.DeviceModeModel.Type.Responsive) {
523 this._modeButton.setEnabled(true);
524 this._modeButton.setTitle(ls`Rotate`);
525 } else {
526 this._modeButton.setEnabled(false);
527 }
Blink Reformat4c46d092018-04-07 15:32:37528 }
529
530 const size = this._model.appliedDeviceSize();
531 this._updateHeightInput(
532 this._model.type() === Emulation.DeviceModeModel.Type.Responsive && this._model.isFullHeight() ?
533 '' :
534 String(size.height));
535 this._updateWidthInput(String(size.width));
536 this._heightInput.placeholder = size.height;
537
538 if (this._model.scale() !== this._cachedScale) {
539 this._scaleItem.setText(Common.UIString('%.0f%%', this._model.scale() * 100));
540 this._cachedScale = this._model.scale();
541 }
542
543 const deviceScale = this._model.appliedDeviceScaleFactor();
544 if (deviceScale !== this._cachedDeviceScale) {
545 this._deviceScaleItem.setText(Common.UIString('DPR: %.1f', deviceScale));
546 this._cachedDeviceScale = deviceScale;
547 }
548
549 const uaType = this._model.appliedUserAgentType();
550 if (uaType !== this._cachedUaType) {
551 this._uaItem.setText(uaType);
552 this._cachedUaType = uaType;
553 }
554
555 let deviceItemTitle = Common.UIString('None');
556 if (this._model.type() === Emulation.DeviceModeModel.Type.Responsive)
557 deviceItemTitle = Common.UIString('Responsive');
558 if (this._model.type() === Emulation.DeviceModeModel.Type.Device)
559 deviceItemTitle = this._model.device().title;
560 this._deviceSelectItem.setText(deviceItemTitle);
561
562 if (this._model.device() !== this._cachedModelDevice) {
563 const device = this._model.device();
564 if (device) {
565 const modeCount = device ? device.modes.length : 0;
566 this._modeButton.setEnabled(modeCount >= 2);
567 this._modeButton.setTitle(modeCount === 2 ? Common.UIString('Rotate') : Common.UIString('Screen options'));
Blink Reformat4c46d092018-04-07 15:32:37568 }
569 this._cachedModelDevice = device;
570 }
571
572 if (this._model.type() === Emulation.DeviceModeModel.Type.Device) {
573 this._lastMode.set(
574 /** @type {!Emulation.EmulatedDevice} */ (this._model.device()),
575 /** @type {!Emulation.EmulatedDevice.Mode} */ (this._model.mode()));
576 }
577
578 if (this._model.mode() !== this._cachedModelMode && this._model.type() !== Emulation.DeviceModeModel.Type.None) {
579 this._cachedModelMode = this._model.mode();
580 const value = this._persistenceSetting.get();
581 if (this._model.device()) {
582 value.device = this._model.device().title;
583 value.orientation = this._model.mode() ? this._model.mode().orientation : '';
584 value.mode = this._model.mode() ? this._model.mode().title : '';
585 } else {
586 value.device = '';
587 value.orientation = '';
588 value.mode = '';
589 }
590 this._persistenceSetting.set(value);
591 }
592 }
593
594 restore() {
595 for (const device of this._allDevices()) {
596 if (device.title === this._persistenceSetting.get().device) {
597 for (const mode of device.modes) {
598 if (mode.orientation === this._persistenceSetting.get().orientation &&
599 mode.title === this._persistenceSetting.get().mode) {
600 this._lastMode.set(device, mode);
601 this._emulateDevice(device);
602 return;
603 }
604 }
605 }
606 }
607
608 this._model.emulate(Emulation.DeviceModeModel.Type.Responsive, null, null);
609 }
610};