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_CHECKED = 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 UpdateCurrentUIState(UIState state)71 void UpdateCurrentUIState(UIState state) 72 { 73 if (!HasStateStyle(state)) { 74 return; 75 } 76 auto temp = currentState_ | state; 77 if (temp != currentState_) { 78 currentState_ = temp; 79 FireStateFunc(); 80 } 81 } 82 ResetCurrentUIState(UIState state)83 void ResetCurrentUIState(UIState state) 84 { 85 if (!HasStateStyle(state)) { 86 return; 87 } 88 if ((currentState_ & state) != state) { 89 LOGD("current %{public}d state is not set yet.", static_cast<int32_t>(state)); 90 return; 91 } 92 auto temp = currentState_ ^ state; 93 if (temp != currentState_) { 94 currentState_ = temp; 95 FireStateFunc(); 96 } 97 } 98 99 const RefPtr<TouchEventImpl>& GetPressedListener(); 100 101 private: 102 void FireStateFunc(); 103 104 WeakPtr<FrameNode> host_; 105 RefPtr<TouchEventImpl> pressedFunc_; 106 107 UIState supportedStates_ = UI_STATE_NORMAL; 108 UIState currentState_ = UI_STATE_NORMAL; 109 110 ACE_DISALLOW_COPY_AND_MOVE(StateStyleManager); 111 }; 112 113 } // namespace OHOS::Ace::NG 114 115 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_STATE_STYLE_MANAGER_H