1 /*
2 * Copyright (c) 2008, 2009, Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT{
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,{
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "third_party/blink/renderer/core/scroll/scrollbar_theme_aura.h"
32
33 #include "build/build_config.h"
34 #include "cc/input/scrollbar.h"
35 #include "third_party/blink/public/common/input/web_mouse_event.h"
36 #include "third_party/blink/public/platform/platform.h"
37 #include "third_party/blink/public/platform/web_rect.h"
38 #include "third_party/blink/public/platform/web_theme_engine.h"
39 #include "third_party/blink/renderer/core/scroll/scrollable_area.h"
40 #include "third_party/blink/renderer/core/scroll/scrollbar.h"
41 #include "third_party/blink/renderer/core/scroll/scrollbar_theme_overlay.h"
42 #include "third_party/blink/renderer/platform/geometry/int_rect_outsets.h"
43 #include "third_party/blink/renderer/platform/graphics/graphics_context.h"
44 #include "third_party/blink/renderer/platform/graphics/paint/drawing_recorder.h"
45 #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
46 #include "third_party/blink/renderer/platform/web_test_support.h"
47
48 namespace blink {
49
50 namespace {
51
52 // Use fixed scrollbar thickness for web_tests because many tests are
53 // expecting that. Rebaselining is relatively easy for platform differences,
54 // but tens of testharness tests will fail without this on Windows.
55 // TODO(crbug.com/953847): Adapt testharness tests to native themes and remove
56 // this.
57 constexpr int kScrollbarThicknessForWebTests = 15;
58
59 // Contains a flag indicating whether WebThemeEngine should paint a UI widget
60 // for a scrollbar part, and if so, what part and state apply.
61 //
62 // If the PartPaintingParams are not affected by a change in the scrollbar
63 // state, then the corresponding scrollbar part does not need to be repainted.
64 struct PartPaintingParams {
PartPaintingParamsblink::__anon8b2a6b2b0111::PartPaintingParams65 PartPaintingParams()
66 : should_paint(false),
67 part(WebThemeEngine::kPartScrollbarDownArrow),
68 state(WebThemeEngine::kStateNormal) {}
PartPaintingParamsblink::__anon8b2a6b2b0111::PartPaintingParams69 PartPaintingParams(WebThemeEngine::Part part, WebThemeEngine::State state)
70 : should_paint(true), part(part), state(state) {}
71
72 bool should_paint;
73 WebThemeEngine::Part part;
74 WebThemeEngine::State state;
75 };
76
operator ==(const PartPaintingParams & a,const PartPaintingParams & b)77 bool operator==(const PartPaintingParams& a, const PartPaintingParams& b) {
78 return (!a.should_paint && !b.should_paint) ||
79 std::tie(a.should_paint, a.part, a.state) ==
80 std::tie(b.should_paint, b.part, b.state);
81 }
82
operator !=(const PartPaintingParams & a,const PartPaintingParams & b)83 bool operator!=(const PartPaintingParams& a, const PartPaintingParams& b) {
84 return !(a == b);
85 }
86
ButtonPartPaintingParams(const Scrollbar & scrollbar,float position,ScrollbarPart part)87 PartPaintingParams ButtonPartPaintingParams(const Scrollbar& scrollbar,
88 float position,
89 ScrollbarPart part) {
90 WebThemeEngine::Part paint_part;
91 WebThemeEngine::State state = WebThemeEngine::kStateNormal;
92 bool check_min = false;
93 bool check_max = false;
94
95 if (scrollbar.Orientation() == kHorizontalScrollbar) {
96 if (part == kBackButtonStartPart) {
97 paint_part = WebThemeEngine::kPartScrollbarLeftArrow;
98 check_min = true;
99 } else {
100 paint_part = WebThemeEngine::kPartScrollbarRightArrow;
101 check_max = true;
102 }
103 } else {
104 if (part == kBackButtonStartPart) {
105 paint_part = WebThemeEngine::kPartScrollbarUpArrow;
106 check_min = true;
107 } else {
108 paint_part = WebThemeEngine::kPartScrollbarDownArrow;
109 check_max = true;
110 }
111 }
112
113 if ((check_min && (position <= 0)) ||
114 (check_max && position >= scrollbar.Maximum())) {
115 state = WebThemeEngine::kStateDisabled;
116 } else {
117 if (part == scrollbar.PressedPart())
118 state = WebThemeEngine::kStatePressed;
119 else if (part == scrollbar.HoveredPart())
120 state = WebThemeEngine::kStateHover;
121 }
122
123 return PartPaintingParams(paint_part, state);
124 }
125
126 } // namespace
127
NativeTheme()128 ScrollbarTheme& ScrollbarTheme::NativeTheme() {
129 if (OverlayScrollbarsEnabled())
130 return ScrollbarThemeOverlay::GetInstance();
131
132 DEFINE_STATIC_LOCAL(ScrollbarThemeAura, theme, ());
133 return theme;
134 }
135
SupportsDragSnapBack() const136 bool ScrollbarThemeAura::SupportsDragSnapBack() const {
137 // Disable snapback on desktop Linux to better integrate with the desktop
138 // behavior. Typically, Linux apps do not implement scrollbar snapback (this
139 // is true for at least GTK and QT apps).
140 #if defined(OS_LINUX) || defined(OS_BSD)
141 return false;
142 #else
143 return true;
144 #endif
145 }
146
ScrollbarThickness(float scale_from_dip)147 int ScrollbarThemeAura::ScrollbarThickness(float scale_from_dip) {
148 if (WebTestSupport::IsRunningWebTest())
149 return kScrollbarThicknessForWebTests * scale_from_dip;
150
151 // Horiz and Vert scrollbars are the same thickness.
152 IntSize scrollbar_size = IntSize(Platform::Current()->ThemeEngine()->GetSize(
153 WebThemeEngine::kPartScrollbarVerticalTrack));
154 return scrollbar_size.Width() * scale_from_dip;
155 }
156
HasThumb(const Scrollbar & scrollbar)157 bool ScrollbarThemeAura::HasThumb(const Scrollbar& scrollbar) {
158 // This method is just called as a paint-time optimization to see if
159 // painting the thumb can be skipped. We don't have to be exact here.
160 return ThumbLength(scrollbar) > 0;
161 }
162
BackButtonRect(const Scrollbar & scrollbar)163 IntRect ScrollbarThemeAura::BackButtonRect(const Scrollbar& scrollbar) {
164 IntSize size = ButtonSize(scrollbar);
165 return IntRect(scrollbar.X(), scrollbar.Y(), size.Width(), size.Height());
166 }
167
ForwardButtonRect(const Scrollbar & scrollbar)168 IntRect ScrollbarThemeAura::ForwardButtonRect(const Scrollbar& scrollbar) {
169 IntSize size = ButtonSize(scrollbar);
170 int x, y;
171 if (scrollbar.Orientation() == kHorizontalScrollbar) {
172 x = scrollbar.X() + scrollbar.Width() - size.Width();
173 y = scrollbar.Y();
174 } else {
175 x = scrollbar.X();
176 y = scrollbar.Y() + scrollbar.Height() - size.Height();
177 }
178 return IntRect(x, y, size.Width(), size.Height());
179 }
180
TrackRect(const Scrollbar & scrollbar)181 IntRect ScrollbarThemeAura::TrackRect(const Scrollbar& scrollbar) {
182 // The track occupies all space between the two buttons.
183 IntSize bs = ButtonSize(scrollbar);
184 if (scrollbar.Orientation() == kHorizontalScrollbar) {
185 if (scrollbar.Width() <= 2 * bs.Width())
186 return IntRect();
187 return IntRect(scrollbar.X() + bs.Width(), scrollbar.Y(),
188 scrollbar.Width() - 2 * bs.Width(), scrollbar.Height());
189 }
190 if (scrollbar.Height() <= 2 * bs.Height())
191 return IntRect();
192 return IntRect(scrollbar.X(), scrollbar.Y() + bs.Height(), scrollbar.Width(),
193 scrollbar.Height() - 2 * bs.Height());
194 }
195
MinimumThumbLength(const Scrollbar & scrollbar)196 int ScrollbarThemeAura::MinimumThumbLength(const Scrollbar& scrollbar) {
197 if (scrollbar.Orientation() == kVerticalScrollbar) {
198 return scrollbar.ScaleFromDIP() *
199 Platform::Current()
200 ->ThemeEngine()
201 ->GetSize(WebThemeEngine::kPartScrollbarVerticalThumb)
202 .height();
203 }
204
205 return scrollbar.ScaleFromDIP() *
206 Platform::Current()
207 ->ThemeEngine()
208 ->GetSize(WebThemeEngine::kPartScrollbarHorizontalThumb)
209 .width();
210 }
211
PaintTrack(GraphicsContext & context,const Scrollbar & scrollbar,const IntRect & rect)212 void ScrollbarThemeAura::PaintTrack(GraphicsContext& context,
213 const Scrollbar& scrollbar,
214 const IntRect& rect) {
215 if (rect.IsEmpty())
216 return;
217
218 // We always paint the track as a single piece, so don't support hover state
219 // of the back track and forward track.
220 auto state = WebThemeEngine::kStateNormal;
221
222 // TODO(wangxianzhu): The extra params for scrollbar track were for painting
223 // back and forward tracks separately, which we don't support. Remove them.
224 IntRect align_rect = TrackRect(scrollbar);
225 WebThemeEngine::ExtraParams extra_params;
226 extra_params.scrollbar_track.is_back = false;
227 extra_params.scrollbar_track.track_x = align_rect.X();
228 extra_params.scrollbar_track.track_y = align_rect.Y();
229 extra_params.scrollbar_track.track_width = align_rect.Width();
230 extra_params.scrollbar_track.track_height = align_rect.Height();
231
232 Platform::Current()->ThemeEngine()->Paint(
233 context.Canvas(),
234 scrollbar.Orientation() == kHorizontalScrollbar
235 ? WebThemeEngine::kPartScrollbarHorizontalTrack
236 : WebThemeEngine::kPartScrollbarVerticalTrack,
237 state, gfx::Rect(rect), &extra_params, scrollbar.UsedColorScheme());
238 }
239
PaintButton(GraphicsContext & gc,const Scrollbar & scrollbar,const IntRect & rect,ScrollbarPart part)240 void ScrollbarThemeAura::PaintButton(GraphicsContext& gc,
241 const Scrollbar& scrollbar,
242 const IntRect& rect,
243 ScrollbarPart part) {
244 PartPaintingParams params =
245 ButtonPartPaintingParams(scrollbar, scrollbar.CurrentPos(), part);
246 if (!params.should_paint)
247 return;
248
249 WebThemeEngine::ExtraParams extra_params;
250 extra_params.scrollbar_button.zoom = scrollbar.EffectiveZoom();
251 extra_params.scrollbar_button.right_to_left =
252 scrollbar.ContainerIsRightToLeft();
253 Platform::Current()->ThemeEngine()->Paint(
254 gc.Canvas(), params.part, params.state, gfx::Rect(rect), &extra_params,
255 scrollbar.UsedColorScheme());
256 }
257
PaintThumb(GraphicsContext & gc,const Scrollbar & scrollbar,const IntRect & rect)258 void ScrollbarThemeAura::PaintThumb(GraphicsContext& gc,
259 const Scrollbar& scrollbar,
260 const IntRect& rect) {
261 if (DrawingRecorder::UseCachedDrawingIfPossible(gc, scrollbar,
262 DisplayItem::kScrollbarThumb))
263 return;
264
265 DrawingRecorder recorder(gc, scrollbar, DisplayItem::kScrollbarThumb, rect);
266
267 WebThemeEngine::State state;
268 cc::PaintCanvas* canvas = gc.Canvas();
269 if (scrollbar.PressedPart() == kThumbPart)
270 state = WebThemeEngine::kStatePressed;
271 else if (scrollbar.HoveredPart() == kThumbPart)
272 state = WebThemeEngine::kStateHover;
273 else
274 state = WebThemeEngine::kStateNormal;
275
276 Platform::Current()->ThemeEngine()->Paint(
277 canvas,
278 scrollbar.Orientation() == kHorizontalScrollbar
279 ? WebThemeEngine::kPartScrollbarHorizontalThumb
280 : WebThemeEngine::kPartScrollbarVerticalThumb,
281 state, gfx::Rect(rect), nullptr, scrollbar.UsedColorScheme());
282 }
283
ShouldRepaintAllPartsOnInvalidation() const284 bool ScrollbarThemeAura::ShouldRepaintAllPartsOnInvalidation() const {
285 // This theme can separately handle thumb invalidation.
286 return false;
287 }
288
PartsToInvalidateOnThumbPositionChange(const Scrollbar & scrollbar,float old_position,float new_position) const289 ScrollbarPart ScrollbarThemeAura::PartsToInvalidateOnThumbPositionChange(
290 const Scrollbar& scrollbar,
291 float old_position,
292 float new_position) const {
293 ScrollbarPart invalid_parts = kNoPart;
294 static const ScrollbarPart kButtonParts[] = {kBackButtonStartPart,
295 kForwardButtonEndPart};
296 for (ScrollbarPart part : kButtonParts) {
297 if (ButtonPartPaintingParams(scrollbar, old_position, part) !=
298 ButtonPartPaintingParams(scrollbar, new_position, part))
299 invalid_parts = static_cast<ScrollbarPart>(invalid_parts | part);
300 }
301 return invalid_parts;
302 }
303
ShouldCenterOnThumb(const Scrollbar & scrollbar,const WebMouseEvent & event)304 bool ScrollbarThemeAura::ShouldCenterOnThumb(const Scrollbar& scrollbar,
305 const WebMouseEvent& event) {
306 #if defined(OS_LINUX) || defined(OS_BSD)
307 if (event.button == WebPointerProperties::Button::kMiddle)
308 return true;
309 #endif
310 bool shift_key_pressed = event.GetModifiers() & WebInputEvent::kShiftKey;
311 return (event.button == WebPointerProperties::Button::kLeft) &&
312 shift_key_pressed;
313 }
314
ShouldSnapBackToDragOrigin(const Scrollbar & scrollbar,const WebMouseEvent & event)315 bool ScrollbarThemeAura::ShouldSnapBackToDragOrigin(
316 const Scrollbar& scrollbar,
317 const WebMouseEvent& event) {
318 if (!SupportsDragSnapBack())
319 return false;
320
321 // There is a drag rect around the scrollbar outside of which the scrollbar
322 // thumb should snap back to its origin. This rect is infinitely large in
323 // the scrollbar's scrolling direction and an expansion of the scrollbar's
324 // width or height in the non-scrolling direction. As only one axis triggers
325 // snapping back, the code below only uses the thickness of the scrollbar for
326 // its calculations.
327 bool is_horizontal = scrollbar.Orientation() == kHorizontalScrollbar;
328 int thickness = is_horizontal ? TrackRect(scrollbar).Height()
329 : TrackRect(scrollbar).Width();
330 // Even if the platform's scrollbar is narrower than the default Windows one,
331 // we still want to provide at least as much slop area, since a slightly
332 // narrower scrollbar doesn't necessarily imply that users will drag
333 // straighter.
334 int expansion_amount =
335 kOffSideMultiplier * std::max(thickness, kDefaultWinScrollbarThickness);
336
337 int snap_outside_of_min = -expansion_amount;
338 int snap_outside_of_max = expansion_amount + thickness;
339
340 IntPoint mouse_position = scrollbar.ConvertFromRootFrame(
341 FlooredIntPoint(event.PositionInRootFrame()));
342 int mouse_offset_in_scrollbar =
343 is_horizontal ? mouse_position.Y() : mouse_position.X();
344
345 return (mouse_offset_in_scrollbar < snap_outside_of_min ||
346 mouse_offset_in_scrollbar >= snap_outside_of_max);
347 }
348
HasScrollbarButtons(ScrollbarOrientation orientation) const349 bool ScrollbarThemeAura::HasScrollbarButtons(
350 ScrollbarOrientation orientation) const {
351 WebThemeEngine* theme_engine = Platform::Current()->ThemeEngine();
352 if (orientation == kVerticalScrollbar) {
353 return !theme_engine->GetSize(WebThemeEngine::kPartScrollbarDownArrow)
354 .IsEmpty();
355 }
356 return !theme_engine->GetSize(WebThemeEngine::kPartScrollbarLeftArrow)
357 .IsEmpty();
358 }
359
ButtonSize(const Scrollbar & scrollbar)360 IntSize ScrollbarThemeAura::ButtonSize(const Scrollbar& scrollbar) {
361 if (!HasScrollbarButtons(scrollbar.Orientation()))
362 return IntSize(0, 0);
363
364 if (scrollbar.Orientation() == kVerticalScrollbar) {
365 int square_size = scrollbar.Width();
366 return IntSize(square_size, scrollbar.Height() < 2 * square_size
367 ? scrollbar.Height() / 2
368 : square_size);
369 }
370
371 // HorizontalScrollbar
372 int square_size = scrollbar.Height();
373 return IntSize(
374 scrollbar.Width() < 2 * square_size ? scrollbar.Width() / 2 : square_size,
375 square_size);
376 }
377
378 } // namespace blink
379