blob: 4dad6ec53864e6f149e0dd0b3f0aeced49a21f50 [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/**
8 * @unrestricted
9 */
Tim van der Lippe23338392020-01-24 15:13:2810export class InspectedPagePlaceholder extends UI.Widget.Widget {
Blink Reformat4c46d092018-04-07 15:32:3711 constructor() {
12 super(true);
13 this.registerRequiredCSS('emulation/inspectedPagePlaceholder.css');
Paul Lewis50993692020-01-23 15:22:2614 self.UI.zoomManager.addEventListener(UI.ZoomManager.Events.ZoomChanged, this.onResize, this);
Blink Reformat4c46d092018-04-07 15:32:3715 this.restoreMinimumSize();
16 }
17
18 /**
19 * @override
20 */
21 onResize() {
Tim van der Lippe1d6e57a2019-09-30 11:55:3422 if (this._updateId) {
Blink Reformat4c46d092018-04-07 15:32:3723 this.element.window().cancelAnimationFrame(this._updateId);
Tim van der Lippe1d6e57a2019-09-30 11:55:3424 }
Blink Reformat4c46d092018-04-07 15:32:3725 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 Lewis50993692020-01-23 15:22:2637 const zoomFactor = self.UI.zoomManager.zoomFactor();
Blink Reformat4c46d092018-04-07 15:32:3738 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 Lewisd9b33e92019-12-10 14:30:5664 this.dispatchEventToListeners(Events.Update, bounds);
Blink Reformat4c46d092018-04-07 15:32:3765 ++bounds.height;
66 }
Paul Lewisd9b33e92019-12-10 14:30:5667 this.dispatchEventToListeners(Events.Update, bounds);
Blink Reformat4c46d092018-04-07 15:32:3768 }
Paul Lewisd9b33e92019-12-10 14:30:5669}
Blink Reformat4c46d092018-04-07 15:32:3770
71/**
Paul Lewisd9b33e92019-12-10 14:30:5672 * @return {!InspectedPagePlaceholder}
Blink Reformat4c46d092018-04-07 15:32:3773 */
Paul Lewisd9b33e92019-12-10 14:30:5674export const instance = function() {
75 return self.singleton(InspectedPagePlaceholder);
Blink Reformat4c46d092018-04-07 15:32:3776};
77
78/** @enum {symbol} */
Paul Lewisd9b33e92019-12-10 14:30:5679export const Events = {
Blink Reformat4c46d092018-04-07 15:32:3780 Update: Symbol('Update')
81};