summaryrefslogtreecommitdiffstats
path: root/widget/android/nsNativeThemeAndroid.cpp
blob: 3f42a38374de334a2cf6b8b076c15a01dffb4edc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
/* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsNativeThemeAndroid.h"

#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/ClearOnShutdown.h"
#include "nsCSSRendering.h"
#include "nsDateTimeControlFrame.h"
#include "nsDeviceContext.h"
#include "nsLayoutUtils.h"
#include "PathHelpers.h"

using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::widget;

namespace mozilla {
namespace widget {

static const sRGBColor sBackgroundColor(sRGBColor(1.0f, 1.0f, 1.0f));
static const sRGBColor sBackgroundActiveColor(sRGBColor(0.88f, 0.88f, 0.9f));
static const sRGBColor sBackgroundActiveColorDisabled(sRGBColor(0.88f, 0.88f,
                                                                0.9f, 0.4f));
static const sRGBColor sBorderColor(sRGBColor(0.62f, 0.62f, 0.68f));
static const sRGBColor sBorderColorDisabled(sRGBColor(0.44f, 0.44f, 0.44f,
                                                      0.4f));
static const sRGBColor sBorderHoverColor(sRGBColor(0.5f, 0.5f, 0.56f));
static const sRGBColor sBorderHoverColorDisabled(sRGBColor(0.5f, 0.5f, 0.56f,
                                                           0.4f));
static const sRGBColor sBorderFocusColor(sRGBColor(0.04f, 0.52f, 1.0f));
static const sRGBColor sCheckBackgroundColor(sRGBColor(0.18f, 0.39f, 0.89f));
static const sRGBColor sCheckBackgroundColorDisabled(sRGBColor(0.18f, 0.39f,
                                                               0.89f, 0.4f));
static const sRGBColor sCheckBackgroundHoverColor(sRGBColor(0.02f, 0.24f,
                                                            0.58f));
static const sRGBColor sCheckBackgroundHoverColorDisabled(
    sRGBColor(0.02f, 0.24f, 0.58f, 0.4f));
static const sRGBColor sCheckBackgroundActiveColor(sRGBColor(0.03f, 0.19f,
                                                             0.45f));
static const sRGBColor sCheckBackgroundActiveColorDisabled(
    sRGBColor(0.03f, 0.19f, 0.45f, 0.4f));
static const sRGBColor sDisabledColor(sRGBColor(0.89f, 0.89f, 0.89f));
static const sRGBColor sActiveColor(sRGBColor(0.47f, 0.47f, 0.48f));
static const sRGBColor sInputHoverColor(sRGBColor(0.05f, 0.05f, 0.05f, 0.5f));
static const sRGBColor sRangeInputBackgroundColor(sRGBColor(0.89f, 0.89f,
                                                            0.89f));
static const sRGBColor sButtonColor(sRGBColor(0.98f, 0.98f, 0.98f));
static const sRGBColor sButtonHoverColor(sRGBColor(0.94f, 0.94f, 0.96f));
static const sRGBColor sButtonActiveColor(sRGBColor(0.88f, 0.88f, 0.90f));
static const sRGBColor sWhiteColor(sRGBColor(1.0f, 1.0f, 1.0f, 0.0f));

static const CSSIntCoord kMinimumAndroidWidgetSize = 17;

}  // namespace widget
}  // namespace mozilla

NS_IMPL_ISUPPORTS_INHERITED(nsNativeThemeAndroid, nsNativeTheme, nsITheme)

static uint32_t GetDPIRatio(nsIFrame* aFrame) {
  return AppUnitsPerCSSPixel() / aFrame->PresContext()
                                     ->DeviceContext()
                                     ->AppUnitsPerDevPixelAtUnitFullZoom();
}

static bool IsDateTimeResetButton(nsIFrame* aFrame) {
  nsIFrame* parent = aFrame->GetParent();
  if (parent && (parent = parent->GetParent()) &&
      (parent = parent->GetParent())) {
    nsDateTimeControlFrame* dateTimeFrame = do_QueryFrame(parent);
    if (dateTimeFrame) {
      return true;
    }
  }
  return false;
}

static bool IsDateTimeTextField(nsIFrame* aFrame) {
  nsDateTimeControlFrame* dateTimeFrame = do_QueryFrame(aFrame);
  return dateTimeFrame;
}

static void ComputeCheckColors(const EventStates& aState,
                               sRGBColor& aBackgroundColor,
                               sRGBColor& aBorderColor) {
  bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
  bool isPressed = !isDisabled && aState.HasAllStates(NS_EVENT_STATE_HOVER |
                                                      NS_EVENT_STATE_ACTIVE);
  bool isHovered = !isDisabled && aState.HasState(NS_EVENT_STATE_HOVER);
  bool isFocused = aState.HasState(NS_EVENT_STATE_FOCUS);
  bool isChecked = aState.HasState(NS_EVENT_STATE_CHECKED);

  sRGBColor fillColor = sBackgroundColor;
  sRGBColor borderColor = sBorderColor;
  if (isDisabled) {
    if (isChecked) {
      fillColor = borderColor = sCheckBackgroundColorDisabled;
    } else {
      fillColor = sBackgroundColor;
      borderColor = sBorderColorDisabled;
    }
  } else {
    if (isChecked) {
      if (isPressed) {
        fillColor = borderColor = sCheckBackgroundActiveColor;
      } else if (isHovered) {
        fillColor = borderColor = sCheckBackgroundHoverColor;
      } else {
        fillColor = borderColor = sCheckBackgroundColor;
      }
    } else if (isPressed) {
      fillColor = sBackgroundActiveColor;
      borderColor = sBorderHoverColor;
    } else if (isFocused) {
      fillColor = sBackgroundActiveColor;
      borderColor = sBorderFocusColor;
    } else if (isHovered) {
      fillColor = sBackgroundColor;
      borderColor = sBorderHoverColor;
    } else {
      fillColor = sBackgroundColor;
      borderColor = sBorderColor;
    }
  }

  aBackgroundColor = fillColor;
  aBorderColor = borderColor;
}

// Checkbox and radio need to preserve aspect-ratio for compat.
static Rect FixAspectRatio(const Rect& aRect) {
  Rect rect(aRect);
  if (rect.width == rect.height) {
    return rect;
  }

  if (rect.width > rect.height) {
    auto diff = rect.width - rect.height;
    rect.width = rect.height;
    rect.x += diff / 2;
  } else {
    auto diff = rect.height - rect.width;
    rect.height = rect.width;
    rect.y += diff / 2;
  }

  return rect;
}

// This pushes and pops a clip rect to the draw target.
//
// This is done to reduce fuzz in places where we may have antialiasing, because
// skia is not clip-invariant: given different clips, it does not guarantee the
// same result, even if the painted content doesn't intersect the clips.
//
// This is a bit sad, overall, but...
struct MOZ_RAII AutoClipRect {
  AutoClipRect(DrawTarget& aDt, const Rect& aRect) : mDt(aDt) {
    mDt.PushClipRect(aRect);
  }

  ~AutoClipRect() { mDt.PopClip(); }

 private:
  DrawTarget& mDt;
};

static void PaintRoundedRectWithBorder(DrawTarget* aDrawTarget,
                                       const Rect& aRect,
                                       const sRGBColor& aBackgroundColor,
                                       const sRGBColor& aBorderColor,
                                       CSSCoord aBorderWidth, CSSCoord aRadius,
                                       uint32_t aDpi) {
  const LayoutDeviceCoord borderWidth(aBorderWidth * aDpi);
  const LayoutDeviceCoord radius(aRadius * aDpi);

  Rect rect(aRect);
  // Deflate the rect by half the border width, so that the middle of the stroke
  // fills exactly the area we want to fill and not more.
  rect.Deflate(borderWidth * 0.5f);

  RectCornerRadii radii(radius, radius, radius, radius);
  RefPtr<Path> roundedRect = MakePathForRoundedRect(*aDrawTarget, rect, radii);

  aDrawTarget->Fill(roundedRect, ColorPattern(ToDeviceColor(aBackgroundColor)));
  aDrawTarget->Stroke(roundedRect, ColorPattern(ToDeviceColor(aBorderColor)),
                      StrokeOptions(borderWidth));
}

static void PaintCheckboxControl(DrawTarget* aDrawTarget, const Rect& aRect,
                                 const EventStates& aState, uint32_t aDpi) {
  const CSSCoord kBorderWidth = 2.0f;
  const CSSCoord kRadius = 4.0f;

  sRGBColor backgroundColor;
  sRGBColor borderColor;
  ComputeCheckColors(aState, backgroundColor, borderColor);
  PaintRoundedRectWithBorder(aDrawTarget, aRect, backgroundColor, borderColor,
                             kBorderWidth, kRadius, aDpi);
}

static void PaintCheckMark(DrawTarget* aDrawTarget, const Rect& aRect,
                           const EventStates& aState, uint32_t aDpi) {
  // Points come from the coordinates on a 7X7 unit box centered at 0,0
  const float checkPolygonX[] = {-2.5, -0.7, 2.5};
  const float checkPolygonY[] = {-0.3, 1.7, -1.5};
  const int32_t checkNumPoints = sizeof(checkPolygonX) / sizeof(float);
  const int32_t checkSize = 8;

  auto center = aRect.Center();

  // Scale the checkmark based on the smallest dimension
  nscoord paintScale = std::min(aRect.width, aRect.height) / checkSize;
  RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
  Point p = center +
            Point(checkPolygonX[0] * paintScale, checkPolygonY[0] * paintScale);
  builder->MoveTo(p);
  for (int32_t polyIndex = 1; polyIndex < checkNumPoints; polyIndex++) {
    p = center + Point(checkPolygonX[polyIndex] * paintScale,
                       checkPolygonY[polyIndex] * paintScale);
    builder->LineTo(p);
  }
  RefPtr<Path> path = builder->Finish();
  aDrawTarget->Stroke(path, ColorPattern(ToDeviceColor(sBackgroundColor)),
                      StrokeOptions(2.0f * aDpi));
}

static void PaintIndeterminateMark(DrawTarget* aDrawTarget, const Rect& aRect,
                                   const EventStates& aState) {
  bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);

  Rect rect(aRect);
  rect.y += (rect.height - rect.height / 4) / 2;
  rect.height /= 4;

  aDrawTarget->FillRect(
      rect, ColorPattern(
                ToDeviceColor(isDisabled ? sDisabledColor : sBackgroundColor)));
}

static void PaintStrokedEllipse(DrawTarget* aDrawTarget, const Rect& aRect,
                                const sRGBColor& aBackgroundColor,
                                const sRGBColor& aBorderColor,
                                const CSSCoord aBorderWidth, uint32_t aDpi) {
  const LayoutDeviceCoord borderWidth(aBorderWidth * aDpi);
  RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();

  // Deflate for the same reason as PaintRoundedRectWithBorder. Note that the
  // size is the diameter, so we just shrink by the border width once.
  Size size(aRect.Size() - Size(borderWidth, borderWidth));
  AppendEllipseToPath(builder, aRect.Center(), size);
  RefPtr<Path> ellipse = builder->Finish();

  aDrawTarget->Fill(ellipse, ColorPattern(ToDeviceColor(aBackgroundColor)));
  aDrawTarget->Stroke(ellipse, ColorPattern(ToDeviceColor(aBorderColor)),
                      StrokeOptions(borderWidth));
}

static void PaintRadioControl(DrawTarget* aDrawTarget, const Rect& aRect,
                              const EventStates& aState, uint32_t aDpi) {
  const CSSCoord kBorderWidth = 2.0f;

  sRGBColor backgroundColor;
  sRGBColor borderColor;
  ComputeCheckColors(aState, backgroundColor, borderColor);

  PaintStrokedEllipse(aDrawTarget, aRect, backgroundColor, borderColor,
                      kBorderWidth, aDpi);
}

static void PaintCheckedRadioButton(DrawTarget* aDrawTarget, const Rect& aRect,
                                    uint32_t aDpi) {
  Rect rect(aRect);
  rect.x += 4.5f * aDpi;
  rect.width -= 9.0f * aDpi;
  rect.y += 4.5f * aDpi;
  rect.height -= 9.0f * aDpi;

  RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
  AppendEllipseToPath(builder, rect.Center(), rect.Size());
  RefPtr<Path> ellipse = builder->Finish();
  aDrawTarget->Fill(ellipse, ColorPattern(ToDeviceColor(sBackgroundColor)));
}

static sRGBColor ComputeBorderColor(const EventStates& aState) {
  bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
  bool isHovered = !isDisabled && aState.HasState(NS_EVENT_STATE_HOVER);
  bool isFocused = aState.HasState(NS_EVENT_STATE_FOCUS);
  if (isFocused) {
    return sBorderFocusColor;
  }
  if (isHovered) {
    return sBorderHoverColor;
  }
  return sBorderColor;
}

static void PaintTextField(DrawTarget* aDrawTarget, const Rect& aRect,
                           const EventStates& aState, uint32_t aDpi) {
  bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
  const sRGBColor& backgroundColor =
      isDisabled ? sDisabledColor : sBackgroundColor;
  const sRGBColor borderColor = ComputeBorderColor(aState);

  const CSSCoord kRadius = 4.0f;

  PaintRoundedRectWithBorder(aDrawTarget, aRect, backgroundColor, borderColor,
                             kTextFieldBorderWidth, kRadius, aDpi);
}

std::pair<sRGBColor, sRGBColor> ComputeButtonColors(
    const EventStates& aState, bool aIsDatetimeResetButton = false) {
  bool isActive =
      aState.HasAllStates(NS_EVENT_STATE_HOVER | NS_EVENT_STATE_ACTIVE);
  bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
  bool isHovered = !isDisabled && aState.HasState(NS_EVENT_STATE_HOVER);

  const sRGBColor& backgroundColor = [&] {
    if (isDisabled) {
      return sDisabledColor;
    }
    if (aIsDatetimeResetButton) {
      return sWhiteColor;
    }
    if (isActive) {
      return sButtonActiveColor;
    }
    if (isHovered) {
      return sButtonHoverColor;
    }
    return sButtonColor;
  }();

  const sRGBColor borderColor = ComputeBorderColor(aState);

  return std::make_pair(backgroundColor, borderColor);
}

static void PaintMenulist(DrawTarget* aDrawTarget, const Rect& aRect,
                          const EventStates& aState, uint32_t aDpi) {
  const CSSCoord kRadius = 4.0f;

  sRGBColor backgroundColor, borderColor;
  std::tie(backgroundColor, borderColor) = ComputeButtonColors(aState);

  PaintRoundedRectWithBorder(aDrawTarget, aRect, backgroundColor, borderColor,
                             kMenulistBorderWidth, kRadius, aDpi);
}

static void PaintArrow(DrawTarget* aDrawTarget, const Rect& aRect,
                       const int32_t aArrowPolygonX[],
                       const int32_t aArrowPolygonY[],
                       const int32_t aArrowNumPoints, const int32_t aArrowSize,
                       const sRGBColor aFillColor, uint32_t aDpi) {
  nscoord paintScale = std::min(aRect.width, aRect.height) / aArrowSize;
  RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
  Point p = aRect.Center() + Point(aArrowPolygonX[0] * paintScale,
                                   aArrowPolygonY[0] * paintScale);

  builder->MoveTo(p);
  for (int32_t polyIndex = 1; polyIndex < aArrowNumPoints; polyIndex++) {
    p = aRect.Center() + Point(aArrowPolygonX[polyIndex] * paintScale,
                               aArrowPolygonY[polyIndex] * paintScale);
    builder->LineTo(p);
  }
  RefPtr<Path> path = builder->Finish();

  aDrawTarget->Stroke(path, ColorPattern(ToDeviceColor(aFillColor)),
                      StrokeOptions(2.0f * aDpi));
}

static void PaintMenulistArrowButton(nsIFrame* aFrame, DrawTarget* aDrawTarget,
                                     const Rect& aRect,
                                     const EventStates& aState, uint32_t aDpi) {
  bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
  bool isPressed = !isDisabled && aState.HasAllStates(NS_EVENT_STATE_HOVER |
                                                      NS_EVENT_STATE_ACTIVE);
  bool isHovered = !isDisabled && aState.HasState(NS_EVENT_STATE_HOVER);
  bool isHTML = nsNativeTheme::IsHTMLContent(aFrame);

  if (!isHTML && nsNativeTheme::CheckBooleanAttr(aFrame, nsGkAtoms::open)) {
    isHovered = false;
  }

  const int32_t arrowSize = 8;
  int32_t arrowPolygonX[] = {-4, -2, 0};
  int32_t arrowPolygonY[] = {-1, 1, -1};
  const int32_t arrowNumPoints = ArrayLength(arrowPolygonX);

  PaintArrow(aDrawTarget, aRect, arrowPolygonX, arrowPolygonY, arrowNumPoints,
             arrowSize,
             isPressed   ? sActiveColor
             : isHovered ? sBorderHoverColor
                         : sBorderColor,
             aDpi);
}

static void PaintSpinnerButton(DrawTarget* aDrawTarget, const Rect& aRect,
                               const EventStates& aState,
                               StyleAppearance aAppearance, uint32_t aDpi) {
  bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
  bool isPressed = !isDisabled && aState.HasAllStates(NS_EVENT_STATE_HOVER |
                                                      NS_EVENT_STATE_ACTIVE);
  bool isHovered = !isDisabled && aState.HasState(NS_EVENT_STATE_HOVER);

  const int32_t arrowSize = 8;
  int32_t arrowPolygonX[] = {0, 2, 4};
  int32_t arrowPolygonY[] = {-3, -1, -3};
  const int32_t arrowNumPoints = ArrayLength(arrowPolygonX);

  if (aAppearance == StyleAppearance::SpinnerUpbutton) {
    for (int32_t i = 0; i < arrowNumPoints; i++) {
      arrowPolygonY[i] *= -1;
    }
  }

  PaintArrow(aDrawTarget, aRect, arrowPolygonX, arrowPolygonY, arrowNumPoints,
             arrowSize,
             isPressed   ? sActiveColor
             : isHovered ? sBorderHoverColor
                         : sBorderColor,
             aDpi);
}

static void PaintRangeInputBackground(DrawTarget* aDrawTarget,
                                      const Rect& aRect,
                                      const EventStates& aState, uint32_t aDpi,
                                      bool aHorizontal) {
  Rect rect(aRect);
  const LayoutDeviceCoord kVerticalSize =
      kMinimumAndroidWidgetSize * 0.25f * aDpi;

  if (aHorizontal) {
    rect.y += (rect.height - kVerticalSize) / 2;
    rect.height = kVerticalSize;
  } else {
    rect.x += (rect.width - kVerticalSize) / 2;
    rect.width = kVerticalSize;
  }

  aDrawTarget->FillRect(
      rect, ColorPattern(ToDeviceColor(sRangeInputBackgroundColor)));
  aDrawTarget->StrokeRect(rect,
                          ColorPattern(ToDeviceColor(sButtonActiveColor)));
}

static void PaintScrollbarthumbHorizontal(DrawTarget* aDrawTarget,
                                          const Rect& aRect,
                                          const EventStates& aState) {
  sRGBColor thumbColor = sScrollbarThumbColor;
  if (aState.HasAllStates(NS_EVENT_STATE_HOVER | NS_EVENT_STATE_ACTIVE)) {
    thumbColor = sScrollbarThumbColorActive;
  } else if (aState.HasState(NS_EVENT_STATE_HOVER)) {
    thumbColor = sScrollbarThumbColorHover;
  }
  aDrawTarget->FillRect(aRect, ColorPattern(ToDeviceColor(thumbColor)));
}

static void PaintScrollbarthumbVertical(DrawTarget* aDrawTarget,
                                        const Rect& aRect,
                                        const EventStates& aState) {
  sRGBColor thumbColor = sScrollbarThumbColor;
  if (aState.HasAllStates(NS_EVENT_STATE_HOVER | NS_EVENT_STATE_ACTIVE)) {
    thumbColor = sScrollbarThumbColorActive;
  } else if (aState.HasState(NS_EVENT_STATE_HOVER)) {
    thumbColor = sScrollbarThumbColorHover;
  }
  aDrawTarget->FillRect(aRect, ColorPattern(ToDeviceColor(thumbColor)));
}

static void PaintScrollbarHorizontal(DrawTarget* aDrawTarget,
                                     const Rect& aRect) {
  aDrawTarget->FillRect(aRect, ColorPattern(ToDeviceColor(sScrollbarColor)));
  RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
  builder->MoveTo(Point(aRect.x, aRect.y));
  builder->LineTo(Point(aRect.x + aRect.width, aRect.y));
  RefPtr<Path> path = builder->Finish();
  aDrawTarget->Stroke(path, ColorPattern(ToDeviceColor(sScrollbarBorderColor)));
}

static void PaintScrollbarVerticalAndCorner(DrawTarget* aDrawTarget,
                                            const Rect& aRect, uint32_t aDpi) {
  aDrawTarget->FillRect(aRect, ColorPattern(ToDeviceColor(sScrollbarColor)));
  RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
  builder->MoveTo(Point(aRect.x, aRect.y));
  builder->LineTo(Point(aRect.x, aRect.y + aRect.height));
  RefPtr<Path> path = builder->Finish();
  aDrawTarget->Stroke(path, ColorPattern(ToDeviceColor(sScrollbarBorderColor)),
                      StrokeOptions(1.0f * aDpi));
}

static void PaintScrollbarbutton(DrawTarget* aDrawTarget,
                                 StyleAppearance aAppearance, const Rect& aRect,
                                 const EventStates& aState, uint32_t aDpi) {
  bool isActive =
      aState.HasAllStates(NS_EVENT_STATE_HOVER | NS_EVENT_STATE_ACTIVE);
  bool isHovered = aState.HasState(NS_EVENT_STATE_HOVER);

  aDrawTarget->FillRect(
      aRect, ColorPattern(ToDeviceColor(isActive ? sScrollbarButtonActiveColor
                                        : isHovered ? sScrollbarButtonHoverColor
                                                    : sScrollbarColor)));

  // Start with Up arrow.
  int32_t arrowPolygonX[] = {3, 0, -3};
  int32_t arrowPolygonY[] = {2, -1, 2};
  const int32_t arrowNumPoints = ArrayLength(arrowPolygonX);
  const int32_t arrowSize = 14;

  switch (aAppearance) {
    case StyleAppearance::ScrollbarbuttonUp:
      break;
    case StyleAppearance::ScrollbarbuttonDown:
      for (int32_t i = 0; i < arrowNumPoints; i++) {
        arrowPolygonY[i] *= -1;
      }
      break;
    case StyleAppearance::ScrollbarbuttonLeft:
      for (int32_t i = 0; i < arrowNumPoints; i++) {
        int32_t temp = arrowPolygonX[i];
        arrowPolygonX[i] = arrowPolygonY[i];
        arrowPolygonY[i] = temp;
      }
      break;
    case StyleAppearance::ScrollbarbuttonRight:
      for (int32_t i = 0; i < arrowNumPoints; i++) {
        int32_t temp = arrowPolygonX[i];
        arrowPolygonX[i] = arrowPolygonY[i] * -1;
        arrowPolygonY[i] = temp;
      }
      break;
    default:
      return;
  }

  PaintArrow(aDrawTarget, aRect, arrowPolygonX, arrowPolygonY, arrowNumPoints,
             arrowSize,
             isActive    ? sScrollbarArrowColorActive
             : isHovered ? sScrollbarArrowColorHover
                         : sScrollbarArrowColor,
             aDpi);

  RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
  builder->MoveTo(Point(aRect.x, aRect.y));
  if (aAppearance == StyleAppearance::ScrollbarbuttonUp ||
      aAppearance == StyleAppearance::ScrollbarbuttonDown) {
    builder->LineTo(Point(aRect.x, aRect.y + aRect.height));
  } else {
    builder->LineTo(Point(aRect.x + aRect.width, aRect.y));
  }

  RefPtr<Path> path = builder->Finish();
  aDrawTarget->Stroke(path, ColorPattern(ToDeviceColor(sScrollbarBorderColor)),
                      StrokeOptions(1.0f * aDpi));
}

static void PaintButton(nsIFrame* aFrame, DrawTarget* aDrawTarget,
                        const Rect& aRect, const EventStates& aState,
                        uint32_t aDpi) {
  const CSSCoord kRadius = 4.0f;

  // FIXME: The DateTimeResetButton bit feels like a bit of a hack.
  sRGBColor backgroundColor, borderColor;
  std::tie(backgroundColor, borderColor) =
      ComputeButtonColors(aState, IsDateTimeResetButton(aFrame));

  PaintRoundedRectWithBorder(aDrawTarget, aRect, backgroundColor, borderColor,
                             kButtonBorderWidth, kRadius, aDpi);
}

static void PaintRangeThumb(DrawTarget* aDrawTarget, const Rect& aRect,
                            const EventStates& aState, uint32_t aDpi) {
  const CSSCoord kBorderWidth = 2.0f;

  sRGBColor backgroundColor, borderColor;
  std::tie(backgroundColor, borderColor) = ComputeButtonColors(aState);

  PaintStrokedEllipse(aDrawTarget, aRect, backgroundColor, borderColor,
                      kBorderWidth, aDpi);
}

NS_IMETHODIMP
nsNativeThemeAndroid::DrawWidgetBackground(gfxContext* aContext,
                                           nsIFrame* aFrame,
                                           StyleAppearance aAppearance,
                                           const nsRect& aRect,
                                           const nsRect& /* aDirtyRect */) {
  DrawTarget* dt = aContext->GetDrawTarget();
  const nscoord twipsPerPixel = aFrame->PresContext()->AppUnitsPerDevPixel();
  EventStates eventState = GetContentState(aFrame, aAppearance);

  Rect devPxRect = NSRectToSnappedRect(aRect, twipsPerPixel, *dt);
  AutoClipRect clip(*dt, devPxRect);

  if (aAppearance == StyleAppearance::MozMenulistArrowButton) {
    bool isHTML = IsHTMLContent(aFrame);
    nsIFrame* parentFrame = aFrame->GetParent();
    bool isMenulist = !isHTML && parentFrame->IsMenuFrame();
    // HTML select and XUL menulist dropdown buttons get state from the
    // parent.
    if (isHTML || isMenulist) {
      aFrame = parentFrame;
      eventState = GetContentState(parentFrame, aAppearance);
    }
  }

  uint32_t dpi = GetDPIRatio(aFrame);

  switch (aAppearance) {
    case StyleAppearance::Radio: {
      auto rect = FixAspectRatio(devPxRect);
      PaintRadioControl(dt, rect, eventState, dpi);
      if (IsSelected(aFrame)) {
        PaintCheckedRadioButton(dt, rect, dpi);
      }
      break;
    }
    case StyleAppearance::Checkbox: {
      auto rect = FixAspectRatio(devPxRect);
      PaintCheckboxControl(dt, rect, eventState, dpi);
      if (IsChecked(aFrame)) {
        PaintCheckMark(dt, rect, eventState, dpi);
      }
      if (GetIndeterminate(aFrame)) {
        PaintIndeterminateMark(dt, rect, eventState);
      }
      break;
    }
    case StyleAppearance::Textarea:
    case StyleAppearance::Textfield:
    case StyleAppearance::NumberInput:
      PaintTextField(dt, devPxRect, eventState, dpi);
      break;
    case StyleAppearance::Listbox:
    case StyleAppearance::Menulist:
    case StyleAppearance::MenulistButton:
      PaintMenulist(dt, devPxRect, eventState, dpi);
      break;
    case StyleAppearance::MozMenulistArrowButton:
      PaintMenulistArrowButton(aFrame, dt, devPxRect, eventState, dpi);
      break;
    case StyleAppearance::SpinnerUpbutton:
    case StyleAppearance::SpinnerDownbutton:
      PaintSpinnerButton(dt, devPxRect, eventState, aAppearance, dpi);
      break;
    case StyleAppearance::Range:
      PaintRangeInputBackground(dt, devPxRect, eventState, dpi,
                                IsRangeHorizontal(aFrame));
      break;
    case StyleAppearance::RangeThumb:
      // TODO(emilio): Do we want to enforce it being a circle using
      // FixAspectRatio here? For now let authors tweak, it's a custom pseudo so
      // it doesn't probably have much compat impact if at all.
      PaintRangeThumb(dt, devPxRect, eventState, dpi);
      break;
    case StyleAppearance::ScrollbarthumbHorizontal:
      PaintScrollbarthumbHorizontal(dt, devPxRect, eventState);
      break;
    case StyleAppearance::ScrollbarthumbVertical:
      PaintScrollbarthumbVertical(dt, devPxRect, eventState);
      break;
    case StyleAppearance::ScrollbarHorizontal:
      PaintScrollbarHorizontal(dt, devPxRect);
      break;
    case StyleAppearance::ScrollbarVertical:
    case StyleAppearance::Scrollcorner:
      PaintScrollbarVerticalAndCorner(dt, devPxRect, dpi);
      break;
    case StyleAppearance::ScrollbarbuttonUp:
    case StyleAppearance::ScrollbarbuttonDown:
    case StyleAppearance::ScrollbarbuttonLeft:
    case StyleAppearance::ScrollbarbuttonRight:
      PaintScrollbarbutton(dt, aAppearance, devPxRect, eventState, dpi);
      break;
    case StyleAppearance::Button:
      PaintButton(aFrame, dt, devPxRect, eventState, dpi);
      break;
    default:
      MOZ_ASSERT_UNREACHABLE(
          "Should not get here with a widget type we don't support.");
      return NS_ERROR_NOT_IMPLEMENTED;
  }

  return NS_OK;
}

