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_SWITCH_SWITCH_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_PATTERN_H 18 19 #include "base/geometry/axis.h" 20 #include "base/geometry/size.h" 21 #include "base/memory/referenced.h" 22 #include "core/components/checkable/checkable_theme.h" 23 #include "core/components_ng/base/frame_node.h" 24 #include "core/components_ng/event/event_hub.h" 25 #include "core/components_ng/pattern/pattern.h" 26 #include "core/components_ng/pattern/toggle/switch_accessibility_property.h" 27 #include "core/components_ng/pattern/toggle/switch_event_hub.h" 28 #include "core/components_ng/pattern/toggle/switch_layout_algorithm.h" 29 #include "core/components_ng/pattern/toggle/switch_paint_method.h" 30 #include "core/components_ng/pattern/toggle/switch_paint_property.h" 31 #include "core/components_ng/pattern/toggle/toggle_model_ng.h" 32 33 namespace OHOS::Ace::NG { 34 35 class SwitchPattern : public Pattern { 36 DECLARE_ACE_TYPE(SwitchPattern, Pattern); 37 38 public: 39 SwitchPattern() = default; 40 41 ~SwitchPattern() override = default; 42 IsAtomicNode()43 bool IsAtomicNode() const override 44 { 45 return false; 46 } 47 CreateEventHub()48 RefPtr<EventHub> CreateEventHub() override 49 { 50 return MakeRefPtr<SwitchEventHub>(); 51 } 52 CreateLayoutAlgorithm()53 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 54 { 55 return MakeRefPtr<SwitchLayoutAlgorithm>(); 56 } 57 CreatePaintProperty()58 RefPtr<PaintProperty> CreatePaintProperty() override 59 { 60 return MakeRefPtr<SwitchPaintProperty>(); 61 } 62 CreateNodePaintMethod()63 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 64 { 65 auto host = GetHost(); 66 CHECK_NULL_RETURN(host, nullptr); 67 if (!paintMethod_) { 68 paintMethod_ = MakeRefPtr<SwitchPaintMethod>(); 69 } 70 paintMethod_->SetUseContentModifier(UseContentModifier()); 71 paintMethod_->SetDirection(direction_); 72 paintMethod_->SetIsSelect(isOn_.value_or(false)); 73 paintMethod_->SetEnabled(enabled_); 74 paintMethod_->SetDragOffsetX(dragOffsetX_); 75 paintMethod_->SetTouchHoverAnimationType(touchHoverType_); 76 paintMethod_->SetIsDragEvent(isDragEvent_); 77 paintMethod_->SetShowHoverEffect(showHoverEffect_); 78 paintMethod_->SetUseContentModifier(UseContentModifier()); 79 return paintMethod_; 80 } 81 CreateAccessibilityProperty()82 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 83 { 84 return MakeRefPtr<SwitchAccessibilityProperty>(); 85 } 86 GetFocusPattern()87 FocusPattern GetFocusPattern() const override 88 { 89 FocusPaintParam focusPaintParams; 90 91 auto pipelineContext = PipelineBase::GetCurrentContext(); 92 CHECK_NULL_RETURN(pipelineContext, FocusPattern()); 93 auto switchTheme = pipelineContext->GetTheme<SwitchTheme>(); 94 CHECK_NULL_RETURN(switchTheme, FocusPattern()); 95 auto focusPaintcolor = switchTheme->GetActiveColor(); 96 focusPaintParams.SetPaintColor(focusPaintcolor); 97 focusPaintParams.SetFocusPadding(Dimension(2.0_vp)); 98 99 return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION, focusPaintParams }; 100 } 101 IsChecked()102 bool IsChecked() 103 { 104 return isOn_.value_or(false); 105 } 106 107 void MarkIsSelected(bool isSelected); 108 SetIsUserSetResponseRegion(bool isUserSetResponseRegion)109 void SetIsUserSetResponseRegion(bool isUserSetResponseRegion) 110 { 111 isUserSetResponseRegion_ = isUserSetResponseRegion; 112 } 113 SetShowHoverEffect(bool showHoverEffect)114 void SetShowHoverEffect(bool showHoverEffect) 115 { 116 showHoverEffect_ = showHoverEffect; 117 } 118 119 std::string ProvideRestoreInfo() override; 120 121 void OnRestoreInfo(const std::string& restoreInfo) override; 122 void OnColorConfigurationUpdate() override; SetBuilderFunc(SwitchMakeCallback && makeFunc)123 void SetBuilderFunc(SwitchMakeCallback&& makeFunc) 124 { 125 if (makeFunc == nullptr) { 126 makeFunc_ = std::nullopt; 127 contentModifierNode_ = nullptr; 128 OnModifyDone(); 129 return; 130 } 131 makeFunc_ = std::move(makeFunc); 132 } 133 GetBuilderId()134 int32_t GetBuilderId() 135 { 136 return nodeId_; 137 } 138 UseContentModifier()139 bool UseContentModifier() 140 { 141 return contentModifierNode_ != nullptr; 142 } 143 144 void SetSwitchIsOn(bool value); 145 146 private: 147 void OnModifyDone() override; 148 void SetAccessibilityAction(); 149 void UpdateSelectStatus(bool isSelected); 150 void OnAfterModifyDone() override; 151 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override; 152 RefPtr<Curve> GetCurve() const; 153 int32_t GetDuration() const; 154 int32_t nodeId_ = -1; 155 void UpdateChangeEvent() const; 156 void OnChange(); 157 void OnTouchDown(); 158 void OnTouchUp(); 159 void HandleMouseEvent(bool isHover); 160 float GetSwitchWidth() const; 161 float GetSwitchContentOffsetX() const; 162 163 // Init pan recognizer to move items when drag update, play translate animation when drag end. 164 void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub); 165 void InitClickEvent(); 166 void InitTouchEvent(); 167 void InitMouseEvent(); 168 169 // Init key event 170 void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub); 171 void GetInnerFocusPaintRect(RoundRect& paintRect); 172 173 void HandleDragStart(); 174 void HandleDragUpdate(const GestureEvent& info); 175 void HandleDragEnd(); 176 177 bool IsOutOfBoundary(double mainOffset) const; 178 void OnClick(); 179 void AddHotZoneRect(); 180 void RemoveLastHotZoneRect() const; 181 void UpdateSwitchPaintProperty(); 182 void UpdateSwitchLayoutProperty(); 183 void FireBuilder(); 184 185 RefPtr<FrameNode> BuildContentModifierNode(); 186 std::optional<SwitchMakeCallback> makeFunc_; 187 RefPtr<FrameNode> contentModifierNode_; 188 189 RefPtr<PanEvent> panEvent_; 190 191 RefPtr<ClickEvent> clickListener_; 192 std::optional<bool> isOn_; 193 float currentOffset_ = 0.0f; 194 float dragOffsetX_ = 0.0f; 195 196 RefPtr<TouchEventImpl> touchListener_; 197 RefPtr<InputEvent> mouseEvent_; 198 bool isTouch_ = false; 199 bool isHover_ = false; 200 bool isUserSetResponseRegion_ = false; 201 bool showHoverEffect_ = true; 202 bool enabled_ = true; 203 204 float width_ = 0.0f; 205 float height_ = 0.0f; 206 Dimension hotZoneHorizontalPadding_; 207 Dimension hotZoneVerticalPadding_; 208 OffsetF offset_; 209 SizeF size_; 210 OffsetF hotZoneOffset_; 211 SizeF hotZoneSize_; 212 TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; 213 TextDirection direction_ = TextDirection::AUTO; 214 bool isDragEvent_ = false; 215 RefPtr<SwitchPaintMethod> paintMethod_; 216 ACE_DISALLOW_COPY_AND_MOVE(SwitchPattern); 217 }; 218 } // namespace OHOS::Ace::NG 219 220 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_PATTERN_H 221