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 #include "core/components_ng/event/event_hub.h" 17 18 #include "base/utils/utils.h" 19 #include "core/components_ng/base/frame_node.h" 20 21 namespace OHOS::Ace::NG { 22 AttachHost(const WeakPtr<FrameNode> & host)23void EventHub::AttachHost(const WeakPtr<FrameNode>& host) 24 { 25 host_ = host; 26 } 27 GetFrameNode() const28RefPtr<FrameNode> EventHub::GetFrameNode() const 29 { 30 return host_.Upgrade(); 31 } 32 AddSupportedState(UIState state)33void EventHub::AddSupportedState(UIState state) 34 { 35 if (!stateStyleMgr_) { 36 stateStyleMgr_ = MakeRefPtr<StateStyleManager>(host_); 37 } 38 stateStyleMgr_->AddSupportedState(state); 39 } 40 SetSupportedStates(UIState state)41void EventHub::SetSupportedStates(UIState state) 42 { 43 if (!stateStyleMgr_) { 44 stateStyleMgr_ = MakeRefPtr<StateStyleManager>(host_); 45 } 46 stateStyleMgr_->SetSupportedStates(state); 47 } 48 IsCurrentStateOn(UIState state)49bool EventHub::IsCurrentStateOn(UIState state) 50 { 51 if (!stateStyleMgr_) { 52 return false; 53 } 54 return stateStyleMgr_->IsCurrentStateOn(state); 55 } 56 CreateGetEventTargetImpl() const57GetEventTargetImpl EventHub::CreateGetEventTargetImpl() const 58 { 59 auto impl = [weak = host_]() -> std::optional<EventTarget> { 60 auto host = weak.Upgrade(); 61 CHECK_NULL_RETURN_NOLOG(host, std::nullopt); 62 EventTarget eventTarget; 63 eventTarget.id = host->GetId(); 64 eventTarget.type = host->GetTag(); 65 auto geometryNode = host->GetGeometryNode(); 66 auto offset = geometryNode->GetFrameOffset(); 67 auto size = geometryNode->GetFrameSize(); 68 eventTarget.area.SetOffset(DimensionOffset(offset)); 69 eventTarget.area.SetHeight(Dimension(size.Height())); 70 eventTarget.area.SetWidth(Dimension(size.Width())); 71 eventTarget.origin = DimensionOffset(geometryNode->GetParentGlobalOffset()); 72 return eventTarget; 73 }; 74 return impl; 75 } 76 MarkModifyDone()77void EventHub::MarkModifyDone() 78 { 79 if (stateStyleMgr_) { 80 // focused style is managered in focus event hub. 81 if (stateStyleMgr_->HasStateStyle(UI_STATE_PRESSED)) { 82 GetOrCreateGestureEventHub()->AddTouchEvent(stateStyleMgr_->GetPressedListener()); 83 } 84 if (stateStyleMgr_->HasStateStyle(UI_STATE_DISABLED)) { 85 if (enabled_) { 86 stateStyleMgr_->ResetCurrentUIState(UI_STATE_DISABLED); 87 } else { 88 stateStyleMgr_->UpdateCurrentUIState(UI_STATE_DISABLED); 89 } 90 } 91 } 92 93 if (gestureEventHub_) { 94 gestureEventHub_->OnModifyDone(); 95 } 96 OnModifyDone(); 97 } 98 99 } // namespace OHOS::Ace::NG