Add CSS -webkit-appearance property for Apple Pay buttons
[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 "PassKitSPI.h"
32 #include "RenderElement.h"
33 #include "RenderStyle.h"
34 #include "SoftLinking.h"
35 #include "TranslateTransformOperation.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 namespace WebCore {
46
47 static const auto applePayButtonMinimumWidth = 140;
48 static const auto applePayButtonPlainMinimumWidth = 100;
49 static const auto applePayButtonMinimumHeight = 30;
50
51 void RenderThemeCocoa::adjustApplePayButtonStyle(StyleResolver&, RenderStyle& style, const Element*) const
52 {
53     if (style.applePayButtonType() == ApplePayButtonType::Plain)
54         style.setMinWidth(Length(applePayButtonPlainMinimumWidth, Fixed));
55     else
56         style.setMinWidth(Length(applePayButtonMinimumWidth, Fixed));
57     style.setMinHeight(Length(applePayButtonMinimumHeight, Fixed));
58 }
59
60 static PKPaymentButtonStyle toPKPaymentButtonStyle(ApplePayButtonStyle style)
61 {
62     switch (style) {
63     case ApplePayButtonStyle::White:
64         return PKPaymentButtonStyleWhite;
65     case ApplePayButtonStyle::WhiteOutline:
66         return PKPaymentButtonStyleWhiteOutline;
67     case ApplePayButtonStyle::Black:
68         return PKPaymentButtonStyleBlack;
69     }
70 }
71
72 static PKPaymentButtonType toPKPaymentButtonType(ApplePayButtonType type)
73 {
74     switch (type) {
75     case ApplePayButtonType::Plain:
76         return PKPaymentButtonTypePlain;
77     case ApplePayButtonType::Buy:
78         return PKPaymentButtonTypeBuy;
79     case ApplePayButtonType::SetUp:
80         return PKPaymentButtonTypeSetUp;
81     case ApplePayButtonType::InStore:
82         return PKPaymentButtonTypeInStore;
83     }
84 }
85
86 bool RenderThemeCocoa::paintApplePayButton(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& paintRect)
87 {
88     if (!canLoadPKDrawApplePayButton())
89         return false;
90
91     GraphicsContextStateSaver stateSaver(paintInfo.context());
92
93     paintInfo.context().setShouldSmoothFonts(true);
94     paintInfo.context().scale(FloatSize(1, -1));
95
96     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());
97
98     return false;
99 }
100
101 }
102
103 #endif