/*bool
nsNativeThemeAndroid::CreateWebRenderCommandsForWidget(mozilla::wr::DisplayListBuilder&
aBuilder, mozilla::wr::IpcResourceUpdateQueue& aResources, const
mozilla::layers::StackingContextHelper& aSc,
                                      mozilla::layers::RenderRootStateManager*
aManager, nsIFrame* aFrame, StyleAppearance aAppearance, const nsRect& aRect) {
}*/

LayoutDeviceIntMargin nsNativeThemeAndroid::GetWidgetBorder(
    nsDeviceContext* aContext, nsIFrame* aFrame, StyleAppearance aAppearance) {
  uint32_t dpi = GetDPIRatio(aFrame);
  switch (aAppearance) {
    case StyleAppearance::Textfield:
    case StyleAppearance::Textarea:
    case StyleAppearance::NumberInput: {
      const LayoutDeviceIntCoord w = kTextFieldBorderWidth * dpi;
      return LayoutDeviceIntMargin(w, w, w, w);
    }
    case StyleAppearance::Listbox:
    case StyleAppearance::Menulist:
    case StyleAppearance::MenulistButton: {
      const LayoutDeviceIntCoord w = kMenulistBorderWidth * dpi;
      return LayoutDeviceIntMargin(w, w, w, w);
    }
    case StyleAppearance::Button: {
      const LayoutDeviceIntCoord w = kButtonBorderWidth * dpi;
      return LayoutDeviceIntMargin(w, w, w, w);
    }
    default:
      return LayoutDeviceIntMargin();
  }
}

