1 /* 2 * Copyright (c) 2022-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_TOGGLE_BUTTON_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_TOGGLE_BUTTON_PATTERN_H 18 19 #include <optional> 20 21 #include "core/components/common/properties/color.h" 22 #include "core/components_ng/event/event_hub.h" 23 #include "core/components_ng/pattern/button/button_pattern.h" 24 #include "core/components_ng/pattern/button/toggle_button_accessibility_property.h" 25 #include "core/components_ng/pattern/button/toggle_button_event_hub.h" 26 #include "core/components_ng/pattern/button/toggle_button_paint_property.h" 27 #include "core/components_ng/pattern/toggle/toggle_model_ng.h" 28 29 namespace OHOS::Ace::NG { 30 class ToggleButtonPattern : public ButtonPattern { 31 DECLARE_ACE_TYPE(ToggleButtonPattern, ButtonPattern); 32 33 public: 34 ToggleButtonPattern() = default; 35 36 ~ToggleButtonPattern() override = default; 37 IsAtomicNode()38 bool IsAtomicNode() const override 39 { 40 return false; 41 } 42 UseContentModifier()43 bool UseContentModifier() const override 44 { 45 return contentModifierNode_ != nullptr; 46 } 47 CreateEventHub()48 RefPtr<EventHub> CreateEventHub() override 49 { 50 return MakeRefPtr<ToggleButtonEventHub>(); 51 } 52 CreatePaintProperty()53 RefPtr<PaintProperty> CreatePaintProperty() override 54 { 55 return MakeRefPtr<ToggleButtonPaintProperty>(); 56 } 57 CreateAccessibilityProperty()58 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 59 { 60 return MakeRefPtr<ToggleButtonAccessibilityProperty>(); 61 } 62 GetTouchListener()63 RefPtr<TouchEventImpl>& GetTouchListener() 64 { 65 return touchListener_; 66 } 67 68 void SetToggleBuilderFunc(SwitchMakeCallback&& toggleMakeFunc); 69 GetBuilderId()70 int32_t GetBuilderId() const override 71 { 72 return nodeId_; 73 } 74 std::string ProvideRestoreInfo() override; 75 void OnRestoreInfo(const std::string& restoreInfo) override; 76 void OnClick(); 77 void OnColorConfigurationUpdate() override; 78 void MarkIsSelected(bool isSelected); 79 void SetButtonPress(bool value); 80 81 private: 82 void OnAttachToFrameNode() override; 83 void InitParameters(); 84 void OnModifyDone() override; 85 void OnAfterModifyDone() override; 86 void InitClickEvent(); 87 void InitButtonAndText(); 88 void InitOnKeyEvent(); 89 bool OnKeyEvent(const KeyEvent& event); 90 void SetAccessibilityAction(); 91 void UpdateSelectStatus(bool isSelected); 92 void InitTouchEvent(); 93 void OnTouchDown(); 94 void OnTouchUp(); 95 void FireBuilder(); 96 97 RefPtr<FrameNode> BuildContentModifierNode(); 98 std::optional<SwitchMakeCallback> toggleMakeFunc_; 99 RefPtr<FrameNode> contentModifierNode_; 100 RefPtr<TouchEventImpl> touchListener_; 101 int32_t nodeId_ = -1; 102 103 RefPtr<ClickEvent> clickListener_; 104 std::optional<bool> isOn_; 105 Color checkedColor_; 106 Color unCheckedColor_; 107 Color backgroundColor_; 108 float disabledAlpha_ { 1.0f }; 109 Dimension textMargin_; 110 Dimension buttonMargin_; 111 Dimension buttonHeight_; 112 Dimension buttonRadius_; 113 Dimension textFontSize_; 114 Color textColor_; 115 bool isPress_ = false; 116 bool isSetClickedColor_ = false; 117 bool IsNeedToHandleHoverOpacity(); 118 119 ACE_DISALLOW_COPY_AND_MOVE(ToggleButtonPattern); 120 }; 121 } // namespace OHOS::Ace::NG 122 123 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_TOGGLE_BUTTON_PATTERN_H 124