• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_event_hub.h"
27 #include "core/components_ng/pattern/toggle/switch_layout_algorithm.h"
28 #include "core/components_ng/pattern/toggle/switch_paint_method.h"
29 #include "core/components_ng/pattern/toggle/switch_paint_property.h"
30 
31 namespace OHOS::Ace::NG {
32 
33 class SwitchPattern : public Pattern {
34     DECLARE_ACE_TYPE(SwitchPattern, Pattern);
35 
36 public:
37     SwitchPattern() = default;
38 
39     ~SwitchPattern() override = default;
40 
CreateEventHub()41     RefPtr<EventHub> CreateEventHub() override
42     {
43         return MakeRefPtr<SwitchEventHub>();
44     }
45 
CreateLayoutAlgorithm()46     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
47     {
48         return MakeRefPtr<SwitchLayoutAlgorithm>();
49     }
50 
CreatePaintProperty()51     RefPtr<PaintProperty> CreatePaintProperty() override
52     {
53         auto paintProperty = MakeRefPtr<SwitchPaintProperty>();
54         paintProperty->UpdateCurrentOffset(currentOffset_);
55         return paintProperty;
56     }
57 
CreateNodePaintMethod()58     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
59     {
60         auto host = GetHost();
61         CHECK_NULL_RETURN(host, nullptr);
62         if (!switchModifier_) {
63             auto pipeline = PipelineBase::GetCurrentContext();
64             auto switchTheme = pipeline->GetTheme<SwitchTheme>();
65             auto paintProperty = host->GetPaintProperty<SwitchPaintProperty>();
66             auto isSelect = paintProperty->GetIsOnValue(false);
67             auto boardColor = isSelect ? paintProperty->GetSelectedColorValue(switchTheme->GetActiveColor())
68                                        : switchTheme->GetInactivePointColor();
69             switchModifier_ = AceType::MakeRefPtr<SwitchModifier>(isSelect, boardColor, currentOffset_);
70         }
71         auto paintMethod = MakeRefPtr<SwitchPaintMethod>(switchModifier_);
72         paintMethod->SetIsSelect(isOnBeforeAnimate_.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->SetMainDelta(currentOffset_);
78         paintMethod->SetIsHover(isHover_);
79         return paintMethod;
80     }
81 
GetFocusPattern()82     FocusPattern GetFocusPattern() const override
83     {
84         FocusPaintParam focusPaintParams;
85 
86         auto pipelineContext = PipelineBase::GetCurrentContext();
87         CHECK_NULL_RETURN(pipelineContext, FocusPattern());
88         auto switchTheme = pipelineContext->GetTheme<SwitchTheme>();
89         CHECK_NULL_RETURN(switchTheme, FocusPattern());
90         auto focusPaintcolor = switchTheme->GetActiveColor();
91         focusPaintParams.SetPaintColor(focusPaintcolor);
92         focusPaintParams.SetFocusPadding(Dimension(2.0_vp));
93 
94         return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION, focusPaintParams };
95     }
96 
97 private:
98     void OnModifyDone() override;
99     void UpdateCurrentOffset(float offset);
100     void OnAttachToFrameNode() override;
101     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override;
102     void PlayTranslateAnimation(float startPos, float endPos);
103     RefPtr<Curve> GetCurve() const;
104     int32_t GetDuration() const;
105     void StopTranslateAnimation();
106     void UpdateChangeEvent() const;
107     void OnChange();
108     void OnTouchDown();
109     void OnTouchUp();
110     void HandleMouseEvent(bool isHover);
111     float GetSwitchWidth() const;
112 
113     // Init pan recognizer to move items when drag update, play translate animation when drag end.
114     void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub);
115     void InitClickEvent();
116     void InitTouchEvent();
117     void InitMouseEvent();
118 
119     // Init key event
120     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
121     bool OnKeyEvent(const KeyEvent& event);
122     void GetInnerFocusPaintRect(RoundRect& paintRect);
123 
124     void HandleDragUpdate(const GestureEvent& info);
125     void HandleDragEnd();
126 
127     bool IsOutOfBoundary(double mainOffset) const;
128     void OnClick();
129     void AddHotZoneRect();
130 
131     RefPtr<PanEvent> panEvent_;
132 
133     RefPtr<Animator> controller_;
134     RefPtr<ClickEvent> clickListener_;
135     RefPtr<CurveAnimation<double>> translate_;
136     std::optional<bool> isOn_;
137     std::optional<bool> isOnBeforeAnimate_;
138     bool changeFlag_ = false;
139     float currentOffset_ = 0.0f;
140 
141     RefPtr<TouchEventImpl> touchListener_;
142     RefPtr<InputEvent> mouseEvent_;
143     bool isTouch_ = false;
144     bool isHover_ = false;
145 
146     float width_ = 0.0f;
147     float height_ = 0.0f;
148     Dimension hotZoneHorizontalPadding_;
149     Dimension hotZoneVerticalPadding_;
150     OffsetF offset_;
151     SizeF size_;
152     OffsetF hotZoneOffset_;
153     SizeF hotZoneSize_;
154 
155     RefPtr<SwitchModifier> switchModifier_;
156 
157     ACE_DISALLOW_COPY_AND_MOVE(SwitchPattern);
158 };
159 } // namespace OHOS::Ace::NG
160 
161 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_PATTERN_H