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