df2c731d0896761d27742dd93ee6e1313ddfd291
[WebKit-https.git] / Source / WebCore / rendering / RenderThemeCocoa.mm
1 /*
2  * Copyright (C) 2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "RenderThemeCocoa.h"
28
29 #if ENABLE(APPLE_PAY)
30
31 #include "RenderElement.h"
32 #include "RenderStyle.h"
33 #include "TranslateTransformOperation.h"
34 #include <pal/spi/cocoa/PassKitSPI.h>
35 #include <wtf/SoftLinking.h>
36
37 #if PLATFORM(MAC)
38 SOFT_LINK_PRIVATE_FRAMEWORK(PassKit);
39 #else
40 SOFT_LINK_FRAMEWORK(PassKit);
41 #endif
42
43 SOFT_LINK_MAY_FAIL(PassKit, PKDrawApplePayButton, void, (CGContextRef context, CGRect drawRect, CGFloat scale, PKPaymentButtonType type, PKPaymentButtonStyle style, NSString *languageCode), (context, drawRect, scale, type, style, languageCode));
44
45 #endif // ENABLE(APPLE_PAY)
46
47 #if ENABLE(VIDEO)
48 #include "LocalizedStrings.h"
49 #include <wtf/BlockObjCExceptions.h>
50 #endif
51
52 namespace WebCore {
53
54 #if ENABLE(APPLE_PAY)
55
56 static const auto applePayButtonMinimumWidth = 140;
57 static const auto applePayButtonPlainMinimumWidth = 100;
58 static const auto applePayButtonMinimumHeight = 30;
59
60 void RenderThemeCocoa::adjustApplePayButtonStyle(StyleResolver&, RenderStyle& style, const Element*) const
61 {
62     if (style.applePayButtonType() == ApplePayButtonType::Plain)
63         style.setMinWidth(Length(applePayButtonPlainMinimumWidth, Fixed));
64     else
65         style.setMinWidth(Length(applePayButtonMinimumWidth, Fixed));
66     style.setMinHeight(Length(applePayButtonMinimumHeight, Fixed));
67 }
68
69 static PKPaymentButtonStyle toPKPaymentButtonStyle(ApplePayButtonStyle style)
70 {
71     switch (style) {
72     case ApplePayButtonStyle::White:
73         return PKPaymentButtonStyleWhite;
74     case ApplePayButtonStyle::WhiteOutline:
75         return PKPaymentButtonStyleWhiteOutline;
76     case ApplePayButtonStyle::Black:
77         return PKPaymentButtonStyleBlack;
78     }
79 }
80
81 static PKPaymentButtonType toPKPaymentButtonType(ApplePayButtonType type)
82 {
83     switch (type) {
84     case ApplePayButtonType::Plain:
85         return PKPaymentButtonTypePlain;
86     case ApplePayButtonType::Buy:
87         return PKPaymentButtonTypeBuy;
88     case ApplePayButtonType::SetUp:
89         return PKPaymentButtonTypeSetUp;
90     case ApplePayButtonType::Donate:
91         // FIXME: Use a named constant here.
92         return (PKPaymentButtonType)4;
93     }
94 }
95
96 bool RenderThemeCocoa::paintApplePayButton(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& paintRect)
97 {
98     if (!canLoadPKDrawApplePayButton())
99         return false;
100
101     GraphicsContextStateSaver stateSaver(paintInfo.context());
102
103     paintInfo.context().setShouldSmoothFonts(true);
104     paintInfo.context().scale(FloatSize(1, -1));
105
106     PKDrawApplePayButton(paintInfo.context().platformContext(), CGRectMake(paintRect.x(), -paintRect.maxY(), paintRect.width(), paintRect.height()), 1.0, toPKPaymentButtonType(renderer.style().applePayButtonType()), toPKPaymentButtonStyle(renderer.style().applePayButtonStyle()), renderer.style().locale());
107
108     return false;
109 }
110
111 #endif // ENABLE(APPLE_PAY)
112
113 #if ENABLE(VIDEO)
114
115 String RenderThemeCocoa::mediaControlsFormattedStringForDuration(const double durationInSeconds)
116 {
117 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
118     if (!std::isfinite(durationInSeconds))
119         return WEB_UI_STRING("indefinite time", "accessibility help text for an indefinite media controller time value");
120
121     BEGIN_BLOCK_OBJC_EXCEPTIONS;
122     if (!m_durationFormatter) {
123         m_durationFormatter = adoptNS([NSDateComponentsFormatter new]);
124         m_durationFormatter.get().unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
125         m_durationFormatter.get().allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
126         m_durationFormatter.get().formattingContext = NSFormattingContextStandalone;
127         m_durationFormatter.get().maximumUnitCount = 2;
128     }
129     return [m_durationFormatter.get() stringFromTimeInterval:durationInSeconds];
130     END_BLOCK_OBJC_EXCEPTIONS;
131 #else
132     return emptyString();
133 #endif
134 }
135
136 #endif // ENABLE(VIDEO)
137
138 }