Bug 1734943 [wpt PR 31170] - Correct scrolling contents cull rect, a=testonly
[gecko.git] / widget / nsNativeBasicThemeAndroid.cpp
blobf2c6013737068c7fa24d6c5c0f6535118aa7a4b1
1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsNativeBasicThemeAndroid.h"
8 #include "mozilla/ClearOnShutdown.h"
9 #include "nsStyleConsts.h"
10 #include "nsIFrame.h"
12 auto nsNativeBasicThemeAndroid::GetScrollbarSizes(nsPresContext* aPresContext,
13 StyleScrollbarWidth aWidth,
14 Overlay aOverlay)
15 -> ScrollbarSizes {
16 // We force auto-width scrollbars because scrollbars on android are already
17 // thin enough.
18 return nsNativeBasicTheme::GetScrollbarSizes(
19 aPresContext, StyleScrollbarWidth::Auto, aOverlay);
22 NS_IMETHODIMP
23 nsNativeBasicThemeAndroid::GetMinimumWidgetSize(
24 nsPresContext* aPresContext, nsIFrame* aFrame, StyleAppearance aAppearance,
25 mozilla::LayoutDeviceIntSize* aResult, bool* aIsOverridable) {
26 if (!IsWidgetScrollbarPart(aAppearance)) {
27 return nsNativeBasicTheme::GetMinimumWidgetSize(
28 aPresContext, aFrame, aAppearance, aResult, aIsOverridable);
31 auto sizes =
32 GetScrollbarSizes(aPresContext, StyleScrollbarWidth::Auto, Overlay::Yes);
33 aResult->SizeTo(sizes.mHorizontal, sizes.mHorizontal);
34 MOZ_ASSERT(sizes.mHorizontal == sizes.mVertical);
36 *aIsOverridable = true;
37 return NS_OK;
40 template <typename PaintBackendData>
41 void nsNativeBasicThemeAndroid::DoPaintScrollbarThumb(
42 PaintBackendData& aPaintData, const LayoutDeviceRect& aRect,
43 bool aHorizontal, nsIFrame* aFrame, const ComputedStyle& aStyle,
44 const EventStates& aElementState, const EventStates& aDocumentState,
45 const Colors& aColors, DPIRatio aDpiRatio) {
46 // TODO(emilio): Maybe do like macOS and draw a stroke?
47 const auto color = ComputeScrollbarThumbColor(aFrame, aStyle, aElementState,
48 aDocumentState, aColors);
50 // Draw the thumb rect centered in the scrollbar.
51 LayoutDeviceRect thumbRect(aRect);
52 if (aHorizontal) {
53 thumbRect.height *= 0.5f;
54 thumbRect.y += thumbRect.height * 0.5f;
55 } else {
56 thumbRect.width *= 0.5f;
57 thumbRect.x += thumbRect.width * 0.5f;
60 const LayoutDeviceCoord radius =
61 (aHorizontal ? thumbRect.height : thumbRect.width) / 2.0f;
62 PaintRoundedRectWithRadius(aPaintData, thumbRect, color,
63 sRGBColor::White(0.0f), 0.0f, radius / aDpiRatio,
64 aDpiRatio);
67 bool nsNativeBasicThemeAndroid::PaintScrollbarThumb(
68 DrawTarget& aDt, const LayoutDeviceRect& aRect, bool aHorizontal,
69 nsIFrame* aFrame, const ComputedStyle& aStyle,
70 const EventStates& aElementState, const EventStates& aDocumentState,
71 const Colors& aColors, DPIRatio aDpiRatio) {
72 DoPaintScrollbarThumb(aDt, aRect, aHorizontal, aFrame, aStyle, aElementState,
73 aDocumentState, aColors, aDpiRatio);
74 return true;
77 bool nsNativeBasicThemeAndroid::PaintScrollbarThumb(
78 WebRenderBackendData& aWrData, const LayoutDeviceRect& aRect,
79 bool aHorizontal, nsIFrame* aFrame, const ComputedStyle& aStyle,
80 const EventStates& aElementState, const EventStates& aDocumentState,
81 const Colors& aColors, DPIRatio aDpiRatio) {
82 DoPaintScrollbarThumb(aWrData, aRect, aHorizontal, aFrame, aStyle,
83 aElementState, aDocumentState, aColors, aDpiRatio);
84 return true;
87 already_AddRefed<nsITheme> do_GetAndroidNonNativeThemeDoNotUseDirectly() {
88 static mozilla::StaticRefPtr<nsITheme> gInstance;
89 if (MOZ_UNLIKELY(!gInstance)) {
90 gInstance = new nsNativeBasicThemeAndroid();
91 ClearOnShutdown(&gInstance);
93 return do_AddRef(gInstance);
97 #ifdef ANDROID
98 already_AddRefed<nsITheme> do_GetBasicNativeThemeDoNotUseDirectly() {
99 return do_GetAndroidNonNativeThemeDoNotUseDirectly();
102 already_AddRefed<nsITheme> do_GetNativeThemeDoNotUseDirectly() {
103 // Android doesn't have a native theme.
104 return do_GetBasicNativeThemeDoNotUseDirectly();
106 #endif