• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ACTIVE = 8,
43     ON_INACTIVE = 9,
44     ON_BACKPRESS = 100,
45 };
46 
47 struct NavDestinationInfo {
48     std::string navigationId;
49     std::string name;
50     NavDestinationState state;
51     int32_t index;
52     napi_value param;
53     std::string navDestinationId;
54     NavDestinationMode mode;
55     int32_t uniqueId;
56 
57     NavDestinationInfo() = default;
58 
NavDestinationInfoNavDestinationInfo59     NavDestinationInfo(std::string id, std::string name, NavDestinationState state)
60         : navigationId(std::move(id)), name(std::move(name)), state(state)
61     {}
62 
NavDestinationInfoNavDestinationInfo63     NavDestinationInfo(std::string id, std::string name, NavDestinationState state,
64         int32_t index, napi_value param, std::string navDesId)
65         : navigationId(std::move(id)), name(std::move(name)), state(state),
66           index(index), param(param), navDestinationId(std::move(navDesId))
67     {}
68 
NavDestinationInfoNavDestinationInfo69     NavDestinationInfo(std::string id, std::string name, NavDestinationState state,
70         int32_t index, napi_value param, std::string navDesId, NavDestinationMode mode, int32_t uniqueId)
71         : navigationId(std::move(id)), name(std::move(name)), state(state),
72         index(index), param(param), navDestinationId(std::move(navDesId)), mode(mode), uniqueId(std::move(uniqueId))
73     {}
74 };
75 
76 enum class ScrollEventType {
77     SCROLL_START = 0,
78     SCROLL_STOP = 1,
79 };
80 
81 struct ScrollEventInfo {
82     std::string id;
83     int32_t uniqueId;
84     ScrollEventType scrollEvent;
85     float offset;
86 
ScrollEventInfoScrollEventInfo87     ScrollEventInfo(std::string id, int32_t uniqueId, ScrollEventType scrollEvent, float offset)
88         : id(std::move(id)), uniqueId(uniqueId), scrollEvent(scrollEvent), offset(offset)
89     {}
90 };
91 
92 struct NavDestinationSwitchInfo {
93     // UIContext
94     napi_value context;
95     std::optional<NavDestinationInfo> from;
96     std::optional<NavDestinationInfo> to;
97     NavigationOperation operation;
98 
NavDestinationSwitchInfoNavDestinationSwitchInfo99     NavDestinationSwitchInfo(napi_value ctx, std::optional<NavDestinationInfo>&& fromInfo,
100         std::optional<NavDestinationInfo>&& toInfo, NavigationOperation op)
101         : context(ctx), from(std::forward<std::optional<NavDestinationInfo>>(fromInfo)),
102           to(std::forward<std::optional<NavDestinationInfo>>(toInfo)), operation(op)
103     {}
104 };
105 
106 struct RouterPageInfoNG {
107     napi_value context;
108     int32_t index;
109     std::string name;
110     std::string path;
111     RouterPageState state;
112     std::string pageId;
113 
RouterPageInfoNGRouterPageInfoNG114     RouterPageInfoNG(napi_value context, int32_t index, std::string name, std::string path, RouterPageState state,
115         std::string pageId)
116         : context(context), index(index), name(std::move(name)), path(std::move(path)), state(state),
117           pageId(std::move(pageId))
118     {}
119 };
120 
121 struct AbilityContextInfo {
122     std::string name = "";
123     std::string bundleName = "";
124     std::string moduleName = "";
125 
IsEqualAbilityContextInfo126     bool IsEqual(const AbilityContextInfo& info) const
127     {
128         return name == info.name && bundleName == info.bundleName && moduleName == info.moduleName;
129     }
130 };
131 
132 enum class TabContentState {
133     ON_SHOW = 0,
134     ON_HIDE = 1,
135 };
136 
137 struct TabContentInfo {
138     std::string tabContentId;
139     int32_t tabContentUniqueId = 0;
140     TabContentState state;
141     int32_t index = 0;
142     std::string id;
143     int32_t uniqueId = 0;
144 
TabContentInfoTabContentInfo145     TabContentInfo(std::string tabContentId, int32_t tabContentUniqueId, TabContentState state, int32_t index,
146         std::string id, int32_t uniqueId)
147         : tabContentId(std::move(tabContentId)), tabContentUniqueId(tabContentUniqueId), state(state), index(index),
148         id(std::move(id)), uniqueId(uniqueId)
149     {}
150 };
151 
152 enum class PanGestureState {
153     BEFORE = 0,
154     AFTER = 1,
155 };
156 
157 struct PanGestureInfo {
158     PanGestureState gestureState;
159     CurrentCallbackState callbackState;
160 };
161 
162 class ACE_FORCE_EXPORT UIObserverHandler {
163 public:
164     UIObserverHandler() = default;
165     ~UIObserverHandler() = default;
166     static UIObserverHandler& GetInstance();
167     void NotifyNavigationStateChange(const WeakPtr<AceType>& weakPattern, NavDestinationState state);
168     void NotifyScrollEventStateChange(const WeakPtr<AceType>& weakPattern, ScrollEventType scrollEvent);
169     void NotifyRouterPageStateChange(const RefPtr<PageInfo>& pageInfo, RouterPageState state);
170     void NotifyDensityChange(double density);
171     void NotifyWillClick(const GestureEvent& gestureEventInfo,
172         const ClickInfo& clickInfo, const RefPtr<FrameNode>& frameNode);
173     void NotifyDidClick(const GestureEvent& gestureEventInfo,
174         const ClickInfo& clickInfo, const RefPtr<FrameNode>& frameNode);
175     void NotifyPanGestureStateChange(const GestureEvent& gestureEventInfo,
176         const RefPtr<PanRecognizer>& current, const RefPtr<FrameNode>& frameNode,
177         const PanGestureInfo& panGestureInfo);
178     void NotifyTabContentStateUpdate(const TabContentInfo& info);
179     std::shared_ptr<NavDestinationInfo> GetNavigationState(const RefPtr<AceType>& node);
180     std::shared_ptr<NavDestinationInfo> GetNavDestinationInfo(const RefPtr<UINode>& current);
181     std::shared_ptr<NavDestinationInfo> GetNavigationInnerState(const RefPtr<AceType>& node);
182     std::shared_ptr<NavDestinationInfo> GetNavigationOuterState(const RefPtr<AceType>& node);
183     std::shared_ptr<ScrollEventInfo> GetScrollEventState(const RefPtr<AceType>& node);
184     std::shared_ptr<RouterPageInfoNG> GetRouterPageState(const RefPtr<AceType>& node);
185     void NotifyNavDestinationSwitch(std::optional<NavDestinationInfo>&& from,
186         std::optional<NavDestinationInfo>&& to, NavigationOperation operation);
187     using NavigationHandleFunc = void (*)(const NavDestinationInfo& info);
188     using ScrollEventHandleFunc = void (*)(const std::string&, int32_t, ScrollEventType, float);
189     using RouterPageHandleFunc = void (*)(AbilityContextInfo&, const RouterPageInfoNG&);
190     using DrawCommandSendHandleFunc = void (*)();
191     using LayoutDoneHandleFunc = void (*)();
192     using NavDestinationSwitchHandleFunc = void (*)(const AbilityContextInfo&, NavDestinationSwitchInfo&);
193     using WillClickHandleFunc = void (*)(
194         AbilityContextInfo&, const GestureEvent&, const ClickInfo&, const RefPtr<FrameNode>&);
195     using DidClickHandleFunc = void (*)(
196         AbilityContextInfo&, const GestureEvent&, const ClickInfo&, const RefPtr<FrameNode>&);
197     using PanGestureHandleFunc = void (*)(AbilityContextInfo&, const GestureEvent&,
198         const RefPtr<PanRecognizer>& current, const RefPtr<FrameNode>&, const NG::PanGestureInfo& panGestureInfo);
199     using TabContentStateHandleFunc = void (*)(const TabContentInfo&);
200     NavDestinationSwitchHandleFunc GetHandleNavDestinationSwitchFunc();
201     void SetHandleNavigationChangeFunc(NavigationHandleFunc func);
202     void SetHandleScrollEventChangeFunc(ScrollEventHandleFunc func);
203     void SetHandleRouterPageChangeFunc(RouterPageHandleFunc func);
204     using DensityHandleFunc = void (*)(AbilityContextInfo&, double);
205     void SetHandleDensityChangeFunc(DensityHandleFunc func);
206     void SetLayoutDoneHandleFunc(DrawCommandSendHandleFunc func);
207     void HandleLayoutDoneCallBack();
208     void SetDrawCommandSendHandleFunc(LayoutDoneHandleFunc func);
209     void HandleDrawCommandSendCallBack();
210     void SetHandleNavDestinationSwitchFunc(NavDestinationSwitchHandleFunc func);
211     void SetWillClickFunc(WillClickHandleFunc func);
212     void SetDidClickFunc(DidClickHandleFunc func);
213     void SetPanGestureHandleFunc(PanGestureHandleFunc func);
214     void SetHandleTabContentStateUpdateFunc(TabContentStateHandleFunc func);
215 private:
216     NavigationHandleFunc navigationHandleFunc_ = nullptr;
217     ScrollEventHandleFunc scrollEventHandleFunc_ = nullptr;
218     RouterPageHandleFunc routerPageHandleFunc_ = nullptr;
219     LayoutDoneHandleFunc layoutDoneHandleFunc_ = nullptr;
220     DrawCommandSendHandleFunc drawCommandSendHandleFunc_ = nullptr;
221     DensityHandleFunc densityHandleFunc_ = nullptr;
222     NavDestinationSwitchHandleFunc navDestinationSwitchHandleFunc_ = nullptr;
223     WillClickHandleFunc willClickHandleFunc_ = nullptr;
224     DidClickHandleFunc didClickHandleFunc_ = nullptr;
225     PanGestureHandleFunc panGestureHandleFunc_ = nullptr;
226     TabContentStateHandleFunc tabContentStateHandleFunc_ = nullptr;
227 
228     napi_value GetUIContextValue();
229 };
230 } // namespace OHOS::Ace::NG
231 
232 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_OBSERVER_HANDLER_H
233