Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | /** |
| 5 | * @unrestricted |
| 6 | */ |
| 7 | Emulation.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); |
Erik Luo | 5e5a836 | 2018-05-31 23:43:22 | [diff] [blame^] | 51 | optionsToolbar.makeWrappable(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 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) { |
Erik Luo | 5e5a836 | 2018-05-31 23:43:22 | [diff] [blame^] | 192 | toolbar.appendToolbarItem( |
| 193 | this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-toolbar-element'))); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 194 | const moreOptionsButton = new UI.ToolbarMenuButton(this._appendOptionsMenuItems.bind(this)); |
| 195 | moreOptionsButton.setTitle(Common.UIString('More options')); |
| 196 | toolbar.appendToolbarItem(moreOptionsButton); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @param {!UI.ContextMenu} contextMenu |
| 201 | */ |
| 202 | _appendScaleMenuItems(contextMenu) { |
| 203 | if (this._model.type() === Emulation.DeviceModeModel.Type.Device) { |
| 204 | contextMenu.footerSection().appendItem( |
| 205 | Common.UIString('Fit to window (%.0f%%)', this._model.fitScale() * 100), |
| 206 | this._onScaleMenuChanged.bind(this, this._model.fitScale()), false); |
| 207 | } |
| 208 | contextMenu.footerSection().appendCheckboxItem( |
| 209 | ls`Auto-adjust zoom`, this._onAutoAdjustScaleChanged.bind(this), this._autoAdjustScaleSetting.get()); |
| 210 | const boundAppendScaleItem = appendScaleItem.bind(this); |
| 211 | boundAppendScaleItem(Common.UIString('50%'), 0.5); |
| 212 | boundAppendScaleItem(Common.UIString('75%'), 0.75); |
| 213 | boundAppendScaleItem(Common.UIString('100%'), 1); |
| 214 | boundAppendScaleItem(Common.UIString('125%'), 1.25); |
| 215 | boundAppendScaleItem(Common.UIString('150%'), 1.5); |
| 216 | |
| 217 | /** |
| 218 | * @param {string} title |
| 219 | * @param {number} value |
| 220 | * @this {!Emulation.DeviceModeToolbar} |
| 221 | */ |
| 222 | function appendScaleItem(title, value) { |
| 223 | contextMenu.defaultSection().appendCheckboxItem( |
| 224 | title, this._onScaleMenuChanged.bind(this, value), this._model.scaleSetting().get() === value, false); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * @param {number} value |
| 230 | */ |
| 231 | _onScaleMenuChanged(value) { |
| 232 | this._model.scaleSetting().set(value); |
| 233 | } |
| 234 | |
| 235 | _onAutoAdjustScaleChanged() { |
| 236 | this._autoAdjustScaleSetting.set(!this._autoAdjustScaleSetting.get()); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @param {!UI.ContextMenu} contextMenu |
| 241 | */ |
| 242 | _appendDeviceScaleMenuItems(contextMenu) { |
| 243 | const deviceScaleFactorSetting = this._model.deviceScaleFactorSetting(); |
| 244 | const defaultValue = this._model.uaSetting().get() === Emulation.DeviceModeModel.UA.Mobile || |
| 245 | this._model.uaSetting().get() === Emulation.DeviceModeModel.UA.MobileNoTouch ? |
| 246 | Emulation.DeviceModeModel.defaultMobileScaleFactor : |
| 247 | window.devicePixelRatio; |
| 248 | appendDeviceScaleFactorItem(contextMenu.headerSection(), Common.UIString('Default: %.1f', defaultValue), 0); |
| 249 | appendDeviceScaleFactorItem(contextMenu.defaultSection(), Common.UIString('1'), 1); |
| 250 | appendDeviceScaleFactorItem(contextMenu.defaultSection(), Common.UIString('2'), 2); |
| 251 | appendDeviceScaleFactorItem(contextMenu.defaultSection(), Common.UIString('3'), 3); |
| 252 | |
| 253 | /** |
| 254 | * @param {!UI.ContextMenuSection} section |
| 255 | * @param {string} title |
| 256 | * @param {number} value |
| 257 | */ |
| 258 | function appendDeviceScaleFactorItem(section, title, value) { |
| 259 | section.appendCheckboxItem( |
| 260 | title, deviceScaleFactorSetting.set.bind(deviceScaleFactorSetting, value), |
| 261 | deviceScaleFactorSetting.get() === value); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @param {!UI.ContextMenu} contextMenu |
| 267 | */ |
| 268 | _appendUserAgentMenuItems(contextMenu) { |
| 269 | const uaSetting = this._model.uaSetting(); |
| 270 | appendUAItem(Emulation.DeviceModeModel.UA.Mobile, Emulation.DeviceModeModel.UA.Mobile); |
| 271 | appendUAItem(Emulation.DeviceModeModel.UA.MobileNoTouch, Emulation.DeviceModeModel.UA.MobileNoTouch); |
| 272 | appendUAItem(Emulation.DeviceModeModel.UA.Desktop, Emulation.DeviceModeModel.UA.Desktop); |
| 273 | appendUAItem(Emulation.DeviceModeModel.UA.DesktopTouch, Emulation.DeviceModeModel.UA.DesktopTouch); |
| 274 | |
| 275 | /** |
| 276 | * @param {string} title |
| 277 | * @param {!Emulation.DeviceModeModel.UA} value |
| 278 | */ |
| 279 | function appendUAItem(title, value) { |
| 280 | contextMenu.defaultSection().appendCheckboxItem( |
| 281 | title, uaSetting.set.bind(uaSetting, value), uaSetting.get() === value); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @param {!UI.ContextMenu} contextMenu |
| 287 | */ |
| 288 | _appendOptionsMenuItems(contextMenu) { |
| 289 | const model = this._model; |
| 290 | appendToggleItem( |
| 291 | contextMenu.headerSection(), this._deviceOutlineSetting, Common.UIString('Hide device frame'), |
| 292 | Common.UIString('Show device frame'), model.type() !== Emulation.DeviceModeModel.Type.Device); |
| 293 | appendToggleItem( |
| 294 | contextMenu.headerSection(), this._showMediaInspectorSetting, Common.UIString('Hide media queries'), |
| 295 | Common.UIString('Show media queries')); |
| 296 | appendToggleItem( |
| 297 | contextMenu.headerSection(), this._showRulersSetting, Common.UIString('Hide rulers'), |
| 298 | Common.UIString('Show rulers')); |
| 299 | appendToggleItem( |
| 300 | contextMenu.defaultSection(), this._showDeviceScaleFactorSetting, Common.UIString('Remove device pixel ratio'), |
| 301 | Common.UIString('Add device pixel ratio')); |
| 302 | appendToggleItem( |
| 303 | contextMenu.defaultSection(), this._showUserAgentTypeSetting, Common.UIString('Remove device type'), |
| 304 | Common.UIString('Add device type')); |
| 305 | contextMenu.appendItemsAtLocation('deviceModeMenu'); |
| 306 | contextMenu.footerSection().appendItem(Common.UIString('Reset to defaults'), this._reset.bind(this)); |
Erik Luo | 92b9bb8 | 2018-05-22 18:51:26 | [diff] [blame] | 307 | contextMenu.footerSection().appendItem( |
| 308 | ls`Close DevTools`, InspectorFrontendHost.closeWindow.bind(InspectorFrontendHost)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 309 | |
| 310 | /** |
| 311 | * @param {!UI.ContextMenuSection} section |
| 312 | * @param {!Common.Setting} setting |
| 313 | * @param {string} title1 |
| 314 | * @param {string} title2 |
| 315 | * @param {boolean=} disabled |
| 316 | */ |
| 317 | function appendToggleItem(section, setting, title1, title2, disabled) { |
| 318 | if (typeof disabled === 'undefined') |
| 319 | disabled = model.type() === Emulation.DeviceModeModel.Type.None; |
| 320 | section.appendItem(setting.get() ? title1 : title2, setting.set.bind(setting, !setting.get()), disabled); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | _reset() { |
| 325 | this._deviceOutlineSetting.set(false); |
| 326 | this._showDeviceScaleFactorSetting.set(false); |
| 327 | this._showUserAgentTypeSetting.set(false); |
| 328 | this._showMediaInspectorSetting.set(false); |
| 329 | this._showRulersSetting.set(false); |
| 330 | this._model.reset(); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * @param {!Element} element |
| 335 | * @return {!UI.ToolbarItem} |
| 336 | */ |
| 337 | _wrapToolbarItem(element) { |
| 338 | const container = createElement('div'); |
| 339 | const shadowRoot = UI.createShadowRootWithCoreStyles(container, 'emulation/deviceModeToolbar.css'); |
| 340 | shadowRoot.appendChild(element); |
| 341 | return new UI.ToolbarItem(container); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * @param {!Emulation.EmulatedDevice} device |
| 346 | */ |
| 347 | _emulateDevice(device) { |
| 348 | const scale = this._autoAdjustScaleSetting.get() ? undefined : this._model.scaleSetting().get(); |
| 349 | this._model.emulate( |
| 350 | Emulation.DeviceModeModel.Type.Device, device, this._lastMode.get(device) || device.modes[0], scale); |
| 351 | } |
| 352 | |
| 353 | _switchToResponsive() { |
| 354 | this._model.emulate(Emulation.DeviceModeModel.Type.Responsive, null, null); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * @param {!Array<!Emulation.EmulatedDevice>} devices |
| 359 | * @return {!Array<!Emulation.EmulatedDevice>} |
| 360 | */ |
| 361 | _filterDevices(devices) { |
| 362 | devices = devices.filter(function(d) { |
| 363 | return d.show(); |
| 364 | }); |
| 365 | devices.sort(Emulation.EmulatedDevice.deviceComparator); |
| 366 | return devices; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * @return {!Array<!Emulation.EmulatedDevice>} |
| 371 | */ |
| 372 | _standardDevices() { |
| 373 | return this._filterDevices(this._emulatedDevicesList.standard()); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * @return {!Array<!Emulation.EmulatedDevice>} |
| 378 | */ |
| 379 | _customDevices() { |
| 380 | return this._filterDevices(this._emulatedDevicesList.custom()); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * @return {!Array<!Emulation.EmulatedDevice>} |
| 385 | */ |
| 386 | _allDevices() { |
| 387 | return this._standardDevices().concat(this._customDevices()); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * @param {!UI.ContextMenu} contextMenu |
| 392 | */ |
| 393 | _appendDeviceMenuItems(contextMenu) { |
| 394 | contextMenu.headerSection().appendCheckboxItem( |
| 395 | Common.UIString('Responsive'), this._switchToResponsive.bind(this), |
| 396 | this._model.type() === Emulation.DeviceModeModel.Type.Responsive, false); |
| 397 | appendGroup.call(this, this._standardDevices()); |
| 398 | appendGroup.call(this, this._customDevices()); |
| 399 | contextMenu.footerSection().appendItem( |
| 400 | Common.UIString('Edit\u2026'), this._emulatedDevicesList.revealCustomSetting.bind(this._emulatedDevicesList), |
| 401 | false); |
| 402 | |
| 403 | /** |
| 404 | * @param {!Array<!Emulation.EmulatedDevice>} devices |
| 405 | * @this {Emulation.DeviceModeToolbar} |
| 406 | */ |
| 407 | function appendGroup(devices) { |
| 408 | if (!devices.length) |
| 409 | return; |
| 410 | const section = contextMenu.section(); |
| 411 | for (const device of devices) { |
| 412 | section.appendCheckboxItem( |
| 413 | device.title, this._emulateDevice.bind(this, device), this._model.device() === device, false); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * @this {Emulation.DeviceModeToolbar} |
| 420 | */ |
| 421 | _deviceListChanged() { |
| 422 | const device = this._model.device(); |
| 423 | if (!device) |
| 424 | return; |
| 425 | |
| 426 | const devices = this._allDevices(); |
| 427 | if (devices.indexOf(device) === -1) { |
| 428 | if (devices.length) |
| 429 | this._emulateDevice(devices[0]); |
| 430 | else |
| 431 | this._model.emulate(Emulation.DeviceModeModel.Type.Responsive, null, null); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | _updateDeviceScaleFactorVisibility() { |
| 436 | this._deviceScaleItem.setVisible(this._showDeviceScaleFactorSetting.get()); |
| 437 | } |
| 438 | |
| 439 | _updateUserAgentTypeVisibility() { |
| 440 | this._uaItem.setVisible(this._showUserAgentTypeSetting.get()); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * @param {!Common.Event} event |
| 445 | */ |
| 446 | _modeMenuClicked(event) { |
| 447 | const device = this._model.device(); |
| 448 | const model = this._model; |
| 449 | const autoAdjustScaleSetting = this._autoAdjustScaleSetting; |
| 450 | |
| 451 | if (model.type() === Emulation.DeviceModeModel.Type.Responsive) { |
| 452 | const appliedSize = model.appliedDeviceSize(); |
| 453 | if (autoAdjustScaleSetting.get()) { |
| 454 | model.setSizeAndScaleToFit(appliedSize.height, appliedSize.width); |
| 455 | } else { |
| 456 | model.setWidth(appliedSize.height); |
| 457 | model.setHeight(appliedSize.width); |
| 458 | } |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | if (device.modes.length === 2 && device.modes[0].orientation !== device.modes[1].orientation) { |
| 463 | const scale = autoAdjustScaleSetting.get() ? undefined : model.scaleSetting().get(); |
| 464 | model.emulate( |
| 465 | model.type(), model.device(), model.mode() === device.modes[0] ? device.modes[1] : device.modes[0], scale); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | const contextMenu = new UI.ContextMenu( |
| 470 | /** @type {!Event} */ (event.data), false, this._modeButton.element.totalOffsetLeft(), |
| 471 | this._modeButton.element.totalOffsetTop() + this._modeButton.element.offsetHeight); |
| 472 | addOrientation(Emulation.EmulatedDevice.Vertical, Common.UIString('Portrait')); |
| 473 | addOrientation(Emulation.EmulatedDevice.Horizontal, Common.UIString('Landscape')); |
| 474 | contextMenu.show(); |
| 475 | |
| 476 | /** |
| 477 | * @param {string} orientation |
| 478 | * @param {string} title |
| 479 | */ |
| 480 | function addOrientation(orientation, title) { |
| 481 | const modes = device.modesForOrientation(orientation); |
| 482 | if (!modes.length) |
| 483 | return; |
| 484 | if (modes.length === 1) { |
| 485 | addMode(modes[0], title); |
| 486 | } else { |
| 487 | for (let index = 0; index < modes.length; index++) |
| 488 | addMode(modes[index], title + ' \u2013 ' + modes[index].title); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * @param {!Emulation.EmulatedDevice.Mode} mode |
| 494 | * @param {string} title |
| 495 | */ |
| 496 | function addMode(mode, title) { |
| 497 | contextMenu.defaultSection().appendCheckboxItem(title, applyMode.bind(null, mode), model.mode() === mode, false); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * @param {!Emulation.EmulatedDevice.Mode} mode |
| 502 | */ |
| 503 | function applyMode(mode) { |
| 504 | const scale = autoAdjustScaleSetting.get() ? undefined : model.scaleSetting().get(); |
| 505 | model.emulate(model.type(), model.device(), mode, scale); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * @return {!Element} |
| 511 | */ |
| 512 | element() { |
| 513 | return this._element; |
| 514 | } |
| 515 | |
| 516 | update() { |
| 517 | if (this._model.type() !== this._cachedModelType) { |
| 518 | this._cachedModelType = this._model.type(); |
| 519 | this._widthInput.disabled = this._model.type() !== Emulation.DeviceModeModel.Type.Responsive; |
| 520 | this._heightInput.disabled = this._model.type() !== Emulation.DeviceModeModel.Type.Responsive; |
| 521 | this._deviceScaleItem.setEnabled(this._model.type() === Emulation.DeviceModeModel.Type.Responsive); |
| 522 | this._uaItem.setEnabled(this._model.type() === Emulation.DeviceModeModel.Type.Responsive); |
Erik Luo | 60d3b22 | 2018-05-14 20:18:44 | [diff] [blame] | 523 | if (this._model.type() === Emulation.DeviceModeModel.Type.Responsive) { |
| 524 | this._modeButton.setEnabled(true); |
| 525 | this._modeButton.setTitle(ls`Rotate`); |
| 526 | } else { |
| 527 | this._modeButton.setEnabled(false); |
| 528 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | const size = this._model.appliedDeviceSize(); |
| 532 | this._updateHeightInput( |
| 533 | this._model.type() === Emulation.DeviceModeModel.Type.Responsive && this._model.isFullHeight() ? |
| 534 | '' : |
| 535 | String(size.height)); |
| 536 | this._updateWidthInput(String(size.width)); |
| 537 | this._heightInput.placeholder = size.height; |
| 538 | |
| 539 | if (this._model.scale() !== this._cachedScale) { |
| 540 | this._scaleItem.setText(Common.UIString('%.0f%%', this._model.scale() * 100)); |
| 541 | this._cachedScale = this._model.scale(); |
| 542 | } |
| 543 | |
| 544 | const deviceScale = this._model.appliedDeviceScaleFactor(); |
| 545 | if (deviceScale !== this._cachedDeviceScale) { |
| 546 | this._deviceScaleItem.setText(Common.UIString('DPR: %.1f', deviceScale)); |
| 547 | this._cachedDeviceScale = deviceScale; |
| 548 | } |
| 549 | |
| 550 | const uaType = this._model.appliedUserAgentType(); |
| 551 | if (uaType !== this._cachedUaType) { |
| 552 | this._uaItem.setText(uaType); |
| 553 | this._cachedUaType = uaType; |
| 554 | } |
| 555 | |
| 556 | let deviceItemTitle = Common.UIString('None'); |
| 557 | if (this._model.type() === Emulation.DeviceModeModel.Type.Responsive) |
| 558 | deviceItemTitle = Common.UIString('Responsive'); |
| 559 | if (this._model.type() === Emulation.DeviceModeModel.Type.Device) |
| 560 | deviceItemTitle = this._model.device().title; |
| 561 | this._deviceSelectItem.setText(deviceItemTitle); |
| 562 | |
| 563 | if (this._model.device() !== this._cachedModelDevice) { |
| 564 | const device = this._model.device(); |
| 565 | if (device) { |
| 566 | const modeCount = device ? device.modes.length : 0; |
| 567 | this._modeButton.setEnabled(modeCount >= 2); |
| 568 | this._modeButton.setTitle(modeCount === 2 ? Common.UIString('Rotate') : Common.UIString('Screen options')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 569 | } |
| 570 | this._cachedModelDevice = device; |
| 571 | } |
| 572 | |
| 573 | if (this._model.type() === Emulation.DeviceModeModel.Type.Device) { |
| 574 | this._lastMode.set( |
| 575 | /** @type {!Emulation.EmulatedDevice} */ (this._model.device()), |
| 576 | /** @type {!Emulation.EmulatedDevice.Mode} */ (this._model.mode())); |
| 577 | } |
| 578 | |
| 579 | if (this._model.mode() !== this._cachedModelMode && this._model.type() !== Emulation.DeviceModeModel.Type.None) { |
| 580 | this._cachedModelMode = this._model.mode(); |
| 581 | const value = this._persistenceSetting.get(); |
| 582 | if (this._model.device()) { |
| 583 | value.device = this._model.device().title; |
| 584 | value.orientation = this._model.mode() ? this._model.mode().orientation : ''; |
| 585 | value.mode = this._model.mode() ? this._model.mode().title : ''; |
| 586 | } else { |
| 587 | value.device = ''; |
| 588 | value.orientation = ''; |
| 589 | value.mode = ''; |
| 590 | } |
| 591 | this._persistenceSetting.set(value); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | restore() { |
| 596 | for (const device of this._allDevices()) { |
| 597 | if (device.title === this._persistenceSetting.get().device) { |
| 598 | for (const mode of device.modes) { |
| 599 | if (mode.orientation === this._persistenceSetting.get().orientation && |
| 600 | mode.title === this._persistenceSetting.get().mode) { |
| 601 | this._lastMode.set(device, mode); |
| 602 | this._emulateDevice(device); |
| 603 | return; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | this._model.emulate(Emulation.DeviceModeModel.Type.Responsive, null, null); |
| 610 | } |
| 611 | }; |