1 /*
2 * Copyright (c) 2022-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
16 #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h"
17
18 #include "base/geometry/ng/size_t.h"
19 #include "base/ressched/ressched_report.h"
20 #include "base/utils/system_properties.h"
21 #include "base/utils/utils.h"
22 #include "bridge/declarative_frontend/declarative_frontend.h"
23 #include "core/components_ng/event/input_event.h"
24 #include "core/components_ng/pattern/xcomponent/xcomponent_event_hub.h"
25 #include "core/components_ng/pattern/xcomponent/xcomponent_ext_surface_callback_client.h"
26 #include "core/event/mouse_event.h"
27 #include "core/event/touch_event.h"
28 #include "core/pipeline/pipeline_context.h"
29 #include "core/pipeline_ng/pipeline_context.h"
30
31 namespace OHOS::Ace::NG {
32 namespace {
33 #ifdef OHOS_PLATFORM
34 constexpr int64_t INCREASE_CPU_TIME_ONCE = 4000000000; // 4s(unit: ns)
35 #endif
ConvertNativeXComponentTouchEvent(const TouchType & touchType)36 OH_NativeXComponent_TouchEventType ConvertNativeXComponentTouchEvent(const TouchType& touchType)
37 {
38 switch (touchType) {
39 case TouchType::DOWN:
40 return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_DOWN;
41 case TouchType::UP:
42 return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UP;
43 case TouchType::MOVE:
44 return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_MOVE;
45 case TouchType::CANCEL:
46 return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_CANCEL;
47 default:
48 return OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UNKNOWN;
49 }
50 }
51
ConvertNativeXComponentTouchToolType(const SourceTool & toolType)52 OH_NativeXComponent_TouchPointToolType ConvertNativeXComponentTouchToolType(const SourceTool& toolType)
53 {
54 switch (toolType) {
55 case SourceTool::FINGER:
56 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_FINGER;
57 case SourceTool::PEN:
58 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_PEN;
59 case SourceTool::RUBBER:
60 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_RUBBER;
61 case SourceTool::BRUSH:
62 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_BRUSH;
63 case SourceTool::PENCIL:
64 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_PENCIL;
65 case SourceTool::AIRBRUSH:
66 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_AIRBRUSH;
67 case SourceTool::MOUSE:
68 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_MOUSE;
69 case SourceTool::LENS:
70 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_LENS;
71 default:
72 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN;
73 }
74 }
75 } // namespace
76
XComponentPattern(const std::string & id,XComponentType type,const std::string & libraryname,const RefPtr<XComponentController> & xcomponentController)77 XComponentPattern::XComponentPattern(const std::string& id, XComponentType type, const std::string& libraryname,
78 const RefPtr<XComponentController>& xcomponentController)
79 : id_(id), type_(type), libraryname_(libraryname), xcomponentController_(xcomponentController)
80 {}
81
OnAttachToFrameNode()82 void XComponentPattern::OnAttachToFrameNode()
83 {
84 auto host = GetHost();
85 CHECK_NULL_VOID(host);
86 auto renderContext = host->GetRenderContext();
87 if (type_ == XComponentType::SURFACE) {
88 renderContext->SetClipToFrame(true);
89 renderContext->SetClipToBounds(true);
90 renderSurface_ = RenderSurface::Create();
91 renderContextForSurface_ = RenderContext::Create();
92 renderContextForSurface_->InitContext(false, id_ + "Surface");
93 renderContextForSurface_->UpdateBackgroundColor(Color::BLACK);
94 scopeId_ = Container::CurrentId();
95 if (!SystemProperties::GetExtSurfaceEnabled()) {
96 renderSurface_->SetRenderContext(renderContextForSurface_);
97 } else {
98 auto pipelineContext = PipelineContext::GetCurrentContext();
99 CHECK_NULL_VOID(pipelineContext);
100 pipelineContext->AddOnAreaChangeNode(host->GetId());
101 extSurfaceClient_ = MakeRefPtr<XComponentExtSurfaceCallbackClient>(WeakClaim(this));
102 renderSurface_->SetExtSurfaceCallback(extSurfaceClient_);
103 }
104 renderSurface_->InitSurface();
105 renderSurface_->UpdateXComponentConfig();
106 InitEvent();
107 SetMethodCall();
108 }
109 renderContext->UpdateBackgroundColor(Color::TRANSPARENT);
110 }
111
OnAreaChangedInner()112 void XComponentPattern::OnAreaChangedInner()
113 {
114 if (SystemProperties::GetExtSurfaceEnabled()) {
115 auto host = GetHost();
116 CHECK_NULL_VOID(host);
117 auto geometryNode = host->GetGeometryNode();
118 CHECK_NULL_VOID(geometryNode);
119 auto xcomponentNodeSize = geometryNode->GetContentSize();
120 auto xcomponentNodeOffset = geometryNode->GetContentOffset();
121 auto transformRelativeOffset = host->GetTransformRelativeOffset();
122 renderSurface_->SetExtSurfaceBounds(transformRelativeOffset.GetX() + xcomponentNodeOffset.GetX(),
123 transformRelativeOffset.GetY() + xcomponentNodeOffset.GetY(), xcomponentNodeSize.Width(),
124 xcomponentNodeSize.Height());
125 }
126 }
127
OnRebuildFrame()128 void XComponentPattern::OnRebuildFrame()
129 {
130 if (type_ == XComponentType::COMPONENT) {
131 return;
132 }
133 if (!renderSurface_->IsSurfaceValid()) {
134 LOGE("surface not valid");
135 return;
136 }
137 auto host = GetHost();
138 CHECK_NULL_VOID(host);
139 auto renderContext = host->GetRenderContext();
140 CHECK_NULL_VOID(renderContext);
141 renderContext->AddChild(renderContextForSurface_, 0);
142 }
143
OnDetachFromFrameNode(FrameNode * frameNode)144 void XComponentPattern::OnDetachFromFrameNode(FrameNode* frameNode)
145 {
146 CHECK_NULL_VOID(frameNode);
147 if (!hasXComponentInit_) {
148 return;
149 }
150 if (type_ == XComponentType::SURFACE) {
151 NativeXComponentDestroy();
152 auto eventHub = frameNode->GetEventHub<XComponentEventHub>();
153 CHECK_NULL_VOID(eventHub);
154 eventHub->FireDestroyEvent();
155 }
156 }
157
SetMethodCall()158 void XComponentPattern::SetMethodCall()
159 {
160 CHECK_NULL_VOID(xcomponentController_);
161 auto pipelineContext = PipelineContext::GetCurrentContext();
162 CHECK_NULL_VOID(pipelineContext);
163 auto uiTaskExecutor = SingleTaskExecutor::Make(pipelineContext->GetTaskExecutor(), TaskExecutor::TaskType::UI);
164 xcomponentController_->SetConfigSurfaceImpl(
165 [weak = WeakClaim(this), uiTaskExecutor](uint32_t surfaceWidth, uint32_t surfaceHeight) {
166 uiTaskExecutor.PostSyncTask([weak, surfaceWidth, surfaceHeight]() {
167 auto pattern = weak.Upgrade();
168 CHECK_NULL_VOID(pattern);
169 pattern->ConfigSurface(surfaceWidth, surfaceHeight);
170 });
171 });
172
173 xcomponentController_->surfaceId_ = renderSurface_->GetUniqueId();
174 }
175
ConfigSurface(uint32_t surfaceWidth,uint32_t surfaceHeight)176 void XComponentPattern::ConfigSurface(uint32_t surfaceWidth, uint32_t surfaceHeight)
177 {
178 renderSurface_->ConfigSurface(surfaceWidth, surfaceHeight);
179 }
180
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)181 bool XComponentPattern::OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config)
182 {
183 if (type_ == XComponentType::COMPONENT || config.skipMeasure || dirty->SkipMeasureContent()) {
184 return false;
185 }
186 auto geometryNode = dirty->GetGeometryNode();
187 CHECK_NULL_RETURN(geometryNode, false);
188 auto drawSize = geometryNode->GetContentSize();
189 if (drawSize.IsNonPositive()) {
190 return false;
191 }
192
193 if (!hasXComponentInit_) {
194 initSize_ = drawSize;
195 auto position = geometryNode->GetContentOffset() + geometryNode->GetFrameOffset();
196 if (!SystemProperties::GetExtSurfaceEnabled()) {
197 XComponentSizeInit();
198 }
199 NativeXComponentOffset(position.GetX(), position.GetY());
200 hasXComponentInit_ = true;
201 } else {
202 if (config.frameOffsetChange || config.contentOffsetChange) {
203 auto position = geometryNode->GetContentOffset() + geometryNode->GetFrameOffset();
204 NativeXComponentOffset(position.GetX(), position.GetY());
205 }
206 if (config.contentSizeChange) {
207 if (!SystemProperties::GetExtSurfaceEnabled()) {
208 XComponentSizeChange(drawSize.Width(), drawSize.Height());
209 }
210 }
211 }
212 auto offset = geometryNode->GetContentOffset();
213 if (SystemProperties::GetExtSurfaceEnabled()) {
214 auto host = GetHost();
215 CHECK_NULL_RETURN(host, false);
216 auto transformRelativeOffset = host->GetTransformRelativeOffset();
217 renderSurface_->SetExtSurfaceBounds(static_cast<int32_t>(transformRelativeOffset.GetX() + offset.GetX()),
218 static_cast<int32_t>(transformRelativeOffset.GetY() + offset.GetY()),
219 static_cast<int32_t>(drawSize.Width()), static_cast<int32_t>(drawSize.Height()));
220 }
221 renderContextForSurface_->SetBounds(offset.GetX(), offset.GetY(), drawSize.Width(), drawSize.Height());
222 auto host = GetHost();
223 CHECK_NULL_RETURN(host, false);
224 host->MarkNeedSyncRenderTree();
225 return false;
226 }
227
OnPaint()228 void XComponentPattern::OnPaint()
229 {
230 auto host = GetHost();
231 CHECK_NULL_VOID(host);
232 auto renderContext = host->GetRenderContext();
233 renderContext->UpdateBackgroundColor(Color::BLACK);
234 }
235
NativeXComponentChange(float width,float height)236 void XComponentPattern::NativeXComponentChange(float width, float height)
237 {
238 CHECK_RUN_ON(UI);
239 CHECK_NULL_VOID(nativeXComponent_);
240 CHECK_NULL_VOID(nativeXComponentImpl_);
241 nativeXComponentImpl_->SetXComponentWidth(static_cast<int32_t>(width));
242 nativeXComponentImpl_->SetXComponentHeight(static_cast<int32_t>(height));
243 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
244 const auto* callback = nativeXComponentImpl_->GetCallback();
245 CHECK_NULL_VOID_NOLOG(callback);
246 CHECK_NULL_VOID_NOLOG(callback->OnSurfaceChanged);
247 callback->OnSurfaceChanged(nativeXComponent_.get(), surface);
248 }
249
NativeXComponentDestroy()250 void XComponentPattern::NativeXComponentDestroy()
251 {
252 CHECK_RUN_ON(UI);
253 CHECK_NULL_VOID(nativeXComponent_);
254 CHECK_NULL_VOID(nativeXComponentImpl_);
255 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
256 const auto* callback = nativeXComponentImpl_->GetCallback();
257 CHECK_NULL_VOID_NOLOG(callback);
258 CHECK_NULL_VOID_NOLOG(callback->OnSurfaceDestroyed);
259 callback->OnSurfaceDestroyed(nativeXComponent_.get(), surface);
260 }
261
NativeXComponentOffset(double x,double y)262 void XComponentPattern::NativeXComponentOffset(double x, double y)
263 {
264 CHECK_RUN_ON(UI);
265 CHECK_NULL_VOID(nativeXComponent_);
266 CHECK_NULL_VOID(nativeXComponentImpl_);
267 auto pipelineContext = PipelineContext::GetCurrentContext();
268 CHECK_NULL_VOID(pipelineContext);
269 float scale = pipelineContext->GetViewScale();
270 nativeXComponentImpl_->SetXComponentOffsetX(x * scale);
271 nativeXComponentImpl_->SetXComponentOffsetY(y * scale);
272 }
273
NativeXComponentDispatchTouchEvent(const OH_NativeXComponent_TouchEvent & touchEvent,const std::vector<XComponentTouchPoint> & xComponentTouchPoints)274 void XComponentPattern::NativeXComponentDispatchTouchEvent(
275 const OH_NativeXComponent_TouchEvent& touchEvent, const std::vector<XComponentTouchPoint>& xComponentTouchPoints)
276 {
277 CHECK_RUN_ON(UI);
278 CHECK_NULL_VOID(nativeXComponent_);
279 CHECK_NULL_VOID(nativeXComponentImpl_);
280 nativeXComponentImpl_->SetTouchEvent(touchEvent);
281 nativeXComponentImpl_->SetTouchPoint(xComponentTouchPoints);
282 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
283 const auto* callback = nativeXComponentImpl_->GetCallback();
284 CHECK_NULL_VOID_NOLOG(callback);
285 CHECK_NULL_VOID_NOLOG(callback->DispatchTouchEvent);
286 callback->DispatchTouchEvent(nativeXComponent_.get(), surface);
287 }
288
InitNativeWindow(float textureWidth,float textureHeight)289 void XComponentPattern::InitNativeWindow(float textureWidth, float textureHeight)
290 {
291 auto context = PipelineContext::GetCurrentContext();
292 CHECK_NULL_VOID(context);
293 if (renderSurface_->IsSurfaceValid() && type_ == XComponentType::SURFACE) {
294 float viewScale = context->GetViewScale();
295 renderSurface_->CreateNativeWindow();
296 renderSurface_->AdjustNativeWindowSize(
297 static_cast<uint32_t>(textureWidth * viewScale), static_cast<uint32_t>(textureHeight * viewScale));
298 }
299 }
300
XComponentSizeInit()301 void XComponentPattern::XComponentSizeInit()
302 {
303 ContainerScope scope(scopeId_);
304 auto context = PipelineContext::GetCurrentContext();
305 CHECK_NULL_VOID(context);
306 InitNativeWindow(initSize_.Width(), initSize_.Height());
307 auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::JS);
308 platformTaskExecutor.PostTask([weak = WeakClaim(this)] {
309 auto pattern = weak.Upgrade();
310 CHECK_NULL_VOID(pattern);
311 auto host = pattern->GetHost();
312 CHECK_NULL_VOID(host);
313 auto xcId = pattern->GetId();
314 auto eventHub = host->GetEventHub<XComponentEventHub>();
315 eventHub->FireSurfaceInitEvent(xcId, host->GetId());
316 eventHub->FireLoadEvent(xcId);
317 });
318 }
319
XComponentSizeChange(float textureWidth,float textureHeight)320 void XComponentPattern::XComponentSizeChange(float textureWidth, float textureHeight)
321 {
322 ContainerScope scope(scopeId_);
323 auto context = PipelineContext::GetCurrentContext();
324 CHECK_NULL_VOID(context);
325 auto viewScale = context->GetViewScale();
326 renderSurface_->AdjustNativeWindowSize(
327 static_cast<uint32_t>(textureWidth * viewScale), static_cast<uint32_t>(textureHeight * viewScale));
328 NativeXComponentChange(textureWidth, textureHeight);
329 }
330
InitEvent()331 void XComponentPattern::InitEvent()
332 {
333 auto host = GetHost();
334 CHECK_NULL_VOID(host);
335 auto eventHub = host->GetEventHub<XComponentEventHub>();
336 CHECK_NULL_VOID(eventHub);
337 eventHub->SetOnSurfaceInitEvent(CreateExternalEvent());
338 auto gestureHub = eventHub->GetOrCreateGestureEventHub();
339 CHECK_NULL_VOID(gestureHub);
340 InitTouchEvent(gestureHub);
341 auto inputHub = eventHub->GetOrCreateInputEventHub();
342 InitMouseEvent(inputHub);
343 InitMouseHoverEvent(inputHub);
344 }
345
InitTouchEvent(const RefPtr<GestureEventHub> & gestureHub)346 void XComponentPattern::InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub)
347 {
348 CHECK_NULL_VOID_NOLOG(!touchEvent_);
349
350 auto touchTask = [weak = WeakClaim(this)](const TouchEventInfo& info) {
351 auto pattern = weak.Upgrade();
352 CHECK_NULL_VOID(pattern);
353 pattern->HandleTouchEvent(info);
354 };
355
356 touchEvent_ = MakeRefPtr<TouchEventImpl>(std::move(touchTask));
357 gestureHub->AddTouchEvent(touchEvent_);
358 }
359
InitMouseEvent(const RefPtr<InputEventHub> & inputHub)360 void XComponentPattern::InitMouseEvent(const RefPtr<InputEventHub>& inputHub)
361 {
362 CHECK_NULL_VOID_NOLOG(!mouseEvent_);
363
364 auto mouseTask = [weak = WeakClaim(this)](const MouseInfo& info) {
365 auto pattern = weak.Upgrade();
366 CHECK_NULL_VOID(pattern);
367 pattern->HandleMouseEvent(info);
368 };
369
370 mouseEvent_ = MakeRefPtr<InputEvent>(std::move(mouseTask));
371 inputHub->AddOnMouseEvent(mouseEvent_);
372 }
373
InitMouseHoverEvent(const RefPtr<InputEventHub> & inputHub)374 void XComponentPattern::InitMouseHoverEvent(const RefPtr<InputEventHub>& inputHub)
375 {
376 CHECK_NULL_VOID_NOLOG(!mouseHoverEvent_);
377 auto mouseHoverTask = [weak = WeakClaim(this)](bool isHover) {
378 auto pattern = weak.Upgrade();
379 CHECK_NULL_VOID(pattern);
380 pattern->HandleMouseHoverEvent(isHover);
381 };
382 mouseHoverEvent_ = MakeRefPtr<InputEvent>(std::move(mouseHoverTask));
383 inputHub->AddOnHoverEvent(mouseHoverEvent_);
384 }
385
HandleTouchEvent(const TouchEventInfo & info)386 void XComponentPattern::HandleTouchEvent(const TouchEventInfo& info)
387 {
388 auto touchInfoList = info.GetChangedTouches();
389 if (touchInfoList.empty()) {
390 return;
391 }
392 const auto& locationInfo = touchInfoList.front();
393 const auto& screenOffset = locationInfo.GetGlobalLocation();
394 const auto& localOffset = locationInfo.GetLocalLocation();
395 touchEventPoint_.id = locationInfo.GetFingerId();
396 touchEventPoint_.screenX = static_cast<float>(screenOffset.GetX());
397 touchEventPoint_.screenY = static_cast<float>(screenOffset.GetY());
398 touchEventPoint_.x = static_cast<float>(localOffset.GetX());
399 touchEventPoint_.y = static_cast<float>(localOffset.GetY());
400 touchEventPoint_.size = locationInfo.GetSize();
401 touchEventPoint_.force = locationInfo.GetForce();
402 touchEventPoint_.deviceId = locationInfo.GetTouchDeviceId();
403 const auto timeStamp = info.GetTimeStamp().time_since_epoch().count();
404 touchEventPoint_.timeStamp = timeStamp;
405 auto touchType = touchInfoList.front().GetTouchType();
406 touchEventPoint_.type = ConvertNativeXComponentTouchEvent(touchType);
407 #ifdef OHOS_PLATFORM
408 // increase cpu frequency
409 if (touchType == TouchType::MOVE) {
410 auto currentTime = GetSysTimestamp();
411 auto increaseCpuTime = currentTime - startIncreaseTime_;
412 if (increaseCpuTime >= INCREASE_CPU_TIME_ONCE) {
413 LOGD("HandleTouchEvent increase cpu frequency");
414 startIncreaseTime_ = currentTime;
415 ResSchedReport::GetInstance().ResSchedDataReport("slide_on");
416 }
417 } else if (touchType == TouchType::UP) {
418 startIncreaseTime_ = 0;
419 ResSchedReport::GetInstance().ResSchedDataReport("slide_off");
420 }
421 #endif
422 SetTouchPoint(info.GetTouches(), timeStamp, touchType);
423
424 NativeXComponentDispatchTouchEvent(touchEventPoint_, nativeXComponentTouchPoints_);
425 }
426
HandleMouseEvent(const MouseInfo & info)427 void XComponentPattern::HandleMouseEvent(const MouseInfo& info)
428 {
429 OH_NativeXComponent_MouseEvent mouseEventPoint;
430 mouseEventPoint.x = static_cast<float>(info.GetLocalLocation().GetX());
431 mouseEventPoint.y = static_cast<float>(info.GetLocalLocation().GetY());
432 mouseEventPoint.screenX = static_cast<float>(info.GetScreenLocation().GetX());
433 mouseEventPoint.screenY = static_cast<float>(info.GetScreenLocation().GetY());
434 switch (info.GetAction()) {
435 case MouseAction::PRESS:
436 mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_PRESS;
437 break;
438 case MouseAction::RELEASE:
439 mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_RELEASE;
440 break;
441 case MouseAction::MOVE:
442 mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_MOVE;
443 break;
444 default:
445 mouseEventPoint.action = OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_NONE;
446 break;
447 }
448 switch (info.GetButton()) {
449 case MouseButton::LEFT_BUTTON:
450 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_LEFT_BUTTON;
451 break;
452 case MouseButton::RIGHT_BUTTON:
453 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_RIGHT_BUTTON;
454 break;
455 case MouseButton::MIDDLE_BUTTON:
456 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_MIDDLE_BUTTON;
457 break;
458 case MouseButton::BACK_BUTTON:
459 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_BACK_BUTTON;
460 break;
461 case MouseButton::FORWARD_BUTTON:
462 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_FORWARD_BUTTON;
463 break;
464 default:
465 mouseEventPoint.button = OH_NativeXComponent_MouseEventButton::OH_NATIVEXCOMPONENT_NONE_BUTTON;
466 break;
467 }
468 mouseEventPoint.timestamp = info.GetTimeStamp().time_since_epoch().count();
469 NativeXComponentDispatchMouseEvent(mouseEventPoint);
470 }
471
HandleMouseHoverEvent(bool isHover)472 void XComponentPattern::HandleMouseHoverEvent(bool isHover)
473 {
474 CHECK_RUN_ON(UI);
475 CHECK_NULL_VOID(nativeXComponent_);
476 CHECK_NULL_VOID(nativeXComponentImpl_);
477 const auto* callback = nativeXComponentImpl_->GetMouseEventCallback();
478 CHECK_NULL_VOID_NOLOG(callback);
479 CHECK_NULL_VOID_NOLOG(callback->DispatchHoverEvent);
480 callback->DispatchHoverEvent(nativeXComponent_.get(), isHover);
481 }
482
NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent & mouseEvent)483 void XComponentPattern::NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent)
484 {
485 CHECK_RUN_ON(UI);
486 CHECK_NULL_VOID(nativeXComponent_);
487 CHECK_NULL_VOID(nativeXComponentImpl_);
488 nativeXComponentImpl_->SetMouseEvent(mouseEvent);
489 auto* surface = const_cast<void*>(nativeXComponentImpl_->GetSurface());
490 const auto* callback = nativeXComponentImpl_->GetMouseEventCallback();
491 CHECK_NULL_VOID_NOLOG(callback);
492 CHECK_NULL_VOID_NOLOG(callback->DispatchMouseEvent);
493 callback->DispatchMouseEvent(nativeXComponent_.get(), surface);
494 }
495
SetTouchPoint(const std::list<TouchLocationInfo> & touchInfoList,const int64_t timeStamp,const TouchType & touchType)496 void XComponentPattern::SetTouchPoint(
497 const std::list<TouchLocationInfo>& touchInfoList, const int64_t timeStamp, const TouchType& touchType)
498 {
499 touchEventPoint_.numPoints =
500 touchInfoList.size() <= OH_MAX_TOUCH_POINTS_NUMBER ? touchInfoList.size() : OH_MAX_TOUCH_POINTS_NUMBER;
501 nativeXComponentTouchPoints_.clear();
502 uint32_t index = 0;
503 for (auto iterator = touchInfoList.begin(); iterator != touchInfoList.end() && index < OH_MAX_TOUCH_POINTS_NUMBER;
504 iterator++) {
505 OH_NativeXComponent_TouchPoint ohTouchPoint;
506 const auto& pointTouchInfo = *iterator;
507 const auto& pointScreenOffset = pointTouchInfo.GetGlobalLocation();
508 const auto& pointLocalOffset = pointTouchInfo.GetLocalLocation();
509 ohTouchPoint.id = pointTouchInfo.GetFingerId();
510 ohTouchPoint.screenX = static_cast<float>(pointScreenOffset.GetX());
511 ohTouchPoint.screenY = static_cast<float>(pointScreenOffset.GetY());
512 ohTouchPoint.x = static_cast<float>(pointLocalOffset.GetX());
513 ohTouchPoint.y = static_cast<float>(pointLocalOffset.GetY());
514 ohTouchPoint.type = ConvertNativeXComponentTouchEvent(touchType);
515 ohTouchPoint.size = pointTouchInfo.GetSize();
516 ohTouchPoint.force = pointTouchInfo.GetForce();
517 ohTouchPoint.timeStamp = timeStamp;
518 ohTouchPoint.isPressed = (touchType == TouchType::DOWN);
519 touchEventPoint_.touchPoints[index++] = ohTouchPoint;
520 // set tiltX, tiltY and sourceToolType
521 XComponentTouchPoint xcomponentTouchPoint;
522 xcomponentTouchPoint.tiltX = pointTouchInfo.GetTiltX().value_or(0.0f);
523 xcomponentTouchPoint.tiltY = pointTouchInfo.GetTiltY().value_or(0.0f);
524 xcomponentTouchPoint.sourceToolType = ConvertNativeXComponentTouchToolType(pointTouchInfo.GetSourceTool());
525 nativeXComponentTouchPoints_.emplace_back(xcomponentTouchPoint);
526 }
527 while (index < OH_MAX_TOUCH_POINTS_NUMBER) {
528 OH_NativeXComponent_TouchPoint ohTouchPoint;
529 ohTouchPoint.id = 0;
530 ohTouchPoint.screenX = 0;
531 ohTouchPoint.screenY = 0;
532 ohTouchPoint.x = 0;
533 ohTouchPoint.y = 0;
534 ohTouchPoint.type = OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UNKNOWN;
535 ohTouchPoint.size = 0;
536 ohTouchPoint.force = 0;
537 ohTouchPoint.timeStamp = 0;
538 ohTouchPoint.isPressed = false;
539 touchEventPoint_.touchPoints[index++] = ohTouchPoint;
540 }
541 }
542
CreateExternalEvent()543 ExternalEvent XComponentPattern::CreateExternalEvent()
544 {
545 return [weak = AceType::WeakClaim(this), scopeId = scopeId_](
546 const std::string& componentId, const uint32_t nodeId, const bool isDestroy) {
547 ContainerScope scope(scopeId);
548 auto context = PipelineContext::GetCurrentContext();
549 CHECK_NULL_VOID(context);
550 auto frontEnd = AceType::DynamicCast<DeclarativeFrontend>(context->GetFrontend());
551 CHECK_NULL_VOID(frontEnd);
552 auto jsEngine = frontEnd->GetJsEngine();
553 jsEngine->FireExternalEvent(componentId, nodeId, isDestroy);
554 };
555 }
556 } // namespace OHOS::Ace::NG
557