1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "third_party/blink/renderer/core/paint/custom_scrollbar_theme.h"
27
28 #include "third_party/blink/renderer/core/layout/custom_scrollbar.h"
29 #include "third_party/blink/renderer/core/layout/layout_custom_scrollbar_part.h"
30 #include "third_party/blink/renderer/core/paint/object_painter.h"
31 #include "third_party/blink/renderer/core/paint/paint_info.h"
32 #include "third_party/blink/renderer/core/scroll/scrollbar.h"
33 #include "third_party/blink/renderer/platform/graphics/graphics_context.h"
34 #include "third_party/blink/renderer/platform/graphics/paint/drawing_recorder.h"
35 #include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
36
37 namespace blink {
38
GetCustomScrollbarTheme()39 CustomScrollbarTheme* CustomScrollbarTheme::GetCustomScrollbarTheme() {
40 DEFINE_STATIC_LOCAL(CustomScrollbarTheme, theme, ());
41 return &theme;
42 }
43
HitTest(const Scrollbar & scrollbar,const IntPoint & test_position)44 ScrollbarPart CustomScrollbarTheme::HitTest(const Scrollbar& scrollbar,
45 const IntPoint& test_position) {
46 auto result = ScrollbarTheme::HitTest(scrollbar, test_position);
47 if (result == kScrollbarBGPart) {
48 // The ScrollbarTheme knows nothing about the double buttons.
49 if (ButtonRect(scrollbar, kBackButtonEndPart).Contains(test_position))
50 return kBackButtonEndPart;
51 if (ButtonRect(scrollbar, kForwardButtonStartPart).Contains(test_position))
52 return kForwardButtonStartPart;
53 }
54 return result;
55 }
56
ButtonSizesAlongTrackAxis(const Scrollbar & scrollbar,int & before_size,int & after_size)57 void CustomScrollbarTheme::ButtonSizesAlongTrackAxis(const Scrollbar& scrollbar,
58 int& before_size,
59 int& after_size) {
60 IntRect first_button = ButtonRect(scrollbar, kBackButtonStartPart);
61 IntRect second_button = ButtonRect(scrollbar, kForwardButtonStartPart);
62 IntRect third_button = ButtonRect(scrollbar, kBackButtonEndPart);
63 IntRect fourth_button = ButtonRect(scrollbar, kForwardButtonEndPart);
64 if (scrollbar.Orientation() == kHorizontalScrollbar) {
65 before_size = first_button.Width() + second_button.Width();
66 after_size = third_button.Width() + fourth_button.Width();
67 } else {
68 before_size = first_button.Height() + second_button.Height();
69 after_size = third_button.Height() + fourth_button.Height();
70 }
71 }
72
HasButtons(const Scrollbar & scrollbar)73 bool CustomScrollbarTheme::HasButtons(const Scrollbar& scrollbar) {
74 int start_size;
75 int end_size;
76 ButtonSizesAlongTrackAxis(scrollbar, start_size, end_size);
77 return (start_size + end_size) <=
78 (scrollbar.Orientation() == kHorizontalScrollbar ? scrollbar.Width()
79 : scrollbar.Height());
80 }
81
HasThumb(const Scrollbar & scrollbar)82 bool CustomScrollbarTheme::HasThumb(const Scrollbar& scrollbar) {
83 return TrackLength(scrollbar) - ThumbLength(scrollbar) >= 0;
84 }
85
MinimumThumbLength(const Scrollbar & scrollbar)86 int CustomScrollbarTheme::MinimumThumbLength(const Scrollbar& scrollbar) {
87 return To<CustomScrollbar>(scrollbar).MinimumThumbLength();
88 }
89
ButtonRect(const Scrollbar & scrollbar,ScrollbarPart part_type)90 IntRect CustomScrollbarTheme::ButtonRect(const Scrollbar& scrollbar,
91 ScrollbarPart part_type) {
92 return To<CustomScrollbar>(scrollbar).ButtonRect(part_type);
93 }
94
BackButtonRect(const Scrollbar & scrollbar)95 IntRect CustomScrollbarTheme::BackButtonRect(const Scrollbar& scrollbar) {
96 return ButtonRect(scrollbar, kBackButtonStartPart);
97 }
98
ForwardButtonRect(const Scrollbar & scrollbar)99 IntRect CustomScrollbarTheme::ForwardButtonRect(const Scrollbar& scrollbar) {
100 return ButtonRect(scrollbar, kForwardButtonEndPart);
101 }
102
TrackRect(const Scrollbar & scrollbar)103 IntRect CustomScrollbarTheme::TrackRect(const Scrollbar& scrollbar) {
104 if (!HasButtons(scrollbar))
105 return scrollbar.FrameRect();
106
107 int start_length;
108 int end_length;
109 ButtonSizesAlongTrackAxis(scrollbar, start_length, end_length);
110
111 return To<CustomScrollbar>(scrollbar).TrackRect(start_length, end_length);
112 }
113
ConstrainTrackRectToTrackPieces(const Scrollbar & scrollbar,const IntRect & rect)114 IntRect CustomScrollbarTheme::ConstrainTrackRectToTrackPieces(
115 const Scrollbar& scrollbar,
116 const IntRect& rect) {
117 IntRect back_rect = To<CustomScrollbar>(scrollbar).TrackPieceRectWithMargins(
118 kBackTrackPart, rect);
119 IntRect forward_rect =
120 To<CustomScrollbar>(scrollbar).TrackPieceRectWithMargins(
121 kForwardTrackPart, rect);
122 IntRect result = rect;
123 if (scrollbar.Orientation() == kHorizontalScrollbar) {
124 result.SetX(back_rect.X());
125 result.SetWidth(forward_rect.MaxX() - back_rect.X());
126 } else {
127 result.SetY(back_rect.Y());
128 result.SetHeight(forward_rect.MaxY() - back_rect.Y());
129 }
130 return result;
131 }
132
PaintScrollCorner(GraphicsContext & context,const Scrollbar * vertical_scrollbar,const DisplayItemClient & display_item_client,const IntRect & corner_rect,WebColorScheme color_scheme)133 void CustomScrollbarTheme::PaintScrollCorner(
134 GraphicsContext& context,
135 const Scrollbar* vertical_scrollbar,
136 const DisplayItemClient& display_item_client,
137 const IntRect& corner_rect,
138 WebColorScheme color_scheme) {
139 if (DrawingRecorder::UseCachedDrawingIfPossible(context, display_item_client,
140 DisplayItem::kScrollCorner))
141 return;
142
143 DrawingRecorder recorder(context, display_item_client,
144 DisplayItem::kScrollCorner);
145 // FIXME: Implement.
146 context.FillRect(corner_rect, Color::kWhite);
147 }
148
PaintTrackAndButtons(GraphicsContext & context,const Scrollbar & scrollbar,const IntPoint & offset)149 void CustomScrollbarTheme::PaintTrackAndButtons(GraphicsContext& context,
150 const Scrollbar& scrollbar,
151 const IntPoint& offset) {
152 // Custom scrollbars are always painted in their original coordinate space,
153 // i.e. the space of Scrollbar::FrameRect() and ScrollbarTheme::XXXRect()
154 // which is |context|'s current space.
155 DCHECK_EQ(offset, IntPoint());
156
157 PaintPart(context, scrollbar, scrollbar.FrameRect(), kScrollbarBGPart);
158
159 if (HasButtons(scrollbar)) {
160 PaintButton(context, scrollbar, ButtonRect(scrollbar, kBackButtonStartPart),
161 kBackButtonStartPart);
162 PaintButton(context, scrollbar, ButtonRect(scrollbar, kBackButtonEndPart),
163 kBackButtonEndPart);
164 PaintButton(context, scrollbar,
165 ButtonRect(scrollbar, kForwardButtonStartPart),
166 kForwardButtonStartPart);
167 PaintButton(context, scrollbar,
168 ButtonRect(scrollbar, kForwardButtonEndPart),
169 kForwardButtonEndPart);
170 }
171
172 IntRect track_rect = TrackRect(scrollbar);
173 PaintPart(context, scrollbar, track_rect, kTrackBGPart);
174
175 if (HasThumb(scrollbar)) {
176 IntRect start_track_rect;
177 IntRect thumb_rect;
178 IntRect end_track_rect;
179 SplitTrack(scrollbar, track_rect, start_track_rect, thumb_rect,
180 end_track_rect);
181 PaintPart(context, scrollbar, start_track_rect, kBackTrackPart);
182 PaintPart(context, scrollbar, end_track_rect, kForwardTrackPart);
183 }
184 }
185
PaintButton(GraphicsContext & context,const Scrollbar & scrollbar,const IntRect & rect,ScrollbarPart part)186 void CustomScrollbarTheme::PaintButton(GraphicsContext& context,
187 const Scrollbar& scrollbar,
188 const IntRect& rect,
189 ScrollbarPart part) {
190 PaintPart(context, scrollbar, rect, part);
191 }
192
PaintThumb(GraphicsContext & context,const Scrollbar & scrollbar,const IntRect & rect)193 void CustomScrollbarTheme::PaintThumb(GraphicsContext& context,
194 const Scrollbar& scrollbar,
195 const IntRect& rect) {
196 PaintPart(context, scrollbar, rect, kThumbPart);
197 }
198
PaintTickmarks(GraphicsContext & context,const Scrollbar & scrollbar,const IntRect & rect)199 void CustomScrollbarTheme::PaintTickmarks(GraphicsContext& context,
200 const Scrollbar& scrollbar,
201 const IntRect& rect) {
202 GetTheme().PaintTickmarks(context, scrollbar, rect);
203 }
204
PaintIntoRect(const LayoutCustomScrollbarPart & layout_custom_scrollbar_part,GraphicsContext & graphics_context,const PhysicalOffset & paint_offset,const PhysicalRect & rect,const CustomScrollbar * scrollbar)205 void CustomScrollbarTheme::PaintIntoRect(
206 const LayoutCustomScrollbarPart& layout_custom_scrollbar_part,
207 GraphicsContext& graphics_context,
208 const PhysicalOffset& paint_offset,
209 const PhysicalRect& rect,
210 const CustomScrollbar* scrollbar) {
211 // Make sure our dimensions match the rect.
212 // TODO(crbug.com/856802): Setting these is a bad layering violation!
213 // Move these into layout stage.
214 const_cast<LayoutCustomScrollbarPart&>(layout_custom_scrollbar_part)
215 .SetLocation((rect.offset - paint_offset).ToLayoutPoint());
216 const_cast<LayoutCustomScrollbarPart&>(layout_custom_scrollbar_part)
217 .SetWidth(rect.size.width);
218 const_cast<LayoutCustomScrollbarPart&>(layout_custom_scrollbar_part)
219 .SetHeight(rect.size.height);
220 // TODO(crbug.com/856802): Move this into PaintPropertyTreeBuilder.
221 layout_custom_scrollbar_part.GetMutableForPainting()
222 .FirstFragment()
223 .SetPaintOffset((scrollbar ? PhysicalOffset(scrollbar->Location())
224 : PhysicalOffset()) +
225 layout_custom_scrollbar_part.PhysicalLocation());
226
227 PaintInfo paint_info(graphics_context, PixelSnappedIntRect(rect),
228 PaintPhase::kForeground, kGlobalPaintNormalPhase,
229 kPaintLayerNoFlag);
230 ObjectPainter(layout_custom_scrollbar_part)
231 .PaintAllPhasesAtomically(paint_info);
232 }
233
PaintPart(GraphicsContext & context,const Scrollbar & scrollbar,const IntRect & rect,ScrollbarPart part)234 void CustomScrollbarTheme::PaintPart(GraphicsContext& context,
235 const Scrollbar& scrollbar,
236 const IntRect& rect,
237 ScrollbarPart part) {
238 const auto& custom_scrollbar = To<CustomScrollbar>(scrollbar);
239 const auto* part_layout_object = custom_scrollbar.GetPart(part);
240 if (!part_layout_object)
241 return;
242 PaintIntoRect(*part_layout_object, context,
243 PhysicalOffset(custom_scrollbar.Location()), PhysicalRect(rect),
244 &custom_scrollbar);
245 }
246
247 } // namespace blink
248