bool nsNativeThemeAndroid::GetWidgetPadding(nsDeviceContext* aContext,
                                            nsIFrame* aFrame,
                                            StyleAppearance aAppearance,
                                            LayoutDeviceIntMargin* aResult) {
  uint32_t dpiRatio = GetDPIRatio(aFrame);
  switch (aAppearance) {
    // Radios and checkboxes return a fixed size in GetMinimumWidgetSize
    // and have a meaningful baseline, so they can't have
    // author-specified padding.
    case StyleAppearance::Radio:
    case StyleAppearance::Checkbox:
      aResult->SizeTo(0, 0, 0, 0);
      return true;
    case StyleAppearance::MozMenulistArrowButton:
      aResult->SizeTo(1 * dpiRatio, 4 * dpiRatio, 1 * dpiRatio, 4 * dpiRatio);
      return true;
    default:
      break;
  }

  // Respect author padding.
  //
  // TODO(emilio): Consider just unconditionally returning false, so that the
  // default size of all elements matches other platforms and the UA stylesheet.
  if (aFrame->PresContext()->HasAuthorSpecifiedRules(
          aFrame, NS_AUTHOR_SPECIFIED_PADDING)) {
    return false;
  }

  uint32_t dpi = GetDPIRatio(aFrame);
  switch (aAppearance) {
    case StyleAppearance::Textarea:
    case StyleAppearance::Listbox:
    case StyleAppearance::Menulist:
    case StyleAppearance::MenulistButton:
    case StyleAppearance::NumberInput:
      aResult->SizeTo(6 * dpi, 7 * dpi, 6 * dpi, 7 * dpi);
      return true;
    case StyleAppearance::Button:
      aResult->SizeTo(6 * dpi, 21 * dpi, 6 * dpi, 21 * dpi);
      return true;
    case StyleAppearance::Textfield:
      if (IsDateTimeTextField(aFrame)) {
        aResult->SizeTo(7 * dpi, 7 * dpi, 5 * dpi, 7 * dpi);
        return true;
      }
      aResult->SizeTo(6 * dpi, 7 * dpi, 6 * dpi, 7 * dpi);
      return true;
    default:
      return false;
  }
}

