Chromium Code Reviews| 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) |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 { | 129 { |
| 130 var resetPageScaleFactor = this._type !== type || this._device !== devic e || this._mode !== mode; | 130 var resetPageScaleFactor = this._type !== type || this._device !== devic e || this._mode !== mode; |
| 131 this._type = type; | 131 this._type = type; |
| 132 | 132 |
| 133 if (type === WebInspector.DeviceModeModel.Type.Device) { | 133 if (type === WebInspector.DeviceModeModel.Type.Device) { |
| 134 console.assert(device && mode, "Must pass device and mode for device emulation"); | 134 console.assert(device && mode, "Must pass device and mode for device emulation"); |
| 135 this._device = device; | 135 this._device = device; |
| 136 this._mode = mode; | 136 this._mode = mode; |
| 137 if (this._initialized) { | 137 if (this._initialized) { |
| 138 var orientation = device.orientationByName(mode.orientation); | 138 var orientation = device.orientationByName(mode.orientation); |
| 139 this._scaleSetting.set(this._calculateFitScale(orientation.width , orientation.height, this._currentOutline())); | 139 this._scaleSetting.set(this._calculateFitScale(orientation.width , orientation.height, this._currentOutline(), this._currentInsets())); |
| 140 } | 140 } |
| 141 } else { | 141 } else { |
| 142 this._device = null; | 142 this._device = null; |
| 143 this._mode = null; | 143 this._mode = null; |
| 144 } | 144 } |
| 145 | 145 |
| 146 if (type !== WebInspector.DeviceModeModel.Type.None) | 146 if (type !== WebInspector.DeviceModeModel.Type.None) |
| 147 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action .DeviceModeEnabled); | 147 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action .DeviceModeEnabled); |
| 148 this._calculateAndEmulate(resetPageScaleFactor); | 148 this._calculateAndEmulate(resetPageScaleFactor); |
| 149 }, | 149 }, |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 431 var outline = new Insets(0, 0, 0, 0); | 431 var outline = new Insets(0, 0, 0, 0); |
| 432 if (this._type !== WebInspector.DeviceModeModel.Type.Device) | 432 if (this._type !== WebInspector.DeviceModeModel.Type.Device) |
| 433 return outline; | 433 return outline; |
| 434 var orientation = this._device.orientationByName(this._mode.orientation) ; | 434 var orientation = this._device.orientationByName(this._mode.orientation) ; |
| 435 if (this._deviceOutlineSetting.get()) | 435 if (this._deviceOutlineSetting.get()) |
| 436 outline = orientation.outlineInsets || outline; | 436 outline = orientation.outlineInsets || outline; |
| 437 return outline; | 437 return outline; |
| 438 }, | 438 }, |
| 439 | 439 |
| 440 /** | 440 /** |
| 441 * @return {!Insets} | |
| 442 */ | |
| 443 _currentInsets: function() | |
| 444 { | |
| 445 if (this._type !== WebInspector.DeviceModeModel.Type.Device || !this._mo de) | |
|
lushnikov
2016/06/02 03:40:58
how could this._type === WebInspector.DeviceModeMo
dgozman
2016/06/03 17:47:45
Correct.
| |
| 446 return new Insets(0, 0, 0, 0); | |
| 447 return this._mode.insets; | |
| 448 }, | |
| 449 | |
| 450 /** | |
| 441 * @param {boolean} resetPageScaleFactor | 451 * @param {boolean} resetPageScaleFactor |
| 442 */ | 452 */ |
| 443 _calculateAndEmulate: function(resetPageScaleFactor) | 453 _calculateAndEmulate: function(resetPageScaleFactor) |
| 444 { | 454 { |
| 445 if (!this._target) | 455 if (!this._target) |
| 446 this._onTargetAvailable = this._calculateAndEmulate.bind(this, reset PageScaleFactor); | 456 this._onTargetAvailable = this._calculateAndEmulate.bind(this, reset PageScaleFactor); |
| 447 | 457 |
| 448 if (this._type === WebInspector.DeviceModeModel.Type.Device) { | 458 if (this._type === WebInspector.DeviceModeModel.Type.Device) { |
| 449 var orientation = this._device.orientationByName(this._mode.orientat ion); | 459 var orientation = this._device.orientationByName(this._mode.orientat ion); |
| 450 var outline = this._currentOutline(); | 460 var outline = this._currentOutline(); |
| 451 this._fitScale = this._calculateFitScale(orientation.width, orientat ion.height, outline); | 461 var insets = this._currentInsets(); |
| 462 this._fitScale = this._calculateFitScale(orientation.width, orientat ion.height, outline, insets); | |
| 452 if (this._device.mobile()) | 463 if (this._device.mobile()) |
| 453 this._appliedUserAgentType = this._device.touch() ? WebInspector .DeviceModeModel.UA.Mobile : WebInspector.DeviceModeModel.UA.MobileNoTouch; | 464 this._appliedUserAgentType = this._device.touch() ? WebInspector .DeviceModeModel.UA.Mobile : WebInspector.DeviceModeModel.UA.MobileNoTouch; |
| 454 else | 465 else |
| 455 this._appliedUserAgentType = this._device.touch() ? WebInspector .DeviceModeModel.UA.DesktopTouch : WebInspector.DeviceModeModel.UA.Desktop; | 466 this._appliedUserAgentType = this._device.touch() ? WebInspector .DeviceModeModel.UA.DesktopTouch : WebInspector.DeviceModeModel.UA.Desktop; |
| 456 this._applyDeviceMetrics(new Size(orientation.width, orientation.hei ght), this._mode.insets, outline, this._scaleSetting.get(), this._device.deviceS caleFactor, this._device.mobile(), this._mode.orientation === WebInspector.Emula tedDevice.Horizontal ? "landscapePrimary" : "portraitPrimary", resetPageScaleFac tor); | 467 this._applyDeviceMetrics(new Size(orientation.width, orientation.hei ght), insets, outline, this._scaleSetting.get(), this._device.deviceScaleFactor, this._device.mobile(), this._mode.orientation === WebInspector.EmulatedDevice.H orizontal ? "landscapePrimary" : "portraitPrimary", resetPageScaleFactor); |
| 457 this._applyUserAgent(this._device.userAgent); | 468 this._applyUserAgent(this._device.userAgent); |
| 458 this._applyTouch(this._device.touch(), this._device.mobile()); | 469 this._applyTouch(this._device.touch(), this._device.mobile()); |
| 459 } else if (this._type === WebInspector.DeviceModeModel.Type.None) { | 470 } else if (this._type === WebInspector.DeviceModeModel.Type.None) { |
| 460 this._fitScale = this._calculateFitScale(this._availableSize.width, this._availableSize.height); | 471 this._fitScale = this._calculateFitScale(this._availableSize.width, this._availableSize.height); |
| 461 this._appliedUserAgentType = WebInspector.DeviceModeModel.UA.Desktop ; | 472 this._appliedUserAgentType = WebInspector.DeviceModeModel.UA.Desktop ; |
| 462 this._applyDeviceMetrics(this._availableSize, new Insets(0, 0, 0, 0) , new Insets(0, 0, 0, 0), 1, 0, false, "", resetPageScaleFactor); | 473 this._applyDeviceMetrics(this._availableSize, new Insets(0, 0, 0, 0) , new Insets(0, 0, 0, 0), 1, 0, false, "", resetPageScaleFactor); |
| 463 this._applyUserAgent(""); | 474 this._applyUserAgent(""); |
| 464 this._applyTouch(false, false); | 475 this._applyTouch(false, false); |
| 465 } else if (this._type === WebInspector.DeviceModeModel.Type.Responsive) { | 476 } else if (this._type === WebInspector.DeviceModeModel.Type.Responsive) { |
| 466 var screenWidth = this._widthSetting.get(); | 477 var screenWidth = this._widthSetting.get(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 479 } | 490 } |
| 480 if (this._target) | 491 if (this._target) |
| 481 this._target.renderingAgent().setShowViewportSizeOnResize(this._type === WebInspector.DeviceModeModel.Type.None); | 492 this._target.renderingAgent().setShowViewportSizeOnResize(this._type === WebInspector.DeviceModeModel.Type.None); |
| 482 this._updateCallback.call(null); | 493 this._updateCallback.call(null); |
| 483 }, | 494 }, |
| 484 | 495 |
| 485 /** | 496 /** |
| 486 * @param {number} screenWidth | 497 * @param {number} screenWidth |
| 487 * @param {number} screenHeight | 498 * @param {number} screenHeight |
| 488 * @param {!Insets=} outline | 499 * @param {!Insets=} outline |
| 500 * @param {!Insets=} insets | |
| 489 * @return {number} | 501 * @return {number} |
| 490 */ | 502 */ |
| 491 _calculateFitScale: function(screenWidth, screenHeight, outline) | 503 _calculateFitScale: function(screenWidth, screenHeight, outline, insets) |
| 492 { | 504 { |
| 493 var outlineWidth = outline ? outline.left + outline.right : 0; | 505 var outlineWidth = outline ? outline.left + outline.right : 0; |
| 494 var outlineHeight = outline ? outline.top + outline.bottom : 0; | 506 var outlineHeight = outline ? outline.top + outline.bottom : 0; |
| 507 var insetsWidth = insets ? insets.left + insets.right : 0; | |
| 508 var insetsHeight = insets ? insets.top + insets.bottom : 0; | |
| 495 var scale = Math.min(screenWidth ? this._preferredSize.width / (screenWi dth + outlineWidth) : 1, screenHeight ? this._preferredSize.height / (screenHeig ht + outlineHeight) : 1); | 509 var scale = Math.min(screenWidth ? this._preferredSize.width / (screenWi dth + outlineWidth) : 1, screenHeight ? this._preferredSize.height / (screenHeig ht + outlineHeight) : 1); |
| 496 return Math.min(scale, 1); | 510 scale = Math.min(Math.ceil(scale * 100), 100); |
| 511 | |
| 512 var sharpScale = scale; | |
| 513 while (sharpScale > scale * 0.7) { | |
|
lushnikov
2016/06/02 03:40:58
where does 0.7 come from?
dgozman
2016/06/03 17:47:45
Arbitrary number to not make scale too small. For
| |
| 514 var sharp = true; | |
| 515 if (screenWidth) | |
| 516 sharp = sharp && this._isInteger((screenWidth - insetsWidth) * s harpScale / 100); | |
|
lushnikov
2016/06/02 03:40:58
i don't follow this logic. Can you please explain?
dgozman
2016/06/03 17:47:45
We try to scale so that no rounding error will occ
| |
| 517 if (screenHeight) | |
| 518 sharp = sharp && this._isInteger((screenHeight - insetsHeight) * sharpScale / 100); | |
| 519 if (sharp) | |
| 520 return sharpScale / 100; | |
| 521 sharpScale -= 1; | |
| 522 } | |
| 523 return scale / 100; | |
| 497 }, | 524 }, |
| 498 | 525 |
| 499 /** | 526 /** |
| 527 * @param {number} n | |
| 528 * @return {boolean} | |
| 529 */ | |
| 530 _isInteger: function(n) | |
|
lushnikov
2016/06/02 03:40:58
There is a built-in: Number.isInteger(n)
dgozman
2016/06/20 18:59:42
Done.
| |
| 531 { | |
| 532 return n === Math.round(n); | |
| 533 }, | |
| 534 | |
| 535 /** | |
| 500 * @param {number} width | 536 * @param {number} width |
| 501 * @param {number} height | 537 * @param {number} height |
| 502 */ | 538 */ |
| 503 setSizeAndScaleToFit: function(width, height) | 539 setSizeAndScaleToFit: function(width, height) |
| 504 { | 540 { |
| 505 this._scaleSetting.set(this._calculateFitScale(width, height)); | 541 this._scaleSetting.set(this._calculateFitScale(width, height)); |
| 506 this.setWidth(width); | 542 this.setWidth(width); |
| 507 }, | 543 }, |
| 508 | 544 |
| 509 /** | 545 /** |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 553 positionY * scale, | 589 positionY * scale, |
| 554 Math.min(pageWidth * scale, this._availableSize.width - this._screen Rect.left - positionX * scale), | 590 Math.min(pageWidth * scale, this._availableSize.width - this._screen Rect.left - positionX * scale), |
| 555 Math.min(pageHeight * scale, this._availableSize.height - this._scre enRect.top - positionY * scale)); | 591 Math.min(pageHeight * scale, this._availableSize.height - this._scre enRect.top - positionY * scale)); |
| 556 this._scale = scale; | 592 this._scale = scale; |
| 557 | 593 |
| 558 if (scale === 1 && this._availableSize.width >= screenSize.width && this ._availableSize.height >= screenSize.height) { | 594 if (scale === 1 && this._availableSize.width >= screenSize.width && this ._availableSize.height >= screenSize.height) { |
| 559 // When we have enough space, no page size override is required. Thi s will speed things up and remove lag. | 595 // When we have enough space, no page size override is required. Thi s will speed things up and remove lag. |
| 560 pageWidth = 0; | 596 pageWidth = 0; |
| 561 pageHeight = 0; | 597 pageHeight = 0; |
| 562 } | 598 } |
| 563 if (this._visiblePageRect.width === pageWidth * scale && this._visiblePa geRect.height === pageHeight * scale) { | 599 if (this._visiblePageRect.width === pageWidth * scale && this._visiblePa geRect.height === pageHeight * scale && this._isInteger(pageWidth * scale) && th is._isInteger(pageHeight * scale)) { |
| 564 // When we only have to apply scale, do not resize the page. This wi ll speed things up and remove lag. | 600 // When we only have to apply scale, do not resize the page. This wi ll speed things up and remove lag. |
| 565 pageWidth = 0; | 601 pageWidth = 0; |
| 566 pageHeight = 0; | 602 pageHeight = 0; |
| 567 } | 603 } |
| 568 | 604 |
| 569 this._deviceMetricsThrottler.schedule(setDeviceMetricsOverride.bind(this )); | 605 this._deviceMetricsThrottler.schedule(setDeviceMetricsOverride.bind(this )); |
| 570 | 606 |
| 571 /** | 607 /** |
| 572 * @this {WebInspector.DeviceModeModel} | 608 * @this {WebInspector.DeviceModeModel} |
| 573 * @return {!Promise.<?>} | 609 * @return {!Promise.<?>} |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 602 | 638 |
| 603 /** | 639 /** |
| 604 * @param {boolean} touchEnabled | 640 * @param {boolean} touchEnabled |
| 605 * @param {boolean} mobile | 641 * @param {boolean} mobile |
| 606 */ | 642 */ |
| 607 _applyTouch: function(touchEnabled, mobile) | 643 _applyTouch: function(touchEnabled, mobile) |
| 608 { | 644 { |
| 609 WebInspector.MultitargetTouchModel.instance().setTouchEnabled(touchEnabl ed, mobile); | 645 WebInspector.MultitargetTouchModel.instance().setTouchEnabled(touchEnabl ed, mobile); |
| 610 } | 646 } |
| 611 } | 647 } |
| OLD | NEW |