1 /* 2 * Copyright (c) 2022 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 #ifndef UPDATER_UI_EVENT_PRODUCT_H 17 #define UPDATER_UI_EVENT_PRODUCT_H 18 19 #include <functional> 20 #include <mutex> 21 #include "components/root_view.h" 22 #include "components/ui_view.h" 23 24 namespace Updater { 25 struct Callback { 26 std::function<void(OHOS::UIView &)> func {nullptr}; 27 bool isAsync {false}; 28 }; 29 using KeyCallback = std::function<void(bool)>; 30 enum class EventType { 31 TOUCHEVENT, 32 CLICKEVENT, 33 DRAGEVENT 34 }; 35 36 // avoid callback on invisible component 37 class CallBackDecorator final { 38 public: CallBackDecorator(Callback cb)39 explicit CallBackDecorator(Callback cb) : cb_(cb) {} 40 ~CallBackDecorator() = default; 41 void operator()(OHOS::UIView &view, bool isAsync) const; 42 private: 43 static void CallbackWithGuard(Callback cb, OHOS::UIView &view); 44 Callback cb_; 45 static std::mutex mtx_; 46 }; 47 48 class LabelOnTouchListener final : public OHOS::UIView::OnTouchListener { 49 public: LabelOnTouchListener(Callback cb,bool isConsumed)50 LabelOnTouchListener(Callback cb, bool isConsumed) 51 : cb_(cb), isConsumed_(isConsumed) {} 52 ~LabelOnTouchListener() = default; 53 bool OnRelease(OHOS::UIView &view, const OHOS::ReleaseEvent &event) override; 54 55 private: 56 Callback cb_; 57 bool isConsumed_; 58 }; 59 60 class BtnOnEventListener final : public OHOS::UIView::OnClickListener, public OHOS::UIView::OnTouchListener { 61 public: BtnOnEventListener(Callback cb,bool isConsumed)62 BtnOnEventListener(Callback cb, bool isConsumed) 63 : cb_(cb), isConsumed_(isConsumed) {} 64 ~BtnOnEventListener() = default; 65 bool OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event) override; 66 bool OnPress(OHOS::UIView &view, const OHOS::PressEvent &event) override; 67 bool OnRelease(OHOS::UIView &view, const OHOS::ReleaseEvent &event) override; 68 bool OnCancel(OHOS::UIView &view, const OHOS::CancelEvent &event) override; 69 private: 70 Callback cb_; 71 bool isConsumed_; 72 }; 73 74 // note: only used for debug 75 class BtnOnDragListener final : public OHOS::UIView::OnDragListener { 76 public: BtnOnDragListener(Callback cb,bool isConsumed)77 BtnOnDragListener(Callback cb, bool isConsumed) 78 : cb_(cb), isConsumed_(isConsumed) { 79 } 80 ~BtnOnDragListener() = default; 81 bool OnDragStart(OHOS::UIView &view, const OHOS::DragEvent &event) override; 82 bool OnDrag(OHOS::UIView &view, const OHOS::DragEvent &event) override; 83 bool OnDragEnd(OHOS::UIView &view, const OHOS::DragEvent &event) override; 84 85 private: 86 Callback cb_; 87 bool isConsumed_; 88 }; 89 90 class KeyListener : public OHOS::RootView::OnKeyActListener { 91 public: 92 KeyListener() = default; 93 virtual ~KeyListener() = default; 94 bool OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event) override; 95 static void SetButtonPressed(bool isPressed); 96 bool ProcessPowerKey(OHOS::UIView &view, const OHOS::KeyEvent &event); 97 bool ProcessVolumeKey(OHOS::UIView &view, const OHOS::KeyEvent &event); 98 private: 99 static bool isButtonPressed_; 100 }; 101 } 102 #endif 103