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