1 /*
2  * Copyright (c) 2023 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_BASE_OBSERVER_HANDLER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_OBSERVER_HANDLER_H
18 
19 #include <functional>
20 #include <optional>
21 #include <string>
22 #include <utility>
23 
24 #include "base/memory/ace_type.h"
25 #include "base/memory/referenced.h"
26 #include "core/common/frontend.h"
27 #include "core/common/container.h"
28 #include "core/components_ng/pattern/navigation/navigation_declaration.h"
29 #include "core/components_ng/pattern/stage/page_pattern.h"
30 
31 namespace OHOS::Ace::NG {
32 enum class NavDestinationState {
33     NONE = -1,
34     ON_SHOWN = 0,
35     ON_HIDDEN = 1,
36     ON_APPEAR = 2,
37     ON_DISAPPEAR = 3,
38     ON_WILL_SHOW = 4,
39     ON_WILL_HIDE = 5,
40     ON_WILL_APPEAR = 6,
41     ON_WILL_DISAPPEAR = 7,
42     ON_BACKPRESS = 100,
43 };
44 
45 struct NavDestinationInfo {
46     std::string navigationId;
47     std::string name;
48     NavDestinationState state;
49     int32_t index;
50     napi_value param;
51     std::string navDestinationId;
52 
53     NavDestinationInfo() = default;
54 
NavDestinationInfoNavDestinationInfo55     NavDestinationInfo(std::string id, std::string name, NavDestinationState state)
56         : navigationId(std::move(id)), name(std::move(name)), state(state)
57     {}
58 
NavDestinationInfoNavDestinationInfo59     NavDestinationInfo(std::string id, std::string name, NavDestinationState state,
60         int32_t index, napi_value param, std::string navDesId)
61         : navigationId(std::move(id)), name(std::move(name)), state(state),
62           index(index), param(param), navDestinationId(std::move(navDesId))
63     {}
64 };
65 
66 enum class ScrollEventType {
67     SCROLL_START = 0,
68     SCROLL_STOP = 1,
69 };
70 
71 struct ScrollEventInfo {
72     std::string id;
73     int32_t uniqueId;
74     ScrollEventType scrollEvent;
75     float offset;
76 
ScrollEventInfoScrollEventInfo77     ScrollEventInfo(std::string id, int32_t uniqueId, ScrollEventType scrollEvent, float offset)
78         : id(std::move(id)), uniqueId(uniqueId), scrollEvent(scrollEvent), offset(offset)
79     {}
80 };
81 
82 struct NavDestinationSwitchInfo {
83     // UIContext
84     napi_value context;
85     std::optional<NavDestinationInfo> from;
86     std::optional<NavDestinationInfo> to;
87     NavigationOperation operation;
88 
NavDestinationSwitchInfoNavDestinationSwitchInfo89     NavDestinationSwitchInfo(napi_value ctx, std::optional<NavDestinationInfo>&& fromInfo,
90         std::optional<NavDestinationInfo>&& toInfo, NavigationOperation op)
91         : context(ctx), from(std::forward<std::optional<NavDestinationInfo>>(fromInfo)),
92           to(std::forward<std::optional<NavDestinationInfo>>(toInfo)), operation(op)
93     {}
94 };
95 
96 struct RouterPageInfoNG {
97     napi_value context;
98     int32_t index;
99     std::string name;
100     std::string path;
101     RouterPageState state;
102     std::string pageId;
103 
RouterPageInfoNGRouterPageInfoNG104     RouterPageInfoNG(napi_value context, int32_t index, std::string name, std::string path, RouterPageState state,
105         std::string pageId)
106         : context(context), index(index), name(std::move(name)), path(std::move(path)), state(state),
107           pageId(std::move(pageId))
108     {}
109 };
110 
111 struct AbilityContextInfo {
112     std::string name = "";
113     std::string bundleName = "";
114     std::string moduleName = "";
115 
IsEqualAbilityContextInfo116     bool IsEqual(const AbilityContextInfo& info) const
117     {
118         return name == info.name && bundleName == info.bundleName && moduleName == info.moduleName;
119     }
120 };
121 
122 enum class TabContentState {
123     ON_SHOW = 0,
124     ON_HIDE = 1,
125 };
126 
127 struct TabContentInfo {
128     std::string tabContentId;
129     int32_t tabContentUniqueId = 0;
130     TabContentState state;
131     int32_t index = 0;
132     std::string id;
133     int32_t uniqueId = 0;
134 
TabContentInfoTabContentInfo135     TabContentInfo(std::string tabContentId, int32_t tabContentUniqueId, TabContentState state, int32_t index,
136         std::string id, int32_t uniqueId)
137         : tabContentId(std::move(tabContentId)), tabContentUniqueId(tabContentUniqueId), state(state), index(index),
138         id(std::move(id)), uniqueId(uniqueId)
139     {}
140 };
141 
142 class ACE_FORCE_EXPORT UIObserverHandler {
143 public:
144     UIObserverHandler() = default;
145     ~UIObserverHandler() = default;
146     static UIObserverHandler& GetInstance();
147     void NotifyNavigationStateChange(const WeakPtr<AceType>& weakPattern, NavDestinationState state);
148     void NotifyScrollEventStateChange(const WeakPtr<AceType>& weakPattern, ScrollEventType scrollEvent);
149     void NotifyRouterPageStateChange(const RefPtr<PageInfo>& pageInfo, RouterPageState state);
150     void NotifyDensityChange(double density);
151     void NotifyWillClick(const GestureEvent& gestureEventInfo,
152         const ClickInfo& clickInfo, const RefPtr<FrameNode>& frameNode);
153     void NotifyDidClick(const GestureEvent& gestureEventInfo,
154         const ClickInfo& clickInfo, const RefPtr<FrameNode>& frameNode);
155     void NotifyTabContentStateUpdate(const TabContentInfo& info);
156     std::shared_ptr<NavDestinationInfo> GetNavigationState(const RefPtr<AceType>& node);
157     std::shared_ptr<ScrollEventInfo> GetScrollEventState(const RefPtr<AceType>& node);
158     std::shared_ptr<RouterPageInfoNG> GetRouterPageState(const RefPtr<AceType>& node);
159     void NotifyNavDestinationSwitch(std::optional<NavDestinationInfo>&& from,
160         std::optional<NavDestinationInfo>&& to, NavigationOperation operation);
161     using NavigationHandleFunc = void (*)(const NavDestinationInfo& info);
162     using ScrollEventHandleFunc = void (*)(const std::string&, int32_t, ScrollEventType, float);
163     using RouterPageHandleFunc = void (*)(AbilityContextInfo&, const RouterPageInfoNG&);
164     using DrawCommandSendHandleFunc = void (*)();
165     using LayoutDoneHandleFunc = void (*)();
166     using NavDestinationSwitchHandleFunc = void (*)(const AbilityContextInfo&, NavDestinationSwitchInfo&);
167     using WillClickHandleFunc = void (*)(
168         AbilityContextInfo&, const GestureEvent&, const ClickInfo&, const RefPtr<FrameNode>&);
169     using DidClickHandleFunc = void (*)(
170         AbilityContextInfo&, const GestureEvent&, const ClickInfo&, const RefPtr<FrameNode>&);
171     using TabContentStateHandleFunc = void (*)(const TabContentInfo&);
172     NavDestinationSwitchHandleFunc GetHandleNavDestinationSwitchFunc();
173     void SetHandleNavigationChangeFunc(NavigationHandleFunc func);
174     void SetHandleScrollEventChangeFunc(ScrollEventHandleFunc func);
175     void SetHandleRouterPageChangeFunc(RouterPageHandleFunc func);
176     using DensityHandleFunc = void (*)(AbilityContextInfo&, double);
177     void SetHandleDensityChangeFunc(DensityHandleFunc func);
178     void SetLayoutDoneHandleFunc(DrawCommandSendHandleFunc func);
179     void HandleLayoutDoneCallBack();
180     void SetDrawCommandSendHandleFunc(LayoutDoneHandleFunc func);
181     void HandleDrawCommandSendCallBack();
182     void SetHandleNavDestinationSwitchFunc(NavDestinationSwitchHandleFunc func);
183     void SetWillClickFunc(WillClickHandleFunc func);
184     void SetDidClickFunc(DidClickHandleFunc func);
185     void SetHandleTabContentStateUpdateFunc(TabContentStateHandleFunc func);
186 private:
187     NavigationHandleFunc navigationHandleFunc_ = nullptr;
188     ScrollEventHandleFunc scrollEventHandleFunc_ = nullptr;
189     RouterPageHandleFunc routerPageHandleFunc_ = nullptr;
190     LayoutDoneHandleFunc layoutDoneHandleFunc_ = nullptr;
191     DrawCommandSendHandleFunc drawCommandSendHandleFunc_ = nullptr;
192     DensityHandleFunc densityHandleFunc_ = nullptr;
193     NavDestinationSwitchHandleFunc navDestinationSwitchHandleFunc_ = nullptr;
194     WillClickHandleFunc willClickHandleFunc_ = nullptr;
195     DidClickHandleFunc didClickHandleFunc_ = nullptr;
196     TabContentStateHandleFunc tabContentStateHandleFunc_ = nullptr;
197 
198     napi_value GetUIContextValue();
199 };
200 } // namespace OHOS::Ace::NG
201 
202 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_OBSERVER_HANDLER_H
203