Combine event and touch action regions into a single class
[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 #import "config.h"
27 #import "RenderThemeCocoa.h"
28
29 #import "GraphicsContextCG.h"
30 #import "HTMLInputElement.h"
31 #import "RenderText.h"
32
33 #if ENABLE(APPLE_PAY)
34
35 #import <pal/cocoa/PassKitSoftLink.h>
36
37 #endif // ENABLE(APPLE_PAY)
38
39 #if ENABLE(VIDEO)
40 #import "LocalizedStrings.h"
41 #import <wtf/BlockObjCExceptions.h>
42 #endif
43
44 namespace WebCore {
45
46 bool RenderThemeCocoa::shouldHaveCapsLockIndicator(const HTMLInputElement& element) const
47 {
48     return element.isPasswordField();
49 }
50
51 #if ENABLE(APPLE_PAY)
52
53 static const auto applePayButtonMinimumWidth = 140;
54 static const auto applePayButtonPlainMinimumWidth = 100;
55 static const auto applePayButtonMinimumHeight = 30;
56
57 void RenderThemeCocoa::adjustApplePayButtonStyle(StyleResolver&, RenderStyle& style, const Element*) const
58 {
59     if (style.applePayButtonType() == ApplePayButtonType::Plain)
60         style.setMinWidth(Length(applePayButtonPlainMinimumWidth, Fixed));
61     else
62         style.setMinWidth(Length(applePayButtonMinimumWidth, Fixed));
63     style.setMinHeight(Length(applePayButtonMinimumHeight, Fixed));
64 }
65
66 static PKPaymentButtonStyle toPKPaymentButtonStyle(ApplePayButtonStyle style)
67 {
68     switch (style) {
69     case ApplePayButtonStyle::White:
70         return PKPaymentButtonStyleWhite;
71     case ApplePayButtonStyle::WhiteOutline:
72         return PKPaymentButtonStyleWhiteOutline;
73     case ApplePayButtonStyle::Black:
74         return PKPaymentButtonStyleBlack;
75     }
76 }
77
78 static PKPaymentButtonType toPKPaymentButtonType(ApplePayButtonType type)
79 {
80     switch (type) {
81     case ApplePayButtonType::Plain:
82         return PKPaymentButtonTypePlain;
83     case ApplePayButtonType::Buy:
84         return PKPaymentButtonTypeBuy;
85     case ApplePayButtonType::SetUp:
86         return PKPaymentButtonTypeSetUp;
87     case ApplePayButtonType::Donate:
88         return PKPaymentButtonTypeDonate;
89 #if ENABLE(APPLE_PAY_SESSION_V4)
90     case ApplePayButtonType::CheckOut:
91         return PKPaymentButtonTypeCheckout;
92     case ApplePayButtonType::Book:
93         return PKPaymentButtonTypeBook;
94     case ApplePayButtonType::Subscribe:
95         return PKPaymentButtonTypeSubscribe;
96 #endif
97     }
98 }
99
100 bool RenderThemeCocoa::paintApplePayButton(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& paintRect)
101 {
102     GraphicsContextStateSaver stateSaver(paintInfo.context());
103
104     paintInfo.context().setShouldSmoothFonts(true);
105     paintInfo.context().scale(FloatSize(1, -1));
106
107     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());
108
109     return false;
110 }
111
112 #endif // ENABLE(APPLE_PAY)
113
114 #if ENABLE(VIDEO)
115
116 String RenderThemeCocoa::mediaControlsFormattedStringForDuration(const double durationInSeconds)
117 {
118 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
119     if (!std::isfinite(durationInSeconds))
120         return WEB_UI_STRING("indefinite time", "accessibility help text for an indefinite media controller time value");
121
122     BEGIN_BLOCK_OBJC_EXCEPTIONS;
123     if (!m_durationFormatter) {
124         m_durationFormatter = adoptNS([NSDateComponentsFormatter new]);
125         m_durationFormatter.get().unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
126         m_durationFormatter.get().allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
127         m_durationFormatter.get().formattingContext = NSFormattingContextStandalone;
128         m_durationFormatter.get().maximumUnitCount = 2;
129     }
130     return [m_durationFormatter.get() stringFromTimeInterval:durationInSeconds];
131     END_BLOCK_OBJC_EXCEPTIONS;
132 #else
133     return emptyString();
134 #endif
135 }
136
137 #endif // ENABLE(VIDEO)
138
139 }