• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 #include "event_listener.h"
16 #include "ace_log.h"
17 #include "jerryscript.h"
18 #include "js_fwk_common.h"
19 #include "root_view.h"
20 
21 namespace OHOS {
22 namespace ACELite {
23 #ifdef JS_EXTRA_EVENT_SUPPORT
KeyBoardEventListener(jerry_value_t fn,const uint16_t id)24 KeyBoardEventListener::KeyBoardEventListener(jerry_value_t fn, const uint16_t id)
25 {
26     fn_ = jerry_acquire_value(fn);
27     id_ = id;
28 }
29 
~KeyBoardEventListener()30 KeyBoardEventListener::~KeyBoardEventListener()
31 {
32     jerry_release_value(fn_);
33 }
34 
OnKeyAct(UIView & view,const KeyEvent & event)35 bool KeyBoardEventListener::OnKeyAct(UIView &view, const KeyEvent &event)
36 {
37     if (jerry_value_is_undefined(fn_)) {
38         return false;
39     }
40 
41     jerry_value_t *args = ConvertKeyEventInfo(event);
42     jerry_release_value(CallJSFunctionOnRoot(fn_, args, 1));
43     ClearEventListener(args, 1);
44     return true;
45 }
46 
ViewOnTouchCancelListener(jerry_value_t fn,uint16_t id)47 ViewOnTouchCancelListener::ViewOnTouchCancelListener(jerry_value_t fn, uint16_t id)
48 {
49     fn_ = jerry_acquire_value(fn);
50     id_ = id;
51 }
52 
~ViewOnTouchCancelListener()53 ViewOnTouchCancelListener::~ViewOnTouchCancelListener()
54 {
55     jerry_release_value(fn_);
56 }
OnCancel(UIView & view,const CancelEvent & event)57 bool ViewOnTouchCancelListener::OnCancel(UIView &view, const CancelEvent &event)
58 {
59     return CallBaseEvent(fn_, event, id_);
60 }
61 #endif // JS_EXTRA_EVENT_SUPPORT
62 
SetBindTouchStartFuncName(jerry_value_t bindTouchStartFunc)63 void ViewOnTouchListener::SetBindTouchStartFuncName(jerry_value_t bindTouchStartFunc)
64 {
65     if (!jerry_value_is_undefined(bindTouchStartFunc)) {
66         bindTouchStartFunc_ = jerry_acquire_value(bindTouchStartFunc);
67     }
68 }
69 
SetBindTouchMoveFuncName(jerry_value_t bindTouchMoveFunc)70 void ViewOnTouchListener::SetBindTouchMoveFuncName(jerry_value_t bindTouchMoveFunc)
71 {
72     if (!jerry_value_is_undefined(bindTouchMoveFunc)) {
73         bindTouchMoveFunc_ = jerry_acquire_value(bindTouchMoveFunc);
74     }
75 }
76 
77 
SetBindTouchEndFuncName(jerry_value_t bindTouchEndFunc)78 void ViewOnTouchListener::SetBindTouchEndFuncName(jerry_value_t bindTouchEndFunc)
79 {
80     if (!jerry_value_is_undefined(bindTouchEndFunc)) {
81         bindTouchEndFunc_ = jerry_acquire_value(bindTouchEndFunc);
82     }
83 }
84 
SetBindSwipeFuncName(jerry_value_t bindSwipeFunc)85 void ViewOnTouchListener::SetBindSwipeFuncName(jerry_value_t bindSwipeFunc)
86 {
87     if (!jerry_value_is_undefined(bindSwipeFunc)) {
88         bindSwipeFunc_ = jerry_acquire_value(bindSwipeFunc);
89     }
90 }
91 
SetStopPropagation(bool isStopPropogation)92 void ViewOnTouchListener::SetStopPropagation(bool isStopPropogation)
93 {
94     isStopPropagation_ = isStopPropogation;
95 }
96 
OnDragStart(UIView & view,const DragEvent & event)97 bool ViewOnTouchListener::OnDragStart(UIView& view, const DragEvent &event)
98 {
99     if (JSUndefined::Is(bindTouchStartFunc_)) {
100         HILOG_ERROR(HILOG_MODULE_ACE, "OnDragStart received, but no JS function to call");
101         return isStopPropagation_;
102     }
103 
104     HILOG_DEBUG(HILOG_MODULE_ACE, "OnDragStart received");
105 
106     JSValue arg = EventUtil::CreateTouchEvent(view, event);
107     EventUtil::InvokeCallback(vm_, bindTouchStartFunc_, arg, this);
108     return isStopPropagation_;
109 }
110 
OnDrag(UIView & view,const DragEvent & event)111 bool ViewOnTouchListener::OnDrag(UIView& view, const DragEvent& event)
112 {
113     if (JSUndefined::Is(bindTouchMoveFunc_)) {
114         HILOG_ERROR(HILOG_MODULE_ACE, "OnDrag received, but no JS function to call");
115         return isStopPropagation_;
116     }
117 
118     HILOG_DEBUG(HILOG_MODULE_ACE, "OnDrag received");
119 
120     JSValue arg = EventUtil::CreateTouchEvent(view, event);
121     EventUtil::InvokeCallback(vm_, bindTouchMoveFunc_, arg, this);
122     return isStopPropagation_;
123 }
124 
OnDragEnd(UIView & view,const DragEvent & event)125 bool ViewOnTouchListener::OnDragEnd(UIView& view, const DragEvent &event)
126 {
127     if (!JSUndefined::Is(bindSwipeFunc_)) {
128         JSValue argSwipe = EventUtil::CreateSwipeEvent(view, event);
129         EventUtil::InvokeCallback(vm_, bindSwipeFunc_, argSwipe, this);
130     }
131 
132     if (!JSUndefined::Is(bindTouchEndFunc_)) {
133         JSValue argDragEnd = EventUtil::CreateTouchEvent(view, event);
134         EventUtil::InvokeCallback(vm_, bindTouchEndFunc_, argDragEnd, this);
135     }
136 
137     HILOG_DEBUG(HILOG_MODULE_ACE, "OnDragEnd received");
138     HILOG_DEBUG(HILOG_MODULE_ACE, "Swipe received");
139     return isStopPropagation_;
140 }
141 } // namespace ACELite
142 } // namespace OHOS
143