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
92class InspectorOverlay {
93 WTF_MAKE_FAST_ALLOCATED;
94public:
95 InspectorOverlay(Page&, InspectorClient*);
96 ~InspectorOverlay();
97
98 enum class CoordinateSystem {
99 View, // Adjusts for the main frame's scroll offset.
100 Document, // Does not adjust for the main frame's scroll offset.
101 };
102
103 void update();
104 void paint(GraphicsContext&);
105 void getHighlight(Highlight&, CoordinateSystem) const;
106
107 void hideHighlight();
108 void highlightNodeList(RefPtr<NodeList>&&, const HighlightConfig&);
109 void highlightNode(Node*, const HighlightConfig&);
110 void highlightQuad(std::unique_ptr<FloatQuad>, const HighlightConfig&);
111
112 void setShowPaintRects(bool);
113 void showPaintRect(const FloatRect&);
114
115 void setShowRulers(bool);
116
117 Node* highlightedNode() const;
118
119 void didSetSearchingForNode(bool enabled);
120
121 void setIndicating(bool indicating);
122
123private:
124 using TimeRectPair = std::pair<MonotonicTime, FloatRect>;
125
126 bool shouldShowOverlay() const;
127
128 void drawNodeHighlight(GraphicsContext&, Node&);
129 void drawQuadHighlight(GraphicsContext&, const FloatQuad&);
130 void drawPaintRects(GraphicsContext&, const Deque<TimeRectPair>&);
131 void drawBounds(GraphicsContext&, const FloatRect&);
132 void drawRulers(GraphicsContext&);
133
134 void drawElementTitle(GraphicsContext&, Node&, const FloatRect& bounds);
135
136 void updatePaintRectsTimerFired();
137
138 Page& m_page;
139 InspectorClient* m_client;
140
141 RefPtr<Node> m_highlightNode;
142 RefPtr<NodeList> m_highlightNodeList;
143 HighlightConfig m_nodeHighlightConfig;
144
145 std::unique_ptr<FloatQuad> m_highlightQuad;
146 HighlightConfig m_quadHighlightConfig;
147
148 Deque<TimeRectPair> m_paintRects;
149 Timer m_paintRectUpdateTimer;
150
151 bool m_indicating {false};
152 bool m_showPaintRects {false};
153 bool m_showRulers {false};
154};
155
156} // namespace WebCore
157