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