diff --git a/frameworks/core/components_ng/pattern/window_scene/scene/window_event_process.cpp b/frameworks/core/components_ng/pattern/window_scene/scene/window_event_process.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a786672cb907f614345b5a08f0e700953f78ace9 --- /dev/null +++ b/frameworks/core/components_ng/pattern/window_scene/scene/window_event_process.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2023 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 "window_event_process.h" + +#include + +#include "frameworks/core/event/ace_events.h" + +namespace OHOS { +namespace Rosen { +namespace { +constexpr double epsilon = -0.001f; +constexpr float MOUSE_RECT_HOT = 4; +constexpr float TOUCH_RECT_HOT = 20; +} // namespace + +WindowEventProcess::WindowEventProcess() {} +WindowEventProcess::~WindowEventProcess() {} + +bool WindowEventProcess::IsInWindowHotRect(const Ace::NG::RectF& windowSceneRect, + const Ace::NG::PointF& parentLocalPoint, int32_t souceType) +{ + float hotOffset = (souceType == static_cast(Ace::SourceType::MOUSE)) + ? MOUSE_RECT_HOT : TOUCH_RECT_HOT; + + float x = windowSceneRect.GetX(); + float y = windowSceneRect.GetY(); + float width = windowSceneRect.Width(); + float height = windowSceneRect.Height(); + float hotX = x - hotOffset; + float hotWidth = width + hotOffset * 2; + float hotY = y - hotOffset; + float hotHeight = height + hotOffset * 2; + + return GreatOrEqual(parentLocalPoint.GetX(), hotX) && + LessOrEqual(parentLocalPoint.GetX(), hotX + hotWidth) && + GreatOrEqual(parentLocalPoint.GetY(), hotY) && + LessOrEqual(parentLocalPoint.GetY(), hotY + hotHeight); +} + +bool WindowEventProcess::LessOrEqual(double left, double right) +{ + constexpr double epsilon = 0.001f; + return (left - right) < epsilon; +} + +bool WindowEventProcess::GreatOrEqual(double left, double right) +{ + return (left - right) > epsilon; +} + +void WindowEventProcess::ProcessWindowEvent(const RefPtr& windowNode, + const std::shared_ptr& pointerEvent) +{ + CHECK_NULL_VOID(windowNode); + auto lastWindowNode = lastWeakWindowNode_.Upgrade(); + if (lastWindowNode == nullptr) { + LOGI("First enter window, lastWindowNode is not exit"); + std::shared_ptr enterEvent = std::make_shared(pointerEvent); + + PointerEvent pointerEvent(pointerEvent); + pointerEvent->setPointAction(MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW); + DispatchPointerEvent(windowNode, pointerEvent); + lastWeakWindowNode_ = windowNode; + lastPointEvent_ = pointerEvent; + return; + } + if (windowNode->GetId() != lastWindowNode->GetId()) { + LOGI("Window switching"); + pointerEvent->setPointAction(MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW); + DispatchPointerEvent(windowNode, pointerEvent); + + lastPointEvent_->setPointAction(MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW); + DispatchPointerEvent(lastWindowNode, pointerEvent); + lastWeakWindowNode_ = windowNode; + lastPointEvent_ = pointerEvent; + } +} + +std::optional> WindowEventProcess::CreatePointerEvent( + const std::shared_ptr& pointerEvent,int32_t pointerAction) +{ + std::shared_ptr event = MMI::PointerEvent::Create(); + int32_t pointerID = pointerEvent.GetPointerId(); + MMI::PointerEvent::PointerItem item; + bool ret = pointerEvent->GetPointerItem(pointerID, item); + if (!ret) { + LOGE("get pointer: %{public}d item failed", pointerID); + return nullopt; + } + event->AddPointerItem(item); + event->SetSourceType(pointerEvent->GetSourceType()); + pointerEvent->SetPointerAction(pointerAction); + pointerEvent->SetPointerId(pointerID); + return event; +} + +void WindowEventProcess::DispatchPointerEvent(const RefPtr& windowNode, + const std::shared_ptr& pointerEvent) +{ + CHECK_NULL_VOID(windowNode); + auto pattern = windowNode->GetPattern(); + CHECK_NULL_VOID(pattern); + pattern->DispatchPointerEvent(pointerEvent); +} +} +} \ No newline at end of file diff --git a/frameworks/core/components_ng/pattern/window_scene/scene/window_event_process.h b/frameworks/core/components_ng/pattern/window_scene/scene/window_event_process.h new file mode 100644 index 0000000000000000000000000000000000000000..6be414ab6c2ad9fe8f47de427d4a02967b625945 --- /dev/null +++ b/frameworks/core/components_ng/pattern/window_scene/scene/window_event_process.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef OHOS_ROSEN_WINDOW_SCENE_WINDOW_EVENT_PROCESS_H +#define OHOS_ROSEN_WINDOW_SCENE_WINDOW_EVENT_PROCESS_H + +#include "singleton.h" + +#include "base/geometry/ng/point_t.h" +#include "base/geometry/ng/rect_t.h" + +namespace OHOS::Rosen { +class WindowEventProcess { + DECLARE_DELAYED_SINGLETON(WindowEventProcess); +public: + DISALLOW_COPY_AND_MOVE(WindowEventProcess); + bool IsInWindowHotRect(const Ace::NG::RectF& windowSceneRect, + const Ace::NG::PointF& parentLocalPoint, int32_t souceType); + void ProcessWindowEvent(const RefPtr& windowNode, + const std::shared_ptr& pointerEvent) + +private: + bool GreatOrEqual(double left, double right); + bool LessOrEqual(double left, double right); + void DispatchPointerEvent(const RefPtr& windowNode, + const std::shared_ptr& pointerEvent); + +private: + WeakPtr lastWeakWindowNode_ {nullptr}; + std::shared_ptr lastPointEvent_; +}; + +#define WindowEventHandler DelayedSingleton::GetInstance() +} // namespace OHOS::Rosen +#endif // OHOS_ROSEN_WINDOW_SCENE_WINDOW_EVENT_PROCESS_H \ No newline at end of file