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 "base/memory/ace_type.h" 20 #include "base/memory/referenced.h" 21 22 namespace OHOS::Ace::NG { 23 24 class FrameNode; 25 class TouchEventImpl; 26 27 using UIState = uint64_t; 28 inline constexpr UIState UI_STATE_NORMAL = 0; 29 inline constexpr UIState UI_STATE_PRESSED = 1; 30 inline constexpr UIState UI_STATE_FOCUSED = 1 << 1; 31 inline constexpr UIState UI_STATE_DISABLED = 1 << 2; 32 // used for radio, checkbox, switch. 33 inline constexpr UIState UI_STATE_SELECTED = 1 << 3; 34 35 // StateStyleManager is mainly used to manage the setting and refresh of state styles. 36 class StateStyleManager : public virtual AceType { 37 DECLARE_ACE_TYPE(StateStyleManager, AceType) 38 39 public: 40 explicit StateStyleManager(WeakPtr<FrameNode> frameNode); 41 ~StateStyleManager() override; 42 HasStateStyle(UIState state)43 bool HasStateStyle(UIState state) const 44 { 45 return (supportedStates_ & state) == state; 46 } 47 GetCurrentUIState()48 UIState GetCurrentUIState() const 49 { 50 return currentState_; 51 } 52 AddSupportedState(UIState state)53 void AddSupportedState(UIState state) 54 { 55 supportedStates_ = supportedStates_ | state; 56 } 57 SetSupportedStates(UIState state)58 void SetSupportedStates(UIState state) 59 { 60 supportedStates_ = state; 61 } 62 IsCurrentStateOn(UIState state)63 bool IsCurrentStateOn(UIState state) const 64 { 65 if (state == UI_STATE_NORMAL) { 66 return currentState_ == state; 67 } 68 return (currentState_ & state) == state; 69 } 70 SetCurrentUIState(UIState state,bool flag)71 void SetCurrentUIState(UIState state, bool flag) 72 { 73 if (flag) { 74 currentState_ |= state; 75 } else { 76 currentState_ &= ~state; 77 } 78 } 79 UpdateCurrentUIState(UIState state)80 void UpdateCurrentUIState(UIState state) 81 { 82 if (!HasStateStyle(state)) { 83 return; 84 } 85 auto temp = currentState_ | state; 86 if (temp != currentState_) { 87 currentState_ = temp; 88 FireStateFunc(); 89 } 90 } 91 ResetCurrentUIState(UIState state)92 void ResetCurrentUIState(UIState state) 93 { 94 if (!HasStateStyle(state)) { 95 return; 96 } 97 if ((currentState_ & state) != state) { 98 LOGD("current %{public}d state is not set yet.", static_cast<int32_t>(state)); 99 return; 100 } 101 auto temp = currentState_ ^ state; 102 if (temp != currentState_) { 103 currentState_ = temp; 104 FireStateFunc(); 105 } 106 } 107 108 const RefPtr<TouchEventImpl>& GetPressedListener(); 109 110 private: 111 void FireStateFunc(); 112 113 WeakPtr<FrameNode> host_; 114 RefPtr<TouchEventImpl> pressedFunc_; 115 116 UIState supportedStates_ = UI_STATE_NORMAL; 117 UIState currentState_ = UI_STATE_NORMAL; 118 119 ACE_DISALLOW_COPY_AND_MOVE(StateStyleManager); 120 }; 121 122 } // namespace OHOS::Ace::NG 123 124 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_STATE_STYLE_MANAGER_H