1 /*
2 * Copyright (c) 2023 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 #include "window_event_process.h"
16
17 #include "core/components_ng/pattern/window_scene/scene/window_pattern.h"
18 #include "frameworks/core/event/ace_events.h"
19 #include "pointer_event.h"
20
21 namespace OHOS::Ace::NG {
WindowEventProcess()22 WindowEventProcess::WindowEventProcess() {}
23
~WindowEventProcess()24 WindowEventProcess::~WindowEventProcess() {}
25
ProcessWindowMouseEvent(const RefPtr<WindowNode> & windowNode,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)26 void WindowEventProcess::ProcessWindowMouseEvent(const RefPtr<WindowNode>& windowNode,
27 const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
28 {
29 CHECK_NULL_VOID(windowNode);
30 CHECK_NULL_VOID(pointerEvent);
31 std::shared_ptr<MMI::PointerEvent> enterEvent = std::make_shared<MMI::PointerEvent>(*pointerEvent);
32 auto lastWindowNode = lastWindowNode_.Upgrade();
33
34 int32_t action = pointerEvent->GetPointerAction();
35 if (action == MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW) {
36 lastWindowNode_ = windowNode;
37 lastPointEvent_ = enterEvent;
38 return;
39 }
40 if (lastWindowNode == nullptr) {
41 enterEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW);
42 DispatchPointerEvent(windowNode, enterEvent);
43 lastWindowNode_ = windowNode;
44 lastPointEvent_ = enterEvent;
45 return;
46 }
47
48 if (windowNode->GetId() != lastWindowNode->GetId()) {
49 LOGD("Window switching, enter window:%{public}d, leave window:%{public}d",
50 windowNode->GetId(), lastWindowNode->GetId());
51 if (lastPointEvent_ != nullptr) {
52 lastPointEvent_->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
53 lastPointEvent_->SetId(pointerEvent->GetId());
54 DispatchPointerEvent(lastWindowNode, lastPointEvent_);
55
56 enterEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW);
57 DispatchPointerEvent(windowNode, enterEvent);
58 }
59 }
60 lastWindowNode_ = windowNode;
61 lastPointEvent_ = enterEvent;
62 }
63
ProcessWindowDragEvent(const RefPtr<WindowNode> & windowNode,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)64 void WindowEventProcess::ProcessWindowDragEvent(const RefPtr<WindowNode>& windowNode,
65 const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
66 {
67 CHECK_NULL_VOID(windowNode);
68 CHECK_NULL_VOID(pointerEvent);
69 std::shared_ptr<MMI::PointerEvent> event = std::make_shared<MMI::PointerEvent>(*pointerEvent);
70 auto lastWindowNode = lastDragWindowNode_.Upgrade();
71 if ((lastWindowNode != nullptr) && (windowNode->GetId() != lastWindowNode->GetId())) {
72 LOGD("Window switching, pull in window:%{public}d, pull out window:%{public}d",
73 windowNode->GetId(), lastWindowNode->GetId());
74 if (lastDragPointEvent_ != nullptr) {
75 lastDragPointEvent_->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW);
76 lastDragPointEvent_->SetId(pointerEvent->GetId());
77 DispatchPointerEvent(lastWindowNode, lastDragPointEvent_);
78
79 event->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_IN_WINDOW);
80 DispatchPointerEvent(windowNode, event);
81 if (event->GetSourceType() == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
82 UpdateWindowMouseRecord(windowNode, event);
83 }
84 }
85 }
86 lastDragWindowNode_ = windowNode;
87 lastDragPointEvent_ = event;
88 }
89
CleanWindowMouseRecord()90 void WindowEventProcess::CleanWindowMouseRecord()
91 {
92 lastWindowNode_ = nullptr;
93 lastPointEvent_ = nullptr;
94 }
95
CleanWindowDragEvent()96 void WindowEventProcess::CleanWindowDragEvent()
97 {
98 lastDragWindowNode_ = nullptr;
99 lastDragPointEvent_ = nullptr;
100 }
101
UpdateWindowMouseRecord(const RefPtr<WindowNode> & windowNode,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)102 void WindowEventProcess::UpdateWindowMouseRecord(const RefPtr<WindowNode>& windowNode,
103 const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
104 {
105 lastWindowNode_ = windowNode;
106 lastPointEvent_ = pointerEvent;
107 }
108
DispatchPointerEvent(const RefPtr<WindowNode> & windowNode,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)109 void WindowEventProcess::DispatchPointerEvent(const RefPtr<WindowNode>& windowNode,
110 const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
111 {
112 CHECK_NULL_VOID(windowNode);
113 auto pattern = windowNode->GetPattern<WindowPattern>();
114 CHECK_NULL_VOID(pattern);
115 pattern->DispatchPointerEvent(pointerEvent);
116 }
117 } // namespace OHOS::Ace::NG
118