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("type", ConvertButtonTypeToString(layoutProperty->GetType().value_or(ButtonType::CAPSULE)).c_str()); 93 json->Put("fontSize", layoutProperty->GetFontSizeValue(Dimension(0)).ToString().c_str()); 94 json->Put("fontWeight", 95 V2::ConvertWrapFontWeightToStirng(layoutProperty->GetFontWeight().value_or(FontWeight::NORMAL)).c_str()); 96 json->Put("fontColor", layoutProperty->GetFontColor().value_or(Color::BLACK).ColorToString().c_str()); 97 auto fontFamilyVector = 98 layoutProperty->GetFontFamily().value_or<std::vector<std::string>>({ "HarmonyOS Sans" }); 99 std::string fontFamily = fontFamilyVector.at(0); 100 for (uint32_t i = 1; i < fontFamilyVector.size(); ++i) { 101 fontFamily += ',' + fontFamilyVector.at(i); 102 } 103 json->Put("fontFamily", fontFamily.c_str()); 104 json->Put("fontStyle", layoutProperty->GetFontStyle().value_or(Ace::FontStyle::NORMAL) == Ace::FontStyle::NORMAL 105 ? "FontStyle.Normal" 106 : "FontStyle.Italic"); 107 json->Put("label", layoutProperty->GetLabelValue("").c_str()); 108 auto eventHub = host->GetEventHub<ButtonEventHub>(); 109 CHECK_NULL_VOID(eventHub); 110 json->Put("stateEffect", eventHub->GetStateEffect() ? "true" : "false"); 111 auto optionJson = JsonUtil::Create(true); 112 optionJson->Put( 113 "type", ConvertButtonTypeToString(layoutProperty->GetType().value_or(ButtonType::CAPSULE)).c_str()); 114 optionJson->Put("stateEffect", eventHub->GetStateEffect() ? "true" : "false"); 115 json->Put("options", optionJson->ToString().c_str()); 116 } 117 ConvertButtonTypeToString(ButtonType buttonType)118 static std::string ConvertButtonTypeToString(ButtonType buttonType) 119 { 120 std::string result; 121 switch (buttonType) { 122 case ButtonType::NORMAL: 123 result = "ButtonType.Normal"; 124 break; 125 case ButtonType::CAPSULE: 126 result = "ButtonType.Capsule"; 127 break; 128 case ButtonType::CIRCLE: 129 result = "ButtonType.Circle"; 130 break; 131 default: 132 LOGD("The input does not match any ButtonType"); 133 } 134 return result; 135 } 136 137 protected: 138 void OnModifyDone() override; 139 void OnAttachToFrameNode() override; 140 void InitTouchEvent(); 141 void OnTouchDown(); 142 void OnTouchUp(); 143 void HandleEnabled(); 144 void InitButtonLabel(); 145 void AnimateTouchEffectBoard(float startOpacity, float endOpacity, int32_t duration, const RefPtr<Curve>& curve); 146 Color clickedColor_; 147 148 private: 149 static void SetDefaultAttributes(const RefPtr<FrameNode>& buttonNode, const RefPtr<PipelineBase>& pipeline); 150 151 Color backgroundColor_; 152 Color FocusBorderColor_; 153 bool isSetClickedColor_ = false; 154 ComponentButtonType buttonType_ = ComponentButtonType::BUTTON; 155 RefPtr<TouchEventImpl> touchListener_; 156 RefPtr<InputEvent> mouseEvent_; 157 158 ACE_DISALLOW_COPY_AND_MOVE(ButtonPattern); 159 }; 160 } // namespace OHOS::Ace::NG 161 162 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_BUTTON_PATTERN_H 163