From da8c7008cedacb25d5bd499baceef2082c929df9 Mon Sep 17 00:00:00 2001 From: zhouchaobo Date: Sun, 3 Mar 2024 17:52:23 +0800 Subject: [PATCH] XComponent add axis event Signed-off-by: zhouchaobo Change-Id: I8669abd652aeadfbabbcfde972b9f459a79d09f8 --- .../native_interface_xcomponent_impl.cpp | 12 ++ .../native_interface_xcomponent_impl.h | 18 +- .../gestures/recognizers/pan_recognizer.h | 4 +- .../pattern/xcomponent/xcomponent_pattern.cpp | 35 ++++ .../pattern/xcomponent/xcomponent_pattern.h | 4 + frameworks/core/event/axis_event.h | 90 +++++---- interfaces/native/BUILD.gn | 1 + interfaces/native/event/ui_input_event.cpp | 137 +++++++++++++ interfaces/native/libace.ndk.json | 48 +++++ .../native/native_interface_xcomponent.cpp | 18 +- .../native/native_interface_xcomponent.h | 16 ++ interfaces/native/ui_input_event.h | 183 ++++++++++++++++++ .../core/event/event_manager_test_ng.cpp | 5 +- 13 files changed, 530 insertions(+), 41 deletions(-) create mode 100644 interfaces/native/event/ui_input_event.cpp create mode 100644 interfaces/native/ui_input_event.h diff --git a/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.cpp b/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.cpp index bfcea0d5bed..43c1585c531 100644 --- a/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.cpp +++ b/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.cpp @@ -17,6 +17,8 @@ #include "securec.h" +#include "base/error/error_code.h" + int32_t OH_NativeXComponent::GetXComponentId(char* id, uint64_t* size) { if (xcomponentImpl_ == nullptr) { @@ -257,3 +259,13 @@ int32_t OH_NativeXComponent::DetachNativeRootNode(void* root) xcomponentImpl_->DetachContainer(root); return OH_NATIVEXCOMPONENT_RESULT_SUCCESS; } + +int32_t OH_NativeXComponent::RegisterUIAxisEventCallback( + void (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event, ArkUI_UIInputEvent_Type type)) +{ + if (xcomponentImpl_ == nullptr) { + return OHOS::Ace::ERROR_CODE_PARAM_INVALID; + } + xcomponentImpl_->SetUIAxisEventCallback(callback); + return OH_NATIVEXCOMPONENT_RESULT_SUCCESS; +} diff --git a/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h b/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h index 81e75b71b6c..d7843aea7fc 100644 --- a/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h +++ b/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h @@ -22,6 +22,7 @@ #include #include "interfaces/native/native_interface_xcomponent.h" +#include "interfaces/native/ui_input_event.h" #include "base/memory/ace_type.h" #include "base/utils/utils.h" @@ -46,7 +47,9 @@ namespace OHOS::Ace { class NativeXComponentImpl : public virtual AceType { DECLARE_ACE_TYPE(NativeXComponentImpl, AceType); using NativeXComponent_Callback = void (*)(OH_NativeXComponent*, void*); - using SetExpectedRateRangeEvent_Callback = std::function; + using NativeXComponent_UIEventCallback = void (*)( + OH_NativeXComponent*, ArkUI_UIInputEvent*, ArkUI_UIInputEvent_Type); + using SetExpectedRateRangeEvent_Callback = std::function; using SetOnFrameEvent_Callback = std::function; using SetUnregisterOnFrameEvent_Callback = std::function; using OnFrame_Callback = void (*)(OH_NativeXComponent*, uint64_t, uint64_t); @@ -251,6 +254,16 @@ public: blurEventCallback_ = callback; } + void SetUIAxisEventCallback(NativeXComponent_UIEventCallback callback) + { + uiAxisEventCallback_ = callback; + } + + NativeXComponent_UIEventCallback GetUIAxisEventCallback() const + { + return uiAxisEventCallback_; + } + void SetOnFrameCallback(OnFrame_Callback callback) { onFrameCallback_ = callback; @@ -330,6 +343,7 @@ private: NativeXComponent_Callback focusEventCallback_ = nullptr; NativeXComponent_Callback keyEventCallback_ = nullptr; NativeXComponent_Callback blurEventCallback_ = nullptr; + NativeXComponent_UIEventCallback uiAxisEventCallback_ = nullptr; std::vector touchPoints_; std::vector historicalPoints_; OnFrame_Callback onFrameCallback_ = nullptr; @@ -365,6 +379,8 @@ struct OH_NativeXComponent { int32_t UnregisterOnFrameCallback(); int32_t AttachNativeRootNode(void* root); int32_t DetachNativeRootNode(void* root); + int32_t RegisterUIAxisEventCallback( + void (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event, ArkUI_UIInputEvent_Type type)); private: OHOS::Ace::NativeXComponentImpl* xcomponentImpl_ = nullptr; diff --git a/frameworks/core/components_ng/gestures/recognizers/pan_recognizer.h b/frameworks/core/components_ng/gestures/recognizers/pan_recognizer.h index 73d1c47345c..57ed518c080 100644 --- a/frameworks/core/components_ng/gestures/recognizers/pan_recognizer.h +++ b/frameworks/core/components_ng/gestures/recognizers/pan_recognizer.h @@ -131,9 +131,7 @@ private: PanDirection direction_; double distance_ = 0.0; double mouseDistance_ = 0.0; - AxisEvent lastAxisEvent_ = { - .action = AxisAction::NONE, - }; + AxisEvent lastAxisEvent_; Offset averageDistance_; std::map touchPointsDistance_; Offset delta_; diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp index 3b0bc9eaf95..54b68386c3f 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp @@ -15,6 +15,8 @@ #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" +#include "interfaces/native/ui_input_event.h" + #include "base/geometry/ng/size_t.h" #include "base/log/log_wrapper.h" #include "base/ressched/ressched_report.h" @@ -22,6 +24,7 @@ #include "base/utils/utils.h" #include "core/components/common/layout/constants.h" #include "core/components_ng/pattern/xcomponent/xcomponent_controller_ng.h" +#include "core/event/axis_event.h" #ifdef NG_BUILD #include "bridge/declarative_frontend/ng/declarative_frontend_ng.h" #else @@ -478,6 +481,7 @@ void XComponentPattern::InitEvent() InitTouchEvent(gestureHub); auto inputHub = eventHub->GetOrCreateInputEventHub(); InitMouseEvent(inputHub); + InitAxisEvent(inputHub); InitMouseHoverEvent(inputHub); auto focusHub = host->GetOrCreateFocusHub(); CHECK_NULL_VOID(focusHub); @@ -556,6 +560,20 @@ void XComponentPattern::InitTouchEvent(const RefPtr& gestureHub gestureHub->AddTouchEvent(touchEvent_); } +void XComponentPattern::InitAxisEvent(const RefPtr& inputHub) +{ + CHECK_NULL_VOID(!axisEvent_); + + auto axisTask = [weak = WeakClaim(this)](const AxisInfo& info) { + auto pattern = weak.Upgrade(); + CHECK_NULL_VOID(pattern); + pattern->HandleAxisEvent(info); + }; + + axisEvent_ = MakeRefPtr(std::move(axisTask)); + inputHub->AddOnAxisEvent(axisEvent_); +} + void XComponentPattern::InitMouseEvent(const RefPtr& inputHub) { CHECK_NULL_VOID(!mouseEvent_); @@ -670,6 +688,12 @@ void XComponentPattern::HandleMouseEvent(const MouseInfo& info) NativeXComponentDispatchMouseEvent(mouseEventPoint); } +void XComponentPattern::HandleAxisEvent(const AxisInfo& info) +{ + auto axisEvent = info.ConvertToAxisEvent(); + NativeXComponentDispatchAxisEvent(&axisEvent); +} + void XComponentPattern::HandleMouseHoverEvent(bool isHover) { CHECK_RUN_ON(UI); @@ -694,6 +718,17 @@ void XComponentPattern::NativeXComponentDispatchMouseEvent(const OH_NativeXCompo callback->DispatchMouseEvent(nativeXComponent_.get(), surface); } +void XComponentPattern::NativeXComponentDispatchAxisEvent(AxisEvent* axisEvent) +{ + CHECK_RUN_ON(UI); + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + const auto callback = nativeXComponentImpl_->GetUIAxisEventCallback(); + CHECK_NULL_VOID(callback); + auto* uiEvent = static_cast(axisEvent); + callback(nativeXComponent_.get(), uiEvent, ArkUI_UIInputEvent_Type::ARKUI_UIINPUTEVENT_TYPE_AXIS); +} + void XComponentPattern::SetTouchPoint( const std::list& touchInfoList, const int64_t timeStamp, const TouchType& touchType) { diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h index 2ba63aba73a..400b5670bf7 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h @@ -128,6 +128,7 @@ public: void NativeXComponentDispatchTouchEvent(const OH_NativeXComponent_TouchEvent& touchEvent, const std::vector& xComponentTouchPoints); void NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent); + void NativeXComponentDispatchAxisEvent(AxisEvent* axisEvent); void InitNativeWindow(float textureWidth, float textureHeight); void XComponentSizeInit(); @@ -256,9 +257,11 @@ private: void InitNativeNodeCallbacks(); void InitEvent(); void InitTouchEvent(const RefPtr& gestureHub); + void InitAxisEvent(const RefPtr& inputHub); void HandleTouchEvent(const TouchEventInfo& info); void InitMouseEvent(const RefPtr& inputHub); void HandleMouseEvent(const MouseInfo& info); + void HandleAxisEvent(const AxisInfo& info); void InitMouseHoverEvent(const RefPtr& inputHub); void HandleMouseHoverEvent(bool isHover); void InitFocusEvent(const RefPtr& focusHub); @@ -299,6 +302,7 @@ private: RefPtr touchEvent_; OH_NativeXComponent_TouchEvent touchEventPoint_; RefPtr mouseEvent_; + RefPtr axisEvent_; RefPtr mouseHoverEvent_; std::vector nativeXComponentTouchPoints_; RefPtr extSurfaceClient_; diff --git a/frameworks/core/event/axis_event.h b/frameworks/core/event/axis_event.h index c1efe3d47b6..c9dcfe48469 100644 --- a/frameworks/core/event/axis_event.h +++ b/frameworks/core/event/axis_event.h @@ -18,6 +18,8 @@ #include +#include "interfaces/native/ui_input_event.h" + #include "base/geometry/offset.h" #include "base/memory/ace_type.h" #include "core/event/ace_events.h" @@ -55,7 +57,14 @@ enum class AxisAction : int32_t { CANCEL, }; -struct AxisEvent final { +struct UIInputEvent : public ArkUI_UIInputEvent { + virtual ~UIInputEvent() = default; + ArkUI_UIInputEvent_Type eventType = ArkUI_UIInputEvent_Type::ARKUI_UIINPUTEVENT_TYPE_UNKNOWN; + TimeStamp time; +}; + +struct AxisEvent final : public UIInputEvent { + ~AxisEvent() = default; int32_t id = 0; float x = 0.0; float y = 0.0; @@ -66,50 +75,43 @@ struct AxisEvent final { double pinchAxisScale = 0.0; double rotateAxisAngle = 0.0; bool isRotationEvent = false; - AxisAction action; - TimeStamp time; + AxisAction action = AxisAction::NONE; int64_t deviceId = 0; SourceType sourceType = SourceType::NONE; SourceTool sourceTool = SourceTool::UNKNOWN; std::shared_ptr pointerEvent; int32_t touchEventId; + // Coordinates relative to the upper-left corner of the current component + float localX = 0.0; + float localY = 0.0; + + AxisEvent() + { + eventType = ArkUI_UIInputEvent_Type::ARKUI_UIINPUTEVENT_TYPE_AXIS; + } + + AxisEvent(int32_t id, float x, float y, float screenX, float screenY, double verticalAxis, double horizontalAxis, + double pinchAxisScale, double rotateAxisAngle, bool isRotationEvent, AxisAction action, TimeStamp timestamp, + int64_t deviceId, SourceType sourceType, SourceTool sourceTool, std::shared_ptr pointerEvent) + : id(id), x(x), y(y), screenX(screenX), screenY(screenY), verticalAxis(verticalAxis), + horizontalAxis(horizontalAxis), pinchAxisScale(pinchAxisScale), rotateAxisAngle(rotateAxisAngle), + isRotationEvent(isRotationEvent), action(action), deviceId(deviceId), sourceType(sourceType), + sourceTool(sourceTool), pointerEvent(std::move(pointerEvent)) + { + eventType = ArkUI_UIInputEvent_Type::ARKUI_UIINPUTEVENT_TYPE_AXIS; + time = timestamp; + } + AxisEvent CreateScaleEvent(float scale) const { if (NearZero(scale)) { - return { .id = id, - .x = x, - .y = y, - .screenX = screenX, - .screenY = screenY, - .verticalAxis = verticalAxis, - .horizontalAxis = horizontalAxis, - .pinchAxisScale = pinchAxisScale, - .rotateAxisAngle = rotateAxisAngle, - .isRotationEvent = isRotationEvent, - .action = action, - .time = time, - .deviceId = deviceId, - .sourceType = sourceType, - .sourceTool = sourceTool, - .pointerEvent = pointerEvent }; + return { id, x, y, screenX, screenY, verticalAxis, horizontalAxis, pinchAxisScale, rotateAxisAngle, + isRotationEvent, action, time, deviceId, sourceType, sourceTool, pointerEvent }; } - return { .id = id, - .x = x / scale, - .y = y / scale, - .screenX = screenX / scale, - .screenY = screenY / scale, - .verticalAxis = verticalAxis, - .horizontalAxis = horizontalAxis, - .pinchAxisScale = pinchAxisScale, - .rotateAxisAngle = rotateAxisAngle, - .isRotationEvent = isRotationEvent, - .action = action, - .time = time, - .deviceId = deviceId, - .sourceType = sourceType, - .sourceTool = sourceTool, - .pointerEvent = pointerEvent }; + return { id, x / scale, y / scale, screenX / scale, screenY / scale, verticalAxis, horizontalAxis, + pinchAxisScale, rotateAxisAngle, isRotationEvent, action, time, deviceId, sourceType, sourceTool, + pointerEvent }; } Offset GetOffset() const @@ -291,6 +293,23 @@ public: return globalLocation_; } + AxisEvent ConvertToAxisEvent() const + { + AxisEvent axisEvent; + axisEvent.x = static_cast(globalLocation_.GetX()); + axisEvent.y = static_cast(globalLocation_.GetY()); + axisEvent.screenX = static_cast(screenLocation_.GetX()); + axisEvent.screenY = static_cast(screenLocation_.GetY()); + axisEvent.horizontalAxis = horizontalAxis_; + axisEvent.verticalAxis = verticalAxis_; + axisEvent.pinchAxisScale = pinchAxisScale_; + axisEvent.rotateAxisAngle = rotateAxisAngle_; + axisEvent.time = timeStamp_; + axisEvent.localX = static_cast(localLocation_.GetX()); + axisEvent.localY = static_cast(localLocation_.GetY()); + return axisEvent; + } + private: AxisAction action_ = AxisAction::NONE; float verticalAxis_ = 0.0; @@ -358,6 +377,7 @@ public: Offset localLocation = Offset( event.GetOffset().GetX() - coordinateOffset_.GetX(), event.GetOffset().GetY() - coordinateOffset_.GetY()); AxisInfo info = AxisInfo(event, localLocation, GetEventTarget().value_or(EventTarget())); + info.SetScreenLocation(Offset(event.screenX, event.screenY)); onAxisCallback_(info); return true; } diff --git a/interfaces/native/BUILD.gn b/interfaces/native/BUILD.gn index 90d15c8502f..3d75f72fa6a 100644 --- a/interfaces/native/BUILD.gn +++ b/interfaces/native/BUILD.gn @@ -47,6 +47,7 @@ ohos_shared_library("ace_ndk") { sources = [ "//foundation/arkui/ace_engine/frameworks/core/components/common/properties/color.cpp", "//foundation/arkui/ace_engine/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.cpp", + "event/ui_input_event.cpp", "native_interface_xcomponent.cpp", "node/event_converter.cpp", "node/native_impl.cpp", diff --git a/interfaces/native/event/ui_input_event.cpp b/interfaces/native/event/ui_input_event.cpp new file mode 100644 index 00000000000..a30b4ee74ef --- /dev/null +++ b/interfaces/native/event/ui_input_event.cpp @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "interfaces/native/ui_input_event.h" + +#include "frameworks/core/event/axis_event.h" +#include "frameworks/base/utils/type_definition.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t OH_ArkUI_UIInputEvent_GetType(const ArkUI_UIInputEvent* event) +{ + const auto *uiEvent = static_cast(event); + if (!uiEvent) { + LOGE("The parameter of OH_ArkUI_UIInputEvent_GetType is invalid"); + return 0; + } + return uiEvent->eventType; +} + +int64_t OH_ArkUI_UIInputEvent_GetEventTime(const ArkUI_UIInputEvent *event) +{ + const auto *uiEvent = static_cast(event); + if (!uiEvent) { + LOGE("The parameter of OH_ArkUI_UIInputEvent_GetEventTime is invalid"); + return 0; + } + return uiEvent->time.time_since_epoch().count(); +} + +float OH_ArkUI_PointerEvent_GetX(const ArkUI_UIInputEvent *event) +{ + const auto *axisEvent = static_cast(event); + if (axisEvent) { + return axisEvent->localX; + } + LOGE("The parameter of OH_ArkUI_PointerEvent_GetX is invalid"); + return 0.0f; +} + +float OH_ArkUI_PointerEvent_GetY(const ArkUI_UIInputEvent *event) +{ + const auto *axisEvent = static_cast(event); + if (axisEvent) { + return axisEvent->localY; + } + LOGE("The parameter of OH_ArkUI_PointerEvent_GetY is invalid"); + return 0.0f; +} + +float OH_ArkUI_PointerEvent_GetWindowX(const ArkUI_UIInputEvent* event) +{ + const auto *axisEvent = static_cast(event); + if (axisEvent) { + return axisEvent->x; + } + LOGE("The parameter of OH_ArkUI_PointerEvent_GetWindowX is invalid"); + return 0.0f; +} + +float OH_ArkUI_PointerEvent_GetWindowY(const ArkUI_UIInputEvent* event) +{ + const auto *axisEvent = static_cast(event); + if (axisEvent) { + return axisEvent->y; + } + LOGE("The parameter of OH_ArkUI_PointerEvent_GetWindowY is invalid"); + return 0.0f; +} + +float OH_ArkUI_PointerEvent_GetDisplayX(const ArkUI_UIInputEvent* event) +{ + const auto *axisEvent = static_cast(event); + if (axisEvent) { + return axisEvent->screenX; + } + LOGE("The parameter of OH_ArkUI_PointerEvent_GetDisplayX is invalid"); + return 0.0f; +} + +float OH_ArkUI_PointerEvent_GetDisplayY(const ArkUI_UIInputEvent* event) +{ + const auto *axisEvent = static_cast(event); + if (axisEvent) { + return axisEvent->screenY; + } + LOGE("The parameter of OH_ArkUI_PointerEvent_GetDisplayY is invalid"); + return 0.0f; +} + +double OH_ArkUI_AxisEvent_GetVerticalAxisValue(const ArkUI_UIInputEvent* event) +{ + const auto *axisEvent = static_cast(event); + if (!axisEvent) { + LOGE("The parameter of OH_ArkUI_AxisEvent_GetVerticalAxisValue is invalid"); + return 0.0; + } + return axisEvent->verticalAxis; +} + +double OH_ArkUI_AxisEvent_GetHorizontalAxisValue(const ArkUI_UIInputEvent* event) +{ + const auto *axisEvent = static_cast(event); + if (!axisEvent) { + LOGE("The parameter of OH_ArkUI_AxisEvent_GetHorizontalAxisValue is invalid"); + return 0.0; + } + return axisEvent->horizontalAxis; +} + +double OH_ArkUI_AxisEvent_GetPinchAxisScaleValue(const ArkUI_UIInputEvent *event) +{ + const auto *axisEvent = static_cast(event); + if (!axisEvent) { + LOGE("The parameter of OH_ArkUI_AxisEvent_GetPinchAxisScaleValue is invalid"); + return 0.0; + } + return axisEvent->pinchAxisScale; +} + +#ifdef __cplusplus +}; +#endif diff --git a/interfaces/native/libace.ndk.json b/interfaces/native/libace.ndk.json index 89d54bffe9d..b6a6f560e23 100644 --- a/interfaces/native/libace.ndk.json +++ b/interfaces/native/libace.ndk.json @@ -106,5 +106,53 @@ { "first_introduced": "12", "name": "OH_ArkUI_GetNodeHandleFromNapiValue" + }, + { + "first_introduced": "12", + "name": "OH_NativeXComponent_RegisterUIInputEventCallback" + }, + { + "first_introduced": "12", + "name": "OH_ArkUI_UIInputEvent_GetType" + }, + { + "first_introduced": "12", + "name": "OH_ArkUI_UIInputEvent_GetEventTime" + }, + { + "first_introduced": "12", + "name": "OH_ArkUI_PointerEvent_GetX" + }, + { + "first_introduced": "12", + "name": "OH_ArkUI_PointerEvent_GetY" + }, + { + "first_introduced": "12", + "name": "OH_ArkUI_PointerEvent_GetWindowX" + }, + { + "first_introduced": "12", + "name": "OH_ArkUI_PointerEvent_GetWindowY" + }, + { + "first_introduced": "12", + "name": "OH_ArkUI_PointerEvent_GetDisplayX" + }, + { + "first_introduced": "12", + "name": "OH_ArkUI_PointerEvent_GetDisplayY" + }, + { + "first_introduced": "12", + "name": "OH_ArkUI_AxisEvent_GetVerticalAxisValue" + }, + { + "first_introduced": "12", + "name": "OH_ArkUI_AxisEvent_GetHorizontalAxisValue" + }, + { + "first_introduced": "12", + "name": "OH_ArkUI_AxisEvent_GetPinchAxisScaleValue" } ] diff --git a/interfaces/native/native_interface_xcomponent.cpp b/interfaces/native/native_interface_xcomponent.cpp index ac180448cd6..03b7f32e2a6 100644 --- a/interfaces/native/native_interface_xcomponent.cpp +++ b/interfaces/native/native_interface_xcomponent.cpp @@ -15,8 +15,11 @@ #include "native_interface_xcomponent.h" -#include "frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h" #include "node/node_model.h" +#include "ui_input_event.h" + +#include "base/error/error_code.h" +#include "frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h" #ifdef __cplusplus extern "C" { @@ -247,6 +250,19 @@ int32_t OH_NativeXComponent_DetachNativeRootNode( return component->DetachNativeRootNode(root->uiNodeHandle); } +int32_t OH_NativeXComponent_RegisterUIInputEventCallback(OH_NativeXComponent* component, + void (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event, ArkUI_UIInputEvent_Type type), + ArkUI_UIInputEvent_Type type) +{ + if ((component == nullptr) || (callback == nullptr)) { + return OHOS::Ace::ERROR_CODE_PARAM_INVALID; + } + if (type == ArkUI_UIInputEvent_Type::ARKUI_UIINPUTEVENT_TYPE_AXIS) { + return component->RegisterUIAxisEventCallback(callback); + } + return OHOS::Ace::ERROR_CODE_PARAM_INVALID; +} + #ifdef __cplusplus }; #endif diff --git a/interfaces/native/native_interface_xcomponent.h b/interfaces/native/native_interface_xcomponent.h index e6c8470ca5d..65737060450 100644 --- a/interfaces/native/native_interface_xcomponent.h +++ b/interfaces/native/native_interface_xcomponent.h @@ -41,6 +41,7 @@ #include "native_type.h" #include "native_xcomponent_key_event.h" +#include "ui_input_event.h" #ifdef __cplusplus extern "C" { @@ -646,6 +647,21 @@ int32_t OH_NativeXComponent_AttachNativeRootNode(OH_NativeXComponent* component, */ int32_t OH_NativeXComponent_DetachNativeRootNode(OH_NativeXComponent* component, ArkUI_NodeHandle root); +/** + * @brief Registers a UI input event callback for this OH_NativeXComponent instance and enables the callback to + * be invoked when a UI input event is received. + * + * @param component Indicates the pointer to the OH_NativeXComponent instance. + * @param callback Indicates the pointer to the UI input event callback. + * @param type Indicates the type of the current UI input event. + * @return Returns 0 if success. + * Returns 401 if a parameter exception occurs. + * @since 12 + */ +int32_t OH_NativeXComponent_RegisterUIInputEventCallback(OH_NativeXComponent* component, + void (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event, ArkUI_UIInputEvent_Type type), + ArkUI_UIInputEvent_Type type); + #ifdef __cplusplus }; #endif diff --git a/interfaces/native/ui_input_event.h b/interfaces/native/ui_input_event.h new file mode 100644 index 00000000000..93f1430f6d3 --- /dev/null +++ b/interfaces/native/ui_input_event.h @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup ArkUI_EventModule + * @{ + * + * @brief Declares the UI input event capabilities provided by ArkUI on the native side. + * + * @since 12 + */ + +/** + * @file ui_input_event.h + * + * @brief Provides ArkUI event definitions on the native side. + * + * @library libace_ndk.z.so + * @syscap SystemCapability.ArkUI.ArkUI.Full + * @since 12 + */ + +#ifndef _ARKUI_UI_INPUT_EVENT_H_ +#define _ARKUI_UI_INPUT_EVENT_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Defines the UI input event. + * + * @since 12 + */ +struct ArkUI_UIInputEvent { + virtual ~ArkUI_UIInputEvent() {} +}; + +/** + * @brief Enumerates the UI input event types. + * + * @since 12 + */ +typedef enum { + ARKUI_UIINPUTEVENT_TYPE_UNKNOWN = 0, + ARKUI_UIINPUTEVENT_TYPE_TOUCH = 1, + ARKUI_UIINPUTEVENT_TYPE_AXIS = 2, +} ArkUI_UIInputEvent_Type; + +/** + * @brief Obtains the type of this UI input event. + * + * @param event Indicates the pointer to the current UI input event. + * @return Returns the type of the current UI input event; returns 0 if any parameter error occurs. + * @since 12 + */ +int32_t OH_ArkUI_UIInputEvent_GetType(const ArkUI_UIInputEvent* event); + +/** + * @brief Obtains the time when this UI input event occurs. + * + * @param event Indicates the pointer to the current UI input event. + * @return Returns the time when the UI input event occurs; returns 0 if any parameter error occurs. + * @since 12 + */ +int64_t OH_ArkUI_UIInputEvent_GetEventTime(const ArkUI_UIInputEvent* event); + +/** + * @brief Obtains the X coordinate relative to the upper left corner of the current component from a directional + * input event (such as a touch event, mouse event, or axis event). + * + * @param event Indicates the pointer to the directional input event. + * @return Returns the X coordinate relative to the upper left corner of the current component; + * returns 0 if any parameter error occurs. + * @since 12 + */ +float OH_ArkUI_PointerEvent_GetX(const ArkUI_UIInputEvent* event); + +/** + * @brief Obtains the Y coordinate relative to the upper left corner of the current component from a directional + * input event (such as a touch event, mouse event, or axis event). + * + * @param event Indicates the pointer to the UI input event. + * @return Returns the Y coordinate relative to the upper left corner of the current component; + * returns 0 if any parameter error occurs. + * @since 12 + */ +float OH_ArkUI_PointerEvent_GetY(const ArkUI_UIInputEvent* event); + +/** + * @brief Obtains the X coordinate relative to the upper left corner of the current application window from a + * directional input event (such as a touch event, mouse event, or axis event). + * + * @param event Indicates the pointer to the UI input event. + * @return Returns the X coordinate relative to the upper left corner of the current application window; + * returns 0 if any parameter error occurs. + * @since 12 + */ +float OH_ArkUI_PointerEvent_GetWindowX(const ArkUI_UIInputEvent* event); + +/** + * @brief Obtains the Y coordinate relative to the upper left corner of the current application window from a + * directional input event (such as a touch event, mouse event, or axis event). + * + * @param event Indicates the pointer to the UI input event. + * @return Returns the Y coordinate relative to the upper left corner of the current application window; + * returns 0 if any parameter error occurs. + * @since 12 + */ +float OH_ArkUI_PointerEvent_GetWindowY(const ArkUI_UIInputEvent* event); + +/** + * @brief Obtains the X coordinate relative to the upper left corner of the current screen from a directional input + * event (such as a touch event, mouse event, or axis event). + * + * @param event Indicates the pointer to the UI input event. + * @return Returns the X coordinate relative to the upper left corner of the current screen; + * returns 0 if any parameter error occurs. + * @since 12 + */ +float OH_ArkUI_PointerEvent_GetDisplayX(const ArkUI_UIInputEvent* event); + +/** + * @brief Obtains the Y coordinate relative to the upper left corner of the current screen from a directional input + * event (such as a touch event, mouse event, or axis event). + * + * @param event Indicates the pointer to the UI input event. + * @return Returns the Y coordinate relative to the upper left corner of the current screen; + * returns 0 if any parameter error occurs. + * @since 12 + */ +float OH_ArkUI_PointerEvent_GetDisplayY(const ArkUI_UIInputEvent* event); + +/** + * @brief Obtains the value of the vertical scroll axis for this axis event. + * + * @param event Indicates the pointer to the UI input event. + * @return Returns the value of the vertical scroll axis of the current axis event; + * returns 0 if any parameter error occurs. + * @since 12 + */ +double OH_ArkUI_AxisEvent_GetVerticalAxisValue(const ArkUI_UIInputEvent* event); + +/** + * @brief Obtains the value of the horizontal scroll axis for this axis event. + * + * @param event Indicates the pointer to the UI input event. + * @return Returns the value of the horizontal scroll axis of the current axis event; + * returns 0 if any parameter error occurs. + * @since 12 + */ +double OH_ArkUI_AxisEvent_GetHorizontalAxisValue(const ArkUI_UIInputEvent* event); + +/** + * @brief Obtains the scale value of the pinch axis for this axis event. + * + * @param event Indicates the pointer to the UI input event. + * @return Returns the scale value of the pinch axis of the current axis event; + * returns 0 if any parameter error occurs. + * @since 12 + */ +double OH_ArkUI_AxisEvent_GetPinchAxisScaleValue(const ArkUI_UIInputEvent* event); + +#ifdef __cplusplus +}; +#endif + +#endif // _ARKUI_UI_INPUT_EVENT_H_ +/** @} */ diff --git a/test/unittest/core/event/event_manager_test_ng.cpp b/test/unittest/core/event/event_manager_test_ng.cpp index 645c6bc6a78..ee6d23a3d0e 100644 --- a/test/unittest/core/event/event_manager_test_ng.cpp +++ b/test/unittest/core/event/event_manager_test_ng.cpp @@ -1312,7 +1312,10 @@ HWTEST_F(EventManagerTestNg, EventManagerTest027, TestSize.Level1) auto eventManager = AceType::MakeRefPtr(); ASSERT_NE(eventManager, nullptr); - AxisEvent axisEvent { .x = 1, .y = 2, .sourceType = SourceType::TOUCH }; + AxisEvent axisEvent; + axisEvent.x = 1; + axisEvent.y = 2; + axisEvent.sourceType = SourceType::TOUCH; auto nodeId = ElementRegister::GetInstance()->MakeUniqueId(); auto frameNode = FrameNode::GetOrCreateFrameNode(V2::LOCATION_BUTTON_ETS_TAG, nodeId, nullptr); eventManager->AxisTest(axisEvent, frameNode); -- Gitee