1 /* 2 * Copyright (c) 2022 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_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_BUTTON_PATTERN_H 18 19 #include <optional> 20 21 #include "core/components/common/layout/constants.h" 22 #include "core/components_ng/event/event_hub.h" 23 #include "core/components_ng/pattern/button/button_accessibility_property.h" 24 #include "core/components_ng/pattern/button/button_event_hub.h" 25 #include "core/components_ng/pattern/button/button_layout_algorithm.h" 26 #include "core/components_ng/pattern/button/button_layout_property.h" 27 #include "core/components_ng/pattern/pattern.h" 28 29 namespace OHOS::Ace::NG { 30 enum class ComponentButtonType { POPUP, BUTTON }; 31 class ButtonPattern : public Pattern { 32 DECLARE_ACE_TYPE(ButtonPattern, Pattern); 33 34 public: 35 ButtonPattern() = default; 36 37 ~ButtonPattern() override = default; 38 IsAtomicNode()39 bool IsAtomicNode() const override 40 { 41 return false; 42 } 43 CreateEventHub()44 RefPtr<EventHub> CreateEventHub() override 45 { 46 return MakeRefPtr<ButtonEventHub>(); 47 } 48 CreateLayoutAlgorithm()49 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 50 { 51 return MakeRefPtr<ButtonLayoutAlgorithm>(); 52 } 53 CreateLayoutProperty()54 RefPtr<LayoutProperty> CreateLayoutProperty() override 55 { 56 return MakeRefPtr<ButtonLayoutProperty>(); 57 } 58 CreateAccessibilityProperty()59 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 60 { 61 return MakeRefPtr<ButtonAccessibilityProperty>(); 62 } 63 GetFocusPattern()64 FocusPattern GetFocusPattern() const override 65 { 66 return { FocusType::NODE, true, FocusStyleType::OUTER_BORDER }; 67 } 68 SetClickedColor(const Color & color)69 void SetClickedColor(const Color& color) 70 { 71 clickedColor_ = color; 72 isSetClickedColor_ = true; 73 } 74 SetFocusBorderColor(const Color & color)75 void SetFocusBorderColor(const Color& color) 76 { 77 FocusBorderColor_ = color; 78 } 79 setComponentButtonType(const ComponentButtonType & buttonType)80 void setComponentButtonType(const ComponentButtonType& buttonType) 81 { 82 buttonType_ = buttonType; 83 } 84 ToJsonValue(std::unique_ptr<JsonValue> & json)85 void ToJsonValue(std::unique_ptr<JsonValue>& json) const override 86 { 87 Pattern::ToJsonValue(json); 88 auto host = GetHost(); 89 CHECK_NULL_VOID(host); 90 auto layoutProperty = host->GetLayoutProperty<ButtonLayoutProperty>(); 91 CHECK_NULL_VOID(layoutProperty); 92 json->Put( 93 "type", host->GetTag() == "Toggle" 94 ? "ToggleType.Button" 95 : ConvertButtonTypeToString(layoutProperty->GetType().value_or(ButtonType::CAPSULE)).c_str()); 96 json->Put("fontSize", layoutProperty->GetFontSizeValue(Dimension(0)).ToString().c_str()); 97 json->Put("fontWeight", 98 V2::ConvertWrapFontWeightToStirng(layoutProperty->GetFontWeight().value_or(FontWeight::NORMAL)).c_str()); 99 json->Put("fontColor", layoutProperty->GetFontColor().value_or(Color::BLACK).ColorToString().c_str()); 100 auto fontFamilyVector = 101 layoutProperty->GetFontFamily().value_or<std::vector<std::string>>({ "HarmonyOS Sans" }); 102 std::string fontFamily = fontFamilyVector.at(0); 103 for (uint32_t i = 1; i < fontFamilyVector.size(); ++i) { 104 fontFamily += ',' + fontFamilyVector.at(i); 105 } 106 json->Put("fontFamily", fontFamily.c_str()); 107 json->Put("fontStyle", layoutProperty->GetFontStyle().value_or(Ace::FontStyle::NORMAL) == Ace::FontStyle::NORMAL 108 ? "FontStyle.Normal" 109 : "FontStyle.Italic"); 110 json->Put("label", layoutProperty->GetLabelValue("").c_str()); 111 auto eventHub = host->GetEventHub<ButtonEventHub>(); 112 CHECK_NULL_VOID(eventHub); 113 json->Put("stateEffect", eventHub->GetStateEffect() ? "true" : "false"); 114 auto optionJson = JsonUtil::Create(true); 115 optionJson->Put( 116 "type", ConvertButtonTypeToString(layoutProperty->GetType().value_or(ButtonType::CAPSULE)).c_str()); 117 optionJson->Put("stateEffect", eventHub->GetStateEffect() ? "true" : "false"); 118 json->Put("options", optionJson->ToString().c_str()); 119 } 120 ConvertButtonTypeToString(ButtonType buttonType)121 static std::string ConvertButtonTypeToString(ButtonType buttonType) 122 { 123 std::string result; 124 switch (buttonType) { 125 case ButtonType::NORMAL: 126 result = "ButtonType.Normal"; 127 break; 128 case ButtonType::CAPSULE: 129 result = "ButtonType.Capsule"; 130 break; 131 case ButtonType::CIRCLE: 132 result = "ButtonType.Circle"; 133 break; 134 default: 135 LOGD("The input does not match any ButtonType"); 136 } 137 return result; 138 } 139 140 protected: 141 void OnModifyDone() override; 142 void OnAttachToFrameNode() override; 143 void InitTouchEvent(); 144 void OnTouchDown(); 145 void OnTouchUp(); 146 void HandleEnabled(); 147 void InitButtonLabel(); 148 void AnimateTouchEffectBoard(float startOpacity, float endOpacity, int32_t duration, const RefPtr<Curve>& curve); 149 Color clickedColor_; 150 151 private: 152 static void SetDefaultAttributes(const RefPtr<FrameNode>& buttonNode, const RefPtr<PipelineBase>& pipeline); 153 154 Color backgroundColor_; 155 Color FocusBorderColor_; 156 bool isSetClickedColor_ = false; 157 ComponentButtonType buttonType_ = ComponentButtonType::BUTTON; 158 RefPtr<TouchEventImpl> touchListener_; 159 RefPtr<InputEvent> mouseEvent_; 160 161 ACE_DISALLOW_COPY_AND_MOVE(ButtonPattern); 162 }; 163 } // namespace OHOS::Ace::NG 164 165 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_BUTTON_PATTERN_H 166