OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 /** |
| 5 * @unrestricted |
| 6 */ |
| 7 WebInspector.InspectedPagePlaceholder = class extends WebInspector.Widget { |
| 8 constructor() { |
| 9 super(true); |
| 10 this.registerRequiredCSS('emulation/inspectedPagePlaceholder.css'); |
| 11 WebInspector.zoomManager.addEventListener(WebInspector.ZoomManager.Events.Zo
omChanged, this._scheduleUpdate, this); |
| 12 this._margins = {top: 0, right: 0, bottom: 0, left: 0}; |
| 13 this.restoreMinimumSizeAndMargins(); |
| 14 } |
4 | 15 |
5 /** | 16 _findMargins() { |
6 * @constructor | 17 var margins = {top: 0, right: 0, bottom: 0, left: 0}; |
7 * @extends {WebInspector.Widget} | 18 |
8 */ | 19 if (this._useMargins) { |
9 WebInspector.InspectedPagePlaceholder = function() | 20 var adjacent = {top: true, right: true, bottom: true, left: true}; |
10 { | 21 var widget = this; |
11 WebInspector.Widget.call(this, true); | 22 while (widget.parentWidget()) { |
12 this.registerRequiredCSS("emulation/inspectedPagePlaceholder.css"); | 23 var parent = widget.parentWidget(); |
13 WebInspector.zoomManager.addEventListener(WebInspector.ZoomManager.Events.Zo
omChanged, this._scheduleUpdate, this); | 24 // This view assumes it's always inside the main split widget element, n
ot a sidebar. |
14 this._margins = { top: 0, right: 0, bottom: 0, left: 0 }; | 25 // Every parent which is not a split widget, must be of the same size as
this widget. |
15 this.restoreMinimumSizeAndMargins(); | 26 if (parent instanceof WebInspector.SplitWidget) { |
| 27 var side = parent.sidebarSide(); |
| 28 if (adjacent[side] && !parent.hasCustomResizer() && parent.isResizable
()) |
| 29 margins[side] = WebInspector.InspectedPagePlaceholder.MarginValue; |
| 30 adjacent[side] = false; |
| 31 } |
| 32 widget = parent; |
| 33 } |
| 34 } |
| 35 |
| 36 if (this._margins.top !== margins.top || this._margins.left !== margins.left
|| |
| 37 this._margins.right !== margins.right || this._margins.bottom !== margin
s.bottom) { |
| 38 this._margins = margins; |
| 39 this._scheduleUpdate(); |
| 40 } |
| 41 } |
| 42 |
| 43 /** |
| 44 * @override |
| 45 */ |
| 46 onResize() { |
| 47 this._findMargins(); |
| 48 this._scheduleUpdate(); |
| 49 } |
| 50 |
| 51 _scheduleUpdate() { |
| 52 if (this._updateId) |
| 53 this.element.window().cancelAnimationFrame(this._updateId); |
| 54 this._updateId = this.element.window().requestAnimationFrame(this.update.bin
d(this)); |
| 55 } |
| 56 |
| 57 restoreMinimumSizeAndMargins() { |
| 58 this._useMargins = true; |
| 59 this.setMinimumSize(150, 150); |
| 60 this._findMargins(); |
| 61 } |
| 62 |
| 63 clearMinimumSizeAndMargins() { |
| 64 this._useMargins = false; |
| 65 this.setMinimumSize(1, 1); |
| 66 this._findMargins(); |
| 67 } |
| 68 |
| 69 _dipPageRect() { |
| 70 var zoomFactor = WebInspector.zoomManager.zoomFactor(); |
| 71 var rect = this.element.getBoundingClientRect(); |
| 72 var bodyRect = this.element.ownerDocument.body.getBoundingClientRect(); |
| 73 |
| 74 var left = Math.max(rect.left * zoomFactor + this._margins.left, bodyRect.le
ft * zoomFactor); |
| 75 var top = Math.max(rect.top * zoomFactor + this._margins.top, bodyRect.top *
zoomFactor); |
| 76 var bottom = Math.min(rect.bottom * zoomFactor - this._margins.bottom, bodyR
ect.bottom * zoomFactor); |
| 77 var right = Math.min(rect.right * zoomFactor - this._margins.right, bodyRect
.right * zoomFactor); |
| 78 |
| 79 return {x: left, y: top, width: right - left, height: bottom - top}; |
| 80 } |
| 81 |
| 82 update() { |
| 83 delete this._updateId; |
| 84 var rect = this._dipPageRect(); |
| 85 var bounds = { |
| 86 x: Math.round(rect.x), |
| 87 y: Math.round(rect.y), |
| 88 height: Math.max(1, Math.round(rect.height)), |
| 89 width: Math.max(1, Math.round(rect.width)) |
| 90 }; |
| 91 this.dispatchEventToListeners(WebInspector.InspectedPagePlaceholder.Events.U
pdate, bounds); |
| 92 } |
16 }; | 93 }; |
17 | 94 |
18 /** @enum {symbol} */ | 95 /** @enum {symbol} */ |
19 WebInspector.InspectedPagePlaceholder.Events = { | 96 WebInspector.InspectedPagePlaceholder.Events = { |
20 Update: Symbol("Update") | 97 Update: Symbol('Update') |
21 }; | 98 }; |
22 | 99 |
23 WebInspector.InspectedPagePlaceholder.MarginValue = 3; | 100 WebInspector.InspectedPagePlaceholder.MarginValue = 3; |
24 | |
25 WebInspector.InspectedPagePlaceholder.prototype = { | |
26 _findMargins: function() | |
27 { | |
28 var margins = { top: 0, right: 0, bottom: 0, left: 0 }; | |
29 | |
30 if (this._useMargins) { | |
31 var adjacent = { top: true, right: true, bottom: true, left: true }; | |
32 var widget = this; | |
33 while (widget.parentWidget()) { | |
34 var parent = widget.parentWidget(); | |
35 // This view assumes it's always inside the main split widget el
ement, not a sidebar. | |
36 // Every parent which is not a split widget, must be of the same
size as this widget. | |
37 if (parent instanceof WebInspector.SplitWidget) { | |
38 var side = parent.sidebarSide(); | |
39 if (adjacent[side] && !parent.hasCustomResizer() && parent.i
sResizable()) | |
40 margins[side] = WebInspector.InspectedPagePlaceholder.Ma
rginValue; | |
41 adjacent[side] = false; | |
42 } | |
43 widget = parent; | |
44 } | |
45 } | |
46 | |
47 if (this._margins.top !== margins.top || this._margins.left !== margins.
left || this._margins.right !== margins.right || this._margins.bottom !== margin
s.bottom) { | |
48 this._margins = margins; | |
49 this._scheduleUpdate(); | |
50 } | |
51 }, | |
52 | |
53 onResize: function() | |
54 { | |
55 this._findMargins(); | |
56 this._scheduleUpdate(); | |
57 }, | |
58 | |
59 _scheduleUpdate: function() | |
60 { | |
61 if (this._updateId) | |
62 this.element.window().cancelAnimationFrame(this._updateId); | |
63 this._updateId = this.element.window().requestAnimationFrame(this.update
.bind(this)); | |
64 }, | |
65 | |
66 restoreMinimumSizeAndMargins: function() | |
67 { | |
68 this._useMargins = true; | |
69 this.setMinimumSize(150, 150); | |
70 this._findMargins(); | |
71 }, | |
72 | |
73 clearMinimumSizeAndMargins: function() | |
74 { | |
75 this._useMargins = false; | |
76 this.setMinimumSize(1, 1); | |
77 this._findMargins(); | |
78 }, | |
79 | |
80 _dipPageRect: function() | |
81 { | |
82 var zoomFactor = WebInspector.zoomManager.zoomFactor(); | |
83 var rect = this.element.getBoundingClientRect(); | |
84 var bodyRect = this.element.ownerDocument.body.getBoundingClientRect(); | |
85 | |
86 var left = Math.max(rect.left * zoomFactor + this._margins.left, bodyRec
t.left * zoomFactor); | |
87 var top = Math.max(rect.top * zoomFactor + this._margins.top, bodyRect.t
op * zoomFactor); | |
88 var bottom = Math.min(rect.bottom * zoomFactor - this._margins.bottom, b
odyRect.bottom * zoomFactor); | |
89 var right = Math.min(rect.right * zoomFactor - this._margins.right, body
Rect.right * zoomFactor); | |
90 | |
91 return { x: left, y: top, width: right - left, height: bottom - top }; | |
92 }, | |
93 | |
94 update: function() | |
95 { | |
96 delete this._updateId; | |
97 var rect = this._dipPageRect(); | |
98 var bounds = { x: Math.round(rect.x), y: Math.round(rect.y), height: Mat
h.max(1, Math.round(rect.height)), width: Math.max(1, Math.round(rect.width)) }; | |
99 this.dispatchEventToListeners(WebInspector.InspectedPagePlaceholder.Even
ts.Update, bounds); | |
100 }, | |
101 | |
102 __proto__: WebInspector.Widget.prototype | |
103 }; | |
OLD | NEW |