1 /*
2 * Copyright (c) 2021 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/mouse_listener/render_mouse_listener.h"
17
18 #include "core/components/mouse_listener/mouse_listener_component.h"
19 #include "core/event/ace_event_helper.h"
20
21 namespace OHOS::Ace {
22
Create()23 RefPtr<RenderNode> RenderMouseListener::Create()
24 {
25 return AceType::MakeRefPtr<RenderMouseListener>();
26 }
27
RenderMouseListener()28 RenderMouseListener::RenderMouseListener() : recognizer_(AceType::MakeRefPtr<MouseRawRecognizer>()) {}
29
Update(const RefPtr<Component> & component)30 void RenderMouseListener::Update(const RefPtr<Component>& component)
31 {
32 auto mouseComponent = AceType::DynamicCast<MouseListenerComponent>(component);
33 ACE_DCHECK(mouseComponent);
34 SetOnMouse(AceAsyncEvent<void(const MouseEventInfo&)>::Create(mouseComponent->GetOnMouseId(), context_));
35 SetOnMouseHover(AceAsyncEvent<void()>::Create(mouseComponent->GetOnMouseHoverId(), context_));
36 SetOnMouseHoverExit(AceAsyncEvent<void()>::Create(mouseComponent->GetOnMouseHoverExitId(), context_));
37 // Ace 2.0
38 SetOnHover(mouseComponent->GetOnHoverId());
39 }
40
OnMouseTestHit(const Offset & coordinateOffset,MouseRawResult & result)41 void RenderMouseListener::OnMouseTestHit(const Offset& coordinateOffset, MouseRawResult& result)
42 {
43 recognizer_->SetCoordinateOffset(coordinateOffset);
44 result.emplace_back(recognizer_);
45 }
46
HandleMouseHoverEvent(MouseState mouseState)47 void RenderMouseListener::HandleMouseHoverEvent(MouseState mouseState)
48 {
49 if (recognizer_) {
50 recognizer_->HandleHoverEvent(mouseState);
51 }
52 }
53
UpdateTouchRect()54 void RenderMouseListener::UpdateTouchRect()
55 {
56 const auto& children = GetChildren();
57 for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
58 auto& child = *iter;
59 for (auto& rect : child->GetTouchRectList()) {
60 // unified coordinate system
61 Rect newRect = rect;
62 newRect.SetOffset(rect.GetOffset() + GetPaintRect().GetOffset());
63 touchRectList_.emplace_back(newRect);
64 }
65 }
66 }
67
68 } // namespace OHOS::Ace