bool nsNativeThemeAndroid::GetWidgetOverflow(nsDeviceContext* aContext,
                                             nsIFrame* aFrame,
                                             StyleAppearance aAppearance,
                                             nsRect* aOverflowRect) {
  // TODO(bug 1620360): This should return non-zero for
  // StyleAppearance::FocusOutline, if we implement outline-style: auto.
  return false;
}

NS_IMETHODIMP
nsNativeThemeAndroid::GetMinimumWidgetSize(nsPresContext* aPresContext,
                                           nsIFrame* aFrame,
                                           StyleAppearance aAppearance,
                                           LayoutDeviceIntSize* aResult,
                                           bool* aIsOverridable) {
  uint32_t dpiRatio = GetDPIRatio(aFrame);
  aResult->width = aResult->height =
      static_cast<uint32_t>(kMinimumAndroidWidgetSize) * dpiRatio;
  *aIsOverridable = true;
  if (aAppearance == StyleAppearance::MozMenulistArrowButton) {
    aResult->width =
        static_cast<uint32_t>(kMinimumDropdownArrowButtonWidth) * dpiRatio;
  }
  return NS_OK;
}

nsITheme::Transparency nsNativeThemeAndroid::GetWidgetTransparency(
    nsIFrame* aFrame, StyleAppearance aAppearance) {
  return eUnknownTransparency;
}

