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 32 namespace OHOS::Ace::NG { 33 34 class SwitchPattern : public Pattern { 35 DECLARE_ACE_TYPE(SwitchPattern, Pattern); 36 37 public: 38 SwitchPattern() = default; 39 40 ~SwitchPattern() override = default; 41 CreateEventHub()42 RefPtr<EventHub> CreateEventHub() override 43 { 44 return MakeRefPtr<SwitchEventHub>(); 45 } 46 CreateLayoutAlgorithm()47 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 48 { 49 return MakeRefPtr<SwitchLayoutAlgorithm>(); 50 } 51 CreatePaintProperty()52 RefPtr<PaintProperty> CreatePaintProperty() override 53 { 54 return MakeRefPtr<SwitchPaintProperty>(); 55 } 56 CreateNodePaintMethod()57 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 58 { 59 auto host = GetHost(); 60 CHECK_NULL_RETURN(host, nullptr); 61 if (!switchModifier_) { 62 auto pipeline = PipelineBase::GetCurrentContext(); 63 auto switchTheme = pipeline->GetTheme<SwitchTheme>(); 64 auto paintProperty = host->GetPaintProperty<SwitchPaintProperty>(); 65 auto isSelect = paintProperty->GetIsOnValue(false); 66 auto boardColor = isSelect ? paintProperty->GetSelectedColorValue(switchTheme->GetActiveColor()) 67 : switchTheme->GetInactivePointColor(); 68 switchModifier_ = AceType::MakeRefPtr<SwitchModifier>(isSelect, boardColor, dragOffsetX_); 69 } 70 auto paintMethod = MakeRefPtr<SwitchPaintMethod>(switchModifier_); 71 paintMethod->SetIsSelect(isOn_.value_or(false)); 72 auto eventHub = host->GetEventHub<EventHub>(); 73 CHECK_NULL_RETURN(eventHub, nullptr); 74 auto enabled = eventHub->IsEnabled(); 75 paintMethod->SetEnabled(enabled); 76 paintMethod->SetDragOffsetX(dragOffsetX_); 77 paintMethod->SetTouchHoverAnimationType(touchHoverType_); 78 paintMethod->SetIsDragEvent(isDragEvent_); 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 SetIsUserSetResponseRegion(bool isUserSetResponseRegion)107 void SetIsUserSetResponseRegion(bool isUserSetResponseRegion) 108 { 109 isUserSetResponseRegion_ = isUserSetResponseRegion; 110 } 111 112 std::string ProvideRestoreInfo() override; 113 114 void OnRestoreInfo(const std::string& restoreInfo) override; 115 void OnColorConfigurationUpdate() override; 116 117 private: 118 void OnModifyDone() override; 119 void OnAttachToFrameNode() override; 120 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override; 121 RefPtr<Curve> GetCurve() const; 122 int32_t GetDuration() const; 123 void UpdateChangeEvent() const; 124 void OnChange(); 125 void OnTouchDown(); 126 void OnTouchUp(); 127 void HandleMouseEvent(bool isHover); 128 float GetSwitchWidth() const; 129 float GetSwitchContentOffsetX() const; 130 131 // Init pan recognizer to move items when drag update, play translate animation when drag end. 132 void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub); 133 void InitClickEvent(); 134 void InitTouchEvent(); 135 void InitMouseEvent(); 136 137 // Init key event 138 void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub); 139 void GetInnerFocusPaintRect(RoundRect& paintRect); 140 141 void HandleDragStart(); 142 void HandleDragUpdate(const GestureEvent& info); 143 void HandleDragEnd(); 144 145 bool IsOutOfBoundary(double mainOffset) const; 146 void OnClick(); 147 void AddHotZoneRect(); 148 void RemoveLastHotZoneRect() const; 149 150 RefPtr<PanEvent> panEvent_; 151 152 RefPtr<ClickEvent> clickListener_; 153 std::optional<bool> isOn_; 154 float currentOffset_ = 0.0f; 155 float dragOffsetX_ = 0.0f; 156 157 RefPtr<TouchEventImpl> touchListener_; 158 RefPtr<InputEvent> mouseEvent_; 159 bool isTouch_ = false; 160 bool isHover_ = false; 161 bool isUserSetResponseRegion_ = false; 162 163 float width_ = 0.0f; 164 float height_ = 0.0f; 165 Dimension hotZoneHorizontalPadding_; 166 Dimension hotZoneVerticalPadding_; 167 OffsetF offset_; 168 SizeF size_; 169 OffsetF hotZoneOffset_; 170 SizeF hotZoneSize_; 171 TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; 172 bool isDragEvent_ = false; 173 RefPtr<SwitchModifier> switchModifier_; 174 ACE_DISALLOW_COPY_AND_MOVE(SwitchPattern); 175 }; 176 } // namespace OHOS::Ace::NG 177 178 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_PATTERN_H 179