/
RenderThemeIOS.mm
2530 lines (2052 loc) · 105 KB
/
RenderThemeIOS.mm
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
/*
* Copyright (C) 2005-2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "config.h"
#import "RenderThemeIOS.h"
#if PLATFORM(IOS_FAMILY)
#import "ARKitBadgeSystemImage.h"
#import "BitmapImage.h"
#import "BorderPainter.h"
#import "CSSPrimitiveValue.h"
#import "CSSToLengthConversionData.h"
#import "CSSValueKey.h"
#import "CSSValueKeywords.h"
#import "ColorBlending.h"
#import "ColorCocoa.h"
#import "ColorTypes.h"
#import "DateComponents.h"
#import "DeprecatedGlobalSettings.h"
#import "Document.h"
#import "DrawGlyphsRecorder.h"
#import "File.h"
#import "FloatRoundedRect.h"
#import "FontCache.h"
#import "FontCacheCoreText.h"
#import "FontCascade.h"
#import "FrameSelection.h"
#import "GeometryUtilities.h"
#import "Gradient.h"
#import "GraphicsContext.h"
#import "GraphicsContextCG.h"
#import "HTMLAttachmentElement.h"
#import "HTMLButtonElement.h"
#import "HTMLInputElement.h"
#import "HTMLMeterElement.h"
#import "HTMLNames.h"
#import "HTMLSelectElement.h"
#import "HTMLTextAreaElement.h"
#import "IOSurface.h"
#import "LocalCurrentTraitCollection.h"
#import "LocalFrame.h"
#import "LocalFrameView.h"
#import "LocalizedDateCache.h"
#import "NodeRenderStyle.h"
#import "Page.h"
#import "PaintInfo.h"
#import "PathUtilities.h"
#import "PlatformLocale.h"
#import "RenderAttachment.h"
#import "RenderButton.h"
#import "RenderMenuList.h"
#import "RenderMeter.h"
#import "RenderObject.h"
#import "RenderProgress.h"
#import "RenderSlider.h"
#import "RenderStyle.h"
#import "RenderView.h"
#import "Settings.h"
#import "Theme.h"
#import "TypedElementDescendantIteratorInlines.h"
#import "UTIUtilities.h"
#import "WebCoreThreadRun.h"
#import <CoreGraphics/CoreGraphics.h>
#import <CoreImage/CoreImage.h>
#import <objc/runtime.h>
#import <pal/spi/cf/CoreTextSPI.h>
#import <pal/spi/ios/UIKitSPI.h>
#import <wtf/NeverDestroyed.h>
#import <wtf/ObjCRuntimeExtras.h>
#import <wtf/StdLibExtras.h>
#import <wtf/cocoa/TypeCastsCocoa.h>
#if ENABLE(DATALIST_ELEMENT)
#include "HTMLDataListElement.h"
#include "HTMLOptionElement.h"
#endif
#import <pal/ios/UIKitSoftLink.h>
namespace WebCore {
using namespace HTMLNames;
const float ControlBaseHeight = 20;
const float ControlBaseFontSize = 11;
struct IOSGradient {
float* start; // points to static float[4]
float* end; // points to static float[4]
IOSGradient(float start[4], float end[4])
: start(start)
, end(end)
{
}
};
typedef IOSGradient* IOSGradientRef;
enum Interpolation
{
LinearInterpolation,
ExponentialInterpolation
};
static void interpolateLinearGradient(void *info, const CGFloat *inData, CGFloat *outData)
{
IOSGradientRef gradient = static_cast<IOSGradientRef>(info);
float alpha = inData[0];
float inverse = 1.0f - alpha;
outData[0] = inverse * gradient->start[0] + alpha * gradient->end[0];
outData[1] = inverse * gradient->start[1] + alpha * gradient->end[1];
outData[2] = inverse * gradient->start[2] + alpha * gradient->end[2];
outData[3] = inverse * gradient->start[3] + alpha * gradient->end[3];
}
static void interpolateExponentialGradient(void *info, const CGFloat *inData, CGFloat *outData)
{
IOSGradientRef gradient = static_cast<IOSGradientRef>(info);
float a = inData[0];
for (int paintInfo = 0; paintInfo < 4; ++paintInfo) {
float end = logf(std::max(gradient->end[paintInfo], 0.01f));
float start = logf(std::max(gradient->start[paintInfo], 0.01f));
outData[paintInfo] = expf(start - (end + start) * a);
}
}
static CGFunctionRef getSharedFunctionRef(IOSGradientRef gradient, Interpolation interpolation)
{
static NeverDestroyed<HashMap<IOSGradientRef, RetainPtr<CGFunctionRef>>> linearFunctionRefs;
static NeverDestroyed<HashMap<IOSGradientRef, RetainPtr<CGFunctionRef>>> exponentialFunctionRefs;
if (interpolation == LinearInterpolation) {
auto function = linearFunctionRefs->get(gradient);
if (!function) {
static struct CGFunctionCallbacks linearFunctionCallbacks = { 0, interpolateLinearGradient, 0 };
linearFunctionRefs->set(gradient, function = adoptCF(CGFunctionCreate(gradient, 1, nullptr, 4, nullptr, &linearFunctionCallbacks)));
}
return function.get();
}
auto function = exponentialFunctionRefs->get(gradient);
if (!function) {
static struct CGFunctionCallbacks exponentialFunctionCallbacks = { 0, interpolateExponentialGradient, 0 };
exponentialFunctionRefs->set(gradient, function = adoptCF(CGFunctionCreate(gradient, 1, 0, 4, 0, &exponentialFunctionCallbacks)));
}
return function.get();
}
static void drawAxialGradient(CGContextRef context, IOSGradientRef gradient, const FloatPoint& startPoint, const FloatPoint& stopPoint, Interpolation interpolation)
{
RetainPtr<CGShadingRef> shading = adoptCF(CGShadingCreateAxial(sRGBColorSpaceRef(), startPoint, stopPoint, getSharedFunctionRef(gradient, interpolation), false, false));
CGContextDrawShading(context, shading.get());
}
static void drawRadialGradient(CGContextRef context, IOSGradientRef gradient, const FloatPoint& startPoint, float startRadius, const FloatPoint& stopPoint, float stopRadius, Interpolation interpolation)
{
RetainPtr<CGShadingRef> shading = adoptCF(CGShadingCreateRadial(sRGBColorSpaceRef(), startPoint, startRadius, stopPoint, stopRadius, getSharedFunctionRef(gradient, interpolation), false, false));
CGContextDrawShading(context, shading.get());
}
enum IOSGradientType {
InsetGradient,
ShineGradient,
ShadeGradient,
ConvexGradient,
ConcaveGradient,
SliderTrackGradient,
ReadonlySliderTrackGradient,
SliderThumbOpaquePressedGradient,
};
static IOSGradientRef getInsetGradient()
{
static float end[4] = { 0 / 255.0, 0 / 255.0, 0 / 255.0, 0 };
static float start[4] = { 0 / 255.0, 0 / 255.0, 0 / 255.0, 0.2 };
static NeverDestroyed<IOSGradient> gradient(start, end);
return &gradient.get();
}
static IOSGradientRef getShineGradient()
{
static float end[4] = { 1, 1, 1, 0.8 };
static float start[4] = { 1, 1, 1, 0 };
static NeverDestroyed<IOSGradient> gradient(start, end);
return &gradient.get();
}
static IOSGradientRef getShadeGradient()
{
static float end[4] = { 178 / 255.0, 178 / 255.0, 178 / 255.0, 0.65 };
static float start[4] = { 252 / 255.0, 252 / 255.0, 252 / 255.0, 0.65 };
static NeverDestroyed<IOSGradient> gradient(start, end);
return &gradient.get();
}
static IOSGradientRef getConvexGradient()
{
static float end[4] = { 255 / 255.0, 255 / 255.0, 255 / 255.0, 0.05 };
static float start[4] = { 255 / 255.0, 255 / 255.0, 255 / 255.0, 0.43 };
static NeverDestroyed<IOSGradient> gradient(start, end);
return &gradient.get();
}
static IOSGradientRef getConcaveGradient()
{
static float end[4] = { 255 / 255.0, 255 / 255.0, 255 / 255.0, 0.46 };
static float start[4] = { 255 / 255.0, 255 / 255.0, 255 / 255.0, 0 };
static NeverDestroyed<IOSGradient> gradient(start, end);
return &gradient.get();
}
static IOSGradientRef getSliderTrackGradient()
{
static float end[4] = { 132 / 255.0, 132 / 255.0, 132 / 255.0, 1 };
static float start[4] = { 74 / 255.0, 77 / 255.0, 80 / 255.0, 1 };
static NeverDestroyed<IOSGradient> gradient(start, end);
return &gradient.get();
}
static IOSGradientRef getReadonlySliderTrackGradient()
{
static float end[4] = { 132 / 255.0, 132 / 255.0, 132 / 255.0, 0.4 };
static float start[4] = { 74 / 255.0, 77 / 255.0, 80 /255.0, 0.4 };
static NeverDestroyed<IOSGradient> gradient(start, end);
return &gradient.get();
}
static IOSGradientRef getSliderThumbOpaquePressedGradient()
{
static float end[4] = { 144 / 255.0, 144 / 255.0, 144 / 255.0, 1};
static float start[4] = { 55 / 255.0, 55 / 255.0, 55 / 255.0, 1 };
static NeverDestroyed<IOSGradient> gradient(start, end);
return &gradient.get();
}
static IOSGradientRef gradientWithName(IOSGradientType gradientType)
{
switch (gradientType) {
case InsetGradient:
return getInsetGradient();
case ShineGradient:
return getShineGradient();
case ShadeGradient:
return getShadeGradient();
case ConvexGradient:
return getConvexGradient();
case ConcaveGradient:
return getConcaveGradient();
case SliderTrackGradient:
return getSliderTrackGradient();
case ReadonlySliderTrackGradient:
return getReadonlySliderTrackGradient();
case SliderThumbOpaquePressedGradient:
return getSliderThumbOpaquePressedGradient();
}
ASSERT_NOT_REACHED();
return nullptr;
}
RenderThemeIOS::RenderThemeIOS() = default;
RenderTheme& RenderTheme::singleton()
{
static NeverDestroyed<RenderThemeIOS> theme;
return theme;
}
bool RenderThemeIOS::canCreateControlPartForRenderer(const RenderObject& renderer) const
{
auto type = renderer.style().effectiveAppearance();
#if ENABLE(APPLE_PAY)
return type == StyleAppearance::ApplePayButton;
#else
UNUSED_PARAM(type);
return false;
#endif
}
FloatRect RenderThemeIOS::addRoundedBorderClip(const RenderObject& box, GraphicsContext& context, const IntRect& rect)
{
// To fix inner border bleeding issues <rdar://problem/9812507>, we clip to the outer border and assert that
// the border is opaque or transparent, unless we're checked because checked radio/checkboxes show no bleeding.
auto& style = box.style();
RoundedRect border = isChecked(box) ? style.getRoundedInnerBorderFor(rect) : style.getRoundedBorderFor(rect);
if (border.isRounded())
context.clipRoundedRect(FloatRoundedRect(border));
else
context.clip(border.rect());
if (isChecked(box)) {
ASSERT(style.visitedDependentColor(CSSPropertyBorderTopColor).alphaByte() % 255 == 0);
ASSERT(style.visitedDependentColor(CSSPropertyBorderRightColor).alphaByte() % 255 == 0);
ASSERT(style.visitedDependentColor(CSSPropertyBorderBottomColor).alphaByte() % 255 == 0);
ASSERT(style.visitedDependentColor(CSSPropertyBorderLeftColor).alphaByte() % 255 == 0);
}
return border.rect();
}
void RenderThemeIOS::adjustStyleForAlternateFormControlDesignTransition(RenderStyle& style, const Element* element) const
{
if (!element)
return;
if (!element->document().settings().alternateFormControlDesignEnabled())
return;
#if ENABLE(CSS_TRANSFORM_STYLE_OPTIMIZED_3D)
// FIXME: We need to find a way to not do this for any running transition, only the UA-owned transition.
style.setTransformStyle3D(element->hasRunningTransitionForProperty(PseudoId::None, CSSPropertyID::CSSPropertyTranslate) || element->hovered() ? TransformStyle3D::Optimized3D : TransformStyle3D::Flat);
#else
UNUSED_PARAM(style);
#endif
}
void RenderThemeIOS::adjustCheckboxStyle(RenderStyle& style, const Element* element) const
{
adjustStyleForAlternateFormControlDesignTransition(style, element);
adjustMinimumIntrinsicSizeForAppearance(StyleAppearance::Checkbox, style);
if (!style.width().isIntrinsicOrAuto() && !style.height().isAuto())
return;
int size = std::max(style.computedFontPixelSize(), 10U);
style.setWidth({ size, LengthType::Fixed });
style.setHeight({ size, LengthType::Fixed });
}
static CGPoint shortened(CGPoint start, CGPoint end, float width)
{
float x = end.x - start.x;
float y = end.y - start.y;
float ratio = (!x && !y) ? 0 : width / std::hypot(x, y);
return CGPointMake(start.x + x * ratio, start.y + y * ratio);
}
static void drawJoinedLines(CGContextRef context, const Vector<CGPoint>& points, CGLineCap lineCap, float lineWidth, Color strokeColor)
{
CGContextSetLineWidth(context, lineWidth);
CGContextSetStrokeColorWithColor(context, cachedCGColor(strokeColor).get());
CGContextSetShouldAntialias(context, true);
CGContextBeginPath(context);
CGContextSetLineCap(context, lineCap);
CGContextMoveToPoint(context, points[0].x, points[0].y);
for (unsigned i = 1; i < points.size(); ++i)
CGContextAddLineToPoint(context, points[i].x, points[i].y);
CGContextStrokePath(context);
}
bool RenderThemeIOS::canPaint(const PaintInfo& paintInfo, const Settings& settings, StyleAppearance) const
{
#if ENABLE(IOS_FORM_CONTROL_REFRESH)
if (settings.iOSFormControlRefreshEnabled())
return true;
#else
UNUSED_PARAM(settings);
#endif
return paintInfo.context().hasPlatformContext();
}
void RenderThemeIOS::paintCheckboxDecorations(const RenderObject& box, const PaintInfo& paintInfo, const IntRect& rect)
{
#if ENABLE(IOS_FORM_CONTROL_REFRESH)
if (box.settings().iOSFormControlRefreshEnabled())
return;
#endif
bool checked = isChecked(box);
bool indeterminate = isIndeterminate(box);
CGContextRef cgContext = paintInfo.context().platformContext();
GraphicsContextStateSaver stateSaver { paintInfo.context() };
if (checked || indeterminate) {
auto border = box.style().getRoundedBorderFor(rect);
paintInfo.context().fillRoundedRect(border.pixelSnappedRoundedRectForPainting(box.document().deviceScaleFactor()), Color::black.colorWithAlphaByte(204));
auto clip = addRoundedBorderClip(box, paintInfo.context(), rect);
auto width = clip.width();
auto height = clip.height();
drawAxialGradient(cgContext, gradientWithName(ConcaveGradient), clip.location(), FloatPoint { clip.x(), clip.maxY() }, LinearInterpolation);
constexpr float thicknessRatio = 2 / 14.0;
float lineWidth = std::min(width, height) * 2.0f * thicknessRatio;
Vector<CGPoint, 3> line;
Vector<CGPoint, 3> shadow;
if (checked) {
constexpr CGSize size { 14.0f, 14.0f };
constexpr CGPoint pathRatios[] = {
{ 2.5f / size.width, 7.5f / size.height },
{ 5.5f / size.width, 10.5f / size.height },
{ 11.5f / size.width, 2.5f / size.height }
};
line.uncheckedAppend(CGPointMake(clip.x() + width * pathRatios[0].x, clip.y() + height * pathRatios[0].y));
line.uncheckedAppend(CGPointMake(clip.x() + width * pathRatios[1].x, clip.y() + height * pathRatios[1].y));
line.uncheckedAppend(CGPointMake(clip.x() + width * pathRatios[2].x, clip.y() + height * pathRatios[2].y));
shadow.uncheckedAppend(shortened(line[0], line[1], lineWidth / 4.0f));
shadow.uncheckedAppend(line[1]);
shadow.uncheckedAppend(shortened(line[2], line[1], lineWidth / 4.0f));
} else {
line.uncheckedAppend(CGPointMake(clip.x() + 3.5, clip.center().y()));
line.uncheckedAppend(CGPointMake(clip.maxX() - 3.5, clip.center().y()));
shadow.uncheckedAppend(shortened(line[0], line[1], lineWidth / 4.0f));
shadow.uncheckedAppend(shortened(line[1], line[0], lineWidth / 4.0f));
}
lineWidth = std::max<float>(lineWidth, 1);
drawJoinedLines(cgContext, Vector<CGPoint> { WTFMove(shadow) }, kCGLineCapSquare, lineWidth, Color::black.colorWithAlphaByte(179));
lineWidth = std::max<float>(std::min(width, height) * thicknessRatio, 1);
drawJoinedLines(cgContext, Vector<CGPoint> { WTFMove(line) }, kCGLineCapButt, lineWidth, Color::white.colorWithAlphaByte(240));
} else {
auto clip = addRoundedBorderClip(box, paintInfo.context(), rect);
auto width = clip.width();
auto height = clip.height();
FloatPoint bottomCenter { clip.x() + width / 2.0f, clip.maxY() };
drawAxialGradient(cgContext, gradientWithName(ShadeGradient), clip.location(), FloatPoint { clip.x(), clip.maxY() }, LinearInterpolation);
drawRadialGradient(cgContext, gradientWithName(ShineGradient), bottomCenter, 0, bottomCenter, sqrtf((width * width) / 4.0f + height * height), ExponentialInterpolation);
}
}
LayoutRect RenderThemeIOS::adjustedPaintRect(const RenderBox& box, const LayoutRect& paintRect) const
{
// Workaround for <rdar://problem/6209763>. Force the painting bounds of checkboxes and radio controls to be square.
if (box.style().effectiveAppearance() == StyleAppearance::Checkbox || box.style().effectiveAppearance() == StyleAppearance::Radio) {
float width = std::min(paintRect.width(), paintRect.height());
float height = width;
return enclosingLayoutRect(FloatRect(paintRect.x(), paintRect.y() + (box.height() - height) / 2, width, height)); // Vertically center the checkbox, like on desktop
}
return paintRect;
}
int RenderThemeIOS::baselinePosition(const RenderBox& box) const
{
if (box.style().effectiveAppearance() == StyleAppearance::Checkbox || box.style().effectiveAppearance() == StyleAppearance::Radio)
return box.marginTop() + box.height() - 2; // The baseline is 2px up from the bottom of the checkbox/radio in AppKit.
if (box.style().effectiveAppearance() == StyleAppearance::Menulist)
return box.marginTop() + box.height() - 5; // This is to match AppKit. There might be a better way to calculate this though.
return RenderTheme::baselinePosition(box);
}
bool RenderThemeIOS::isControlStyled(const RenderStyle& style, const RenderStyle& userAgentStyle) const
{
// Buttons and MenulistButtons are styled if they contain a background image.
if (style.effectiveAppearance() == StyleAppearance::PushButton || style.effectiveAppearance() == StyleAppearance::MenulistButton)
return !style.visitedDependentColor(CSSPropertyBackgroundColor).isVisible() || style.backgroundLayers().hasImage();
if (style.effectiveAppearance() == StyleAppearance::TextField || style.effectiveAppearance() == StyleAppearance::TextArea || style.effectiveAppearance() == StyleAppearance::SearchField)
return !style.borderAndBackgroundEqual(userAgentStyle);
#if ENABLE(DATALIST_ELEMENT)
if (style.effectiveAppearance() == StyleAppearance::ListButton)
return style.hasContent() || style.hasEffectiveContentNone();
#endif
return RenderTheme::isControlStyled(style, userAgentStyle);
}
void RenderThemeIOS::adjustMinimumIntrinsicSizeForAppearance(StyleAppearance appearance, RenderStyle& style) const
{
auto minControlSize = Theme::singleton().minimumControlSize(appearance, style.fontCascade(), { style.minWidth(), style.minHeight() }, { style.width(), style.height() }, style.effectiveZoom());
if (minControlSize.width.value() > style.minWidth().value())
style.setMinWidth(WTFMove(minControlSize.width));
if (minControlSize.height.value() > style.minHeight().value())
style.setMinHeight(WTFMove(minControlSize.height));
}
void RenderThemeIOS::adjustRadioStyle(RenderStyle& style, const Element* element) const
{
adjustStyleForAlternateFormControlDesignTransition(style, element);
adjustMinimumIntrinsicSizeForAppearance(StyleAppearance::Radio, style);
if (!style.width().isIntrinsicOrAuto() && !style.height().isAuto())
return;
int size = std::max(style.computedFontPixelSize(), 10U);
style.setWidth({ size, LengthType::Fixed });
style.setHeight({ size, LengthType::Fixed });
style.setBorderRadius({ size / 2, size / 2 });
}
void RenderThemeIOS::paintRadioDecorations(const RenderObject& box, const PaintInfo& paintInfo, const IntRect& rect)
{
#if ENABLE(IOS_FORM_CONTROL_REFRESH)
if (box.settings().iOSFormControlRefreshEnabled())
return;
#endif
GraphicsContextStateSaver stateSaver(paintInfo.context());
CGContextRef cgContext = paintInfo.context().platformContext();
auto drawShadeAndShineGradients = [&](auto clip) {
FloatPoint bottomCenter(clip.x() + clip.width() / 2.0, clip.maxY());
drawAxialGradient(cgContext, gradientWithName(ShadeGradient), clip.location(), FloatPoint(clip.x(), clip.maxY()), LinearInterpolation);
drawRadialGradient(cgContext, gradientWithName(ShineGradient), bottomCenter, 0, bottomCenter, std::max(clip.width(), clip.height()), ExponentialInterpolation);
};
if (isChecked(box)) {
auto border = box.style().getRoundedBorderFor(rect);
paintInfo.context().fillRoundedRect(border.pixelSnappedRoundedRectForPainting(box.document().deviceScaleFactor()), Color::black.colorWithAlphaByte(204));
auto clip = addRoundedBorderClip(box, paintInfo.context(), rect);
drawAxialGradient(cgContext, gradientWithName(ConcaveGradient), clip.location(), FloatPoint(clip.x(), clip.maxY()), LinearInterpolation);
// The inner circle is 6 / 14 the size of the surrounding circle,
// leaving 8 / 14 around it. (8 / 14) / 2 = 2 / 7.
static const float InnerInverseRatio = 2 / 7.0;
clip.inflateX(-clip.width() * InnerInverseRatio);
clip.inflateY(-clip.height() * InnerInverseRatio);
constexpr auto shadowColor = Color::black.colorWithAlphaByte(179);
paintInfo.context().drawRaisedEllipse(clip, Color::white, shadowColor);
FloatSize radius(clip.width() / 2.0f, clip.height() / 2.0f);
paintInfo.context().clipRoundedRect(FloatRoundedRect(clip, radius, radius, radius, radius));
drawShadeAndShineGradients(clip);
} else {
auto clip = addRoundedBorderClip(box, paintInfo.context(), rect);
drawShadeAndShineGradients(clip);
}
}
void RenderThemeIOS::adjustTextFieldStyle(RenderStyle& style, const Element* element) const
{
if (!element)
return;
bool hasTextfieldAppearance = false;
// Do not force a background color for elements that have a textfield
// appearance by default, so that their background color can be styled.
if (is<HTMLInputElement>(*element)) {
auto& input = downcast<HTMLInputElement>(*element);
// <input type=search> is the only TextFieldInputType that has a
// non-textfield appearance value.
hasTextfieldAppearance = input.isTextField() && !input.isSearchField();
}
auto adjustBackgroundColor = [&] {
auto styleColorOptions = element->document().styleColorOptions(&style);
if (!style.backgroundColorEqualsToColorIgnoringVisited(systemColor(CSSValueAppleSystemOpaqueTertiaryFill, styleColorOptions)))
return;
style.setBackgroundColor(systemColor(CSSValueWebkitControlBackground, styleColorOptions));
};
bool useAlternateDesign = element->document().settings().alternateFormControlDesignEnabled();
if (useAlternateDesign) {
if (hasTextfieldAppearance)
style.setBackgroundColor(Color::transparentBlack);
else
adjustBackgroundColor();
style.resetBorderExceptRadius();
return;
}
if (hasTextfieldAppearance)
return;
adjustBackgroundColor();
}
void RenderThemeIOS::paintTextFieldInnerShadow(const PaintInfo& paintInfo, const FloatRoundedRect& roundedRect)
{
auto& context = paintInfo.context();
const FloatSize innerShadowOffset { 0, 5 };
constexpr auto innerShadowBlur = 10.0f;
auto innerShadowColor = DisplayP3<float> { 0, 0, 0, 0.04f };
context.setShadow(innerShadowOffset, innerShadowBlur, innerShadowColor);
context.setFillColor(Color::black);
Path innerShadowPath;
FloatRect innerShadowRect = roundedRect.rect();
innerShadowRect.inflate(std::max<float>(innerShadowOffset.width(), innerShadowOffset.height()) + innerShadowBlur);
innerShadowPath.addRect(innerShadowRect);
FloatRoundedRect innerShadowHoleRect = roundedRect;
// FIXME: This is not from the spec; but without it we get antialiasing fringe from the fill; we need a better solution.
innerShadowHoleRect.inflate(0.5);
innerShadowPath.addRoundedRect(innerShadowHoleRect);
context.setFillRule(WindRule::EvenOdd);
context.fillPath(innerShadowPath);
}
void RenderThemeIOS::paintTextFieldDecorations(const RenderBox& box, const PaintInfo& paintInfo, const FloatRect& rect)
{
auto& context = paintInfo.context();
GraphicsContextStateSaver stateSaver(context);
auto& style = box.style();
auto roundedRect = style.getRoundedBorderFor(LayoutRect(rect)).pixelSnappedRoundedRectForPainting(box.document().deviceScaleFactor());
#if ENABLE(IOS_FORM_CONTROL_REFRESH)
if (box.settings().iOSFormControlRefreshEnabled()) {
bool shouldPaintFillAndInnerShadow = false;
auto element = box.element();
if (is<HTMLInputElement>(*element)) {
auto& input = downcast<HTMLInputElement>(*element);
if (input.isTextField() && !input.isSearchField())
shouldPaintFillAndInnerShadow = true;
} else if (is<HTMLTextAreaElement>(*element))
shouldPaintFillAndInnerShadow = true;
bool useAlternateDesign = box.settings().alternateFormControlDesignEnabled();
if (useAlternateDesign && shouldPaintFillAndInnerShadow) {
Path path;
path.addRoundedRect(roundedRect);
context.setFillColor(Color::black.colorWithAlphaByte(10));
context.drawPath(path);
context.clipPath(path);
paintTextFieldInnerShadow(paintInfo, roundedRect);
}
return;
}
#endif
context.clipRoundedRect(roundedRect);
// This gradient gets drawn black when printing.
// Do not draw the gradient if there is no visible top border.
bool topBorderIsInvisible = !style.hasBorder() || !style.borderTopWidth() || style.borderTopIsTransparent();
if (!box.view().printing() && !topBorderIsInvisible) {
FloatPoint point(rect.x() + style.borderLeftWidth(), rect.y() + style.borderTopWidth());
drawAxialGradient(context.platformContext(), gradientWithName(InsetGradient), point, FloatPoint(CGPointMake(point.x(), point.y() + 3.0f)), LinearInterpolation);
}
}
void RenderThemeIOS::adjustTextAreaStyle(RenderStyle& style, const Element* element) const
{
if (!element)
return;
if (!element->document().settings().alternateFormControlDesignEnabled())
return;
style.setBackgroundColor(Color::transparentBlack);
style.resetBorderExceptRadius();
}
void RenderThemeIOS::paintTextAreaDecorations(const RenderBox& box, const PaintInfo& paintInfo, const FloatRect& rect)
{
paintTextFieldDecorations(box, paintInfo, rect);
}
// These values are taken from the UIKit button system.
constexpr auto largeButtonSize = 45;
constexpr auto largeButtonBorderRadiusRatio = 0.35f / 2;
const int MenuListMinHeight = 15;
const float MenuListBaseHeight = 20;
const float MenuListBaseFontSize = 11;
const float MenuListArrowWidth = 7;
const float MenuListArrowHeight = 6;
const float MenuListButtonPaddingAfter = 19;
LengthBox RenderThemeIOS::popupInternalPaddingBox(const RenderStyle& style, const Settings& settings) const
{
float padding = MenuListButtonPaddingAfter;
if (settings.iOSFormControlRefreshEnabled()) {
auto emSize = CSSPrimitiveValue::create(1.0, CSSUnitType::CSS_EMS);
padding = emSize->computeLength<float>({ style, nullptr, nullptr, nullptr });
}
if (style.effectiveAppearance() == StyleAppearance::MenulistButton) {
if (style.direction() == TextDirection::RTL)
return { 0, 0, 0, static_cast<int>(padding + style.borderTopWidth()) };
return { 0, static_cast<int>(padding + style.borderTopWidth()), 0, 0 };
}
return { 0, 0, 0, 0 };
}
static inline bool canAdjustBorderRadiusForAppearance(StyleAppearance appearance, const RenderBox& box)
{
switch (appearance) {
case StyleAppearance::None:
#if ENABLE(APPLE_PAY)
case StyleAppearance::ApplePayButton:
#endif
return false;
#if ENABLE(IOS_FORM_CONTROL_REFRESH)
case StyleAppearance::SearchField:
return !box.settings().iOSFormControlRefreshEnabled();
case StyleAppearance::MenulistButton:
return !box.style().hasExplicitlySetBorderRadius() && box.settings().iOSFormControlRefreshEnabled();
#endif
default:
return true;
};
}
void RenderThemeIOS::adjustRoundBorderRadius(RenderStyle& style, RenderBox& box)
{
if (!canAdjustBorderRadiusForAppearance(style.effectiveAppearance(), box) || style.backgroundLayers().hasImage())
return;
if ((is<RenderButton>(box) || is<RenderMenuList>(box)) && box.height() >= largeButtonSize) {
auto largeButtonBorderRadius = std::min(box.width(), box.height()) * largeButtonBorderRadiusRatio;
style.setBorderRadius({ { largeButtonBorderRadius, LengthType::Fixed }, { largeButtonBorderRadius, LengthType::Fixed } });
return;
}
// FIXME: We should not be relying on border radius for the appearance of our controls <rdar://problem/7675493>.
style.setBorderRadius({ { std::min(box.width(), box.height()) / 2, LengthType::Fixed }, { box.height() / 2, LengthType::Fixed } });
}
static void applyCommonButtonPaddingToStyle(RenderStyle& style, const Element& element)
{
Document& document = element.document();
auto emSize = CSSPrimitiveValue::create(0.5, CSSUnitType::CSS_EMS);
// We don't need this element's parent style to calculate `em` units, so it's okay to pass nullptr for it here.
int pixels = emSize->computeLength<int>({ style, document.renderStyle(), nullptr, document.renderView() });
style.setPaddingBox(LengthBox(0, pixels, 0, pixels));
}
static void adjustSelectListButtonStyle(RenderStyle& style, const Element& element)
{
// Enforce "padding: 0 0.5em".
applyCommonButtonPaddingToStyle(style, element);
// Enforce "line-height: normal".
style.setLineHeight(Length(-100.0, LengthType::Percent));
}
class RenderThemeMeasureTextClient : public MeasureTextClient {
public:
RenderThemeMeasureTextClient(const FontCascade& font, const RenderStyle& style)
: m_font(font)
, m_style(style)
{
}
float measureText(const String& string) const override
{
TextRun run = RenderBlock::constructTextRun(string, m_style);
return m_font.width(run);
}
private:
const FontCascade& m_font;
const RenderStyle& m_style;
};
static void adjustInputElementButtonStyle(RenderStyle& style, const HTMLInputElement& inputElement)
{
// Always Enforce "padding: 0 0.5em".
applyCommonButtonPaddingToStyle(style, inputElement);
// Don't adjust the style if the width is specified.
if (style.width().isFixed() && style.width().value() > 0)
return;
// Don't adjust for unsupported date input types.
DateComponentsType dateType = inputElement.dateType();
if (dateType == DateComponentsType::Invalid || dateType == DateComponentsType::Week)
return;
// Enforce the width and set the box-sizing to content-box to not conflict with the padding.
FontCascade font = style.fontCascade();
float maximumWidth = localizedDateCache().maximumWidthForDateType(dateType, font, RenderThemeMeasureTextClient(font, style));
ASSERT(maximumWidth >= 0);
if (maximumWidth > 0) {
int width = static_cast<int>(maximumWidth + MenuListButtonPaddingAfter);
#if ENABLE(IOS_FORM_CONTROL_REFRESH)
if (inputElement.document().settings().iOSFormControlRefreshEnabled())
width = static_cast<int>(std::ceil(maximumWidth));
#endif
style.setWidth(Length(width, LengthType::Fixed));
style.setBoxSizing(BoxSizing::ContentBox);
}
}
void RenderThemeIOS::adjustMenuListButtonStyle(RenderStyle& style, const Element* element) const
{
adjustStyleForAlternateFormControlDesignTransition(style, element);
// Set the min-height to be at least MenuListMinHeight.
if (style.height().isAuto())
style.setMinHeight(Length(std::max(MenuListMinHeight, static_cast<int>(MenuListBaseHeight / MenuListBaseFontSize * style.fontDescription().computedSize())), LengthType::Fixed));
else
style.setMinHeight(Length(MenuListMinHeight, LengthType::Fixed));
if (!element)
return;
adjustButtonLikeControlStyle(style, *element);
// Enforce some default styles in the case that this is a non-multiple <select> element,
// or a date input. We don't force these if this is just an element with
// "-webkit-appearance: menulist-button".
if (is<HTMLSelectElement>(*element) && !element->hasAttributeWithoutSynchronization(HTMLNames::multipleAttr))
adjustSelectListButtonStyle(style, *element);
else if (is<HTMLInputElement>(*element))
adjustInputElementButtonStyle(style, downcast<HTMLInputElement>(*element));
}
void RenderThemeIOS::paintMenuListButtonDecorations(const RenderBox& box, const PaintInfo& paintInfo, const FloatRect& rect)
{
#if ENABLE(IOS_FORM_CONTROL_REFRESH)
if (box.settings().iOSFormControlRefreshEnabled()) {
paintMenuListButtonDecorationsWithFormControlRefresh(box, paintInfo, rect);
return;
}
#endif
auto& style = box.style();
bool isRTL = style.direction() == TextDirection::RTL;
float borderTopWidth = style.borderTopWidth();
FloatRect clip(rect.x() + style.borderLeftWidth(), rect.y() + style.borderTopWidth(), rect.width() - style.borderLeftWidth() - style.borderRightWidth(), rect.height() - style.borderTopWidth() - style.borderBottomWidth());
CGContextRef cgContext = paintInfo.context().platformContext();
float adjustLeft = 0.5;
float adjustRight = 0.5;
float adjustTop = 0.5;
float adjustBottom = 0.5;
// Paint title portion.
{
float leftInset = isRTL ? MenuListButtonPaddingAfter : 0;
FloatRect titleClip(clip.x() + leftInset - adjustLeft, clip.y() - adjustTop, clip.width() - MenuListButtonPaddingAfter + adjustLeft, clip.height() + adjustTop + adjustBottom);
GraphicsContextStateSaver stateSaver(paintInfo.context());
FloatSize topLeftRadius;
FloatSize topRightRadius;
FloatSize bottomLeftRadius;
FloatSize bottomRightRadius;
if (isRTL) {
topRightRadius = FloatSize(valueForLength(style.borderTopRightRadius().width, rect.width()) - style.borderRightWidth(), valueForLength(style.borderTopRightRadius().height, rect.height()) - style.borderTopWidth());
bottomRightRadius = FloatSize(valueForLength(style.borderBottomRightRadius().width, rect.width()) - style.borderRightWidth(), valueForLength(style.borderBottomRightRadius().height, rect.height()) - style.borderBottomWidth());
} else {
topLeftRadius = FloatSize(valueForLength(style.borderTopLeftRadius().width, rect.width()) - style.borderLeftWidth(), valueForLength(style.borderTopLeftRadius().height, rect.height()) - style.borderTopWidth());
bottomLeftRadius = FloatSize(valueForLength(style.borderBottomLeftRadius().width, rect.width()) - style.borderLeftWidth(), valueForLength(style.borderBottomLeftRadius().height, rect.height()) - style.borderBottomWidth());
}
paintInfo.context().clipRoundedRect(FloatRoundedRect(titleClip,
topLeftRadius, topRightRadius,
bottomLeftRadius, bottomRightRadius));
drawAxialGradient(cgContext, gradientWithName(ShadeGradient), titleClip.location(), FloatPoint(titleClip.x(), titleClip.maxY()), LinearInterpolation);
drawAxialGradient(cgContext, gradientWithName(ShineGradient), FloatPoint(titleClip.x(), titleClip.maxY()), titleClip.location(), ExponentialInterpolation);
}
// Draw the separator after the initial padding.
float separatorPosition = isRTL ? (clip.x() + MenuListButtonPaddingAfter) : (clip.maxX() - MenuListButtonPaddingAfter);
BorderPainter::drawLineForBoxSide(paintInfo.context(), box.document(), FloatRect(FloatPoint(separatorPosition - borderTopWidth, clip.y()), FloatPoint(separatorPosition, clip.maxY())), BoxSide::Right, style.visitedDependentColor(CSSPropertyBorderTopColor), style.borderTopStyle(), 0, 0);
FloatRect buttonClip;
if (isRTL)
buttonClip = FloatRect(clip.x() - adjustTop, clip.y() - adjustTop, MenuListButtonPaddingAfter + adjustTop + adjustLeft, clip.height() + adjustTop + adjustBottom);
else
buttonClip = FloatRect(separatorPosition - adjustTop, clip.y() - adjustTop, MenuListButtonPaddingAfter + adjustTop + adjustRight, clip.height() + adjustTop + adjustBottom);
// Now paint the button portion.
{
GraphicsContextStateSaver stateSaver(paintInfo.context());
FloatSize topLeftRadius;
FloatSize topRightRadius;
FloatSize bottomLeftRadius;
FloatSize bottomRightRadius;
if (isRTL) {
topLeftRadius = FloatSize(valueForLength(style.borderTopLeftRadius().width, rect.width()) - style.borderLeftWidth(), valueForLength(style.borderTopLeftRadius().height, rect.height()) - style.borderTopWidth());
bottomLeftRadius = FloatSize(valueForLength(style.borderBottomLeftRadius().width, rect.width()) - style.borderLeftWidth(), valueForLength(style.borderBottomLeftRadius().height, rect.height()) - style.borderBottomWidth());
} else {
topRightRadius = FloatSize(valueForLength(style.borderTopRightRadius().width, rect.width()) - style.borderRightWidth(), valueForLength(style.borderTopRightRadius().height, rect.height()) - style.borderTopWidth());
bottomRightRadius = FloatSize(valueForLength(style.borderBottomRightRadius().width, rect.width()) - style.borderRightWidth(), valueForLength(style.borderBottomRightRadius().height, rect.height()) - style.borderBottomWidth());
}
paintInfo.context().clipRoundedRect(FloatRoundedRect(buttonClip,
topLeftRadius, topRightRadius,
bottomLeftRadius, bottomRightRadius));
paintInfo.context().fillRect(buttonClip, style.visitedDependentColor(CSSPropertyBorderTopColor));
drawAxialGradient(cgContext, gradientWithName(isFocused(box) && !isReadOnlyControl(box) ? ConcaveGradient : ConvexGradient), buttonClip.location(), FloatPoint(buttonClip.x(), buttonClip.maxY()), LinearInterpolation);
}
// Paint Indicators.
if (box.isMenuList() && downcast<HTMLSelectElement>(box.element())->multiple()) {
int size = 2;
int count = 3;
int padding = 3;
FloatRect ellipse(buttonClip.x() + (buttonClip.width() - count * (size + padding) + padding) / 2.0, buttonClip.maxY() - 10.0, size, size);
for (int i = 0; i < count; ++i) {
paintInfo.context().drawRaisedEllipse(ellipse, Color::white, Color::black.colorWithAlphaByte(128));
ellipse.move(size + padding, 0);
}
} else {
float centerX = floorf(buttonClip.x() + buttonClip.width() / 2.0) - 0.5;
float centerY = floorf(buttonClip.y() + buttonClip.height() * 3.0 / 8.0);
Vector<FloatPoint> arrow = {
{ centerX - MenuListArrowWidth / 2, centerY },
{ centerX + MenuListArrowWidth / 2, centerY },
{ centerX, centerY + MenuListArrowHeight }
};
Vector<FloatPoint> shadow = {
{ arrow[0].x(), arrow[0].y() + 1 },
{ arrow[1].x(), arrow[1].y() + 1 },
{ arrow[2].x(), arrow[2].y() + 1 }
};
uint8_t opacity = isReadOnlyControl(box) ? 51 : 128;
paintInfo.context().setStrokeColor(Color::black.colorWithAlphaByte(opacity));
paintInfo.context().setFillColor(Color::black.colorWithAlphaByte(opacity));
paintInfo.context().drawPath(Path::polygonPathFromPoints(shadow));
paintInfo.context().setStrokeColor(Color::white);
paintInfo.context().setFillColor(Color::white);
paintInfo.context().drawPath(Path::polygonPathFromPoints(arrow));
}
}
const CGFloat kTrackThickness = 4.0;
const CGFloat kTrackRadius = kTrackThickness / 2.0;
const int kDefaultSliderThumbSize = 16;
void RenderThemeIOS::adjustSliderTrackStyle(RenderStyle& style, const Element* element) const
{
RenderTheme::adjustSliderTrackStyle(style, element);
// FIXME: We should not be relying on border radius for the appearance of our controls <rdar://problem/7675493>.
int radius = static_cast<int>(kTrackRadius);
style.setBorderRadius({ { radius, LengthType::Fixed }, { radius, LengthType::Fixed } });
}
bool RenderThemeIOS::paintSliderTrack(const RenderObject& box, const PaintInfo& paintInfo, const IntRect& rect)
{
#if ENABLE(IOS_FORM_CONTROL_REFRESH)
if (box.settings().iOSFormControlRefreshEnabled())
return paintSliderTrackWithFormControlRefresh(box, paintInfo, rect);
#endif
IntRect trackClip = rect;
auto& style = box.style();
bool isHorizontal = true;
switch (style.effectiveAppearance()) {
case StyleAppearance::SliderHorizontal:
isHorizontal = true;
// Inset slightly so the thumb covers the edge.
if (trackClip.width() > 2) {
trackClip.setWidth(trackClip.width() - 2);
trackClip.setX(trackClip.x() + 1);
}
trackClip.setHeight(static_cast<int>(kTrackThickness));
trackClip.setY(rect.y() + rect.height() / 2 - kTrackThickness / 2);
break;
case StyleAppearance::SliderVertical:
isHorizontal = false;
// Inset slightly so the thumb covers the edge.