NS_IMETHODIMP
nsNativeThemeAndroid::WidgetStateChanged(nsIFrame* aFrame,
                                         StyleAppearance aAppearance,
                                         nsAtom* aAttribute,
                                         bool* aShouldRepaint,
                                         const nsAttrValue* aOldValue) {
  if (!aAttribute) {
    // Hover/focus/active changed.  Always repaint.
    *aShouldRepaint = true;
  } else {
    // Check the attribute to see if it's relevant.
    // disabled, checked, dlgtype, default, etc.
    *aShouldRepaint = false;
    if ((aAttribute == nsGkAtoms::disabled) ||
        (aAttribute == nsGkAtoms::checked) ||
        (aAttribute == nsGkAtoms::selected) ||
        (aAttribute == nsGkAtoms::visuallyselected) ||
        (aAttribute == nsGkAtoms::menuactive) ||
        (aAttribute == nsGkAtoms::sortDirection) ||
        (aAttribute == nsGkAtoms::focused) ||
        (aAttribute == nsGkAtoms::_default) ||
        (aAttribute == nsGkAtoms::open) || (aAttribute == nsGkAtoms::hover)) {
      *aShouldRepaint = true;
    }
  }

  return NS_OK;
}

NS_IMETHODIMP
nsNativeThemeAndroid::ThemeChanged() { return NS_OK; }

