forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheme.cpp
1572 lines (1397 loc) · 59.7 KB
/
Theme.cpp
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- 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 "Theme.h"
#include "ThemeCocoa.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/HTMLMeterElement.h"
#include "mozilla/dom/HTMLProgressElement.h"
#include "mozilla/gfx/Rect.h"
#include "mozilla/gfx/Types.h"
#include "mozilla/gfx/Filters.h"
#include "mozilla/RelativeLuminanceUtils.h"
#include "mozilla/StaticPrefs_widget.h"
#include "mozilla/webrender/WebRenderAPI.h"
#include "nsCSSColorUtils.h"
#include "nsCSSRendering.h"
#include "nsDeviceContext.h"
#include "nsLayoutUtils.h"
#include "nsRangeFrame.h"
#include "PathHelpers.h"
#include "ScrollbarDrawingAndroid.h"
#include "ScrollbarDrawingCocoa.h"
#include "ScrollbarDrawingGTK.h"
#include "ScrollbarDrawingWin.h"
#include "ScrollbarDrawingWin11.h"
#ifdef XP_WIN
# include "mozilla/WindowsVersion.h"
#endif
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::gfx;
using namespace mozilla::widget;
namespace {
static constexpr gfx::sRGBColor sColorGrey10(
gfx::sRGBColor::UnusualFromARGB(0xffe9e9ed));
static constexpr gfx::sRGBColor sColorGrey10Alpha50(
gfx::sRGBColor::UnusualFromARGB(0x7fe9e9ed));
static constexpr gfx::sRGBColor sColorGrey20(
gfx::sRGBColor::UnusualFromARGB(0xffd0d0d7));
static constexpr gfx::sRGBColor sColorGrey30(
gfx::sRGBColor::UnusualFromARGB(0xffb1b1b9));
static constexpr gfx::sRGBColor sColorGrey40(
gfx::sRGBColor::UnusualFromARGB(0xff8f8f9d));
static constexpr gfx::sRGBColor sColorGrey40Alpha50(
gfx::sRGBColor::UnusualFromARGB(0x7f8f8f9d));
static constexpr gfx::sRGBColor sColorGrey50(
gfx::sRGBColor::UnusualFromARGB(0xff676774));
static constexpr gfx::sRGBColor sColorGrey60(
gfx::sRGBColor::UnusualFromARGB(0xff484851));
static constexpr gfx::sRGBColor sColorMeterGreen10(
gfx::sRGBColor::UnusualFromARGB(0xff00ab60));
static constexpr gfx::sRGBColor sColorMeterGreen20(
gfx::sRGBColor::UnusualFromARGB(0xff056139));
static constexpr gfx::sRGBColor sColorMeterYellow10(
gfx::sRGBColor::UnusualFromARGB(0xffffbd4f));
static constexpr gfx::sRGBColor sColorMeterYellow20(
gfx::sRGBColor::UnusualFromARGB(0xffd2811e));
static constexpr gfx::sRGBColor sColorMeterRed10(
gfx::sRGBColor::UnusualFromARGB(0xffe22850));
static constexpr gfx::sRGBColor sColorMeterRed20(
gfx::sRGBColor::UnusualFromARGB(0xff810220));
static const CSSCoord kMinimumColorPickerHeight = 32.0f;
static const CSSCoord kMinimumRangeThumbSize = 20.0f;
static const CSSCoord kMinimumDropdownArrowButtonWidth = 18.0f;
static const CSSCoord kMinimumSpinnerButtonWidth = 18.0f;
static const CSSCoord kMinimumSpinnerButtonHeight = 9.0f;
static const CSSCoord kButtonBorderWidth = 1.0f;
static const CSSCoord kMenulistBorderWidth = 1.0f;
static const CSSCoord kTextFieldBorderWidth = 1.0f;
static const CSSCoord kRangeHeight = 6.0f;
static const CSSCoord kProgressbarHeight = 6.0f;
static const CSSCoord kMeterHeight = 12.0f;
// nsCheckboxRadioFrame takes the bottom of the content box as the baseline.
// This border-width makes its baseline 2px under the bottom, which is nice.
static constexpr CSSCoord kCheckboxRadioBorderWidth = 2.0f;
static constexpr sRGBColor sTransparent = sRGBColor::White(0.0);
// 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 LayoutDeviceRect& aRect) : mDt(aDt) {
mDt.PushClipRect(aRect.ToUnknownRect());
}
~AutoClipRect() { mDt.PopClip(); }
private:
DrawTarget& mDt;
};
static StaticRefPtr<Theme> gNativeInstance;
static StaticRefPtr<Theme> gNonNativeInstance;
static StaticRefPtr<Theme> gRDMInstance;
} // namespace
#ifdef ANDROID
already_AddRefed<Theme> do_CreateNativeThemeDoNotUseDirectly() {
// Android doesn't have a native theme.
return do_AddRef(new Theme(Theme::ScrollbarStyle()));
}
#endif
already_AddRefed<nsITheme> do_GetBasicNativeThemeDoNotUseDirectly() {
if (MOZ_UNLIKELY(!gNonNativeInstance)) {
UniquePtr<ScrollbarDrawing> scrollbarDrawing = Theme::ScrollbarStyle();
#ifdef MOZ_WIDGET_COCOA
gNonNativeInstance = new ThemeCocoa(std::move(scrollbarDrawing));
#else
gNonNativeInstance = new Theme(std::move(scrollbarDrawing));
#endif
ClearOnShutdown(&gNonNativeInstance);
}
return do_AddRef(gNonNativeInstance);
}
already_AddRefed<nsITheme> do_GetNativeThemeDoNotUseDirectly() {
if (MOZ_UNLIKELY(!gNativeInstance)) {
gNativeInstance = do_CreateNativeThemeDoNotUseDirectly();
ClearOnShutdown(&gNativeInstance);
}
return do_AddRef(gNativeInstance);
}
already_AddRefed<nsITheme> do_GetRDMThemeDoNotUseDirectly() {
if (MOZ_UNLIKELY(!gRDMInstance)) {
gRDMInstance = new Theme(MakeUnique<ScrollbarDrawingAndroid>());
ClearOnShutdown(&gRDMInstance);
}
return do_AddRef(gRDMInstance);
}
namespace mozilla::widget {
NS_IMPL_ISUPPORTS_INHERITED(Theme, nsNativeTheme, nsITheme)
static constexpr nsLiteralCString kPrefs[] = {
"widget.non-native-theme.use-theme-accent"_ns,
"widget.non-native-theme.win.scrollbar.use-system-size"_ns,
"widget.non-native-theme.scrollbar.size.override"_ns,
"widget.non-native-theme.scrollbar.style"_ns,
};
void Theme::Init() {
for (const auto& pref : kPrefs) {
Preferences::RegisterCallback(PrefChangedCallback, pref);
}
LookAndFeelChanged();
}
void Theme::Shutdown() {
for (const auto& pref : kPrefs) {
Preferences::UnregisterCallback(PrefChangedCallback, pref);
}
}
/* static */
void Theme::LookAndFeelChanged() {
ThemeColors::RecomputeAccentColors();
if (gNonNativeInstance) {
gNonNativeInstance->SetScrollbarDrawing(ScrollbarStyle());
}
if (gNativeInstance) {
gNativeInstance->SetScrollbarDrawing(ScrollbarStyle());
}
}
/* static */
auto Theme::GetDPIRatio(nsPresContext* aPc, StyleAppearance aAppearance)
-> DPIRatio {
// Widgets react to zoom, except scrollbars.
if (IsWidgetScrollbarPart(aAppearance)) {
return ScrollbarDrawing::GetDPIRatioForScrollbarPart(aPc);
}
return DPIRatio(float(AppUnitsPerCSSPixel()) / aPc->AppUnitsPerDevPixel());
}
/* static */
auto Theme::GetDPIRatio(nsIFrame* aFrame, StyleAppearance aAppearance)
-> DPIRatio {
return GetDPIRatio(aFrame->PresContext(), aAppearance);
}
// Checkbox and radio need to preserve aspect-ratio for compat. We also snap the
// size to exact device pixels to avoid snapping disorting the circles.
static LayoutDeviceRect CheckBoxRadioRect(const LayoutDeviceRect& aRect) {
// Place a square rect in the center of aRect.
auto size = std::trunc(std::min(aRect.width, aRect.height));
auto position = aRect.Center() - LayoutDevicePoint(size * 0.5, size * 0.5);
return LayoutDeviceRect(position, LayoutDeviceSize(size, size));
}
std::pair<sRGBColor, sRGBColor> Theme::ComputeCheckboxColors(
const EventStates& aState, StyleAppearance aAppearance,
const Colors& aColors) {
MOZ_ASSERT(aAppearance == StyleAppearance::Checkbox ||
aAppearance == StyleAppearance::Radio);
bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
bool isChecked = aState.HasState(NS_EVENT_STATE_CHECKED);
bool isIndeterminate = aAppearance == StyleAppearance::Checkbox &&
aState.HasState(NS_EVENT_STATE_INDETERMINATE);
if (isChecked || isIndeterminate) {
if (isDisabled) {
auto color = ComputeBorderColor(aState, aColors, OutlineCoversBorder::No);
return std::make_pair(color, color);
}
bool isActive =
aState.HasAllStates(NS_EVENT_STATE_HOVER | NS_EVENT_STATE_ACTIVE);
bool isHovered = aState.HasState(NS_EVENT_STATE_HOVER);
const auto& color = isActive ? aColors.Accent().GetDarker()
: isHovered ? aColors.Accent().GetDark()
: aColors.Accent().Get();
return std::make_pair(color, color);
}
return ComputeTextfieldColors(aState, aColors, OutlineCoversBorder::No);
}
sRGBColor Theme::ComputeCheckmarkColor(const EventStates& aState,
const Colors& aColors) {
if (aColors.HighContrast()) {
return aColors.System(StyleSystemColor::Selecteditemtext);
}
if (aState.HasState(NS_EVENT_STATE_DISABLED)) {
return sRGBColor::White(.8f);
}
return aColors.Accent().GetForeground();
}
sRGBColor Theme::ComputeBorderColor(const EventStates& aState,
const Colors& aColors,
OutlineCoversBorder aOutlineCoversBorder) {
bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
if (aColors.HighContrast()) {
return aColors.System(isDisabled ? StyleSystemColor::Graytext
: StyleSystemColor::Buttontext);
}
bool isActive =
aState.HasAllStates(NS_EVENT_STATE_HOVER | NS_EVENT_STATE_ACTIVE);
bool isHovered = aState.HasState(NS_EVENT_STATE_HOVER);
bool isFocused = aState.HasState(NS_EVENT_STATE_FOCUSRING);
if (isDisabled) {
return sColorGrey40Alpha50;
}
if (isFocused && aOutlineCoversBorder == OutlineCoversBorder::Yes) {
// If we draw the outline over the border, prevent issues where the border
// shows underneath if it snaps in the wrong direction by using a
// transparent border. An alternative to this is ensuring that we snap the
// offset in PaintRoundedFocusRect the same was a we snap border widths, so
// that negative offsets are guaranteed to cover the border.
// But this looks harder to mess up.
return sTransparent;
}
bool dark = aColors.IsDark();
if (isActive) {
return dark ? sColorGrey20 : sColorGrey60;
}
if (isHovered) {
return dark ? sColorGrey30 : sColorGrey50;
}
return sColorGrey40;
}
std::pair<sRGBColor, sRGBColor> Theme::ComputeButtonColors(
const EventStates& aState, const Colors& aColors, nsIFrame* aFrame) {
bool isActive =
aState.HasAllStates(NS_EVENT_STATE_HOVER | NS_EVENT_STATE_ACTIVE);
bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
bool isHovered = aState.HasState(NS_EVENT_STATE_HOVER);
nscolor backgroundColor = [&] {
if (isDisabled) {
return aColors.SystemNs(StyleSystemColor::MozButtondisabledface);
}
if (isActive) {
return aColors.SystemNs(StyleSystemColor::MozButtonactiveface);
}
if (isHovered) {
return aColors.SystemNs(StyleSystemColor::MozButtonhoverface);
}
return aColors.SystemNs(StyleSystemColor::Buttonface);
}();
if (aState.HasState(NS_EVENT_STATE_AUTOFILL)) {
backgroundColor = NS_ComposeColors(
backgroundColor,
aColors.SystemNs(StyleSystemColor::MozAutofillBackground));
}
const sRGBColor borderColor =
ComputeBorderColor(aState, aColors, OutlineCoversBorder::Yes);
return std::make_pair(sRGBColor::FromABGR(backgroundColor), borderColor);
}
std::pair<sRGBColor, sRGBColor> Theme::ComputeTextfieldColors(
const EventStates& aState, const Colors& aColors,
OutlineCoversBorder aOutlineCoversBorder) {
nscolor backgroundColor = [&] {
if (aState.HasState(NS_EVENT_STATE_DISABLED)) {
return aColors.SystemNs(StyleSystemColor::MozDisabledfield);
}
return aColors.SystemNs(StyleSystemColor::Field);
}();
if (aState.HasState(NS_EVENT_STATE_AUTOFILL)) {
backgroundColor = NS_ComposeColors(
backgroundColor,
aColors.SystemNs(StyleSystemColor::MozAutofillBackground));
}
const sRGBColor borderColor =
ComputeBorderColor(aState, aColors, aOutlineCoversBorder);
return std::make_pair(sRGBColor::FromABGR(backgroundColor), borderColor);
}
std::pair<sRGBColor, sRGBColor> Theme::ComputeRangeProgressColors(
const EventStates& aState, const Colors& aColors) {
if (aColors.HighContrast()) {
return aColors.SystemPair(StyleSystemColor::Selecteditem,
StyleSystemColor::Buttontext);
}
bool isActive =
aState.HasAllStates(NS_EVENT_STATE_HOVER | NS_EVENT_STATE_ACTIVE);
bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
bool isHovered = aState.HasState(NS_EVENT_STATE_HOVER);
if (isDisabled) {
return std::make_pair(sColorGrey40Alpha50, sColorGrey40Alpha50);
}
if (isActive || isHovered) {
return std::make_pair(aColors.Accent().GetDark(),
aColors.Accent().GetDarker());
}
return std::make_pair(aColors.Accent().Get(), aColors.Accent().GetDark());
}
std::pair<sRGBColor, sRGBColor> Theme::ComputeRangeTrackColors(
const EventStates& aState, const Colors& aColors) {
if (aColors.HighContrast()) {
return aColors.SystemPair(StyleSystemColor::Window,
StyleSystemColor::Buttontext);
}
bool isActive =
aState.HasAllStates(NS_EVENT_STATE_HOVER | NS_EVENT_STATE_ACTIVE);
bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
bool isHovered = aState.HasState(NS_EVENT_STATE_HOVER);
if (isDisabled) {
return std::make_pair(sColorGrey10Alpha50, sColorGrey40Alpha50);
}
if (isActive || isHovered) {
return std::make_pair(sColorGrey20, sColorGrey50);
}
return std::make_pair(sColorGrey10, sColorGrey40);
}
std::pair<sRGBColor, sRGBColor> Theme::ComputeRangeThumbColors(
const EventStates& aState, const Colors& aColors) {
if (aColors.HighContrast()) {
return aColors.SystemPair(StyleSystemColor::Selecteditemtext,
StyleSystemColor::Selecteditem);
}
bool isActive =
aState.HasAllStates(NS_EVENT_STATE_HOVER | NS_EVENT_STATE_ACTIVE);
bool isDisabled = aState.HasState(NS_EVENT_STATE_DISABLED);
bool isHovered = aState.HasState(NS_EVENT_STATE_HOVER);
const sRGBColor& backgroundColor = [&] {
if (isDisabled) {
return sColorGrey40;
}
if (isActive) {
return aColors.Accent().Get();
}
if (isHovered) {
return sColorGrey60;
}
return sColorGrey50;
}();
const sRGBColor borderColor = sRGBColor::OpaqueWhite();
return std::make_pair(backgroundColor, borderColor);
}
std::pair<sRGBColor, sRGBColor> Theme::ComputeProgressColors(
const Colors& aColors) {
if (aColors.HighContrast()) {
return aColors.SystemPair(StyleSystemColor::Selecteditem,
StyleSystemColor::Buttontext);
}
return std::make_pair(aColors.Accent().Get(), aColors.Accent().GetDark());
}
std::pair<sRGBColor, sRGBColor> Theme::ComputeProgressTrackColors(
const Colors& aColors) {
if (aColors.HighContrast()) {
return aColors.SystemPair(StyleSystemColor::Buttonface,
StyleSystemColor::Buttontext);
}
return std::make_pair(sColorGrey10, sColorGrey40);
}
std::pair<sRGBColor, sRGBColor> Theme::ComputeMeterchunkColors(
const EventStates& aMeterState, const Colors& aColors) {
if (aColors.HighContrast()) {
return ComputeProgressColors(aColors);
}
sRGBColor borderColor = sColorMeterGreen20;
sRGBColor chunkColor = sColorMeterGreen10;
if (aMeterState.HasState(NS_EVENT_STATE_SUB_OPTIMUM)) {
borderColor = sColorMeterYellow20;
chunkColor = sColorMeterYellow10;
} else if (aMeterState.HasState(NS_EVENT_STATE_SUB_SUB_OPTIMUM)) {
borderColor = sColorMeterRed20;
chunkColor = sColorMeterRed10;
}
return std::make_pair(chunkColor, borderColor);
}
std::array<sRGBColor, 3> Theme::ComputeFocusRectColors(const Colors& aColors) {
if (aColors.HighContrast()) {
return {aColors.System(StyleSystemColor::Selecteditem),
aColors.System(StyleSystemColor::Buttontext),
aColors.System(StyleSystemColor::Window)};
}
const auto& accent = aColors.Accent();
const sRGBColor middle =
aColors.IsDark() ? sRGBColor::Black(.3f) : sRGBColor::White(.3f);
return {accent.Get(), middle, accent.GetLight()};
}
static const CSSCoord kInnerFocusOutlineWidth = 2.0f;
template <typename PaintBackendData>
void Theme::PaintRoundedFocusRect(PaintBackendData& aBackendData,
const LayoutDeviceRect& aRect,
const Colors& aColors, DPIRatio aDpiRatio,
CSSCoord aRadius, CSSCoord aOffset) {
// NOTE(emilio): If the widths or offsets here change, make sure to tweak
// the GetWidgetOverflow path for FocusOutline.
auto [innerColor, middleColor, outerColor] = ComputeFocusRectColors(aColors);
LayoutDeviceRect focusRect(aRect);
// The focus rect is painted outside of the border area (aRect), see:
//
// data:text/html,<div style="border: 1px solid; outline: 2px solid
// red">Foobar</div>
//
// But some controls might provide a negative offset to cover the border, if
// necessary.
CSSCoord strokeWidth = kInnerFocusOutlineWidth;
auto strokeWidthDevPx =
LayoutDeviceCoord(ThemeDrawing::SnapBorderWidth(strokeWidth, aDpiRatio));
CSSCoord strokeRadius = aRadius;
focusRect.Inflate(aOffset * aDpiRatio + strokeWidthDevPx);
ThemeDrawing::PaintRoundedRectWithRadius(
aBackendData, focusRect, sTransparent, innerColor, strokeWidth,
strokeRadius, aDpiRatio);
strokeWidth = CSSCoord(1.0f);
strokeWidthDevPx =
LayoutDeviceCoord(ThemeDrawing::SnapBorderWidth(strokeWidth, aDpiRatio));
strokeRadius += strokeWidth;
focusRect.Inflate(strokeWidthDevPx);
ThemeDrawing::PaintRoundedRectWithRadius(
aBackendData, focusRect, sTransparent, middleColor, strokeWidth,
strokeRadius, aDpiRatio);
strokeWidth = CSSCoord(2.0f);
strokeWidthDevPx =
LayoutDeviceCoord(ThemeDrawing::SnapBorderWidth(strokeWidth, aDpiRatio));
strokeRadius += strokeWidth;
focusRect.Inflate(strokeWidthDevPx);
ThemeDrawing::PaintRoundedRectWithRadius(
aBackendData, focusRect, sTransparent, outerColor, strokeWidth,
strokeRadius, aDpiRatio);
}
void Theme::PaintCheckboxControl(DrawTarget& aDrawTarget,
const LayoutDeviceRect& aRect,
const EventStates& aState,
const Colors& aColors, DPIRatio aDpiRatio) {
auto [backgroundColor, borderColor] =
ComputeCheckboxColors(aState, StyleAppearance::Checkbox, aColors);
{
const CSSCoord radius = 2.0f;
CSSCoord borderWidth = kCheckboxRadioBorderWidth;
if (backgroundColor == borderColor) {
borderWidth = 0.0f;
}
ThemeDrawing::PaintRoundedRectWithRadius(aDrawTarget, aRect,
backgroundColor, borderColor,
borderWidth, radius, aDpiRatio);
}
if (aState.HasState(NS_EVENT_STATE_INDETERMINATE)) {
PaintIndeterminateMark(aDrawTarget, aRect, aState, aColors);
} else if (aState.HasState(NS_EVENT_STATE_CHECKED)) {
PaintCheckMark(aDrawTarget, aRect, aState, aColors);
}
if (aState.HasState(NS_EVENT_STATE_FOCUSRING)) {
PaintRoundedFocusRect(aDrawTarget, aRect, aColors, aDpiRatio, 5.0f, 1.0f);
}
}
constexpr CSSCoord kCheckboxRadioContentBoxSize = 10.0f;
constexpr CSSCoord kCheckboxRadioBorderBoxSize =
kCheckboxRadioContentBoxSize + kCheckboxRadioBorderWidth * 2.0f;
void Theme::PaintCheckMark(DrawTarget& aDrawTarget,
const LayoutDeviceRect& aRect,
const EventStates& aState, const Colors& aColors) {
// Points come from the coordinates on a 14X14 (kCheckboxRadioBorderBoxSize)
// unit box centered at 0,0
const float checkPolygonX[] = {-4.5f, -1.5f, -0.5f, 5.0f, 4.75f,
3.5f, -0.5f, -1.5f, -3.5f};
const float checkPolygonY[] = {0.5f, 4.0f, 4.0f, -2.5f, -4.0f,
-4.0f, 1.0f, 1.25f, -1.0f};
const int32_t checkNumPoints = sizeof(checkPolygonX) / sizeof(float);
const float scale =
ThemeDrawing::ScaleToFillRect(aRect, kCheckboxRadioBorderBoxSize);
auto center = aRect.Center().ToUnknownPoint();
RefPtr<PathBuilder> builder = aDrawTarget.CreatePathBuilder();
Point p = center + Point(checkPolygonX[0] * scale, checkPolygonY[0] * scale);
builder->MoveTo(p);
for (int32_t i = 1; i < checkNumPoints; i++) {
p = center + Point(checkPolygonX[i] * scale, checkPolygonY[i] * scale);
builder->LineTo(p);
}
RefPtr<Path> path = builder->Finish();
sRGBColor fillColor = ComputeCheckmarkColor(aState, aColors);
aDrawTarget.Fill(path, ColorPattern(ToDeviceColor(fillColor)));
}
void Theme::PaintIndeterminateMark(DrawTarget& aDrawTarget,
const LayoutDeviceRect& aRect,
const EventStates& aState,
const Colors& aColors) {
const CSSCoord borderWidth = 2.0f;
const float scale =
ThemeDrawing::ScaleToFillRect(aRect, kCheckboxRadioBorderBoxSize);
Rect rect = aRect.ToUnknownRect();
rect.y += (rect.height / 2) - (borderWidth * scale / 2);
rect.height = borderWidth * scale;
rect.x += (borderWidth * scale) + (borderWidth * scale / 8);
rect.width -= ((borderWidth * scale) + (borderWidth * scale / 8)) * 2;
sRGBColor fillColor = ComputeCheckmarkColor(aState, aColors);
aDrawTarget.FillRect(rect, ColorPattern(ToDeviceColor(fillColor)));
}
template <typename PaintBackendData>
void Theme::PaintStrokedCircle(PaintBackendData& aPaintData,
const LayoutDeviceRect& aRect,
const sRGBColor& aBackgroundColor,
const sRGBColor& aBorderColor,
const CSSCoord aBorderWidth,
DPIRatio aDpiRatio) {
auto radius = LayoutDeviceCoord(aRect.Size().width) / aDpiRatio;
ThemeDrawing::PaintRoundedRectWithRadius(aPaintData, aRect, aBackgroundColor,
aBorderColor, aBorderWidth, radius,
aDpiRatio);
}
void Theme::PaintCircleShadow(WebRenderBackendData& aWrData,
const LayoutDeviceRect& aBoxRect,
const LayoutDeviceRect& aClipRect,
float aShadowAlpha, const CSSPoint& aShadowOffset,
CSSCoord aShadowBlurStdDev, DPIRatio aDpiRatio) {
const bool kBackfaceIsVisible = true;
const LayoutDeviceCoord stdDev = aShadowBlurStdDev * aDpiRatio;
const LayoutDevicePoint shadowOffset = aShadowOffset * aDpiRatio;
const IntSize inflation =
gfxAlphaBoxBlur::CalculateBlurRadius(gfxPoint(stdDev, stdDev));
LayoutDeviceRect shadowRect = aBoxRect;
shadowRect.MoveBy(shadowOffset);
shadowRect.Inflate(inflation.width, inflation.height);
const auto boxRect = wr::ToLayoutRect(aBoxRect);
aWrData.mBuilder.PushBoxShadow(
wr::ToLayoutRect(shadowRect), wr::ToLayoutRect(aClipRect),
kBackfaceIsVisible, boxRect,
wr::ToLayoutVector2D(aShadowOffset * aDpiRatio),
wr::ToColorF(DeviceColor(0.0f, 0.0f, 0.0f, aShadowAlpha)), stdDev,
/* aSpread = */ 0.0f,
wr::ToBorderRadius(gfx::RectCornerRadii(aBoxRect.Size().width)),
wr::BoxShadowClipMode::Outset);
}
void Theme::PaintCircleShadow(DrawTarget& aDrawTarget,
const LayoutDeviceRect& aBoxRect,
const LayoutDeviceRect& aClipRect,
float aShadowAlpha, const CSSPoint& aShadowOffset,
CSSCoord aShadowBlurStdDev, DPIRatio aDpiRatio) {
Float stdDev = aShadowBlurStdDev * aDpiRatio;
Point offset = (aShadowOffset * aDpiRatio).ToUnknownPoint();
RefPtr<FilterNode> blurFilter =
aDrawTarget.CreateFilter(FilterType::GAUSSIAN_BLUR);
if (!blurFilter) {
return;
}
blurFilter->SetAttribute(ATT_GAUSSIAN_BLUR_STD_DEVIATION, stdDev);
IntSize inflation =
gfxAlphaBoxBlur::CalculateBlurRadius(gfxPoint(stdDev, stdDev));
Rect inflatedRect = aBoxRect.ToUnknownRect();
inflatedRect.Inflate(inflation.width, inflation.height);
Rect sourceRectInFilterSpace =
inflatedRect - aBoxRect.TopLeft().ToUnknownPoint();
Point destinationPointOfSourceRect = inflatedRect.TopLeft() + offset;
IntSize dtSize = RoundedToInt(aBoxRect.Size().ToUnknownSize());
RefPtr<DrawTarget> ellipseDT = aDrawTarget.CreateSimilarDrawTargetForFilter(
dtSize, SurfaceFormat::A8, blurFilter, blurFilter,
sourceRectInFilterSpace, destinationPointOfSourceRect);
if (!ellipseDT) {
return;
}
AutoClipRect clipRect(aDrawTarget, aClipRect);
RefPtr<Path> ellipse = MakePathForEllipse(
*ellipseDT, (aBoxRect - aBoxRect.TopLeft()).Center().ToUnknownPoint(),
aBoxRect.Size().ToUnknownSize());
ellipseDT->Fill(ellipse,
ColorPattern(DeviceColor(0.0f, 0.0f, 0.0f, aShadowAlpha)));
RefPtr<SourceSurface> ellipseSurface = ellipseDT->Snapshot();
blurFilter->SetInput(IN_GAUSSIAN_BLUR_IN, ellipseSurface);
aDrawTarget.DrawFilter(blurFilter, sourceRectInFilterSpace,
destinationPointOfSourceRect);
}
template <typename PaintBackendData>
void Theme::PaintRadioControl(PaintBackendData& aPaintData,
const LayoutDeviceRect& aRect,
const EventStates& aState, const Colors& aColors,
DPIRatio aDpiRatio) {
auto [backgroundColor, borderColor] =
ComputeCheckboxColors(aState, StyleAppearance::Radio, aColors);
{
CSSCoord borderWidth = kCheckboxRadioBorderWidth;
if (backgroundColor == borderColor) {
borderWidth = 0.0f;
}
PaintStrokedCircle(aPaintData, aRect, backgroundColor, borderColor,
borderWidth, aDpiRatio);
}
if (aState.HasState(NS_EVENT_STATE_CHECKED)) {
LayoutDeviceRect rect(aRect);
auto width = LayoutDeviceCoord(
ThemeDrawing::SnapBorderWidth(kCheckboxRadioBorderWidth, aDpiRatio));
rect.Deflate(width);
auto checkColor = ComputeCheckmarkColor(aState, aColors);
PaintStrokedCircle(aPaintData, rect, backgroundColor, checkColor,
kCheckboxRadioBorderWidth, aDpiRatio);
}
if (aState.HasState(NS_EVENT_STATE_FOCUSRING)) {
PaintRoundedFocusRect(aPaintData, aRect, aColors, aDpiRatio, 5.0f, 1.0f);
}
}
template <typename PaintBackendData>
void Theme::PaintTextField(PaintBackendData& aPaintData,
const LayoutDeviceRect& aRect,
const EventStates& aState, const Colors& aColors,
DPIRatio aDpiRatio) {
auto [backgroundColor, borderColor] =
ComputeTextfieldColors(aState, aColors, OutlineCoversBorder::Yes);
const CSSCoord radius = 2.0f;
ThemeDrawing::PaintRoundedRectWithRadius(aPaintData, aRect, backgroundColor,
borderColor, kTextFieldBorderWidth,
radius, aDpiRatio);
if (aState.HasState(NS_EVENT_STATE_FOCUSRING)) {
PaintRoundedFocusRect(aPaintData, aRect, aColors, aDpiRatio,
radius + kTextFieldBorderWidth,
-kTextFieldBorderWidth);
}
}
template <typename PaintBackendData>
void Theme::PaintListbox(PaintBackendData& aPaintData,
const LayoutDeviceRect& aRect,
const EventStates& aState, const Colors& aColors,
DPIRatio aDpiRatio) {
const CSSCoord radius = 2.0f;
auto [backgroundColor, borderColor] =
ComputeTextfieldColors(aState, aColors, OutlineCoversBorder::Yes);
ThemeDrawing::PaintRoundedRectWithRadius(aPaintData, aRect, backgroundColor,
borderColor, kMenulistBorderWidth,
radius, aDpiRatio);
if (aState.HasState(NS_EVENT_STATE_FOCUSRING)) {
PaintRoundedFocusRect(aPaintData, aRect, aColors, aDpiRatio,
radius + kMenulistBorderWidth, -kMenulistBorderWidth);
}
}
template <typename PaintBackendData>
void Theme::PaintMenulist(PaintBackendData& aDrawTarget,
const LayoutDeviceRect& aRect,
const EventStates& aState, const Colors& aColors,
DPIRatio aDpiRatio) {
const CSSCoord radius = 4.0f;
auto [backgroundColor, borderColor] = ComputeButtonColors(aState, aColors);
ThemeDrawing::PaintRoundedRectWithRadius(aDrawTarget, aRect, backgroundColor,
borderColor, kMenulistBorderWidth,
radius, aDpiRatio);
if (aState.HasState(NS_EVENT_STATE_FOCUSRING)) {
PaintRoundedFocusRect(aDrawTarget, aRect, aColors, aDpiRatio,
radius + kMenulistBorderWidth, -kMenulistBorderWidth);
}
}
void Theme::PaintMenulistArrowButton(nsIFrame* aFrame, DrawTarget& aDrawTarget,
const LayoutDeviceRect& aRect,
const EventStates& aState) {
const float kPolygonX[] = {-4.0f, -0.5f, 0.5f, 4.0f, 4.0f,
3.0f, 0.0f, 0.0f, -3.0f, -4.0f};
const float kPolygonY[] = {-1, 3.0f, 3.0f, -1.0f, -2.0f,
-2.0f, 1.5f, 1.5f, -2.0f, -2.0f};
const float kPolygonSize = kMinimumDropdownArrowButtonWidth;
const auto arrowColor = sRGBColor::FromABGR(
nsLayoutUtils::GetColor(aFrame, &nsStyleText::mWebkitTextFillColor));
ThemeDrawing::PaintArrow(aDrawTarget, aRect, kPolygonX, kPolygonY,
kPolygonSize, ArrayLength(kPolygonX), arrowColor);
}
void Theme::PaintSpinnerButton(nsIFrame* aFrame, DrawTarget& aDrawTarget,
const LayoutDeviceRect& aRect,
const EventStates& aState,
StyleAppearance aAppearance,
const Colors& aColors, DPIRatio aDpiRatio) {
auto [backgroundColor, borderColor] = ComputeButtonColors(aState, aColors);
aDrawTarget.FillRect(aRect.ToUnknownRect(),
ColorPattern(ToDeviceColor(backgroundColor)));
const float kPolygonX[] = {-3.5f, -0.5f, 0.5f, 3.5f, 3.5f,
2.5f, 0.0f, 0.0f, -2.5f, -3.5f};
float polygonY[] = {-1.5f, 1.5f, 1.5f, -1.5f, -2.5f,
-2.5f, 0.0f, 0.0f, -2.5f, -2.5f};
const float kPolygonSize = kMinimumSpinnerButtonHeight;
if (aAppearance == StyleAppearance::SpinnerUpbutton) {
for (auto& coord : polygonY) {
coord = -coord;
}
}
ThemeDrawing::PaintArrow(aDrawTarget, aRect, kPolygonX, polygonY,
kPolygonSize, ArrayLength(kPolygonX), borderColor);
}
template <typename PaintBackendData>
void Theme::PaintRange(nsIFrame* aFrame, PaintBackendData& aPaintData,
const LayoutDeviceRect& aRect, const EventStates& aState,
const Colors& aColors, DPIRatio aDpiRatio,
bool aHorizontal) {
nsRangeFrame* rangeFrame = do_QueryFrame(aFrame);
if (!rangeFrame) {
return;
}
double progress = rangeFrame->GetValueAsFractionOfRange();
auto rect = aRect;
LayoutDeviceRect thumbRect(0, 0, kMinimumRangeThumbSize * aDpiRatio,
kMinimumRangeThumbSize * aDpiRatio);
LayoutDeviceRect progressClipRect(aRect);
LayoutDeviceRect trackClipRect(aRect);
const LayoutDeviceCoord verticalSize = kRangeHeight * aDpiRatio;
if (aHorizontal) {
rect.height = verticalSize;
rect.y = aRect.y + (aRect.height - rect.height) / 2;
thumbRect.y = aRect.y + (aRect.height - thumbRect.height) / 2;
if (IsFrameRTL(aFrame)) {
thumbRect.x =
aRect.x + (aRect.width - thumbRect.width) * (1.0 - progress);
float midPoint = thumbRect.Center().X();
trackClipRect.SetBoxX(aRect.X(), midPoint);
progressClipRect.SetBoxX(midPoint, aRect.XMost());
} else {
thumbRect.x = aRect.x + (aRect.width - thumbRect.width) * progress;
float midPoint = thumbRect.Center().X();
progressClipRect.SetBoxX(aRect.X(), midPoint);
trackClipRect.SetBoxX(midPoint, aRect.XMost());
}
} else {
rect.width = verticalSize;
rect.x = aRect.x + (aRect.width - rect.width) / 2;
thumbRect.x = aRect.x + (aRect.width - thumbRect.width) / 2;
thumbRect.y =
aRect.y + (aRect.height - thumbRect.height) * (1.0 - progress);
float midPoint = thumbRect.Center().Y();
trackClipRect.SetBoxY(aRect.Y(), midPoint);
progressClipRect.SetBoxY(midPoint, aRect.YMost());
}
const CSSCoord borderWidth = 1.0f;
const CSSCoord radius = 3.0f;
auto [progressColor, progressBorderColor] =
ComputeRangeProgressColors(aState, aColors);
auto [trackColor, trackBorderColor] =
ComputeRangeTrackColors(aState, aColors);
ThemeDrawing::PaintRoundedRectWithRadius(aPaintData, rect, progressClipRect,
progressColor, progressBorderColor,
borderWidth, radius, aDpiRatio);
ThemeDrawing::PaintRoundedRectWithRadius(aPaintData, rect, trackClipRect,
trackColor, trackBorderColor,
borderWidth, radius, aDpiRatio);
if (!aState.HasState(NS_EVENT_STATE_DISABLED)) {
// Ensure the shadow doesn't expand outside of our overflow rect declared in
// GetWidgetOverflow().
auto overflowRect = aRect;
overflowRect.Inflate(CSSCoord(6.0f) * aDpiRatio);
// Thumb shadow
PaintCircleShadow(aPaintData, thumbRect, overflowRect, 0.3f,
CSSPoint(0.0f, 2.0f), 2.0f, aDpiRatio);
}
// Draw the thumb on top.
const CSSCoord thumbBorderWidth = 2.0f;
auto [thumbColor, thumbBorderColor] =
ComputeRangeThumbColors(aState, aColors);
PaintStrokedCircle(aPaintData, thumbRect, thumbColor, thumbBorderColor,
thumbBorderWidth, aDpiRatio);
if (aState.HasState(NS_EVENT_STATE_FOCUSRING)) {
PaintRoundedFocusRect(aPaintData, aRect, aColors, aDpiRatio, radius, 1.0f);
}
}
template <typename PaintBackendData>
void Theme::PaintProgress(nsIFrame* aFrame, PaintBackendData& aPaintData,
const LayoutDeviceRect& aRect,
const EventStates& aState, const Colors& aColors,
DPIRatio aDpiRatio, bool aIsMeter) {
const CSSCoord borderWidth = 1.0f;
const CSSCoord radius = aIsMeter ? 6.0f : 3.0f;
LayoutDeviceRect rect(aRect);
const LayoutDeviceCoord thickness =
(aIsMeter ? kMeterHeight : kProgressbarHeight) * aDpiRatio;
const bool isHorizontal = !nsNativeTheme::IsVerticalProgress(aFrame);
if (isHorizontal) {
// Center it vertically.
rect.y += (rect.height - thickness) / 2;
rect.height = thickness;
} else {
// Center it horizontally.
rect.x += (rect.width - thickness) / 2;
rect.width = thickness;
}
{
// Paint the track, unclipped.
auto [backgroundColor, borderColor] = ComputeProgressTrackColors(aColors);
ThemeDrawing::PaintRoundedRectWithRadius(aPaintData, rect, rect,
backgroundColor, borderColor,
borderWidth, radius, aDpiRatio);
}
// Now paint the chunk, clipped as needed.
LayoutDeviceRect clipRect = rect;
if (aState.HasState(NS_EVENT_STATE_INDETERMINATE)) {
// For indeterminate progress, we paint an animated chunk of 1/3 of the
// progress size.
//
// Animation speed and math borrowed from GTK.
const LayoutDeviceCoord size = isHorizontal ? rect.width : rect.height;
const LayoutDeviceCoord barSize = size * 0.3333f;
const LayoutDeviceCoord travel = 2.0f * (size - barSize);
// Period equals to travel / pixelsPerMillisecond where pixelsPerMillisecond
// equals progressSize / 1000.0. This is equivalent to 1600.
const unsigned kPeriod = 1600;
const int t = PR_IntervalToMilliseconds(PR_IntervalNow()) % kPeriod;
const LayoutDeviceCoord dx = travel * float(t) / float(kPeriod);
if (isHorizontal) {
rect.width = barSize;
rect.x += (dx < travel * .5f) ? dx : travel - dx;
} else {
rect.height = barSize;
rect.y += (dx < travel * .5f) ? dx : travel - dx;
}
clipRect = rect;
// Queue the next frame if needed.
if (!QueueAnimatedContentForRefresh(aFrame->GetContent(), 60)) {
NS_WARNING("Couldn't refresh indeterminate <progress>");
}
} else {
// This is the progress chunk, clip it to the right amount.
double position = [&] {
if (aIsMeter) {
auto* meter = dom::HTMLMeterElement::FromNode(aFrame->GetContent());
if (!meter) {
return 0.0;
}
return meter->Position();
}
auto* progress = dom::HTMLProgressElement::FromNode(aFrame->GetContent());
if (!progress) {
return 0.0;
}
return progress->Position();
}();
if (isHorizontal) {
double clipWidth = rect.width * position;
clipRect.width = clipWidth;
if (IsFrameRTL(aFrame)) {
clipRect.x += rect.width - clipWidth;
}
} else {
double clipHeight = rect.height * position;
clipRect.height = clipHeight;
clipRect.y += rect.height - clipHeight;
}
}
auto [backgroundColor, borderColor] =
aIsMeter ? ComputeMeterchunkColors(aState, aColors)
: ComputeProgressColors(aColors);
ThemeDrawing::PaintRoundedRectWithRadius(aPaintData, rect, clipRect,
backgroundColor, borderColor,
borderWidth, radius, aDpiRatio);
}
template <typename PaintBackendData>
void Theme::PaintButton(nsIFrame* aFrame, PaintBackendData& aPaintData,
const LayoutDeviceRect& aRect,
const EventStates& aState, const Colors& aColors,
DPIRatio aDpiRatio) {
const CSSCoord radius = 4.0f;
auto [backgroundColor, borderColor] =
ComputeButtonColors(aState, aColors, aFrame);
ThemeDrawing::PaintRoundedRectWithRadius(aPaintData, aRect, backgroundColor,
borderColor, kButtonBorderWidth,
radius, aDpiRatio);
if (aState.HasState(NS_EVENT_STATE_FOCUSRING)) {
PaintRoundedFocusRect(aPaintData, aRect, aColors, aDpiRatio,
radius + kButtonBorderWidth, -kButtonBorderWidth);
}
}