blob: 3630de53a32a4daaf2dba906478a3ce8925a4228 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// 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 Lippe23338392020-01-24 15:13:285import * as UI from '../ui/ui.js';
6
Blink Reformat4c46d092018-04-07 15:32:377/**
Jack Franklindb9cb272020-10-15 13:11:268 * @type {!InspectedPagePlaceholder}
9 */
10let inspectedPagePlaceholderInstance;
11
12/**
Blink Reformat4c46d092018-04-07 15:32:3713 * @unrestricted
14 */
Tim van der Lippe23338392020-01-24 15:13:2815export class InspectedPagePlaceholder extends UI.Widget.Widget {
Blink Reformat4c46d092018-04-07 15:32:3716 constructor() {
17 super(true);
Jack Franklin71519f82020-11-03 12:08:5918 this.registerRequiredCSS('emulation/inspectedPagePlaceholder.css', {enableLegacyPatching: true});
Paul Lewis6c914a12020-03-19 11:23:2119 UI.ZoomManager.ZoomManager.instance().addEventListener(UI.ZoomManager.Events.ZoomChanged, this.onResize, this);
Blink Reformat4c46d092018-04-07 15:32:3720 this.restoreMinimumSize();
21 }
22
Jack Franklindb9cb272020-10-15 13:11:2623 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 Reformat4c46d092018-04-07 15:32:3732 /**
33 * @override
34 */
35 onResize() {
Tim van der Lippe1d6e57a2019-09-30 11:55:3436 if (this._updateId) {
Blink Reformat4c46d092018-04-07 15:32:3737 this.element.window().cancelAnimationFrame(this._updateId);
Tim van der Lippe1d6e57a2019-09-30 11:55:3438 }
Blink Reformat4c46d092018-04-07 15:32:3739 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 Lewis6c914a12020-03-19 11:23:2151 const zoomFactor = UI.ZoomManager.ZoomManager.instance().zoomFactor();
Blink Reformat4c46d092018-04-07 15:32:3752 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 Lewisd9b33e92019-12-10 14:30:5678 this.dispatchEventToListeners(Events.Update, bounds);
Blink Reformat4c46d092018-04-07 15:32:3779 ++bounds.height;
80 }
Paul Lewisd9b33e92019-12-10 14:30:5681 this.dispatchEventToListeners(Events.Update, bounds);
Blink Reformat4c46d092018-04-07 15:32:3782 }
Paul Lewisd9b33e92019-12-10 14:30:5683}
Blink Reformat4c46d092018-04-07 15:32:3784
Blink Reformat4c46d092018-04-07 15:32:3785/** @enum {symbol} */
Paul Lewisd9b33e92019-12-10 14:30:5686export const Events = {
Blink Reformat4c46d092018-04-07 15:32:3787 Update: Symbol('Update')
88};