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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_PLATFORM_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_PLATFORM_PATTERN_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <list>
22 #include <memory>
23 #include <optional>
24 #include <refbase.h>
25 #include <vector>
26 
27 #include "base/memory/referenced.h"
28 #include "base/want/want_wrap.h"
29 #include "core/common/container.h"
30 #include "core/components_ng/event/gesture_event_hub.h"
31 #include "core/components_ng/pattern/pattern.h"
32 #include "core/event/mouse_event.h"
33 #include "core/event/touch_event.h"
34 
35 #define PLATFORM_LOGD(fmt, ...)                                                 \
36     TAG_LOGD(tag_, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, platformId_, \
37         ##__VA_ARGS__)
38 #define PLATFORM_LOGI(fmt, ...)                                                 \
39     TAG_LOGI(tag_, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, platformId_, \
40         ##__VA_ARGS__)
41 #define PLATFORM_LOGW(fmt, ...)                                                 \
42     TAG_LOGW(tag_, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, platformId_, \
43         ##__VA_ARGS__)
44 #define PLATFORM_LOGE(fmt, ...)                                                 \
45     TAG_LOGE(tag_, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, platformId_, \
46         ##__VA_ARGS__)
47 #define PLATFORM_LOGF(fmt, ...)                                                 \
48     TAG_LOGF(tag_, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, platformId_, \
49         ##__VA_ARGS__)
50 
51 namespace OHOS::MMI {
52 class KeyEvent;
53 class PointerEvent;
54 } // namespace OHOS::MMI
55 
56 namespace OHOS::Ace {
57 class ModalUIExtensionProxy;
58 } // namespace OHOS::Ace
59 
60 namespace OHOS::Rosen {
61 class AvoidArea;
62 class RSTransaction;
63 } // namespace OHOS::Rosen
64 
65 namespace OHOS::Ace::NG {
66 class UIExtensionProxy;
67 class PlatformPattern : public Pattern {
68     DECLARE_ACE_TYPE(PlatformPattern, Pattern);
69 
70 public:
71     explicit PlatformPattern(AceLogTag tag, int32_t platformId);
72     ~PlatformPattern() override;
73 
74     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override;
75     FocusPattern GetFocusPattern() const override;
76     void OnMountToParentDone() override;
77     void HandleDragEvent(const PointerEvent& info) override;
78 
79     virtual void SetOnErrorCallback(const std::function<void(int32_t code,
80         const std::string& name, const std::string& message)>&& callback);
81     virtual void FireOnErrorCallback(
82         int32_t code, const std::string& name, const std::string& message);
83 
84     int32_t GetInstanceId();
85     int32_t GetNodeId();
86 
87 protected:
DispatchPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)88     virtual void DispatchPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) {}
DispatchFocusActiveEvent(bool isFocusActive)89     virtual void DispatchFocusActiveEvent(bool isFocusActive) {}
90     virtual bool HandleKeyEvent(const KeyEvent& event);
91     virtual void HandleFocusEvent();
92     virtual void HandleBlurEvent();
93 
94     AceLogTag tag_ = AceLogTag::ACE_DEFAULT_DOMAIN;
95     int32_t platformId_ = -1;
96     int32_t instanceId_ = Container::CurrentId();
97 
98 protected:
99     struct ErrorMsg {
100         int32_t code = 0;
101         std::string name;
102         std::string message;
103     };
104 
105     void OnModifyDone() override;
106 
107     void InitKeyEvent(const RefPtr<FocusHub>& focusHub);
108     void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub);
109     void InitMouseEvent(const RefPtr<InputEventHub>& inputHub);
110     void InitHoverEvent(const RefPtr<InputEventHub>& inputHub);
111     virtual void HandleTouchEvent(const TouchEventInfo& info);
112     void HandleMouseEvent(const MouseInfo& info);
113     void HandleHoverEvent(bool isHover);
114 
115     RefPtr<TouchEventImpl> touchEvent_;
116     RefPtr<InputEvent> mouseEvent_;
117     RefPtr<InputEvent> hoverEvent_;
118     std::shared_ptr<MMI::PointerEvent> lastPointerEvent_ = nullptr;
119     ErrorMsg lastError_;
120 
121     std::function<void(int32_t code, const std::string& name, const std::string& message)> onErrorCallback_;
122     ACE_DISALLOW_COPY_AND_MOVE(PlatformPattern);
123 };
124 } // namespace OHOS::Ace::NG
125 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_PLATFORM_PATTERN_H
126