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