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/state_style_manager.h" 17 18 #include "base/memory/ace_type.h" 19 #include "base/memory/referenced.h" 20 #include "base/utils/utils.h" 21 #include "core/components_ng/base/frame_node.h" 22 #include "core/components_ng/event/touch_event.h" 23 #include "core/components_ng/pattern/custom/custom_node_base.h" 24 #include "core/event/touch_event.h" 25 #include "core/pipeline_ng/pipeline_context.h" 26 27 namespace OHOS::Ace::NG { 28 StateStyleManager(WeakPtr<FrameNode> frameNode)29StateStyleManager::StateStyleManager(WeakPtr<FrameNode> frameNode) : host_(std::move(frameNode)) {} 30 31 StateStyleManager::~StateStyleManager() = default; 32 GetPressedListener()33const RefPtr<TouchEventImpl>& StateStyleManager::GetPressedListener() 34 { 35 if (pressedFunc_) { 36 return pressedFunc_; 37 } 38 auto pressedCallback = [weak = WeakClaim(this)](TouchEventInfo& info) { 39 auto stateStyleMgr = weak.Upgrade(); 40 CHECK_NULL_VOID(stateStyleMgr); 41 const auto& touches = info.GetTouches(); 42 if (touches.empty()) { 43 LOGW("the touch info is illegal"); 44 return; 45 } 46 const auto& type = touches.front().GetTouchType(); 47 if (type == TouchType::DOWN) { 48 stateStyleMgr->UpdateCurrentUIState(UI_STATE_PRESSED); 49 return; 50 } 51 if ((type == TouchType::UP) || (type == TouchType::CANCEL)) { 52 stateStyleMgr->ResetCurrentUIState(UI_STATE_PRESSED); 53 } 54 }; 55 pressedFunc_ = MakeRefPtr<TouchEventImpl>(std::move(pressedCallback)); 56 return pressedFunc_; 57 } 58 FireStateFunc()59void StateStyleManager::FireStateFunc() 60 { 61 auto node = host_.Upgrade(); 62 CHECK_NULL_VOID(node); 63 auto nodeId = node->GetId(); 64 RefPtr<CustomNodeBase> customNode; 65 if (AceType::InstanceOf<CustomNodeBase>(node)) { 66 customNode = DynamicCast<CustomNodeBase>(node); 67 } 68 if (!customNode) { 69 auto parent = node->GetParent(); 70 while (parent) { 71 if (AceType::InstanceOf<CustomNodeBase>(parent)) { 72 customNode = DynamicCast<CustomNodeBase>(parent); 73 break; 74 } 75 parent = parent->GetParent(); 76 } 77 } 78 if (!customNode) { 79 LOGE("fail to find custom node to fire update func of %{public}d", nodeId); 80 return; 81 } 82 auto context = PipelineContext::GetCurrentContext(); 83 CHECK_NULL_VOID(context); 84 context->PostAsyncEvent([customNode, nodeId]() { customNode->FireNodeUpdateFunc(nodeId); }); 85 } 86 87 } // namespace OHOS::Ace::NG