OLD | NEW |
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 | |
5 /** | 4 /** |
6 * @param {!WebInspector.DeviceModeModel} model | 5 * @unrestricted |
7 * @param {!WebInspector.Setting} showMediaInspectorSetting | |
8 * @param {!WebInspector.Setting} showRulersSetting | |
9 * @constructor | |
10 */ | 6 */ |
11 WebInspector.DeviceModeToolbar = function(model, showMediaInspectorSetting, show
RulersSetting) | 7 WebInspector.DeviceModeToolbar = class { |
12 { | 8 /** |
| 9 * @param {!WebInspector.DeviceModeModel} model |
| 10 * @param {!WebInspector.Setting} showMediaInspectorSetting |
| 11 * @param {!WebInspector.Setting} showRulersSetting |
| 12 */ |
| 13 constructor(model, showMediaInspectorSetting, showRulersSetting) { |
13 this._model = model; | 14 this._model = model; |
14 this._showMediaInspectorSetting = showMediaInspectorSetting; | 15 this._showMediaInspectorSetting = showMediaInspectorSetting; |
15 this._showRulersSetting = showRulersSetting; | 16 this._showRulersSetting = showRulersSetting; |
16 | 17 |
17 this._deviceOutlineSetting = this._model.deviceOutlineSetting(); | 18 this._deviceOutlineSetting = this._model.deviceOutlineSetting(); |
18 this._showDeviceScaleFactorSetting = WebInspector.settings.createSetting("em
ulation.showDeviceScaleFactor", false); | 19 this._showDeviceScaleFactorSetting = WebInspector.settings.createSetting('em
ulation.showDeviceScaleFactor', false); |
19 this._showDeviceScaleFactorSetting.addChangeListener(this._updateDeviceScale
FactorVisibility, this); | 20 this._showDeviceScaleFactorSetting.addChangeListener(this._updateDeviceScale
FactorVisibility, this); |
20 | 21 |
21 this._showUserAgentTypeSetting = WebInspector.settings.createSetting("emulat
ion.showUserAgentType", false); | 22 this._showUserAgentTypeSetting = WebInspector.settings.createSetting('emulat
ion.showUserAgentType', false); |
22 this._showUserAgentTypeSetting.addChangeListener(this._updateUserAgentTypeVi
sibility, this); | 23 this._showUserAgentTypeSetting.addChangeListener(this._updateUserAgentTypeVi
sibility, this); |
23 | 24 |
24 this._showNetworkConditionsSetting = WebInspector.settings.createSetting("em
ulation.showNetworkConditions", false); | 25 this._showNetworkConditionsSetting = WebInspector.settings.createSetting('em
ulation.showNetworkConditions', false); |
25 this._showNetworkConditionsSetting.addChangeListener(this._updateNetworkCond
itionsVisibility, this); | 26 this._showNetworkConditionsSetting.addChangeListener(this._updateNetworkCond
itionsVisibility, this); |
26 | 27 |
27 /** @type {!Map<!WebInspector.EmulatedDevice, !WebInspector.EmulatedDevice.M
ode>} */ | 28 /** @type {!Map<!WebInspector.EmulatedDevice, !WebInspector.EmulatedDevice.M
ode>} */ |
28 this._lastMode = new Map(); | 29 this._lastMode = new Map(); |
29 | 30 |
30 /** @type {!Map<!WebInspector.EmulatedDevice, number>} */ | 31 /** @type {!Map<!WebInspector.EmulatedDevice, number>} */ |
31 this._lastScale = new Map(); | 32 this._lastScale = new Map(); |
32 | 33 |
33 this._element = createElementWithClass("div", "device-mode-toolbar"); | 34 this._element = createElementWithClass('div', 'device-mode-toolbar'); |
34 | 35 |
35 var leftContainer = this._element.createChild("div", "device-mode-toolbar-sp
acer"); | 36 var leftContainer = this._element.createChild('div', 'device-mode-toolbar-sp
acer'); |
36 leftContainer.createChild("div", "device-mode-toolbar-spacer"); | 37 leftContainer.createChild('div', 'device-mode-toolbar-spacer'); |
37 var leftToolbar = new WebInspector.Toolbar("", leftContainer); | 38 var leftToolbar = new WebInspector.Toolbar('', leftContainer); |
38 leftToolbar.makeWrappable(); | 39 leftToolbar.makeWrappable(); |
39 this._fillLeftToolbar(leftToolbar); | 40 this._fillLeftToolbar(leftToolbar); |
40 | 41 |
41 var mainToolbar = new WebInspector.Toolbar("", this._element); | 42 var mainToolbar = new WebInspector.Toolbar('', this._element); |
42 mainToolbar.makeWrappable(); | 43 mainToolbar.makeWrappable(); |
43 this._fillMainToolbar(mainToolbar); | 44 this._fillMainToolbar(mainToolbar); |
44 | 45 |
45 var rightContainer = this._element.createChild("div", "device-mode-toolbar-s
pacer"); | 46 var rightContainer = this._element.createChild('div', 'device-mode-toolbar-s
pacer'); |
46 var rightToolbar = new WebInspector.Toolbar("device-mode-toolbar-fixed-size"
, rightContainer); | 47 var rightToolbar = new WebInspector.Toolbar('device-mode-toolbar-fixed-size'
, rightContainer); |
47 rightToolbar.makeWrappable(); | 48 rightToolbar.makeWrappable(); |
48 this._fillRightToolbar(rightToolbar); | 49 this._fillRightToolbar(rightToolbar); |
49 var modeToolbar = new WebInspector.Toolbar("device-mode-toolbar-fixed-size",
rightContainer); | 50 var modeToolbar = new WebInspector.Toolbar('device-mode-toolbar-fixed-size',
rightContainer); |
50 modeToolbar.makeWrappable(); | 51 modeToolbar.makeWrappable(); |
51 this._fillModeToolbar(modeToolbar); | 52 this._fillModeToolbar(modeToolbar); |
52 rightContainer.createChild("div", "device-mode-toolbar-spacer"); | 53 rightContainer.createChild('div', 'device-mode-toolbar-spacer'); |
53 var optionsToolbar = new WebInspector.Toolbar("", rightContainer); | 54 var optionsToolbar = new WebInspector.Toolbar('', rightContainer); |
54 optionsToolbar.makeWrappable(true); | 55 optionsToolbar.makeWrappable(true); |
55 this._fillOptionsToolbar(optionsToolbar); | 56 this._fillOptionsToolbar(optionsToolbar); |
56 | 57 |
57 this._emulatedDevicesList = WebInspector.EmulatedDevicesList.instance(); | 58 this._emulatedDevicesList = WebInspector.EmulatedDevicesList.instance(); |
58 this._emulatedDevicesList.addEventListener(WebInspector.EmulatedDevicesList.
Events.CustomDevicesUpdated, this._deviceListChanged, this); | 59 this._emulatedDevicesList.addEventListener( |
59 this._emulatedDevicesList.addEventListener(WebInspector.EmulatedDevicesList.
Events.StandardDevicesUpdated, this._deviceListChanged, this); | 60 WebInspector.EmulatedDevicesList.Events.CustomDevicesUpdated, this._devi
ceListChanged, this); |
60 | 61 this._emulatedDevicesList.addEventListener( |
61 this._persistenceSetting = WebInspector.settings.createSetting("emulation.de
viceModeValue", {device: "", orientation: "", mode: ""}); | 62 WebInspector.EmulatedDevicesList.Events.StandardDevicesUpdated, this._de
viceListChanged, this); |
| 63 |
| 64 this._persistenceSetting = |
| 65 WebInspector.settings.createSetting('emulation.deviceModeValue', {device
: '', orientation: '', mode: ''}); |
| 66 } |
| 67 |
| 68 /** |
| 69 * @param {!WebInspector.Toolbar} toolbar |
| 70 */ |
| 71 _fillLeftToolbar(toolbar) { |
| 72 toolbar.appendToolbarItem( |
| 73 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-t
oolbar-element'))); |
| 74 this._deviceSelectItem = new WebInspector.ToolbarMenuButton(this._appendDevi
ceMenuItems.bind(this)); |
| 75 this._deviceSelectItem.setGlyph(''); |
| 76 this._deviceSelectItem.turnIntoSelect(95); |
| 77 toolbar.appendToolbarItem(this._deviceSelectItem); |
| 78 } |
| 79 |
| 80 /** |
| 81 * @param {!WebInspector.Toolbar} toolbar |
| 82 */ |
| 83 _fillMainToolbar(toolbar) { |
| 84 var widthInput = createElementWithClass('input', 'device-mode-size-input'); |
| 85 widthInput.maxLength = 4; |
| 86 widthInput.type = 'text'; |
| 87 widthInput.title = WebInspector.UIString('Width'); |
| 88 this._updateWidthInput = WebInspector.bindInput( |
| 89 widthInput, this._applyWidth.bind(this), WebInspector.DeviceModeModel.de
viceSizeValidator, true); |
| 90 this._widthInput = widthInput; |
| 91 this._widthItem = this._wrapToolbarItem(widthInput); |
| 92 toolbar.appendToolbarItem(this._widthItem); |
| 93 |
| 94 var xElement = createElementWithClass('div', 'device-mode-x'); |
| 95 xElement.textContent = '\u00D7'; |
| 96 this._xItem = this._wrapToolbarItem(xElement); |
| 97 toolbar.appendToolbarItem(this._xItem); |
| 98 |
| 99 var heightInput = createElementWithClass('input', 'device-mode-size-input'); |
| 100 heightInput.maxLength = 4; |
| 101 heightInput.type = 'text'; |
| 102 heightInput.title = WebInspector.UIString('Height (leave empty for full)'); |
| 103 this._updateHeightInput = WebInspector.bindInput(heightInput, this._applyHei
ght.bind(this), validateHeight, true); |
| 104 this._heightInput = heightInput; |
| 105 this._heightItem = this._wrapToolbarItem(heightInput); |
| 106 toolbar.appendToolbarItem(this._heightItem); |
| 107 |
| 108 /** |
| 109 * @param {string} value |
| 110 * @return {boolean} |
| 111 */ |
| 112 function validateHeight(value) { |
| 113 return !value || WebInspector.DeviceModeModel.deviceSizeValidator(value); |
| 114 } |
| 115 } |
| 116 |
| 117 /** |
| 118 * @param {string} value |
| 119 */ |
| 120 _applyWidth(value) { |
| 121 var width = value ? Number(value) : 0; |
| 122 this._model.setWidthAndScaleToFit(width); |
| 123 } |
| 124 |
| 125 /** |
| 126 * @param {string} value |
| 127 */ |
| 128 _applyHeight(value) { |
| 129 var height = value ? Number(value) : 0; |
| 130 this._model.setHeightAndScaleToFit(height); |
| 131 } |
| 132 |
| 133 /** |
| 134 * @param {!WebInspector.Toolbar} toolbar |
| 135 */ |
| 136 _fillRightToolbar(toolbar) { |
| 137 toolbar.appendToolbarItem( |
| 138 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-t
oolbar-element'))); |
| 139 this._scaleItem = new WebInspector.ToolbarMenuButton(this._appendScaleMenuIt
ems.bind(this)); |
| 140 this._scaleItem.setTitle(WebInspector.UIString('Zoom')); |
| 141 this._scaleItem.setGlyph(''); |
| 142 this._scaleItem.turnIntoSelect(); |
| 143 toolbar.appendToolbarItem(this._scaleItem); |
| 144 |
| 145 toolbar.appendToolbarItem( |
| 146 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-t
oolbar-element'))); |
| 147 this._deviceScaleItem = new WebInspector.ToolbarMenuButton(this._appendDevic
eScaleMenuItems.bind(this)); |
| 148 this._deviceScaleItem.setVisible(this._showDeviceScaleFactorSetting.get()); |
| 149 this._deviceScaleItem.setTitle(WebInspector.UIString('Device pixel ratio')); |
| 150 this._deviceScaleItem.setGlyph(''); |
| 151 this._deviceScaleItem.turnIntoSelect(); |
| 152 this._deviceScaleItem.element.style.padding = '0 5px'; |
| 153 toolbar.appendToolbarItem(this._deviceScaleItem); |
| 154 |
| 155 toolbar.appendToolbarItem( |
| 156 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-t
oolbar-element'))); |
| 157 this._uaItem = new WebInspector.ToolbarMenuButton(this._appendUserAgentMenuI
tems.bind(this)); |
| 158 this._uaItem.setVisible(this._showUserAgentTypeSetting.get()); |
| 159 this._uaItem.setTitle(WebInspector.UIString('Device type')); |
| 160 this._uaItem.setGlyph(''); |
| 161 this._uaItem.turnIntoSelect(); |
| 162 this._uaItem.element.style.padding = '0 5px'; |
| 163 toolbar.appendToolbarItem(this._uaItem); |
| 164 } |
| 165 |
| 166 /** |
| 167 * @param {!WebInspector.Toolbar} toolbar |
| 168 */ |
| 169 _fillModeToolbar(toolbar) { |
| 170 toolbar.appendToolbarItem( |
| 171 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-t
oolbar-element'))); |
| 172 this._modeButton = new WebInspector.ToolbarButton('', 'rotate-screen-toolbar
-item'); |
| 173 this._modeButton.addEventListener('click', this._modeMenuClicked, this); |
| 174 toolbar.appendToolbarItem(this._modeButton); |
| 175 } |
| 176 |
| 177 /** |
| 178 * @param {!WebInspector.Toolbar} toolbar |
| 179 */ |
| 180 _fillOptionsToolbar(toolbar) { |
| 181 this._networkConditionsItem = WebInspector.NetworkConditionsSelector.createT
oolbarMenuButton(); |
| 182 this._networkConditionsItem.setVisible(this._showNetworkConditionsSetting.ge
t()); |
| 183 this._networkConditionsItem.setTitle(WebInspector.UIString('Network throttli
ng')); |
| 184 this._networkConditionsItem.element.style.padding = '0 5px'; |
| 185 this._networkConditionsItem.element.style.maxWidth = '140px'; |
| 186 toolbar.appendToolbarItem(this._networkConditionsItem); |
| 187 |
| 188 var moreOptionsButton = new WebInspector.ToolbarMenuButton(this._appendOptio
nsMenuItems.bind(this)); |
| 189 moreOptionsButton.setTitle(WebInspector.UIString('More options')); |
| 190 toolbar.appendToolbarItem(moreOptionsButton); |
| 191 |
| 192 toolbar.appendToolbarItem( |
| 193 this._wrapToolbarItem(createElementWithClass('div', 'device-mode-empty-t
oolbar-element'))); |
| 194 } |
| 195 |
| 196 /** |
| 197 * @param {!WebInspector.ContextMenu} contextMenu |
| 198 */ |
| 199 _appendScaleMenuItems(contextMenu) { |
| 200 if (this._model.type() === WebInspector.DeviceModeModel.Type.Device) { |
| 201 contextMenu.appendItem( |
| 202 WebInspector.UIString('Fit to window (%.0f%%)', this._model.fitScale()
* 100), |
| 203 this._onScaleMenuChanged.bind(this, this._model.fitScale()), false); |
| 204 contextMenu.appendSeparator(); |
| 205 } |
| 206 var boundAppendScaleItem = appendScaleItem.bind(this); |
| 207 boundAppendScaleItem(WebInspector.UIString('50%'), 0.5); |
| 208 boundAppendScaleItem(WebInspector.UIString('75%'), 0.75); |
| 209 boundAppendScaleItem(WebInspector.UIString('100%'), 1); |
| 210 boundAppendScaleItem(WebInspector.UIString('125%'), 1.25); |
| 211 boundAppendScaleItem(WebInspector.UIString('150%'), 1.5); |
| 212 |
| 213 /** |
| 214 * @param {string} title |
| 215 * @param {number} value |
| 216 * @this {!WebInspector.DeviceModeToolbar} |
| 217 */ |
| 218 function appendScaleItem(title, value) { |
| 219 contextMenu.appendCheckboxItem( |
| 220 title, this._onScaleMenuChanged.bind(this, value), this._model.scaleSe
tting().get() === value, false); |
| 221 } |
| 222 } |
| 223 |
| 224 /** |
| 225 * @param {number} value |
| 226 */ |
| 227 _onScaleMenuChanged(value) { |
| 228 var device = this._model.device(); |
| 229 if (device) |
| 230 this._lastScale.set(device, value); |
| 231 this._model.scaleSetting().set(value); |
| 232 } |
| 233 |
| 234 /** |
| 235 * @param {!WebInspector.ContextMenu} contextMenu |
| 236 */ |
| 237 _appendDeviceScaleMenuItems(contextMenu) { |
| 238 var deviceScaleFactorSetting = this._model.deviceScaleFactorSetting(); |
| 239 var defaultValue = this._model.uaSetting().get() === WebInspector.DeviceMode
Model.UA.Mobile || |
| 240 this._model.uaSetting().get() === WebInspector.DeviceModeModel.UA.Mo
bileNoTouch ? |
| 241 WebInspector.DeviceModeModel.defaultMobileScaleFactor : |
| 242 window.devicePixelRatio; |
| 243 appendDeviceScaleFactorItem(WebInspector.UIString('Default: %.1f', defaultVa
lue), 0); |
| 244 contextMenu.appendSeparator(); |
| 245 appendDeviceScaleFactorItem(WebInspector.UIString('1'), 1); |
| 246 appendDeviceScaleFactorItem(WebInspector.UIString('2'), 2); |
| 247 appendDeviceScaleFactorItem(WebInspector.UIString('3'), 3); |
| 248 |
| 249 /** |
| 250 * @param {string} title |
| 251 * @param {number} value |
| 252 */ |
| 253 function appendDeviceScaleFactorItem(title, value) { |
| 254 contextMenu.appendCheckboxItem( |
| 255 title, deviceScaleFactorSetting.set.bind(deviceScaleFactorSetting, val
ue), |
| 256 deviceScaleFactorSetting.get() === value); |
| 257 } |
| 258 } |
| 259 |
| 260 /** |
| 261 * @param {!WebInspector.ContextMenu} contextMenu |
| 262 */ |
| 263 _appendUserAgentMenuItems(contextMenu) { |
| 264 var uaSetting = this._model.uaSetting(); |
| 265 appendUAItem(WebInspector.DeviceModeModel.UA.Mobile, WebInspector.DeviceMode
Model.UA.Mobile); |
| 266 appendUAItem(WebInspector.DeviceModeModel.UA.MobileNoTouch, WebInspector.Dev
iceModeModel.UA.MobileNoTouch); |
| 267 appendUAItem(WebInspector.DeviceModeModel.UA.Desktop, WebInspector.DeviceMod
eModel.UA.Desktop); |
| 268 appendUAItem(WebInspector.DeviceModeModel.UA.DesktopTouch, WebInspector.Devi
ceModeModel.UA.DesktopTouch); |
| 269 |
| 270 /** |
| 271 * @param {string} title |
| 272 * @param {!WebInspector.DeviceModeModel.UA} value |
| 273 */ |
| 274 function appendUAItem(title, value) { |
| 275 contextMenu.appendCheckboxItem(title, uaSetting.set.bind(uaSetting, value)
, uaSetting.get() === value); |
| 276 } |
| 277 } |
| 278 |
| 279 /** |
| 280 * @param {!WebInspector.ContextMenu} contextMenu |
| 281 */ |
| 282 _appendOptionsMenuItems(contextMenu) { |
| 283 var model = this._model; |
| 284 appendToggleItem( |
| 285 this._deviceOutlineSetting, WebInspector.UIString('Hide device frame'), |
| 286 WebInspector.UIString('Show device frame'), model.type() !== WebInspecto
r.DeviceModeModel.Type.Device); |
| 287 appendToggleItem( |
| 288 this._showMediaInspectorSetting, WebInspector.UIString('Hide media queri
es'), |
| 289 WebInspector.UIString('Show media queries')); |
| 290 appendToggleItem( |
| 291 this._showRulersSetting, WebInspector.UIString('Hide rulers'), WebInspec
tor.UIString('Show rulers')); |
| 292 contextMenu.appendSeparator(); |
| 293 appendToggleItem( |
| 294 this._showDeviceScaleFactorSetting, WebInspector.UIString('Remove device
pixel ratio'), |
| 295 WebInspector.UIString('Add device pixel ratio')); |
| 296 appendToggleItem( |
| 297 this._showUserAgentTypeSetting, WebInspector.UIString('Remove device typ
e'), |
| 298 WebInspector.UIString('Add device type')); |
| 299 appendToggleItem( |
| 300 this._showNetworkConditionsSetting, WebInspector.UIString('Remove networ
k throttling'), |
| 301 WebInspector.UIString('Add network throttling')); |
| 302 contextMenu.appendSeparator(); |
| 303 contextMenu.appendItemsAtLocation('deviceModeMenu'); |
| 304 contextMenu.appendSeparator(); |
| 305 contextMenu.appendItem(WebInspector.UIString('Reset to defaults'), this._res
et.bind(this)); |
| 306 |
| 307 /** |
| 308 * @param {!WebInspector.Setting} setting |
| 309 * @param {string} title1 |
| 310 * @param {string} title2 |
| 311 * @param {boolean=} disabled |
| 312 */ |
| 313 function appendToggleItem(setting, title1, title2, disabled) { |
| 314 if (typeof disabled === 'undefined') |
| 315 disabled = model.type() === WebInspector.DeviceModeModel.Type.None; |
| 316 contextMenu.appendItem(setting.get() ? title1 : title2, setting.set.bind(s
etting, !setting.get()), disabled); |
| 317 } |
| 318 } |
| 319 |
| 320 _reset() { |
| 321 this._deviceOutlineSetting.set(false); |
| 322 this._showDeviceScaleFactorSetting.set(false); |
| 323 this._showUserAgentTypeSetting.set(false); |
| 324 this._showMediaInspectorSetting.set(false); |
| 325 this._showRulersSetting.set(false); |
| 326 this._showNetworkConditionsSetting.set(false); |
| 327 this._model.reset(); |
| 328 } |
| 329 |
| 330 /** |
| 331 * @param {!Element} element |
| 332 * @return {!WebInspector.ToolbarItem} |
| 333 */ |
| 334 _wrapToolbarItem(element) { |
| 335 var container = createElement('div'); |
| 336 var shadowRoot = WebInspector.createShadowRootWithCoreStyles(container, 'emu
lation/deviceModeToolbar.css'); |
| 337 shadowRoot.appendChild(element); |
| 338 return new WebInspector.ToolbarItem(container); |
| 339 } |
| 340 |
| 341 /** |
| 342 * @param {!WebInspector.EmulatedDevice} device |
| 343 */ |
| 344 _emulateDevice(device) { |
| 345 this._model.emulate( |
| 346 WebInspector.DeviceModeModel.Type.Device, device, this._lastMode.get(dev
ice) || device.modes[0], |
| 347 this._lastScale.get(device)); |
| 348 } |
| 349 |
| 350 _switchToResponsive() { |
| 351 this._model.emulate(WebInspector.DeviceModeModel.Type.Responsive, null, null
); |
| 352 } |
| 353 |
| 354 /** |
| 355 * @param {!Array<!WebInspector.EmulatedDevice>} devices |
| 356 * @return {!Array<!WebInspector.EmulatedDevice>} |
| 357 */ |
| 358 _filterDevices(devices) { |
| 359 devices = devices.filter(function(d) { |
| 360 return d.show(); |
| 361 }); |
| 362 devices.sort(WebInspector.EmulatedDevice.deviceComparator); |
| 363 return devices; |
| 364 } |
| 365 |
| 366 /** |
| 367 * @return {!Array<!WebInspector.EmulatedDevice>} |
| 368 */ |
| 369 _standardDevices() { |
| 370 return this._filterDevices(this._emulatedDevicesList.standard()); |
| 371 } |
| 372 |
| 373 /** |
| 374 * @return {!Array<!WebInspector.EmulatedDevice>} |
| 375 */ |
| 376 _customDevices() { |
| 377 return this._filterDevices(this._emulatedDevicesList.custom()); |
| 378 } |
| 379 |
| 380 /** |
| 381 * @return {!Array<!WebInspector.EmulatedDevice>} |
| 382 */ |
| 383 _allDevices() { |
| 384 return this._standardDevices().concat(this._customDevices()); |
| 385 } |
| 386 |
| 387 /** |
| 388 * @param {!WebInspector.ContextMenu} contextMenu |
| 389 */ |
| 390 _appendDeviceMenuItems(contextMenu) { |
| 391 contextMenu.appendCheckboxItem( |
| 392 WebInspector.UIString('Responsive'), this._switchToResponsive.bind(this)
, |
| 393 this._model.type() === WebInspector.DeviceModeModel.Type.Responsive, fal
se); |
| 394 appendGroup.call(this, this._standardDevices()); |
| 395 appendGroup.call(this, this._customDevices()); |
| 396 contextMenu.appendSeparator(); |
| 397 contextMenu.appendItem( |
| 398 WebInspector.UIString('Edit\u2026'), |
| 399 this._emulatedDevicesList.revealCustomSetting.bind(this._emulatedDevices
List), false); |
| 400 |
| 401 /** |
| 402 * @param {!Array<!WebInspector.EmulatedDevice>} devices |
| 403 * @this {WebInspector.DeviceModeToolbar} |
| 404 */ |
| 405 function appendGroup(devices) { |
| 406 if (!devices.length) |
| 407 return; |
| 408 contextMenu.appendSeparator(); |
| 409 for (var device of devices) |
| 410 contextMenu.appendCheckboxItem( |
| 411 device.title, this._emulateDevice.bind(this, device), this._model.de
vice() === device, false); |
| 412 } |
| 413 } |
| 414 |
| 415 /** |
| 416 * @this {WebInspector.DeviceModeToolbar} |
| 417 */ |
| 418 _deviceListChanged() { |
| 419 var device = this._model.device(); |
| 420 if (!device) |
| 421 return; |
| 422 |
| 423 var devices = this._allDevices(); |
| 424 if (devices.indexOf(device) === -1) { |
| 425 if (devices.length) |
| 426 this._emulateDevice(devices[0]); |
| 427 else |
| 428 this._model.emulate(WebInspector.DeviceModeModel.Type.Responsive, null,
null); |
| 429 } |
| 430 } |
| 431 |
| 432 _updateDeviceScaleFactorVisibility() { |
| 433 this._deviceScaleItem.setVisible(this._showDeviceScaleFactorSetting.get()); |
| 434 } |
| 435 |
| 436 _updateUserAgentTypeVisibility() { |
| 437 this._uaItem.setVisible(this._showUserAgentTypeSetting.get()); |
| 438 } |
| 439 |
| 440 _updateNetworkConditionsVisibility() { |
| 441 this._networkConditionsItem.setVisible(this._showNetworkConditionsSetting.ge
t()); |
| 442 } |
| 443 |
| 444 /** |
| 445 * @param {!WebInspector.Event} event |
| 446 */ |
| 447 _modeMenuClicked(event) { |
| 448 var device = this._model.device(); |
| 449 var model = this._model; |
| 450 |
| 451 if (device.modes.length === 2 && device.modes[0].orientation !== device.mode
s[1].orientation) { |
| 452 model.emulate(model.type(), model.device(), model.mode() === device.modes[
0] ? device.modes[1] : device.modes[0]); |
| 453 return; |
| 454 } |
| 455 |
| 456 var contextMenu = new WebInspector.ContextMenu( |
| 457 /** @type {!Event} */ (event.data), false, event.target.element.totalOff
setLeft(), |
| 458 event.target.element.totalOffsetTop() + event.target.element.offsetHeigh
t); |
| 459 addOrientation(WebInspector.EmulatedDevice.Vertical, WebInspector.UIString('
Portrait')); |
| 460 addOrientation(WebInspector.EmulatedDevice.Horizontal, WebInspector.UIString
('Landscape')); |
| 461 contextMenu.show(); |
| 462 |
| 463 /** |
| 464 * @param {string} orientation |
| 465 * @param {string} title |
| 466 */ |
| 467 function addOrientation(orientation, title) { |
| 468 var modes = device.modesForOrientation(orientation); |
| 469 if (!modes.length) |
| 470 return; |
| 471 if (modes.length === 1) { |
| 472 addMode(modes[0], title); |
| 473 } else { |
| 474 for (var index = 0; index < modes.length; index++) |
| 475 addMode(modes[index], title + ' \u2013 ' + modes[index].title); |
| 476 } |
| 477 } |
| 478 |
| 479 /** |
| 480 * @param {!WebInspector.EmulatedDevice.Mode} mode |
| 481 * @param {string} title |
| 482 */ |
| 483 function addMode(mode, title) { |
| 484 contextMenu.appendCheckboxItem(title, applyMode.bind(null, mode), model.mo
de() === mode, false); |
| 485 } |
| 486 |
| 487 /** |
| 488 * @param {!WebInspector.EmulatedDevice.Mode} mode |
| 489 */ |
| 490 function applyMode(mode) { |
| 491 model.emulate(model.type(), model.device(), mode); |
| 492 } |
| 493 } |
| 494 |
| 495 /** |
| 496 * @return {!Element} |
| 497 */ |
| 498 element() { |
| 499 return this._element; |
| 500 } |
| 501 |
| 502 update() { |
| 503 if (this._model.type() !== this._cachedModelType) { |
| 504 this._cachedModelType = this._model.type(); |
| 505 this._widthInput.disabled = this._model.type() !== WebInspector.DeviceMode
Model.Type.Responsive; |
| 506 this._heightInput.disabled = this._model.type() !== WebInspector.DeviceMod
eModel.Type.Responsive; |
| 507 this._deviceScaleItem.setEnabled(this._model.type() === WebInspector.Devic
eModeModel.Type.Responsive); |
| 508 this._uaItem.setEnabled(this._model.type() === WebInspector.DeviceModeMode
l.Type.Responsive); |
| 509 } |
| 510 |
| 511 var size = this._model.appliedDeviceSize(); |
| 512 this._updateHeightInput( |
| 513 this._model.type() === WebInspector.DeviceModeModel.Type.Responsive && t
his._model.isFullHeight() ? |
| 514 '' : |
| 515 String(size.height)); |
| 516 this._updateWidthInput(String(size.width)); |
| 517 this._heightInput.placeholder = size.height; |
| 518 |
| 519 if (this._model.scale() !== this._cachedScale) { |
| 520 this._scaleItem.setText(WebInspector.UIString('%.0f%%', this._model.scale(
) * 100)); |
| 521 this._cachedScale = this._model.scale(); |
| 522 } |
| 523 |
| 524 var deviceScale = this._model.appliedDeviceScaleFactor(); |
| 525 if (deviceScale !== this._cachedDeviceScale) { |
| 526 this._deviceScaleItem.setText(WebInspector.UIString('DPR: %.1f', deviceSca
le)); |
| 527 this._cachedDeviceScale = deviceScale; |
| 528 } |
| 529 |
| 530 var uaType = this._model.appliedUserAgentType(); |
| 531 if (uaType !== this._cachedUaType) { |
| 532 this._uaItem.setText(uaType); |
| 533 this._cachedUaType = uaType; |
| 534 } |
| 535 |
| 536 var deviceItemTitle = WebInspector.UIString('None'); |
| 537 if (this._model.type() === WebInspector.DeviceModeModel.Type.Responsive) |
| 538 deviceItemTitle = WebInspector.UIString('Responsive'); |
| 539 if (this._model.type() === WebInspector.DeviceModeModel.Type.Device) |
| 540 deviceItemTitle = this._model.device().title; |
| 541 this._deviceSelectItem.setText(deviceItemTitle); |
| 542 |
| 543 if (this._model.device() !== this._cachedModelDevice) { |
| 544 var device = this._model.device(); |
| 545 this._modeButton.setVisible(!!device); |
| 546 if (device) { |
| 547 var modeCount = device ? device.modes.length : 0; |
| 548 this._modeButton.setEnabled(modeCount >= 2); |
| 549 this._modeButton.setTitle( |
| 550 modeCount === 2 ? WebInspector.UIString('Rotate') : WebInspector.UIS
tring('Screen options')); |
| 551 } |
| 552 this._cachedModelDevice = device; |
| 553 } |
| 554 |
| 555 if (this._model.type() === WebInspector.DeviceModeModel.Type.Device) |
| 556 this._lastMode.set( |
| 557 /** @type {!WebInspector.EmulatedDevice} */ (this._model.device()), |
| 558 /** @type {!WebInspector.EmulatedDevice.Mode} */ (this._model.mode()))
; |
| 559 |
| 560 if (this._model.mode() !== this._cachedModelMode && this._model.type() !== W
ebInspector.DeviceModeModel.Type.None) { |
| 561 this._cachedModelMode = this._model.mode(); |
| 562 var value = this._persistenceSetting.get(); |
| 563 if (this._model.device()) { |
| 564 value.device = this._model.device().title; |
| 565 value.orientation = this._model.mode() ? this._model.mode().orientation
: ''; |
| 566 value.mode = this._model.mode() ? this._model.mode().title : ''; |
| 567 } else { |
| 568 value.device = ''; |
| 569 value.orientation = ''; |
| 570 value.mode = ''; |
| 571 } |
| 572 this._persistenceSetting.set(value); |
| 573 } |
| 574 } |
| 575 |
| 576 restore() { |
| 577 for (var device of this._allDevices()) { |
| 578 if (device.title === this._persistenceSetting.get().device) { |
| 579 for (var mode of device.modes) { |
| 580 if (mode.orientation === this._persistenceSetting.get().orientation && |
| 581 mode.title === this._persistenceSetting.get().mode) { |
| 582 this._lastMode.set(device, mode); |
| 583 this._emulateDevice(device); |
| 584 return; |
| 585 } |
| 586 } |
| 587 } |
| 588 } |
| 589 |
| 590 this._model.emulate(WebInspector.DeviceModeModel.Type.Responsive, null, null
); |
| 591 } |
62 }; | 592 }; |
63 | |
64 WebInspector.DeviceModeToolbar.prototype = { | |
65 /** | |
66 * @param {!WebInspector.Toolbar} toolbar | |
67 */ | |
68 _fillLeftToolbar: function(toolbar) | |
69 { | |
70 toolbar.appendToolbarItem(this._wrapToolbarItem(createElementWithClass("
div", "device-mode-empty-toolbar-element"))); | |
71 this._deviceSelectItem = new WebInspector.ToolbarMenuButton(this._append
DeviceMenuItems.bind(this)); | |
72 this._deviceSelectItem.setGlyph(""); | |
73 this._deviceSelectItem.turnIntoSelect(95); | |
74 toolbar.appendToolbarItem(this._deviceSelectItem); | |
75 }, | |
76 | |
77 /** | |
78 * @param {!WebInspector.Toolbar} toolbar | |
79 */ | |
80 _fillMainToolbar: function(toolbar) | |
81 { | |
82 var widthInput = createElementWithClass("input", "device-mode-size-input
"); | |
83 widthInput.maxLength = 4; | |
84 widthInput.type = "text"; | |
85 widthInput.title = WebInspector.UIString("Width"); | |
86 this._updateWidthInput = WebInspector.bindInput(widthInput, this._applyW
idth.bind(this), WebInspector.DeviceModeModel.deviceSizeValidator, true); | |
87 this._widthInput = widthInput; | |
88 this._widthItem = this._wrapToolbarItem(widthInput); | |
89 toolbar.appendToolbarItem(this._widthItem); | |
90 | |
91 var xElement = createElementWithClass("div", "device-mode-x"); | |
92 xElement.textContent = "\u00D7"; | |
93 this._xItem = this._wrapToolbarItem(xElement); | |
94 toolbar.appendToolbarItem(this._xItem); | |
95 | |
96 var heightInput = createElementWithClass("input", "device-mode-size-inpu
t"); | |
97 heightInput.maxLength = 4; | |
98 heightInput.type = "text"; | |
99 heightInput.title = WebInspector.UIString("Height (leave empty for full)
"); | |
100 this._updateHeightInput = WebInspector.bindInput(heightInput, this._appl
yHeight.bind(this), validateHeight, true); | |
101 this._heightInput = heightInput; | |
102 this._heightItem = this._wrapToolbarItem(heightInput); | |
103 toolbar.appendToolbarItem(this._heightItem); | |
104 | |
105 /** | |
106 * @param {string} value | |
107 * @return {boolean} | |
108 */ | |
109 function validateHeight(value) | |
110 { | |
111 return !value || WebInspector.DeviceModeModel.deviceSizeValidator(va
lue); | |
112 } | |
113 }, | |
114 | |
115 /** | |
116 * @param {string} value | |
117 */ | |
118 _applyWidth: function(value) | |
119 { | |
120 var width = value ? Number(value) : 0; | |
121 this._model.setWidthAndScaleToFit(width); | |
122 }, | |
123 | |
124 /** | |
125 * @param {string} value | |
126 */ | |
127 _applyHeight: function(value) | |
128 { | |
129 var height = value ? Number(value) : 0; | |
130 this._model.setHeightAndScaleToFit(height); | |
131 }, | |
132 | |
133 /** | |
134 * @param {!WebInspector.Toolbar} toolbar | |
135 */ | |
136 _fillRightToolbar: function(toolbar) | |
137 { | |
138 toolbar.appendToolbarItem(this._wrapToolbarItem(createElementWithClass("
div", "device-mode-empty-toolbar-element"))); | |
139 this._scaleItem = new WebInspector.ToolbarMenuButton(this._appendScaleMe
nuItems.bind(this)); | |
140 this._scaleItem.setTitle(WebInspector.UIString("Zoom")); | |
141 this._scaleItem.setGlyph(""); | |
142 this._scaleItem.turnIntoSelect(); | |
143 toolbar.appendToolbarItem(this._scaleItem); | |
144 | |
145 toolbar.appendToolbarItem(this._wrapToolbarItem(createElementWithClass("
div", "device-mode-empty-toolbar-element"))); | |
146 this._deviceScaleItem = new WebInspector.ToolbarMenuButton(this._appendD
eviceScaleMenuItems.bind(this)); | |
147 this._deviceScaleItem.setVisible(this._showDeviceScaleFactorSetting.get(
)); | |
148 this._deviceScaleItem.setTitle(WebInspector.UIString("Device pixel ratio
")); | |
149 this._deviceScaleItem.setGlyph(""); | |
150 this._deviceScaleItem.turnIntoSelect(); | |
151 this._deviceScaleItem.element.style.padding = "0 5px"; | |
152 toolbar.appendToolbarItem(this._deviceScaleItem); | |
153 | |
154 toolbar.appendToolbarItem(this._wrapToolbarItem(createElementWithClass("
div", "device-mode-empty-toolbar-element"))); | |
155 this._uaItem = new WebInspector.ToolbarMenuButton(this._appendUserAgentM
enuItems.bind(this)); | |
156 this._uaItem.setVisible(this._showUserAgentTypeSetting.get()); | |
157 this._uaItem.setTitle(WebInspector.UIString("Device type")); | |
158 this._uaItem.setGlyph(""); | |
159 this._uaItem.turnIntoSelect(); | |
160 this._uaItem.element.style.padding = "0 5px"; | |
161 toolbar.appendToolbarItem(this._uaItem); | |
162 }, | |
163 | |
164 /** | |
165 * @param {!WebInspector.Toolbar} toolbar | |
166 */ | |
167 _fillModeToolbar: function(toolbar) | |
168 { | |
169 toolbar.appendToolbarItem(this._wrapToolbarItem(createElementWithClass("
div", "device-mode-empty-toolbar-element"))); | |
170 this._modeButton = new WebInspector.ToolbarButton("", "rotate-screen-too
lbar-item"); | |
171 this._modeButton.addEventListener("click", this._modeMenuClicked, this); | |
172 toolbar.appendToolbarItem(this._modeButton); | |
173 }, | |
174 | |
175 /** | |
176 * @param {!WebInspector.Toolbar} toolbar | |
177 */ | |
178 _fillOptionsToolbar: function(toolbar) | |
179 { | |
180 this._networkConditionsItem = WebInspector.NetworkConditionsSelector.cre
ateToolbarMenuButton(); | |
181 this._networkConditionsItem.setVisible(this._showNetworkConditionsSettin
g.get()); | |
182 this._networkConditionsItem.setTitle(WebInspector.UIString("Network thro
ttling")); | |
183 this._networkConditionsItem.element.style.padding = "0 5px"; | |
184 this._networkConditionsItem.element.style.maxWidth = "140px"; | |
185 toolbar.appendToolbarItem(this._networkConditionsItem); | |
186 | |
187 var moreOptionsButton = new WebInspector.ToolbarMenuButton(this._appendO
ptionsMenuItems.bind(this)); | |
188 moreOptionsButton.setTitle(WebInspector.UIString("More options")); | |
189 toolbar.appendToolbarItem(moreOptionsButton); | |
190 | |
191 toolbar.appendToolbarItem(this._wrapToolbarItem(createElementWithClass("
div", "device-mode-empty-toolbar-element"))); | |
192 }, | |
193 | |
194 | |
195 /** | |
196 * @param {!WebInspector.ContextMenu} contextMenu | |
197 */ | |
198 _appendScaleMenuItems: function(contextMenu) | |
199 { | |
200 if (this._model.type() === WebInspector.DeviceModeModel.Type.Device) { | |
201 contextMenu.appendItem(WebInspector.UIString("Fit to window (%.0f%%)
", this._model.fitScale() * 100), this._onScaleMenuChanged.bind(this, this._mode
l.fitScale()), false); | |
202 contextMenu.appendSeparator(); | |
203 } | |
204 var boundAppendScaleItem = appendScaleItem.bind(this); | |
205 boundAppendScaleItem(WebInspector.UIString("50%"), 0.5); | |
206 boundAppendScaleItem(WebInspector.UIString("75%"), 0.75); | |
207 boundAppendScaleItem(WebInspector.UIString("100%"), 1); | |
208 boundAppendScaleItem(WebInspector.UIString("125%"), 1.25); | |
209 boundAppendScaleItem(WebInspector.UIString("150%"), 1.5); | |
210 | |
211 /** | |
212 * @param {string} title | |
213 * @param {number} value | |
214 * @this {!WebInspector.DeviceModeToolbar} | |
215 */ | |
216 function appendScaleItem(title, value) | |
217 { | |
218 contextMenu.appendCheckboxItem(title, this._onScaleMenuChanged.bind(
this, value), this._model.scaleSetting().get() === value, false); | |
219 } | |
220 }, | |
221 | |
222 /** | |
223 * @param {number} value | |
224 */ | |
225 _onScaleMenuChanged: function(value) | |
226 { | |
227 var device = this._model.device(); | |
228 if (device) | |
229 this._lastScale.set(device, value); | |
230 this._model.scaleSetting().set(value); | |
231 }, | |
232 | |
233 /** | |
234 * @param {!WebInspector.ContextMenu} contextMenu | |
235 */ | |
236 _appendDeviceScaleMenuItems: function(contextMenu) | |
237 { | |
238 var deviceScaleFactorSetting = this._model.deviceScaleFactorSetting(); | |
239 var defaultValue = this._model.uaSetting().get() === WebInspector.Device
ModeModel.UA.Mobile || this._model.uaSetting().get() === WebInspector.DeviceMode
Model.UA.MobileNoTouch ? WebInspector.DeviceModeModel.defaultMobileScaleFactor :
window.devicePixelRatio; | |
240 appendDeviceScaleFactorItem(WebInspector.UIString("Default: %.1f", defau
ltValue), 0); | |
241 contextMenu.appendSeparator(); | |
242 appendDeviceScaleFactorItem(WebInspector.UIString("1"), 1); | |
243 appendDeviceScaleFactorItem(WebInspector.UIString("2"), 2); | |
244 appendDeviceScaleFactorItem(WebInspector.UIString("3"), 3); | |
245 | |
246 /** | |
247 * @param {string} title | |
248 * @param {number} value | |
249 */ | |
250 function appendDeviceScaleFactorItem(title, value) | |
251 { | |
252 contextMenu.appendCheckboxItem(title, deviceScaleFactorSetting.set.b
ind(deviceScaleFactorSetting, value), deviceScaleFactorSetting.get() === value); | |
253 } | |
254 }, | |
255 | |
256 /** | |
257 * @param {!WebInspector.ContextMenu} contextMenu | |
258 */ | |
259 _appendUserAgentMenuItems: function(contextMenu) | |
260 { | |
261 var uaSetting = this._model.uaSetting(); | |
262 appendUAItem(WebInspector.DeviceModeModel.UA.Mobile, WebInspector.Device
ModeModel.UA.Mobile); | |
263 appendUAItem(WebInspector.DeviceModeModel.UA.MobileNoTouch, WebInspector
.DeviceModeModel.UA.MobileNoTouch); | |
264 appendUAItem(WebInspector.DeviceModeModel.UA.Desktop, WebInspector.Devic
eModeModel.UA.Desktop); | |
265 appendUAItem(WebInspector.DeviceModeModel.UA.DesktopTouch, WebInspector.
DeviceModeModel.UA.DesktopTouch); | |
266 | |
267 /** | |
268 * @param {string} title | |
269 * @param {!WebInspector.DeviceModeModel.UA} value | |
270 */ | |
271 function appendUAItem(title, value) | |
272 { | |
273 contextMenu.appendCheckboxItem(title, uaSetting.set.bind(uaSetting,
value), uaSetting.get() === value); | |
274 } | |
275 }, | |
276 | |
277 /** | |
278 * @param {!WebInspector.ContextMenu} contextMenu | |
279 */ | |
280 _appendOptionsMenuItems: function(contextMenu) | |
281 { | |
282 var model = this._model; | |
283 appendToggleItem(this._deviceOutlineSetting, WebInspector.UIString("Hide
device frame"), WebInspector.UIString("Show device frame"), model.type() !== We
bInspector.DeviceModeModel.Type.Device); | |
284 appendToggleItem(this._showMediaInspectorSetting, WebInspector.UIString(
"Hide media queries"), WebInspector.UIString("Show media queries")); | |
285 appendToggleItem(this._showRulersSetting, WebInspector.UIString("Hide ru
lers"), WebInspector.UIString("Show rulers")); | |
286 contextMenu.appendSeparator(); | |
287 appendToggleItem(this._showDeviceScaleFactorSetting, WebInspector.UIStri
ng("Remove device pixel ratio"), WebInspector.UIString("Add device pixel ratio")
); | |
288 appendToggleItem(this._showUserAgentTypeSetting, WebInspector.UIString("
Remove device type"), WebInspector.UIString("Add device type")); | |
289 appendToggleItem(this._showNetworkConditionsSetting, WebInspector.UIStri
ng("Remove network throttling"), WebInspector.UIString("Add network throttling")
); | |
290 contextMenu.appendSeparator(); | |
291 contextMenu.appendItemsAtLocation("deviceModeMenu"); | |
292 contextMenu.appendSeparator(); | |
293 contextMenu.appendItem(WebInspector.UIString("Reset to defaults"), this.
_reset.bind(this)); | |
294 | |
295 /** | |
296 * @param {!WebInspector.Setting} setting | |
297 * @param {string} title1 | |
298 * @param {string} title2 | |
299 * @param {boolean=} disabled | |
300 */ | |
301 function appendToggleItem(setting, title1, title2, disabled) | |
302 { | |
303 if (typeof disabled === "undefined") | |
304 disabled = model.type() === WebInspector.DeviceModeModel.Type.No
ne; | |
305 contextMenu.appendItem(setting.get() ? title1 : title2, setting.set.
bind(setting, !setting.get()), disabled); | |
306 } | |
307 }, | |
308 | |
309 _reset: function() | |
310 { | |
311 this._deviceOutlineSetting.set(false); | |
312 this._showDeviceScaleFactorSetting.set(false); | |
313 this._showUserAgentTypeSetting.set(false); | |
314 this._showMediaInspectorSetting.set(false); | |
315 this._showRulersSetting.set(false); | |
316 this._showNetworkConditionsSetting.set(false); | |
317 this._model.reset(); | |
318 }, | |
319 | |
320 /** | |
321 * @param {!Element} element | |
322 * @return {!WebInspector.ToolbarItem} | |
323 */ | |
324 _wrapToolbarItem: function(element) | |
325 { | |
326 var container = createElement("div"); | |
327 var shadowRoot = WebInspector.createShadowRootWithCoreStyles(container,
"emulation/deviceModeToolbar.css"); | |
328 shadowRoot.appendChild(element); | |
329 return new WebInspector.ToolbarItem(container); | |
330 }, | |
331 | |
332 /** | |
333 * @param {!WebInspector.EmulatedDevice} device | |
334 */ | |
335 _emulateDevice: function(device) | |
336 { | |
337 this._model.emulate(WebInspector.DeviceModeModel.Type.Device, device, th
is._lastMode.get(device) || device.modes[0], this._lastScale.get(device)); | |
338 }, | |
339 | |
340 _switchToResponsive: function() | |
341 { | |
342 this._model.emulate(WebInspector.DeviceModeModel.Type.Responsive, null,
null); | |
343 }, | |
344 | |
345 /** | |
346 * @param {!Array<!WebInspector.EmulatedDevice>} devices | |
347 * @return {!Array<!WebInspector.EmulatedDevice>} | |
348 */ | |
349 _filterDevices: function(devices) | |
350 { | |
351 devices = devices.filter(function(d) { return d.show(); }); | |
352 devices.sort(WebInspector.EmulatedDevice.deviceComparator); | |
353 return devices; | |
354 }, | |
355 | |
356 /** | |
357 * @return {!Array<!WebInspector.EmulatedDevice>} | |
358 */ | |
359 _standardDevices: function() | |
360 { | |
361 return this._filterDevices(this._emulatedDevicesList.standard()); | |
362 }, | |
363 | |
364 /** | |
365 * @return {!Array<!WebInspector.EmulatedDevice>} | |
366 */ | |
367 _customDevices: function() | |
368 { | |
369 return this._filterDevices(this._emulatedDevicesList.custom()); | |
370 }, | |
371 | |
372 /** | |
373 * @return {!Array<!WebInspector.EmulatedDevice>} | |
374 */ | |
375 _allDevices: function() | |
376 { | |
377 return this._standardDevices().concat(this._customDevices()); | |
378 }, | |
379 | |
380 /** | |
381 * @param {!WebInspector.ContextMenu} contextMenu | |
382 */ | |
383 _appendDeviceMenuItems: function(contextMenu) | |
384 { | |
385 contextMenu.appendCheckboxItem(WebInspector.UIString("Responsive"), this
._switchToResponsive.bind(this), this._model.type() === WebInspector.DeviceModeM
odel.Type.Responsive, false); | |
386 appendGroup.call(this, this._standardDevices()); | |
387 appendGroup.call(this, this._customDevices()); | |
388 contextMenu.appendSeparator(); | |
389 contextMenu.appendItem(WebInspector.UIString("Edit\u2026"), this._emulat
edDevicesList.revealCustomSetting.bind(this._emulatedDevicesList), false); | |
390 | |
391 /** | |
392 * @param {!Array<!WebInspector.EmulatedDevice>} devices | |
393 * @this {WebInspector.DeviceModeToolbar} | |
394 */ | |
395 function appendGroup(devices) | |
396 { | |
397 if (!devices.length) | |
398 return; | |
399 contextMenu.appendSeparator(); | |
400 for (var device of devices) | |
401 contextMenu.appendCheckboxItem(device.title, this._emulateDevice
.bind(this, device), this._model.device() === device, false); | |
402 } | |
403 }, | |
404 | |
405 /** | |
406 * @this {WebInspector.DeviceModeToolbar} | |
407 */ | |
408 _deviceListChanged: function() | |
409 { | |
410 var device = this._model.device(); | |
411 if (!device) | |
412 return; | |
413 | |
414 var devices = this._allDevices(); | |
415 if (devices.indexOf(device) === -1) { | |
416 if (devices.length) | |
417 this._emulateDevice(devices[0]); | |
418 else | |
419 this._model.emulate(WebInspector.DeviceModeModel.Type.Responsive
, null, null); | |
420 } | |
421 }, | |
422 | |
423 _updateDeviceScaleFactorVisibility: function() | |
424 { | |
425 this._deviceScaleItem.setVisible(this._showDeviceScaleFactorSetting.get(
)); | |
426 }, | |
427 | |
428 _updateUserAgentTypeVisibility: function() | |
429 { | |
430 this._uaItem.setVisible(this._showUserAgentTypeSetting.get()); | |
431 }, | |
432 | |
433 _updateNetworkConditionsVisibility: function() | |
434 { | |
435 this._networkConditionsItem.setVisible(this._showNetworkConditionsSettin
g.get()); | |
436 }, | |
437 | |
438 /** | |
439 * @param {!WebInspector.Event} event | |
440 */ | |
441 _modeMenuClicked: function(event) | |
442 { | |
443 var device = this._model.device(); | |
444 var model = this._model; | |
445 | |
446 if (device.modes.length === 2 && device.modes[0].orientation !== device.
modes[1].orientation) { | |
447 model.emulate(model.type(), model.device(), model.mode() === device.
modes[0] ? device.modes[1] : device.modes[0]); | |
448 return; | |
449 } | |
450 | |
451 var contextMenu = new WebInspector.ContextMenu(/** @type {!Event} */ (ev
ent.data), | |
452 false, | |
453 event.target.element.totalOffsetLeft(), | |
454 event.target.element.totalOffsetTop() + event.target.element.offsetH
eight); | |
455 addOrientation(WebInspector.EmulatedDevice.Vertical, WebInspector.UIStri
ng("Portrait")); | |
456 addOrientation(WebInspector.EmulatedDevice.Horizontal, WebInspector.UISt
ring("Landscape")); | |
457 contextMenu.show(); | |
458 | |
459 /** | |
460 * @param {string} orientation | |
461 * @param {string} title | |
462 */ | |
463 function addOrientation(orientation, title) | |
464 { | |
465 var modes = device.modesForOrientation(orientation); | |
466 if (!modes.length) | |
467 return; | |
468 if (modes.length === 1) { | |
469 addMode(modes[0], title); | |
470 } else { | |
471 for (var index = 0; index < modes.length; index++) | |
472 addMode(modes[index], title + " \u2013 " + modes[index].titl
e); | |
473 } | |
474 } | |
475 | |
476 /** | |
477 * @param {!WebInspector.EmulatedDevice.Mode} mode | |
478 * @param {string} title | |
479 */ | |
480 function addMode(mode, title) | |
481 { | |
482 contextMenu.appendCheckboxItem(title, applyMode.bind(null, mode), mo
del.mode() === mode, false); | |
483 } | |
484 | |
485 /** | |
486 * @param {!WebInspector.EmulatedDevice.Mode} mode | |
487 */ | |
488 function applyMode(mode) | |
489 { | |
490 model.emulate(model.type(), model.device(), mode); | |
491 } | |
492 }, | |
493 | |
494 /** | |
495 * @return {!Element} | |
496 */ | |
497 element: function() | |
498 { | |
499 return this._element; | |
500 }, | |
501 | |
502 update: function() | |
503 { | |
504 if (this._model.type() !== this._cachedModelType) { | |
505 this._cachedModelType = this._model.type(); | |
506 this._widthInput.disabled = this._model.type() !== WebInspector.Devi
ceModeModel.Type.Responsive; | |
507 this._heightInput.disabled = this._model.type() !== WebInspector.Dev
iceModeModel.Type.Responsive; | |
508 this._deviceScaleItem.setEnabled(this._model.type() === WebInspector
.DeviceModeModel.Type.Responsive); | |
509 this._uaItem.setEnabled(this._model.type() === WebInspector.DeviceMo
deModel.Type.Responsive); | |
510 } | |
511 | |
512 var size = this._model.appliedDeviceSize(); | |
513 this._updateHeightInput(this._model.type() === WebInspector.DeviceModeMo
del.Type.Responsive && this._model.isFullHeight() ? "" : String(size.height)); | |
514 this._updateWidthInput(String(size.width)); | |
515 this._heightInput.placeholder = size.height; | |
516 | |
517 if (this._model.scale() !== this._cachedScale) { | |
518 this._scaleItem.setText(WebInspector.UIString("%.0f%%", this._model.
scale() * 100)); | |
519 this._cachedScale = this._model.scale(); | |
520 } | |
521 | |
522 var deviceScale = this._model.appliedDeviceScaleFactor(); | |
523 if (deviceScale !== this._cachedDeviceScale) { | |
524 this._deviceScaleItem.setText(WebInspector.UIString("DPR: %.1f", dev
iceScale)); | |
525 this._cachedDeviceScale = deviceScale; | |
526 } | |
527 | |
528 var uaType = this._model.appliedUserAgentType(); | |
529 if (uaType !== this._cachedUaType) { | |
530 this._uaItem.setText(uaType); | |
531 this._cachedUaType = uaType; | |
532 } | |
533 | |
534 var deviceItemTitle = WebInspector.UIString("None"); | |
535 if (this._model.type() === WebInspector.DeviceModeModel.Type.Responsive) | |
536 deviceItemTitle = WebInspector.UIString("Responsive"); | |
537 if (this._model.type() === WebInspector.DeviceModeModel.Type.Device) | |
538 deviceItemTitle = this._model.device().title; | |
539 this._deviceSelectItem.setText(deviceItemTitle); | |
540 | |
541 if (this._model.device() !== this._cachedModelDevice) { | |
542 var device = this._model.device(); | |
543 this._modeButton.setVisible(!!device); | |
544 if (device) { | |
545 var modeCount = device ? device.modes.length : 0; | |
546 this._modeButton.setEnabled(modeCount >= 2); | |
547 this._modeButton.setTitle(modeCount === 2 ? WebInspector.UIStrin
g("Rotate") : WebInspector.UIString("Screen options")); | |
548 } | |
549 this._cachedModelDevice = device; | |
550 } | |
551 | |
552 if (this._model.type() === WebInspector.DeviceModeModel.Type.Device) | |
553 this._lastMode.set(/** @type {!WebInspector.EmulatedDevice} */ (this
._model.device()), /** @type {!WebInspector.EmulatedDevice.Mode} */ (this._model
.mode())); | |
554 | |
555 if (this._model.mode() !== this._cachedModelMode && this._model.type() !
== WebInspector.DeviceModeModel.Type.None) { | |
556 this._cachedModelMode = this._model.mode(); | |
557 var value = this._persistenceSetting.get(); | |
558 if (this._model.device()) { | |
559 value.device = this._model.device().title; | |
560 value.orientation = this._model.mode() ? this._model.mode().orie
ntation : ""; | |
561 value.mode = this._model.mode() ? this._model.mode().title : ""; | |
562 } else { | |
563 value.device = ""; | |
564 value.orientation = ""; | |
565 value.mode = ""; | |
566 } | |
567 this._persistenceSetting.set(value); | |
568 } | |
569 }, | |
570 | |
571 restore: function() | |
572 { | |
573 for (var device of this._allDevices()) { | |
574 if (device.title === this._persistenceSetting.get().device) { | |
575 for (var mode of device.modes) { | |
576 if (mode.orientation === this._persistenceSetting.get().orie
ntation && mode.title === this._persistenceSetting.get().mode) { | |
577 this._lastMode.set(device, mode); | |
578 this._emulateDevice(device); | |
579 return; | |
580 } | |
581 } | |
582 } | |
583 } | |
584 | |
585 this._model.emulate(WebInspector.DeviceModeModel.Type.Responsive, null,
null); | |
586 } | |
587 }; | |
OLD | NEW |