1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_BUTTON_MODEL_NG_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_BUTTON_MODEL_NG_H
18 
19 #include "core/components_ng/base/common_configuration.h"
20 #include "core/components_ng/pattern/button/button_model.h"
21 
22 namespace OHOS::Ace::NG {
23 class ButtonConfiguration : public CommonConfiguration {
24     public:
ButtonConfiguration(const std::string & label,bool pressed,bool enabled)25         ButtonConfiguration(const std::string& label, bool pressed, bool enabled)
26             : CommonConfiguration(enabled), label_(label), pressed_(pressed)
27         {}
28         std::string label_;
29         bool pressed_;
30 };
31 using ButtonMakeCallback =
32     std::function<RefPtr<FrameNode>(const ButtonConfiguration& buttonConfiguration)>;
33 class ACE_EXPORT ButtonModelNG : public OHOS::Ace::ButtonModel {
34 public:
35     void SetFontSize(const Dimension& fontSize) override;
36     void SetFontWeight(const Ace::FontWeight& fontWeight) override;
37     void SetFontStyle(const Ace::FontStyle& fontStyle) override;
38     void SetFontFamily(const std::vector<std::string>& fontFamily) override;
39     void SetFontColor(const Color& textColor) override;
40     void SetType(const int value) override;
41     void SetStateEffect(const bool stateEffect) override;
42     void SetLabelStyle(ButtonParameters& buttonParameters) override;
43     void CreateWithLabel(const CreateWithPara& para, std::list<RefPtr<Component>>& buttonChildren) override;
44     void Create(const CreateWithPara& para, std::list<RefPtr<Component>>& buttonChildren) override;
45     void CreateWithChild(const CreateWithPara& para) override;
46     void Padding(const PaddingProperty& paddingNew, const Edge& paddingOld) override;
47     void OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc) override;
48     void BackgroundColor(const Color& color, const bool& colorFlag) override;
49     void SetSize(const std::optional<Dimension>& width, const std::optional<Dimension>& height) override;
50     void SetBorderRadius(const Dimension& radius) override;
51     void SetBorderRadius(const std::optional<Dimension>& radiusTopLeft, const std::optional<Dimension>& radiusTopRight,
52         const std::optional<Dimension>& radiusBottomLeft, const std::optional<Dimension>& radiusBottomRight) override;
53     void ResetBorderRadius() override;
54     void SetButtonStyle(const std::optional<ButtonStyleMode>& buttonStyle) override;
55     void SetControlSize(const std::optional<ControlSize>& controlSize) override;
56     void SetRole(const std::optional<ButtonRole>& buttonRole) override;
57     void SetCreateWithLabel(bool createWithLabel) override;
58     static RefPtr<FrameNode> CreateFrameNode(int32_t nodeId);
59     static void SetFontSize(FrameNode* frameNode, const Dimension& fontSize);
60     static void SetFontWeight(FrameNode* frameNode, const Ace::FontWeight& fontWeight);
61     static void SetFontStyle(FrameNode* frameNode, const Ace::FontStyle& fontStyle);
62     static void SetFontFamily(FrameNode* frameNode, const std::vector<std::string>& fontFamily);
63     static void SetFontColor(FrameNode* frameNode, const Color& textColor);
64     static void SetType(FrameNode* frameNode, const int value);
65     static void SetStateEffect(FrameNode* frameNode, const bool stateEffect);
66     static void SetLabelStyle(FrameNode* frameNode, const ButtonParameters& buttonParameters);
67     static void BackgroundColor(FrameNode* frameNode, const Color& color, const bool& colorFlag);
68     static void SetBorderRadius(FrameNode* frameNode, const Dimension& radius);
69     static void SetBorderRadius(FrameNode* frameNode, const std::optional<Dimension>& radiusTopLeft,
70         const std::optional<Dimension>& radiusTopRight, const std::optional<Dimension>& radiusBottomLeft,
71         const std::optional<Dimension>& radiusBottomRight);
72     static void SetSize(
73         FrameNode* frameNode, const std::optional<Dimension>& width, const std::optional<Dimension>& height);
74     static void SetLabel(FrameNode* frameNode, const char* label);
75     static std::string GetLabel(FrameNode* frameNode);
76     static Dimension GetFontSize(FrameNode* frameNode);
77     static Ace::FontWeight GetFontWeight(FrameNode* frameNode);
78     static Color GetFontColor(FrameNode* frameNode);
79     static void SetRole(FrameNode* frameNode, const std::optional<ButtonRole>& buttonRole);
80     static void SetButtonStyle(FrameNode* frameNode, const std::optional<ButtonStyleMode>& buttonStyle);
81     static void SetBuilderFunc(FrameNode* frameNode, NG::ButtonMakeCallback&& jsMake);
82     static void TriggerClick(FrameNode* frameNode, double xPos, double yPos);
83     static void SetControlSize(FrameNode* frameNode, const std::optional<ControlSize>& controlSize);
84     static ButtonType GetType(FrameNode* frameNode);
85     static void ApplyTheme(FrameNode* frameNode, ButtonStyleMode buttonStyle, ButtonRole buttonRole);
86     static void SetLabelWithCheck(FrameNode* frameNode, const char* label);
87     static void SetCreateWithLabel(FrameNode* frameNode, bool createWithLabel);
88 
89 private:
90     static void CreateWithLabel(const std::string& label);
91     static void Create(const std::string& tagName);
92     static void SetTypeAndStateEffect(const std::optional<ButtonType>& type, const std::optional<bool>& stateEffect);
93     static void SetTextDefaultStyle(const RefPtr<FrameNode>& textNode, const std::string& label);
94 };
95 } // namespace OHOS::Ace::NG
96 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_BUTTON_MODEL_NG_H
97