• 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 
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->SetDirection(direction_);
72         paintMethod->SetIsSelect(isOn_.value_or(false));
73         auto eventHub = host->GetEventHub<EventHub>();
74         CHECK_NULL_RETURN(eventHub, nullptr);
75         auto enabled = eventHub->IsEnabled();
76         paintMethod->SetEnabled(enabled);
77         paintMethod->SetDragOffsetX(dragOffsetX_);
78         paintMethod->SetTouchHoverAnimationType(touchHoverType_);
79         paintMethod->SetIsDragEvent(isDragEvent_);
80         paintMethod->SetShowHoverEffect(showHoverEffect_);
81         return paintMethod;
82     }
83 
CreateAccessibilityProperty()84     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
85     {
86         return MakeRefPtr<SwitchAccessibilityProperty>();
87     }
88 
GetFocusPattern()89     FocusPattern GetFocusPattern() const override
90     {
91         FocusPaintParam focusPaintParams;
92 
93         auto pipelineContext = PipelineBase::GetCurrentContext();
94         CHECK_NULL_RETURN(pipelineContext, FocusPattern());
95         auto switchTheme = pipelineContext->GetTheme<SwitchTheme>();
96         CHECK_NULL_RETURN(switchTheme, FocusPattern());
97         auto focusPaintcolor = switchTheme->GetActiveColor();
98         focusPaintParams.SetPaintColor(focusPaintcolor);
99         focusPaintParams.SetFocusPadding(Dimension(2.0_vp));
100 
101         return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION, focusPaintParams };
102     }
103 
IsChecked()104     bool IsChecked()
105     {
106         return isOn_.value_or(false);
107     }
108 
109     void MarkIsSelected(bool isSelected);
110 
SetIsUserSetResponseRegion(bool isUserSetResponseRegion)111     void SetIsUserSetResponseRegion(bool isUserSetResponseRegion)
112     {
113         isUserSetResponseRegion_ = isUserSetResponseRegion;
114     }
115 
SetShowHoverEffect(bool showHoverEffect)116     void SetShowHoverEffect(bool showHoverEffect)
117     {
118         showHoverEffect_ = showHoverEffect;
119     }
120 
121     std::string ProvideRestoreInfo() override;
122 
123     void OnRestoreInfo(const std::string& restoreInfo) override;
124     void OnColorConfigurationUpdate() override;
125 
126 private:
127     void OnModifyDone() override;
128     void SetAccessibilityAction();
129     void UpdateSelectStatus(bool isSelected);
130     void OnAfterModifyDone() override;
131     void OnAttachToFrameNode() override;
132     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override;
133     RefPtr<Curve> GetCurve() const;
134     int32_t GetDuration() const;
135     void UpdateChangeEvent() const;
136     void OnChange();
137     void OnTouchDown();
138     void OnTouchUp();
139     void HandleMouseEvent(bool isHover);
140     float GetSwitchWidth() const;
141     float GetSwitchContentOffsetX() const;
142 
143     // Init pan recognizer to move items when drag update, play translate animation when drag end.
144     void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub);
145     void InitClickEvent();
146     void InitTouchEvent();
147     void InitMouseEvent();
148 
149     // Init key event
150     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
151     void GetInnerFocusPaintRect(RoundRect& paintRect);
152 
153     void HandleDragStart();
154     void HandleDragUpdate(const GestureEvent& info);
155     void HandleDragEnd();
156 
157     bool IsOutOfBoundary(double mainOffset) const;
158     void OnClick();
159     void AddHotZoneRect();
160     void RemoveLastHotZoneRect() const;
161     void UpdateSwitchPaintProperty();
162     void UpdateSwitchLayoutProperty();
163 
164     RefPtr<PanEvent> panEvent_;
165 
166     RefPtr<ClickEvent> clickListener_;
167     std::optional<bool> isOn_;
168     float currentOffset_ = 0.0f;
169     float dragOffsetX_ = 0.0f;
170 
171     RefPtr<TouchEventImpl> touchListener_;
172     RefPtr<InputEvent> mouseEvent_;
173     bool isTouch_ = false;
174     bool isHover_ = false;
175     bool isUserSetResponseRegion_ = false;
176     bool showHoverEffect_ = true;
177 
178     float width_ = 0.0f;
179     float height_ = 0.0f;
180     Dimension hotZoneHorizontalPadding_;
181     Dimension hotZoneVerticalPadding_;
182     OffsetF offset_;
183     SizeF size_;
184     OffsetF hotZoneOffset_;
185     SizeF hotZoneSize_;
186     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
187     TextDirection direction_ = TextDirection::AUTO;
188     bool isDragEvent_ = false;
189     RefPtr<SwitchModifier> switchModifier_;
190     ACE_DISALLOW_COPY_AND_MOVE(SwitchPattern);
191 };
192 } // namespace OHOS::Ace::NG
193 
194 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_PATTERN_H
195