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 "pointer_event.h"
18
19 namespace OHOS::Ace::NG {
WindowEventProcess()20 WindowEventProcess::WindowEventProcess() {}
21
~WindowEventProcess()22 WindowEventProcess::~WindowEventProcess() {}
23
ProcessEnterLeaveEvent(int32_t nodeId,sptr<Rosen::Session> session,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)24 void WindowEventProcess::ProcessEnterLeaveEvent(int32_t nodeId,
25 sptr<Rosen::Session> session, const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
26 {
27 CHECK_NULL_VOID(session);
28 CHECK_NULL_VOID(pointerEvent);
29 std::shared_ptr<MMI::PointerEvent> enterEvent = std::make_shared<MMI::PointerEvent>(*pointerEvent);
30
31 int32_t action = pointerEvent->GetPointerAction();
32 if (action == MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW) {
33 lastWindowNodeId_ = nodeId;
34 lastWeakSession_ = session;
35 lastPointEvent_ = enterEvent;
36 return;
37 }
38 auto lastSession = lastWeakSession_.promote();
39 if (lastSession == nullptr) {
40 enterEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW);
41 DispatchPointerEvent(session, enterEvent);
42 lastWindowNodeId_ = nodeId;
43 lastWeakSession_ = session;
44 lastPointEvent_ = enterEvent;
45 return;
46 }
47
48 if (lastWindowNodeId_ != nodeId) {
49 if (lastPointEvent_ != nullptr) {
50 lastPointEvent_->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
51 lastPointEvent_->SetId(pointerEvent->GetId());
52 DispatchPointerEvent(lastSession, lastPointEvent_);
53
54 enterEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW);
55 DispatchPointerEvent(session, enterEvent);
56 }
57 lastWindowNodeId_ = nodeId;
58 lastWeakSession_ = session;
59 }
60 lastPointEvent_ = enterEvent;
61 }
62
ProcessWindowMouseEvent(int32_t nodeId,sptr<Rosen::Session> session,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)63 void WindowEventProcess::ProcessWindowMouseEvent(int32_t nodeId,
64 sptr<Rosen::Session> session, const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
65 {
66 CHECK_NULL_VOID(session);
67 CHECK_NULL_VOID(pointerEvent);
68 if (pointerEvent->GetSourceType() != MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
69 return;
70 }
71 int32_t action = pointerEvent->GetPointerAction();
72 if ((action == MMI::PointerEvent::POINTER_ACTION_MOVE &&
73 pointerEvent->GetButtonId() == MMI::PointerEvent::BUTTON_NONE) ||
74 (action == MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW)) {
75 ProcessEnterLeaveEvent(nodeId, session, pointerEvent);
76 }
77 if (action == MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW) {
78 CleanWindowMouseRecord();
79 }
80 }
81
ProcessWindowDragEvent(int32_t nodeId,sptr<Rosen::Session> session,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)82 void WindowEventProcess::ProcessWindowDragEvent(int32_t nodeId,
83 sptr<Rosen::Session> session, const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
84 {
85 CHECK_NULL_VOID(session);
86 CHECK_NULL_VOID(pointerEvent);
87 int32_t action = pointerEvent->GetPointerAction();
88 if (action == MMI::PointerEvent::POINTER_ACTION_PULL_UP) {
89 CleanWindowDragEvent();
90 return;
91 }
92 if (action != MMI::PointerEvent::POINTER_ACTION_PULL_MOVE) {
93 return;
94 }
95 std::shared_ptr<MMI::PointerEvent> event = std::make_shared<MMI::PointerEvent>(*pointerEvent);
96 if ((lastDragWindowNodeId_ != -1) && (nodeId != lastDragWindowNodeId_)) {
97 if (lastDragPointEvent_ != nullptr) {
98 lastDragPointEvent_->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW);
99 lastDragPointEvent_->SetId(pointerEvent->GetId());
100 auto lastSession = lastDragSession_.promote();
101 if (lastSession != nullptr) {
102 DispatchPointerEvent(lastSession, lastDragPointEvent_);
103 }
104 event->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_IN_WINDOW);
105 DispatchPointerEvent(session, event);
106 if (event->GetSourceType() == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
107 UpdateWindowMouseRecord(nodeId, session, event);
108 }
109 }
110 }
111 lastDragWindowNodeId_ = nodeId;
112 lastDragSession_ = session;
113 lastDragPointEvent_ = event;
114 }
115
CleanWindowMouseRecord()116 void WindowEventProcess::CleanWindowMouseRecord()
117 {
118 lastWindowNodeId_ = -1;
119 lastWeakSession_ = nullptr;
120 lastPointEvent_ = nullptr;
121 }
122
CleanWindowDragEvent()123 void WindowEventProcess::CleanWindowDragEvent()
124 {
125 lastDragWindowNodeId_ = -1;
126 lastDragSession_ = nullptr;
127 lastDragPointEvent_ = nullptr;
128 }
129
UpdateWindowMouseRecord(int32_t nodeId,sptr<Rosen::Session> session,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)130 void WindowEventProcess::UpdateWindowMouseRecord(int32_t nodeId,
131 sptr<Rosen::Session> session, const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
132 {
133 CHECK_NULL_VOID(session);
134 lastWindowNodeId_ = nodeId;
135 lastWeakSession_ = session;
136 lastPointEvent_ = pointerEvent;
137 }
138
DispatchPointerEvent(sptr<Rosen::Session> session,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)139 void WindowEventProcess::DispatchPointerEvent(sptr<Rosen::Session> session,
140 const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
141 {
142 CHECK_NULL_VOID(session);
143 CHECK_NULL_VOID(pointerEvent);
144 session->TransferPointerEvent(pointerEvent);
145 }
146 } // namespace OHOS::Ace::NG
147