| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @constructor | 6 * @constructor |
| 7 * @param {function()} updateCallback | 7 * @param {function()} updateCallback |
| 8 * @implements {WebInspector.TargetManager.Observer} | 8 * @implements {WebInspector.TargetManager.Observer} |
| 9 */ | 9 */ |
| 10 WebInspector.DeviceModeModel = function(updateCallback) | 10 WebInspector.DeviceModeModel = function(updateCallback) |
| 11 { | 11 { |
| 12 this._updateCallback = updateCallback; | 12 this._updateCallback = updateCallback; |
| 13 this._screenRect = new WebInspector.Rect(0, 0, 1, 1); | 13 this._screenRect = new WebInspector.Rect(0, 0, 1, 1); |
| 14 this._visiblePageRect = new WebInspector.Rect(0, 0, 1, 1); | 14 this._visiblePageRect = new WebInspector.Rect(0, 0, 1, 1); |
| 15 this._availableSize = new Size(1, 1); | 15 this._availableSize = new Size(1, 1); |
| 16 this._preferredSize = new Size(1, 1); | 16 this._preferredSize = new Size(1, 1); |
| 17 this._initialized = false; |
| 17 this._deviceMetricsThrottler = new WebInspector.Throttler(0); | 18 this._deviceMetricsThrottler = new WebInspector.Throttler(0); |
| 18 this._appliedDeviceSize = new Size(1, 1); | 19 this._appliedDeviceSize = new Size(1, 1); |
| 19 this._currentDeviceScaleFactor = window.devicePixelRatio; | 20 this._currentDeviceScaleFactor = window.devicePixelRatio; |
| 20 this._appliedDeviceScaleFactor = 0; | 21 this._appliedDeviceScaleFactor = 0; |
| 21 | 22 |
| 22 this._scaleSetting = WebInspector.settings.createSetting("emulation.deviceSc
ale", 1); | 23 this._scaleSetting = WebInspector.settings.createSetting("emulation.deviceSc
ale", 1); |
| 23 this._scaleSetting.addChangeListener(this._scaleSettingChanged, this); | 24 this._scaleSetting.addChangeListener(this._scaleSettingChanged, this); |
| 24 // We've used to allow zero before. | 25 // We've used to allow zero before. |
| 25 if (!this._scaleSetting.get()) | 26 if (!this._scaleSetting.get()) |
| 26 this._scaleSetting.set(1); | 27 this._scaleSetting.set(1); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 Device: "Device" | 63 Device: "Device" |
| 63 } | 64 } |
| 64 | 65 |
| 65 /** @enum {string} */ | 66 /** @enum {string} */ |
| 66 WebInspector.DeviceModeModel.UA = { | 67 WebInspector.DeviceModeModel.UA = { |
| 67 Mobile: "Mobile", | 68 Mobile: "Mobile", |
| 68 Desktop: "Desktop", | 69 Desktop: "Desktop", |
| 69 DesktopTouch: "DesktopTouch" | 70 DesktopTouch: "DesktopTouch" |
| 70 } | 71 } |
| 71 | 72 |
| 72 WebInspector.DeviceModeModel.MaxDeviceSize = 10000; | 73 WebInspector.DeviceModeModel.MaxDeviceSize = 9999; |
| 73 | 74 |
| 74 /** | 75 /** |
| 75 * @param {string} value | 76 * @param {string} value |
| 76 * @return {string} | 77 * @return {string} |
| 77 */ | 78 */ |
| 78 WebInspector.DeviceModeModel.deviceSizeValidator = function(value) | 79 WebInspector.DeviceModeModel.deviceSizeValidator = function(value) |
| 79 { | 80 { |
| 80 if (/^[\d]+$/.test(value) && value > 0 && value <= WebInspector.DeviceModeMo
del.MaxDeviceSize) | 81 if (/^[\d]+$/.test(value) && value > 0 && value <= WebInspector.DeviceModeMo
del.MaxDeviceSize) |
| 81 return ""; | 82 return ""; |
| 82 return WebInspector.UIString("Value must be positive integer"); | 83 return WebInspector.UIString("Value must be positive integer"); |
| 83 } | 84 } |
| 84 | 85 |
| 85 WebInspector.DeviceModeModel._touchEventsScriptIdSymbol = Symbol("DeviceModeMode
l.touchEventsScriptIdSymbol"); | 86 WebInspector.DeviceModeModel._touchEventsScriptIdSymbol = Symbol("DeviceModeMode
l.touchEventsScriptIdSymbol"); |
| 86 WebInspector.DeviceModeModel._defaultMobileUserAgent = "Mozilla/5.0 (Linux; Andr
oid 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.
0.2490.76 Mobile Safari/537.36"; | 87 WebInspector.DeviceModeModel._defaultMobileUserAgent = "Mozilla/5.0 (Linux; Andr
oid 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.
0.2490.76 Mobile Safari/537.36"; |
| 87 WebInspector.DeviceModeModel._defaultMobileScaleFactor = 2; | 88 WebInspector.DeviceModeModel._defaultMobileScaleFactor = 2; |
| 88 | 89 |
| 89 WebInspector.DeviceModeModel.prototype = { | 90 WebInspector.DeviceModeModel.prototype = { |
| 90 /** | 91 /** |
| 91 * @param {!Size} availableSize | 92 * @param {!Size} availableSize |
| 92 * @param {!Size} preferredSize | 93 * @param {!Size} preferredSize |
| 93 */ | 94 */ |
| 94 setAvailableSize: function(availableSize, preferredSize) | 95 setAvailableSize: function(availableSize, preferredSize) |
| 95 { | 96 { |
| 96 this._availableSize = availableSize; | 97 this._availableSize = availableSize; |
| 97 this._preferredSize = preferredSize; | 98 this._preferredSize = preferredSize; |
| 99 this._initialized = true; |
| 98 this._calculateAndEmulate(false); | 100 this._calculateAndEmulate(false); |
| 99 }, | 101 }, |
| 100 | 102 |
| 101 /** | 103 /** |
| 102 * @param {!WebInspector.DeviceModeModel.Type} type | 104 * @param {!WebInspector.DeviceModeModel.Type} type |
| 103 * @param {?WebInspector.EmulatedDevice} device | 105 * @param {?WebInspector.EmulatedDevice} device |
| 104 * @param {?WebInspector.EmulatedDevice.Mode} mode | 106 * @param {?WebInspector.EmulatedDevice.Mode} mode |
| 105 */ | 107 */ |
| 106 emulate: function(type, device, mode) | 108 emulate: function(type, device, mode) |
| 107 { | 109 { |
| 108 var resetScrollAndPageScale = this._type !== type || this._device !== de
vice || this._mode !== mode; | 110 var resetScrollAndPageScale = this._type !== type || this._device !== de
vice || this._mode !== mode; |
| 109 this._type = type; | 111 this._type = type; |
| 110 | 112 |
| 111 if (type === WebInspector.DeviceModeModel.Type.Device) { | 113 if (type === WebInspector.DeviceModeModel.Type.Device) { |
| 112 console.assert(device && mode, "Must pass device and mode for device
emulation"); | 114 console.assert(device && mode, "Must pass device and mode for device
emulation"); |
| 113 this._device = device; | 115 this._device = device; |
| 114 this._mode = mode; | 116 this._mode = mode; |
| 117 if (this._initialized) { |
| 118 var orientation = device.orientationByName(mode.orientation); |
| 119 this._scaleSetting.set(this._calculateFitScale(orientation.width
, orientation.height)); |
| 120 } |
| 115 } else { | 121 } else { |
| 116 this._device = null; | 122 this._device = null; |
| 117 this._mode = null; | 123 this._mode = null; |
| 118 } | 124 } |
| 119 | 125 |
| 120 if (type !== WebInspector.DeviceModeModel.Type.None) | 126 if (type !== WebInspector.DeviceModeModel.Type.None) |
| 121 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action
.DeviceModeEnabled); | 127 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action
.DeviceModeEnabled); |
| 122 this._calculateAndEmulate(resetScrollAndPageScale); | 128 this._calculateAndEmulate(resetScrollAndPageScale); |
| 123 }, | 129 }, |
| 124 | 130 |
| 125 /** | 131 /** |
| 126 * @param {number} width | 132 * @param {number} width |
| 127 */ | 133 */ |
| 128 setWidth: function(width) | 134 setWidth: function(width) |
| 129 { | 135 { |
| 130 var max = Math.min(WebInspector.DeviceModeModel.MaxDeviceSize, this._pre
ferredScaledWidth()); | 136 var max = Math.min(WebInspector.DeviceModeModel.MaxDeviceSize, this._pre
ferredScaledWidth()); |
| 131 width = Math.max(Math.min(width, max), 1); | 137 width = Math.max(Math.min(width, max), 1); |
| 132 this._widthSetting.set(width); | 138 this._widthSetting.set(width); |
| 133 }, | 139 }, |
| 134 | 140 |
| 135 /** | 141 /** |
| 142 * @param {number} width |
| 143 */ |
| 144 setWidthAndScaleToFit: function(width) |
| 145 { |
| 146 width = Math.max(Math.min(width, WebInspector.DeviceModeModel.MaxDeviceS
ize), 1); |
| 147 this._scaleSetting.set(this._calculateFitScale(width, this._heightSettin
g.get())); |
| 148 this._widthSetting.set(width); |
| 149 }, |
| 150 |
| 151 /** |
| 136 * @param {number} height | 152 * @param {number} height |
| 137 */ | 153 */ |
| 138 setHeight: function(height) | 154 setHeight: function(height) |
| 139 { | 155 { |
| 140 var max = Math.min(WebInspector.DeviceModeModel.MaxDeviceSize, this._pre
ferredScaledHeight()); | 156 var max = Math.min(WebInspector.DeviceModeModel.MaxDeviceSize, this._pre
ferredScaledHeight()); |
| 141 height = Math.max(Math.min(height, max), 0); | 157 height = Math.max(Math.min(height, max), 0); |
| 142 if (height === this._preferredScaledHeight()) | 158 if (height === this._preferredScaledHeight()) |
| 143 height = 0; | 159 height = 0; |
| 144 this._heightSetting.set(height); | 160 this._heightSetting.set(height); |
| 145 }, | 161 }, |
| 146 | 162 |
| 147 /** | 163 /** |
| 164 * @param {number} height |
| 165 */ |
| 166 setHeightAndScaleToFit: function(height) |
| 167 { |
| 168 height = Math.max(Math.min(height, WebInspector.DeviceModeModel.MaxDevic
eSize), 0); |
| 169 if (height === this._preferredScaledHeight()) |
| 170 height = 0; |
| 171 this._scaleSetting.set(this._calculateFitScale(this._widthSetting.get(),
height)); |
| 172 this._heightSetting.set(height); |
| 173 }, |
| 174 |
| 175 /** |
| 148 * @param {number} scale | 176 * @param {number} scale |
| 149 */ | 177 */ |
| 150 setScale: function(scale) | 178 setScale: function(scale) |
| 151 { | 179 { |
| 152 this._scaleSetting.set(scale); | 180 this._scaleSetting.set(scale); |
| 153 }, | 181 }, |
| 154 | 182 |
| 155 /** | 183 /** |
| 156 * @return {?WebInspector.EmulatedDevice} | 184 * @return {?WebInspector.EmulatedDevice} |
| 157 */ | 185 */ |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 _preferredScaledHeight: function() | 400 _preferredScaledHeight: function() |
| 373 { | 401 { |
| 374 return Math.floor(this._preferredSize.height / (this._scaleSetting.get()
|| 1)); | 402 return Math.floor(this._preferredSize.height / (this._scaleSetting.get()
|| 1)); |
| 375 }, | 403 }, |
| 376 | 404 |
| 377 /** | 405 /** |
| 378 * @param {boolean} resetScrollAndPageScale | 406 * @param {boolean} resetScrollAndPageScale |
| 379 */ | 407 */ |
| 380 _calculateAndEmulate: function(resetScrollAndPageScale) | 408 _calculateAndEmulate: function(resetScrollAndPageScale) |
| 381 { | 409 { |
| 382 if (!this._target) { | 410 if (!this._target) |
| 383 this._onTargetAvailable = this._calculateAndEmulate.bind(this, reset
ScrollAndPageScale); | 411 this._onTargetAvailable = this._calculateAndEmulate.bind(this, reset
ScrollAndPageScale); |
| 384 this._applyDeviceMetrics(this._availableSize, new Insets(0, 0, 0, 0)
, 1, 0, false, resetScrollAndPageScale); | |
| 385 this._updateCallback.call(null); | |
| 386 return; | |
| 387 } | |
| 388 | 412 |
| 389 if (this._type === WebInspector.DeviceModeModel.Type.Device) { | 413 if (this._type === WebInspector.DeviceModeModel.Type.Device) { |
| 390 var orientation = this._device.orientationByName(this._mode.orientat
ion); | 414 var orientation = this._device.orientationByName(this._mode.orientat
ion); |
| 391 this._calculateFitScale(orientation.width, orientation.height); | 415 this._fitScale = this._calculateFitScale(orientation.width, orientat
ion.height); |
| 392 this._applyDeviceMetrics(new Size(orientation.width, orientation.hei
ght), this._mode.insets, this._scaleSetting.get(), this._device.deviceScaleFacto
r, this._device.mobile(), resetScrollAndPageScale); | 416 this._applyDeviceMetrics(new Size(orientation.width, orientation.hei
ght), this._mode.insets, this._scaleSetting.get(), this._device.deviceScaleFacto
r, this._device.mobile(), resetScrollAndPageScale); |
| 393 this._applyUserAgent(this._device.userAgent); | 417 this._applyUserAgent(this._device.userAgent); |
| 394 this._applyTouch(this._device.touch(), this._device.mobile()); | 418 this._applyTouch(this._device.touch(), this._device.mobile()); |
| 395 this._applyScreenOrientation(this._mode.orientation == WebInspector.
EmulatedDevice.Horizontal ? "landscapePrimary" : "portraitPrimary"); | 419 this._applyScreenOrientation(this._mode.orientation == WebInspector.
EmulatedDevice.Horizontal ? "landscapePrimary" : "portraitPrimary"); |
| 396 } else if (this._type === WebInspector.DeviceModeModel.Type.None) { | 420 } else if (this._type === WebInspector.DeviceModeModel.Type.None) { |
| 397 this._calculateFitScale(this._availableSize.width, this._availableSi
ze.height); | 421 this._fitScale = this._calculateFitScale(this._availableSize.width,
this._availableSize.height); |
| 398 this._applyDeviceMetrics(this._availableSize, new Insets(0, 0, 0, 0)
, 1, 0, false, resetScrollAndPageScale); | 422 this._applyDeviceMetrics(this._availableSize, new Insets(0, 0, 0, 0)
, 1, 0, false, resetScrollAndPageScale); |
| 399 this._applyUserAgent(""); | 423 this._applyUserAgent(""); |
| 400 this._applyTouch(false, false); | 424 this._applyTouch(false, false); |
| 401 this._applyScreenOrientation(""); | 425 this._applyScreenOrientation(""); |
| 402 } else if (this._type === WebInspector.DeviceModeModel.Type.Responsive)
{ | 426 } else if (this._type === WebInspector.DeviceModeModel.Type.Responsive)
{ |
| 403 var screenWidth = this._widthSetting.get(); | 427 var screenWidth = this._widthSetting.get(); |
| 404 if (!screenWidth || screenWidth > this._preferredScaledWidth()) | 428 if (!screenWidth || screenWidth > this._preferredScaledWidth()) |
| 405 screenWidth = this._preferredScaledWidth(); | 429 screenWidth = this._preferredScaledWidth(); |
| 406 var screenHeight = this._heightSetting.get(); | 430 var screenHeight = this._heightSetting.get(); |
| 407 if (!screenHeight || screenHeight > this._preferredScaledHeight()) | 431 if (!screenHeight || screenHeight > this._preferredScaledHeight()) |
| 408 screenHeight = this._preferredScaledHeight(); | 432 screenHeight = this._preferredScaledHeight(); |
| 409 var mobile = this._uaSetting.get() === WebInspector.DeviceModeModel.
UA.Mobile; | 433 var mobile = this._uaSetting.get() === WebInspector.DeviceModeModel.
UA.Mobile; |
| 410 var defaultDeviceScaleFactor = mobile ? WebInspector.DeviceModeModel
._defaultMobileScaleFactor : 0; | 434 var defaultDeviceScaleFactor = mobile ? WebInspector.DeviceModeModel
._defaultMobileScaleFactor : 0; |
| 411 this._calculateFitScale(this._widthSetting.get(), this._heightSettin
g.get()); | 435 this._fitScale = this._calculateFitScale(this._widthSetting.get(), t
his._heightSetting.get()); |
| 412 this._applyDeviceMetrics(new Size(screenWidth, screenHeight), new In
sets(0, 0, 0, 0), this._scaleSetting.get(), this._deviceScaleFactorSetting.get()
|| defaultDeviceScaleFactor, mobile, resetScrollAndPageScale); | 436 this._applyDeviceMetrics(new Size(screenWidth, screenHeight), new In
sets(0, 0, 0, 0), this._scaleSetting.get(), this._deviceScaleFactorSetting.get()
|| defaultDeviceScaleFactor, mobile, resetScrollAndPageScale); |
| 413 this._applyUserAgent(mobile ? WebInspector.DeviceModeModel._defaultM
obileUserAgent : ""); | 437 this._applyUserAgent(mobile ? WebInspector.DeviceModeModel._defaultM
obileUserAgent : ""); |
| 414 this._applyTouch(this._uaSetting.get() !== WebInspector.DeviceModeMo
del.UA.Desktop, mobile); | 438 this._applyTouch(this._uaSetting.get() !== WebInspector.DeviceModeMo
del.UA.Desktop, mobile); |
| 415 this._applyScreenOrientation(screenHeight >= screenWidth ? "portrait
Primary" : "landscapePrimary"); | 439 this._applyScreenOrientation(screenHeight >= screenWidth ? "portrait
Primary" : "landscapePrimary"); |
| 416 } | 440 } |
| 417 this._updateCallback.call(null); | 441 this._updateCallback.call(null); |
| 418 }, | 442 }, |
| 419 | 443 |
| 420 /** | 444 /** |
| 421 * @param {number} screenWidth | 445 * @param {number} screenWidth |
| 422 * @param {number} screenHeight | 446 * @param {number} screenHeight |
| 447 * @return {number} |
| 423 */ | 448 */ |
| 424 _calculateFitScale: function(screenWidth, screenHeight) | 449 _calculateFitScale: function(screenWidth, screenHeight) |
| 425 { | 450 { |
| 426 var scale = Math.min(screenWidth ? this._preferredSize.width / screenWid
th: 1, screenHeight ? this._preferredSize.height / screenHeight : 1); | 451 var scale = Math.min(screenWidth ? this._preferredSize.width / screenWid
th: 1, screenHeight ? this._preferredSize.height / screenHeight : 1); |
| 427 this._fitScale = Math.min(scale, 1); | 452 return Math.min(scale, 1); |
| 428 }, | 453 }, |
| 429 | 454 |
| 430 /** | 455 /** |
| 431 * @param {number} width | 456 * @param {number} width |
| 432 * @param {number} height | 457 * @param {number} height |
| 433 */ | 458 */ |
| 434 setSizeAndScaleToFit: function(width, height) | 459 setSizeAndScaleToFit: function(width, height) |
| 435 { | 460 { |
| 436 var scale = Math.min(width ? this._preferredSize.width / width: 1, heigh
t ? this._preferredSize.height / height : 1); | 461 this._scaleSetting.set(this._calculateFitScale(width, height)); |
| 437 this._scaleSetting.set(scale); | |
| 438 this.setWidth(width); | 462 this.setWidth(width); |
| 439 }, | 463 }, |
| 440 | 464 |
| 441 /** | 465 /** |
| 442 * @param {string} userAgent | 466 * @param {string} userAgent |
| 443 */ | 467 */ |
| 444 _applyUserAgent: function(userAgent) | 468 _applyUserAgent: function(userAgent) |
| 445 { | 469 { |
| 446 WebInspector.multitargetNetworkManager.setUserAgentOverride(userAgent); | 470 WebInspector.multitargetNetworkManager.setUserAgentOverride(userAgent); |
| 447 }, | 471 }, |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 if (!this._target || orientation === this._screenOrientation) | 597 if (!this._target || orientation === this._screenOrientation) |
| 574 return; | 598 return; |
| 575 | 599 |
| 576 this._screenOrientation = orientation; | 600 this._screenOrientation = orientation; |
| 577 if (!this._screenOrientation) | 601 if (!this._screenOrientation) |
| 578 this._target.screenOrientationAgent().clearScreenOrientationOverride
(); | 602 this._target.screenOrientationAgent().clearScreenOrientationOverride
(); |
| 579 else | 603 else |
| 580 this._target.screenOrientationAgent().setScreenOrientationOverride(t
his._screenOrientation === "landscapePrimary" ? 90 : 0, /** @type {!ScreenOrient
ationAgent.OrientationType} */ (this._screenOrientation)); | 604 this._target.screenOrientationAgent().setScreenOrientationOverride(t
his._screenOrientation === "landscapePrimary" ? 90 : 0, /** @type {!ScreenOrient
ationAgent.OrientationType} */ (this._screenOrientation)); |
| 581 } | 605 } |
| 582 } | 606 } |
| OLD | NEW |