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