Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Tim van der Lippe | 2333839 | 2020-01-24 15:13:28 | [diff] [blame^] | 5 | import * as UI from '../ui/ui.js'; |
| 6 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 7 | /** |
| 8 | * @unrestricted |
| 9 | */ |
Tim van der Lippe | 2333839 | 2020-01-24 15:13:28 | [diff] [blame^] | 10 | export class InspectedPagePlaceholder extends UI.Widget.Widget { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 11 | constructor() { |
| 12 | super(true); |
| 13 | this.registerRequiredCSS('emulation/inspectedPagePlaceholder.css'); |
Paul Lewis | 5099369 | 2020-01-23 15:22:26 | [diff] [blame] | 14 | self.UI.zoomManager.addEventListener(UI.ZoomManager.Events.ZoomChanged, this.onResize, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 15 | this.restoreMinimumSize(); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * @override |
| 20 | */ |
| 21 | onResize() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 22 | if (this._updateId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 23 | this.element.window().cancelAnimationFrame(this._updateId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 24 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 25 | this._updateId = this.element.window().requestAnimationFrame(this.update.bind(this, false)); |
| 26 | } |
| 27 | |
| 28 | restoreMinimumSize() { |
| 29 | this.setMinimumSize(150, 150); |
| 30 | } |
| 31 | |
| 32 | clearMinimumSize() { |
| 33 | this.setMinimumSize(1, 1); |
| 34 | } |
| 35 | |
| 36 | _dipPageRect() { |
Paul Lewis | 5099369 | 2020-01-23 15:22:26 | [diff] [blame] | 37 | const zoomFactor = self.UI.zoomManager.zoomFactor(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 38 | const rect = this.element.getBoundingClientRect(); |
| 39 | const bodyRect = this.element.ownerDocument.body.getBoundingClientRect(); |
| 40 | |
| 41 | const left = Math.max(rect.left * zoomFactor, bodyRect.left * zoomFactor); |
| 42 | const top = Math.max(rect.top * zoomFactor, bodyRect.top * zoomFactor); |
| 43 | const bottom = Math.min(rect.bottom * zoomFactor, bodyRect.bottom * zoomFactor); |
| 44 | const right = Math.min(rect.right * zoomFactor, bodyRect.right * zoomFactor); |
| 45 | |
| 46 | return {x: left, y: top, width: right - left, height: bottom - top}; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param {boolean=} force |
| 51 | */ |
| 52 | update(force) { |
| 53 | delete this._updateId; |
| 54 | const rect = this._dipPageRect(); |
| 55 | const bounds = { |
| 56 | x: Math.round(rect.x), |
| 57 | y: Math.round(rect.y), |
| 58 | height: Math.max(1, Math.round(rect.height)), |
| 59 | width: Math.max(1, Math.round(rect.width)), |
| 60 | }; |
| 61 | if (force) { |
| 62 | // Short term fix for Lighthouse interop. |
| 63 | --bounds.height; |
Paul Lewis | d9b33e9 | 2019-12-10 14:30:56 | [diff] [blame] | 64 | this.dispatchEventToListeners(Events.Update, bounds); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 65 | ++bounds.height; |
| 66 | } |
Paul Lewis | d9b33e9 | 2019-12-10 14:30:56 | [diff] [blame] | 67 | this.dispatchEventToListeners(Events.Update, bounds); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 68 | } |
Paul Lewis | d9b33e9 | 2019-12-10 14:30:56 | [diff] [blame] | 69 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 70 | |
| 71 | /** |
Paul Lewis | d9b33e9 | 2019-12-10 14:30:56 | [diff] [blame] | 72 | * @return {!InspectedPagePlaceholder} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 73 | */ |
Paul Lewis | d9b33e9 | 2019-12-10 14:30:56 | [diff] [blame] | 74 | export const instance = function() { |
| 75 | return self.singleton(InspectedPagePlaceholder); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /** @enum {symbol} */ |
Paul Lewis | d9b33e9 | 2019-12-10 14:30:56 | [diff] [blame] | 79 | export const Events = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 80 | Update: Symbol('Update') |
| 81 | }; |