1 /*
2  * Copyright (c) 2024 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 "bridge/cj_frontend/cppview/interactable_view.h"
17 
18 #include "core/components_ng/base/view_abstract_model.h"
19 #include "core/event/ace_event_handler.h"
20 
21 namespace OHOS::Ace::Framework {
22 
SetFocusable(bool focusable)23 void InteractableView::SetFocusable(bool focusable)
24 {
25     ViewAbstractModel::GetInstance()->SetFocusable(focusable);
26 }
27 
SetFocusNode(bool isFocusNode)28 void InteractableView::SetFocusNode(bool isFocusNode)
29 {
30     ViewAbstractModel::GetInstance()->SetFocusNode(isFocusNode);
31 }
32 
GestureEventWrapper(std::function<void (const ClickInfo & clickInfo)> callback)33 static GestureEventFunc GestureEventWrapper(std::function<void(const ClickInfo& clickInfo)> callback)
34 {
35     return [callback](GestureEvent& info) {
36         LOGI("About to call InteractableView::GetTapGesture callback method on cj");
37         // js frontend has no fingerid either
38         ClickInfo ffiClickInfo(-1);
39         ffiClickInfo.SetGlobalLocation(info.GetGlobalLocation());
40         ffiClickInfo.SetLocalLocation(info.GetLocalLocation());
41         callback(ffiClickInfo);
42     };
43 }
44 
OnClick(std::function<void (const ClickInfo & clickInfo)> callback)45 void InteractableView::OnClick(std::function<void(const ClickInfo& clickInfo)> callback)
46 {
47     ViewAbstractModel::GetInstance()->SetOnClick(
48         GestureEventWrapper(callback), [callback](const ClickInfo* info)->void {
49         callback(*info);
50     });
51 }
52 
OnTouch(std::function<void (TouchEventInfo & touchInfo)> callback)53 void InteractableView::OnTouch(std::function<void(TouchEventInfo& touchInfo)> callback)
54 {
55     LOGI("InteractableView::OnTouch start!");
56     ViewAbstractModel::GetInstance()->SetOnTouch(std::move(callback));
57 }
58 
OnAppear(std::function<void ()> callback)59 void InteractableView::OnAppear(std::function<void()> callback)
60 {
61     ViewAbstractModel::GetInstance()->SetOnAppear(std::move(callback));
62 }
63 
OnDisAppear(std::function<void ()> callback)64 void InteractableView::OnDisAppear(std::function<void()> callback)
65 {
66     ViewAbstractModel::GetInstance()->SetOnDisAppear(std::move(callback));
67 }
68 
OnHover(std::function<void (bool)> callback)69 void InteractableView::OnHover(std::function<void(bool)> callback)
70 {
71     ViewAbstractModel::GetInstance()->SetOnHover([callback](bool v, HoverInfo& info) {
72         callback(v);
73     });
74 }
75 
OnKey(std::function<bool (KeyEventInfo & keyInfo)> callback)76 void InteractableView::OnKey(std::function<bool(KeyEventInfo& keyInfo)> callback)
77 {
78     ViewAbstractModel::GetInstance()->SetOnKeyEvent(std::move(callback));
79 }
80 
OnDelete(std::function<void ()> callback)81 void InteractableView::OnDelete(std::function<void()> callback)
82 {
83     ViewAbstractModel::GetInstance()->SetOnDelete(std::move(callback));
84 }
85 
86 } // namespace OHOS::Ace::Framework
87