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