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 OnPress(OHOS::UIView &view, const OHOS::PressEvent &event) override; 54 bool OnCancel(OHOS::UIView &view, const OHOS::CancelEvent &event) override; 55 bool OnRelease(OHOS::UIView &view, const OHOS::ReleaseEvent &event) override; 56 57 private: 58 Callback cb_; 59 bool isConsumed_; 60 }; 61 62 class BtnOnEventListener final : public OHOS::UIView::OnClickListener, public OHOS::UIView::OnTouchListener { 63 public: BtnOnEventListener(Callback cb,bool isConsumed)64 BtnOnEventListener(Callback cb, bool isConsumed) 65 : cb_(cb), isConsumed_(isConsumed) {} 66 ~BtnOnEventListener() = default; 67 bool OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event) override; 68 bool OnPress(OHOS::UIView &view, const OHOS::PressEvent &event) override; 69 bool OnRelease(OHOS::UIView &view, const OHOS::ReleaseEvent &event) override; 70 bool OnCancel(OHOS::UIView &view, const OHOS::CancelEvent &event) override; 71 private: 72 Callback cb_; 73 bool isConsumed_; 74 }; 75 76 // note: only used for debug 77 class BtnOnDragListener final : public OHOS::UIView::OnDragListener { 78 public: BtnOnDragListener(Callback cb,bool isConsumed)79 BtnOnDragListener(Callback cb, bool isConsumed) 80 : cb_(cb), isConsumed_(isConsumed) { 81 } 82 ~BtnOnDragListener() = default; 83 bool OnDragStart(OHOS::UIView &view, const OHOS::DragEvent &event) override; 84 bool OnDrag(OHOS::UIView &view, const OHOS::DragEvent &event) override; 85 bool OnDragEnd(OHOS::UIView &view, const OHOS::DragEvent &event) override; 86 87 private: 88 Callback cb_; 89 bool isConsumed_; 90 }; 91 92 class KeyListener : public OHOS::RootView::OnKeyActListener { 93 public: 94 KeyListener() = default; 95 virtual ~KeyListener() = default; 96 bool OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event) override; 97 static void SetButtonPressed(bool isPressed); 98 bool ProcessPowerKey(OHOS::UIView &view, const OHOS::KeyEvent &event); 99 bool ProcessVolumeKey(OHOS::UIView &view, const OHOS::KeyEvent &event); 100 private: 101 OHOS::UIView *GetFirstFocusableViewByDir(uint8_t dir); 102 static bool isButtonPressed_; 103 }; 104 } 105 #endif 106