1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2019 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#pragma once
31
32#include "Color.h"
33#include "FloatQuad.h"
34#include "FloatRect.h"
35#include "Timer.h"
36#include <wtf/Deque.h>
37#include <wtf/MonotonicTime.h>
38#include <wtf/RefPtr.h>
39#include <wtf/Vector.h>
40#include <wtf/text/WTFString.h>
41
42namespace WebCore {
43
44class GraphicsContext;
45class InspectorClient;
46class Node;
47class NodeList;
48class Page;
49
50struct HighlightConfig {
51 WTF_MAKE_FAST_ALLOCATED;
52public:
53 Color content;
54 Color contentOutline;
55 Color padding;
56 Color border;
57 Color margin;
58 bool showInfo;
59 bool usePageCoordinates;
60};
61
62enum class HighlightType {
63 Node, // Provides 4 quads: margin, border, padding, content.
64 NodeList, // Provides a list of nodes.
65 Rects, // Provides a list of quads.
66};
67
68struct Highlight {
69 Highlight() { }
70
71 void setDataFromConfig(const HighlightConfig& highlightConfig)
72 {
73 contentColor = highlightConfig.content;
74 contentOutlineColor = highlightConfig.contentOutline;
75 paddingColor = highlightConfig.padding;
76 borderColor = highlightConfig.border;
77 marginColor = highlightConfig.margin;
78 usePageCoordinates = highlightConfig.usePageCoordinates;
79 }
80
81 Color contentColor;
82 Color contentOutlineColor;
83 Color paddingColor;
84 Color borderColor;
85 Color marginColor;
86
87 HighlightType type {HighlightType::Node};
88 Vector<FloatQuad> quads;
89 bool usePageCoordinates {true};
90
91 using Bounds = FloatRect;
92};
93
94class InspectorOverlay {
95 WTF_MAKE_FAST_ALLOCATED;
96public:
97 InspectorOverlay(Page&, InspectorClient*);
98 ~InspectorOverlay();
99
100 enum class CoordinateSystem {
101 View, // Adjusts for the main frame's scroll offset.
102 Document, // Does not adjust for the main frame's scroll offset.
103 };
104
105 void update();
106 void paint(GraphicsContext&);
107 void getHighlight(Highlight&, CoordinateSystem) const;
108
109 void hideHighlight();
110 void highlightNodeList(RefPtr<NodeList>&&, const HighlightConfig&);
111 void highlightNode(Node*, const HighlightConfig&);
112 void highlightQuad(std::unique_ptr<FloatQuad>, const HighlightConfig&);
113
114 void setShowPaintRects(bool);
115 void showPaintRect(const FloatRect&);
116
117 void setShowRulers(bool);
118 void setShowRulersDuringElementSelection(bool enabled) { m_showRulersDuringElementSelection = enabled; }
119
120 Node* highlightedNode() const;
121
122 void didSetSearchingForNode(bool enabled);
123
124 void setIndicating(bool indicating);
125
126private:
127 using TimeRectPair = std::pair<MonotonicTime, FloatRect>;
128
129 bool shouldShowOverlay() const;
130
131 Highlight::Bounds drawNodeHighlight(GraphicsContext&, Node&);
132 Highlight::Bounds drawQuadHighlight(GraphicsContext&, const FloatQuad&);
133 void drawPaintRects(GraphicsContext&, const Deque<TimeRectPair>&);
134 void drawBounds(GraphicsContext&, const Highlight::Bounds&);
135 void drawRulers(GraphicsContext&, const Highlight::Bounds&);
136
137 void drawElementTitle(GraphicsContext&, Node&, const Highlight::Bounds&);
138
139 void updatePaintRectsTimerFired();
140
141 Page& m_page;
142 InspectorClient* m_client;
143
144 RefPtr<Node> m_highlightNode;
145 RefPtr<NodeList> m_highlightNodeList;
146 HighlightConfig m_nodeHighlightConfig;
147
148 std::unique_ptr<FloatQuad> m_highlightQuad;
149 HighlightConfig m_quadHighlightConfig;
150
151 Deque<TimeRectPair> m_paintRects;
152 Timer m_paintRectUpdateTimer;
153
154 bool m_indicating { false };
155 bool m_showPaintRects { false };
156 bool m_showRulers { false };
157 bool m_showRulersDuringElementSelection { false };
158};
159
160} // namespace WebCore
161