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/common/resource/resource_object.h" 20 #include "core/components_ng/base/common_configuration.h" 21 #include "core/components_ng/pattern/button/button_model.h" 22 #include "core/components/button/button_theme.h" 23 24 namespace OHOS::Ace::NG { 25 class ButtonConfiguration : public CommonConfiguration { 26 public: ButtonConfiguration(const std::string & label,bool pressed,bool enabled)27 ButtonConfiguration(const std::string& label, bool pressed, bool enabled) 28 : CommonConfiguration(enabled), label_(label), pressed_(pressed) 29 {} 30 std::string label_; 31 bool pressed_; 32 }; 33 using ButtonMakeCallback = 34 std::function<RefPtr<FrameNode>(const ButtonConfiguration& buttonConfiguration)>; 35 class ACE_EXPORT ButtonModelNG : public OHOS::Ace::ButtonModel { 36 public: 37 void SetFontSize(const Dimension& fontSize) override; 38 void SetFontWeight(const Ace::FontWeight& fontWeight) override; 39 void SetFontStyle(const Ace::FontStyle& fontStyle) override; 40 void SetFontFamily(const std::vector<std::string>& fontFamily) override; 41 void SetFontColor(const Color& textColor) override; 42 void SetType(const int value) override; 43 void SetStateEffect(const bool stateEffect) override; 44 void SetLabelStyle(ButtonParameters& buttonParameters) override; 45 void CreateWithLabel(const CreateWithPara& para, std::list<RefPtr<Component>>& buttonChildren) override; 46 void Create(const CreateWithPara& para, std::list<RefPtr<Component>>& buttonChildren) override; 47 void CreateWithChild(const CreateWithPara& para) override; 48 void Padding(const PaddingProperty& paddingNew, const Edge& paddingOld) override; 49 void OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc, double distanceThreshold) override; 50 void BackgroundColor(const Color& color, const bool& colorFlag) override; 51 void SetSize(const std::optional<Dimension>& width, const std::optional<Dimension>& height) override; 52 void SetBorderRadius(const Dimension& radius) override; 53 void SetBorderRadius(const std::optional<Dimension>& radiusTopLeft, const std::optional<Dimension>& radiusTopRight, 54 const std::optional<Dimension>& radiusBottomLeft, const std::optional<Dimension>& radiusBottomRight) override; 55 void SetLocalizedBorderRadius(const std::optional<Dimension>& radiusTopStart, 56 const std::optional<Dimension>& radiusTopEnd, const std::optional<Dimension>& radiusBottomStart, 57 const std::optional<Dimension>& radiusBottomEnd) override; 58 void ResetBorderRadius() override; 59 void SetButtonStyle(const std::optional<ButtonStyleMode>& buttonStyle) override; 60 void SetControlSize(const std::optional<ControlSize>& controlSize) override; 61 void SetRole(const std::optional<ButtonRole>& buttonRole) override; 62 void SetCreateWithLabel(bool createWithLabel) override; 63 void SetMinFontScale(float minFontScale) override; 64 void SetMaxFontScale(float maxFontScale) override; 65 void CreateWithColorResourceObj(const RefPtr<ResourceObject>& resObj, 66 const ButtonColorType buttonColorType) override; 67 void CreateWithStringResourceObj(const RefPtr<ResourceObject>& resObj, 68 const ButtonStringType buttonStringType) override; 69 void CreateWithDimensionFpResourceObj(const RefPtr<ResourceObject>& resObj, 70 const ButtonDimensionType buttonDimensionType) override; 71 void CreateWithDoubleResourceObj(const RefPtr<ResourceObject>& resObj, 72 const ButtonDoubleType buttonDoubleType) override; 73 static void UpdateResColor(FrameNode* frameNode, Color result, const ButtonColorType buttonColorType); 74 static void ParseButtonResColor( 75 const RefPtr<ResourceObject>& resObj, Color& result, const ButtonColorType buttonColorType); 76 void CreateWithFamiliesResourceObj(const RefPtr<ResourceObject>& resObj, 77 const ButtonStringType buttonStringType) override; 78 static void CreateWithColorResourceObj(FrameNode* frameNode, const RefPtr<ResourceObject>& resObj, 79 const ButtonColorType buttonColorType); 80 static void UpdateComponentFamilies( 81 FrameNode* frameNode, const std::vector<std::string>& value, const ButtonStringType buttonStringType); 82 static void UpdateDefaultFamilies( 83 FrameNode* frameNode, std::vector<std::string>& value, const ButtonStringType buttonStringType); 84 static void CreateWithFamiliesResourceObj(FrameNode* frameNode, const RefPtr<ResourceObject>& resObj, 85 const ButtonStringType buttonStringType); 86 static bool CheckFontScale(bool resultFlag, double result, const ButtonDoubleType buttonDoubleType); 87 static void CreateWithDoubleResourceObj(FrameNode* frameNode, const RefPtr<ResourceObject>& resObj, 88 const ButtonDoubleType buttonDoubleType); 89 static void CreateWithDimensionFpResourceObj(FrameNode* frameNode, const RefPtr<ResourceObject>& resObj, 90 const ButtonDimensionType buttonDimensionType); 91 static RefPtr<FrameNode> CreateFrameNode(int32_t nodeId); 92 static void SetFontSize(FrameNode* frameNode, const Dimension& fontSize); 93 static void SetFontWeight(FrameNode* frameNode, const Ace::FontWeight& fontWeight); 94 static void SetFontStyle(FrameNode* frameNode, const Ace::FontStyle& fontStyle); 95 static void SetFontFamily(FrameNode* frameNode, const std::vector<std::string>& fontFamily); 96 static void SetFontColor(FrameNode* frameNode, const Color& textColor); 97 static void SetType(FrameNode* frameNode, const int value); 98 static void SetStateEffect(FrameNode* frameNode, const bool stateEffect); 99 static void SetLabelStyle(FrameNode* frameNode, const ButtonParameters& buttonParameters); 100 static void BackgroundColor(FrameNode* frameNode, const Color& color, const bool& colorFlag); 101 static void SetBorderRadius(FrameNode* frameNode, const Dimension& radius); 102 static void SetBorderRadius(FrameNode* frameNode, const std::optional<Dimension>& radiusTopLeft, 103 const std::optional<Dimension>& radiusTopRight, const std::optional<Dimension>& radiusBottomLeft, 104 const std::optional<Dimension>& radiusBottomRight); 105 static void SetSize( 106 FrameNode* frameNode, const std::optional<Dimension>& width, const std::optional<Dimension>& height); 107 static void SetLabel(FrameNode* frameNode, const char* label); 108 static std::string GetLabel(FrameNode* frameNode); 109 static Dimension GetFontSize(FrameNode* frameNode); 110 static Ace::FontWeight GetFontWeight(FrameNode* frameNode); 111 static Color GetFontColor(FrameNode* frameNode); 112 static void SetRole(FrameNode* frameNode, const std::optional<ButtonRole>& buttonRole); 113 static void SetButtonStyle(FrameNode* frameNode, const std::optional<ButtonStyleMode>& buttonStyle); 114 static void SetBuilderFunc(FrameNode* frameNode, NG::ButtonMakeCallback&& jsMake); 115 static void TriggerClick(FrameNode* frameNode, double xPos, double yPos); 116 static void SetControlSize(FrameNode* frameNode, const std::optional<ControlSize>& controlSize); 117 static ButtonType GetType(FrameNode* frameNode); 118 static void ApplyTheme(FrameNode* frameNode, ButtonStyleMode buttonStyle, ButtonRole buttonRole); 119 static void SetLabelWithCheck(FrameNode* frameNode, const char* label); 120 static void SetCreateWithLabel(FrameNode* frameNode, bool createWithLabel); 121 static bool GetAutoDisable(FrameNode* frameNode); 122 static void SetMinFontScale(FrameNode* frameNode, float minFontScale); 123 static void SetMaxFontScale(FrameNode* frameNode, float maxFontScale); 124 static float GetMinFontScale(FrameNode* frameNode); 125 static float GetMaxFontScale(FrameNode* frameNode); 126 127 private: 128 static void CreateWithLabel(const std::string& label); 129 static void Create(const std::string& tagName); 130 static void SetTypeAndStateEffect(const std::optional<ButtonType>& type, const std::optional<bool>& stateEffect); 131 static void SetTextDefaultStyle(const RefPtr<FrameNode>& textNode, const std::string& label); 132 static void SetButtonSize(FrameNode* frameNode, const std::optional<ControlSize>& controlSize, 133 RefPtr<ButtonTheme> buttonTheme); 134 static std::string ColorTypeToString(const ButtonColorType buttonColorType); 135 static std::string StringTypeToStr(const ButtonStringType buttonStringType); 136 static std::string DimensionTypeToString(const ButtonDimensionType buttonDimensionType); 137 static std::string DoubleTypeToString(const ButtonDoubleType buttonDoubleType); 138 }; 139 } // namespace OHOS::Ace::NG 140 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_BUTTON_MODEL_NG_H 141