• 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_EVENT_STATE_STYLE_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_STATE_STYLE_MANAGER_H
18 
19 #include <set>
20 
21 #include "base/geometry/ng/point_t.h"
22 #include "base/geometry/offset.h"
23 #include "base/memory/ace_type.h"
24 #include "base/memory/referenced.h"
25 #include "base/thread/cancelable_callback.h"
26 
27 namespace OHOS::Ace::NG {
28 
29 class FrameNode;
30 class TouchEventImpl;
31 
32 using UIState = uint64_t;
33 inline constexpr UIState UI_STATE_NORMAL = 0;
34 inline constexpr UIState UI_STATE_PRESSED = 1;
35 inline constexpr UIState UI_STATE_FOCUSED = 1 << 1;
36 inline constexpr UIState UI_STATE_DISABLED = 1 << 2;
37 // used for radio, checkbox, switch.
38 inline constexpr UIState UI_STATE_SELECTED = 1 << 3;
39 
40 // StateStyleManager is mainly used to manage the setting and refresh of state styles.
41 class StateStyleManager : public virtual AceType {
42     DECLARE_ACE_TYPE(StateStyleManager, AceType)
43 
44 public:
45     explicit StateStyleManager(WeakPtr<FrameNode> frameNode);
46     ~StateStyleManager() override;
47 
HasStateStyle(UIState state)48     bool HasStateStyle(UIState state) const
49     {
50         return (supportedStates_ & state) == state;
51     }
52 
GetCurrentUIState()53     UIState GetCurrentUIState() const
54     {
55         return currentState_;
56     }
57 
AddSupportedState(UIState state)58     void AddSupportedState(UIState state)
59     {
60         supportedStates_ = supportedStates_ | state;
61     }
62 
SetSupportedStates(UIState state)63     void SetSupportedStates(UIState state)
64     {
65         supportedStates_ = state;
66     }
67 
IsCurrentStateOn(UIState state)68     bool IsCurrentStateOn(UIState state) const
69     {
70         if (state == UI_STATE_NORMAL) {
71             return currentState_ == state;
72         }
73         return (currentState_ & state) == state;
74     }
75 
SetCurrentUIState(UIState state,bool flag)76     void SetCurrentUIState(UIState state, bool flag)
77     {
78         if (flag) {
79             currentState_ |= state;
80         } else {
81             currentState_ &= ~state;
82         }
83     }
84 
UpdateCurrentUIState(UIState state)85     void UpdateCurrentUIState(UIState state)
86     {
87         if (!HasStateStyle(state)) {
88             return;
89         }
90         auto temp = currentState_ | state;
91         if (temp != currentState_) {
92             currentState_ = temp;
93             FireStateFunc();
94         }
95     }
96 
ResetCurrentUIState(UIState state)97     void ResetCurrentUIState(UIState state)
98     {
99         if (!HasStateStyle(state)) {
100             return;
101         }
102         if ((currentState_ & state) != state) {
103             return;
104         }
105         auto temp = currentState_ ^ state;
106         if (temp != currentState_) {
107             currentState_ = temp;
108             FireStateFunc();
109         }
110     }
111 
GetHasScrollingParent()112     bool GetHasScrollingParent() const
113     {
114         return hasScrollingParent_;
115     }
116 
117     const RefPtr<TouchEventImpl>& GetPressedListener();
118     void HandleTouchDown();
119     void HandleTouchUp();
120 
121 private:
122     void FireStateFunc();
123 
124     void PostPressStyleTask(uint32_t delayTime);
125     void PostPressCancelStyleTask(uint32_t delayTime);
126 
127     void HandleScrollingParent();
128 
DeletePressStyleTask()129     void DeletePressStyleTask()
130     {
131         if (pressStyleTask_) {
132             pressStyleTask_.Cancel();
133         }
134     }
135 
DeletePressCancelStyleTask()136     void DeletePressCancelStyleTask()
137     {
138         if (pressCancelStyleTask_) {
139             pressCancelStyleTask_.Cancel();
140         }
141     }
142 
IsPressedStatePending()143     bool IsPressedStatePending()
144     {
145         return pressedPendingState_;
146     }
147 
IsPressedCancelStatePending()148     bool IsPressedCancelStatePending()
149     {
150         return pressedCancelPendingState_;
151     }
152 
ResetPressedPendingState()153     void ResetPressedPendingState()
154     {
155         pressedPendingState_ = false;
156     }
157 
ResetPressedCancelPendingState()158     void ResetPressedCancelPendingState()
159     {
160         pressedCancelPendingState_ = false;
161     }
162 
PendingPressedState()163     void PendingPressedState()
164     {
165         pressedPendingState_ = true;
166     }
167 
PendingCancelPressedState()168     void PendingCancelPressedState()
169     {
170         pressedCancelPendingState_ = true;
171     }
172 
ResetPressedState()173     void ResetPressedState()
174     {
175         ResetCurrentUIState(UI_STATE_PRESSED);
176         DeletePressStyleTask();
177         ResetPressedPendingState();
178     }
179 
ResetPressedCancelState()180     void ResetPressedCancelState()
181     {
182         DeletePressCancelStyleTask();
183         ResetPressedCancelPendingState();
184     }
185 
186     bool IsOutOfPressedRegion(int32_t sourceType, const Offset& location) const;
187     bool IsOutOfPressedRegionWithoutClip(RefPtr<FrameNode> node, int32_t sourceType,
188         const Offset& location) const;
189     void Transform(PointF& localPointF, const WeakPtr<FrameNode>& node) const;
190     void CleanScrollingParentListener();
191 
192     WeakPtr<FrameNode> host_;
193     RefPtr<TouchEventImpl> pressedFunc_;
194 
195     UIState supportedStates_ = UI_STATE_NORMAL;
196     UIState currentState_ = UI_STATE_NORMAL;
197 
198     std::set<int32_t> pointerId_;
199     CancelableCallback<void()> pressStyleTask_;
200     CancelableCallback<void()> pressCancelStyleTask_;
201     bool pressedPendingState_ = false;
202     bool pressedCancelPendingState_ = false;
203     bool hasScrollingParent_ = false;
204 
205     ACE_DISALLOW_COPY_AND_MOVE(StateStyleManager);
206 };
207 
208 } // namespace OHOS::Ace::NG
209 
210 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_STATE_STYLE_MANAGER_H