• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "core/components/theme/app_theme.h"
33 
34 namespace OHOS::Ace::NG {
35 
36 class SwitchPattern : public Pattern {
37     DECLARE_ACE_TYPE(SwitchPattern, Pattern);
38 
39 public:
40     SwitchPattern() = default;
41 
42     ~SwitchPattern() override = default;
43 
IsAtomicNode()44     bool IsAtomicNode() const override
45     {
46         return false;
47     }
48 
CreateEventHub()49     RefPtr<EventHub> CreateEventHub() override
50     {
51         return MakeRefPtr<SwitchEventHub>();
52     }
53 
CreateLayoutAlgorithm()54     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
55     {
56         return MakeRefPtr<SwitchLayoutAlgorithm>();
57     }
58 
CreatePaintProperty()59     RefPtr<PaintProperty> CreatePaintProperty() override
60     {
61         return MakeRefPtr<SwitchPaintProperty>();
62     }
63 
CreateNodePaintMethod()64     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
65     {
66         auto host = GetHost();
67         CHECK_NULL_RETURN(host, nullptr);
68         if (!paintMethod_) {
69             paintMethod_ = MakeRefPtr<SwitchPaintMethod>();
70         }
71         paintMethod_->SetUseContentModifier(UseContentModifier());
72         paintMethod_->SetDirection(direction_);
73         paintMethod_->SetIsSelect(isOn_.value_or(false));
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>(GetThemeScopeId());
94         CHECK_NULL_RETURN(switchTheme, FocusPattern());
95 
96         auto focusPaintcolor = switchTheme->GetActiveColor();
97         focusPaintParams.SetPaintColor(focusPaintcolor);
98         focusPaintParams.SetFocusPadding(switchTheme->GetSwitchFocuPadding());
99 
100         return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION, focusPaintParams };
101     }
102 
IsChecked()103     bool IsChecked()
104     {
105         return isOn_.value_or(false);
106     }
107 
108     void MarkIsSelected(bool isSelected);
109 
SetIsUserSetResponseRegion(bool isUserSetResponseRegion)110     void SetIsUserSetResponseRegion(bool isUserSetResponseRegion)
111     {
112         isUserSetResponseRegion_ = isUserSetResponseRegion;
113     }
114 
SetShowHoverEffect(bool showHoverEffect)115     void SetShowHoverEffect(bool showHoverEffect)
116     {
117         showHoverEffect_ = showHoverEffect;
118     }
119 
120     std::string ProvideRestoreInfo() override;
121 
122     void OnRestoreInfo(const std::string& restoreInfo) override;
123     void OnColorConfigurationUpdate() override;
SetBuilderFunc(SwitchMakeCallback && makeFunc)124     void SetBuilderFunc(SwitchMakeCallback&& makeFunc)
125     {
126         if (makeFunc == nullptr) {
127             makeFunc_ = std::nullopt;
128             contentModifierNode_ = nullptr;
129             OnModifyDone();
130             return;
131         }
132         makeFunc_ = std::move(makeFunc);
133     }
134 
GetBuilderId()135     int32_t GetBuilderId()
136     {
137         return nodeId_;
138     }
139 
UseContentModifier()140     bool UseContentModifier()
141     {
142         return contentModifierNode_ != nullptr;
143     }
144 
145     void SetSwitchIsOn(bool value);
146     bool OnThemeScopeUpdate(int32_t themeScopeId) override;
147     void DumpInfo() override;
SetIsUserSetMargin(bool isUserSetMargin)148     void SetIsUserSetMargin(bool isUserSetMargin)
149     {
150         isUserSetMargin_ = isUserSetMargin;
151     }
152 
IsEnableMatchParent()153     bool IsEnableMatchParent() override
154     {
155         return true;
156     }
157 private:
158     void OnAttachToFrameNode() override;
159     void OnModifyDone() override;
160     void SetAccessibilityAction();
161     void UpdateSelectStatus(bool isSelected);
162     void OnAfterModifyDone() override;
163     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override;
164     RefPtr<Curve> GetCurve() const;
165     int32_t GetDuration() const;
166     int32_t nodeId_ = -1;
167     void UpdateChangeEvent() const;
168     void OnChange();
169     void OnTouchDown();
170     void OnTouchUp();
171     void HandleMouseEvent(bool isHover);
172     void HandleFocusEvent();
173     void HandleBlurEvent();
174     void UpdateColorWhenIsOn(bool isOn);
175     float GetSwitchWidth() const;
176     float GetSwitchContentOffsetX() const;
177 
178     // Init pan recognizer to move items when drag update, play translate animation when drag end.
179     void HandleEnabled();
180     void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub);
181     void InitClickEvent();
182     void InitTouchEvent();
183     void InitMouseEvent();
184     void InitFocusEvent();
185 
186     void AddIsFocusActiveUpdateEvent();
187     void RemoveIsFocusActiveUpdateEvent();
188     void OnIsFocusActiveUpdate(bool isFocusAcitve);
189 
190     // Init key event
191     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
192     void GetInnerFocusPaintRect(RoundRect& paintRect);
193 
194     void HandleDragStart();
195     void HandleDragUpdate(const GestureEvent& info);
196     void HandleDragEnd();
197 
198     bool IsOutOfBoundary(double mainOffset) const;
199     void OnClick();
200     void AddHotZoneRect();
201     void RemoveLastHotZoneRect() const;
202     void UpdateSwitchPaintProperty();
203     void UpdateSwitchLayoutProperty();
204     void FireBuilder();
205     bool OnKeyEvent(const KeyEvent& keyEventInfo);
206     void InitDefaultMargin();
207     void ResetDefaultMargin();
208     RefPtr<FrameNode> BuildContentModifierNode();
209     std::optional<SwitchMakeCallback> makeFunc_;
210     RefPtr<FrameNode> contentModifierNode_;
211 
212     RefPtr<PanEvent> panEvent_;
213     RefPtr<SwitchTheme> switchTheme_;
214     RefPtr<ClickEvent> clickListener_;
215     std::optional<bool> isOn_;
216     float currentOffset_ = 0.0f;
217     float dragOffsetX_ = 0.0f;
218 
219     RefPtr<TouchEventImpl> touchListener_;
220     RefPtr<InputEvent> mouseEvent_;
221     bool isTouch_ = false;
222     bool isHover_ = false;
223     bool isFocus_ = false;
224     bool isUserSetResponseRegion_ = false;
225     bool showHoverEffect_ = true;
226     bool isUserSetMargin_ = false;
227 
228     float width_ = 0.0f;
229     float height_ = 0.0f;
230     Dimension hotZoneHorizontalPadding_;
231     Dimension hotZoneVerticalPadding_;
232     OffsetF offset_;
233     SizeF size_;
234     OffsetF hotZoneOffset_;
235     SizeF hotZoneSize_;
236     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
237     TextDirection direction_ = TextDirection::AUTO;
238     bool isDragEvent_ = false;
239     RefPtr<SwitchPaintMethod> paintMethod_;
240     ACE_DISALLOW_COPY_AND_MOVE(SwitchPattern);
241     std::function<void(bool)> isFocusActiveUpdateEvent_;
242     Dimension hotZoneHorizontalSize_;
243     Dimension hotZoneVerticalSize_;
244 };
245 } // namespace OHOS::Ace::NG
246 
247 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_PATTERN_H
248