bool nsNativeThemeAndroid::WidgetAppearanceDependsOnWindowFocus(
    StyleAppearance) {
  return false;
}

nsITheme::ThemeGeometryType nsNativeThemeAndroid::ThemeGeometryTypeForWidget(
    nsIFrame* aFrame, StyleAppearance aAppearance) {
  return eThemeGeometryTypeUnknown;
}

bool nsNativeThemeAndroid::ThemeSupportsWidget(nsPresContext* aPresContext,
                                               nsIFrame* aFrame,
                                               StyleAppearance aAppearance) {
  if (IsWidgetScrollbarPart(aAppearance)) {
    const auto* style = nsLayoutUtils::StyleForScrollbar(aFrame);
    // We don't currently handle custom scrollbars on
    // nsNativeThemeAndroid. We could, potentially.
    if (style->StyleUI()->HasCustomScrollbars() ||
        style->StyleUIReset()->mScrollbarWidth == StyleScrollbarWidth::Thin) {
      return false;
    }
  }

  switch (aAppearance) {
    case StyleAppearance::Radio:
    case StyleAppearance::Checkbox:
    case StyleAppearance::Textarea:
    case StyleAppearance::Textfield:
    case StyleAppearance::Range:
    case StyleAppearance::RangeThumb:
    case StyleAppearance::ScrollbarbuttonUp:
    case StyleAppearance::ScrollbarbuttonDown:
    case StyleAppearance::ScrollbarbuttonLeft:
    case StyleAppearance::ScrollbarbuttonRight:
    case StyleAppearance::ScrollbarthumbHorizontal:
    case StyleAppearance::ScrollbarthumbVertical:
    case StyleAppearance::ScrollbarHorizontal:
    case StyleAppearance::ScrollbarNonDisappearing:
    case StyleAppearance::ScrollbarVertical:
    case StyleAppearance::Scrollcorner:
    case StyleAppearance::Button:
    case StyleAppearance::Listbox:
    case StyleAppearance::Menulist:
    case StyleAppearance::MenulistButton:
    case StyleAppearance::NumberInput:
    case StyleAppearance::MozMenulistArrowButton:
    case StyleAppearance::SpinnerUpbutton:
    case StyleAppearance::SpinnerDownbutton:
      return !IsWidgetStyled(aPresContext, aFrame, aAppearance);
    default:
      return false;
  }
}

bool nsNativeThemeAndroid::WidgetIsContainer(StyleAppearance aAppearance) {
  switch (aAppearance) {
    case StyleAppearance::MozMenulistArrowButton:
    case StyleAppearance::Radio:
    case StyleAppearance::Checkbox:
      return false;
    default:
      return true;
  }
}

bool nsNativeThemeAndroid::ThemeDrawsFocusForWidget(
    StyleAppearance aAppearance) {
  switch (aAppearance) {
    case StyleAppearance::Range:
    // TODO(emilio): Checkbox / Radio don't have focus indicators when checked.
    // If they did, we could just return true here unconditionally.
    case StyleAppearance::Checkbox:
    case StyleAppearance::Radio:
      return false;
    default:
      return true;
  }
}

bool nsNativeThemeAndroid::ThemeNeedsComboboxDropmarker() { return true; }

already_AddRefed<nsITheme> do_GetNativeThemeDoNotUseDirectly() {
  static StaticRefPtr<nsITheme> gInstance;
  if (MOZ_UNLIKELY(!gInstance)) {
    gInstance = new nsNativeThemeAndroid();
    ClearOnShutdown(&gInstance);
  }
  return do_AddRef(gInstance);
}