• 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     int32_t navigationUniqueId = -1; //Internal use only
57 
58     NavDestinationInfo() = default;
59 
NavDestinationInfoNavDestinationInfo60     NavDestinationInfo(std::string id, std::string name, NavDestinationState state)
61         : navigationId(std::move(id)), name(std::move(name)), state(state)
62     {}
63 
NavDestinationInfoNavDestinationInfo64     NavDestinationInfo(std::string id, std::string name, NavDestinationState state,
65         int32_t index, napi_value param, std::string navDesId)
66         : navigationId(std::move(id)), name(std::move(name)), state(state),
67           index(index), param(param), navDestinationId(std::move(navDesId))
68     {}
69 
NavDestinationInfoNavDestinationInfo70     NavDestinationInfo(std::string id, std::string name, NavDestinationState state,
71         int32_t index, napi_value param, std::string navDesId, NavDestinationMode mode, int32_t uniqueId)
72         : navigationId(std::move(id)), name(std::move(name)), state(state),
73         index(index), param(param), navDestinationId(std::move(navDesId)), mode(mode), uniqueId(std::move(uniqueId))
74     {}
75 
NavDestinationInfoNavDestinationInfo76     NavDestinationInfo(std::string id, std::string name, NavDestinationState state, int32_t index, napi_value param,
77         std::string navDesId, NavDestinationMode mode, int32_t uniqueId, int32_t navigationUniqueId)
78         : navigationId(std::move(id)), name(std::move(name)), state(state), index(index), param(param),
79           navDestinationId(std::move(navDesId)), mode(mode),
80           uniqueId(std::move(uniqueId)), navigationUniqueId(std::move(navigationUniqueId))
81     {}
82 };
83 
84 enum class ScrollEventType {
85     SCROLL_START = 0,
86     SCROLL_STOP = 1,
87 };
88 
89 struct ScrollEventInfo {
90     std::string id;
91     int32_t uniqueId;
92     ScrollEventType scrollEvent;
93     float offset;
94     Ace::Axis axis;
95 
ScrollEventInfoScrollEventInfo96     ScrollEventInfo(std::string id, int32_t uniqueId, ScrollEventType scrollEvent, float offset, Ace::Axis axis)
97         : id(std::move(id)), uniqueId(uniqueId), scrollEvent(scrollEvent), offset(offset), axis(axis)
98     {}
99 };
100 
101 struct NavDestinationSwitchInfo {
102     // UIContext
103     napi_value context;
104     std::optional<NavDestinationInfo> from;
105     std::optional<NavDestinationInfo> to;
106     NavigationOperation operation;
107 
NavDestinationSwitchInfoNavDestinationSwitchInfo108     NavDestinationSwitchInfo(napi_value ctx, std::optional<NavDestinationInfo>&& fromInfo,
109         std::optional<NavDestinationInfo>&& toInfo, NavigationOperation op)
110         : context(ctx), from(std::forward<std::optional<NavDestinationInfo>>(fromInfo)),
111           to(std::forward<std::optional<NavDestinationInfo>>(toInfo)), operation(op)
112     {}
113 };
114 
115 struct RouterPageInfoNG {
116     napi_value context;
117     int32_t index;
118     std::string name;
119     std::string path;
120     RouterPageState state;
121     std::string pageId;
122 
RouterPageInfoNGRouterPageInfoNG123     RouterPageInfoNG(napi_value context, int32_t index, std::string name, std::string path, RouterPageState state,
124         std::string pageId)
125         : context(context), index(index), name(std::move(name)), path(std::move(path)), state(state),
126           pageId(std::move(pageId))
127     {}
128 };
129 
130 struct AbilityContextInfo {
131     std::string name = "";
132     std::string bundleName = "";
133     std::string moduleName = "";
134 
IsEqualAbilityContextInfo135     bool IsEqual(const AbilityContextInfo& info) const
136     {
137         return name == info.name && bundleName == info.bundleName && moduleName == info.moduleName;
138     }
139 };
140 
141 enum class TabContentState {
142     ON_SHOW = 0,
143     ON_HIDE = 1,
144 };
145 
146 struct TabContentInfo {
147     std::string tabContentId;
148     int32_t tabContentUniqueId = 0;
149     TabContentState state;
150     int32_t index = 0;
151     std::string id;
152     int32_t uniqueId = 0;
153 
TabContentInfoTabContentInfo154     TabContentInfo(std::string tabContentId, int32_t tabContentUniqueId, TabContentState state, int32_t index,
155         std::string id, int32_t uniqueId)
156         : tabContentId(std::move(tabContentId)), tabContentUniqueId(tabContentUniqueId), state(state), index(index),
157         id(std::move(id)), uniqueId(uniqueId)
158     {}
159 };
160 
161 enum class PanGestureState {
162     BEFORE = 0,
163     AFTER = 1,
164 };
165 
166 struct PanGestureInfo {
167     PanGestureState gestureState;
168     CurrentCallbackState callbackState;
169 };
170 
171 enum class GestureListenerType { TAP = 0, LONG_PRESS, PAN, PINCH, SWIPE, ROTATION, UNKNOWN };
172 
173 enum class GestureActionPhase { WILL_START = 0, WILL_END = 1, UNKNOWN = 2 };
174 
175 class ACE_FORCE_EXPORT UIObserverHandler {
176 public:
177     UIObserverHandler() = default;
178     ~UIObserverHandler() = default;
179     static UIObserverHandler& GetInstance();
180     void NotifyNavigationStateChange(const WeakPtr<AceType>& weakPattern, NavDestinationState state);
181     void NotifyScrollEventStateChange(const WeakPtr<AceType>& weakPattern, ScrollEventType scrollEvent);
182     void NotifyRouterPageStateChange(const RefPtr<PageInfo>& pageInfo, RouterPageState state);
183     void NotifyDensityChange(double density);
184     void NotifyWillClick(const GestureEvent& gestureEventInfo,
185         const ClickInfo& clickInfo, const RefPtr<FrameNode>& frameNode);
186     void NotifyDidClick(const GestureEvent& gestureEventInfo,
187         const ClickInfo& clickInfo, const RefPtr<FrameNode>& frameNode);
188     void NotifyPanGestureStateChange(const GestureEvent& gestureEventInfo,
189         const RefPtr<PanRecognizer>& current, const RefPtr<FrameNode>& frameNode,
190         const PanGestureInfo& panGestureInfo);
191     void NotifyTabContentStateUpdate(const TabContentInfo& info);
192     void NotifyGestureStateChange(NG::GestureListenerType gestureListenerType, const GestureEvent& gestureEventInfo,
193         const RefPtr<NGGestureRecognizer>& current, const RefPtr<FrameNode>& frameNode, NG::GestureActionPhase phase);
194     std::shared_ptr<NavDestinationInfo> GetNavigationState(const RefPtr<AceType>& node);
195     std::shared_ptr<NavDestinationInfo> GetNavDestinationInfo(const RefPtr<UINode>& current);
196     std::shared_ptr<NavDestinationInfo> GetNavigationInnerState(const RefPtr<AceType>& node);
197     std::shared_ptr<NavDestinationInfo> GetNavigationOuterState(const RefPtr<AceType>& node);
198     std::shared_ptr<ScrollEventInfo> GetScrollEventState(const RefPtr<AceType>& node);
199     std::shared_ptr<RouterPageInfoNG> GetRouterPageState(const RefPtr<AceType>& node);
200     void NotifyNavDestinationSwitch(std::optional<NavDestinationInfo>&& from,
201         std::optional<NavDestinationInfo>&& to, NavigationOperation operation);
202     using NavigationHandleFunc = void (*)(const NavDestinationInfo& info);
203     using ScrollEventHandleFunc = void (*)(const std::string&, int32_t, ScrollEventType, float, Ace::Axis);
204     using RouterPageHandleFunc = void (*)(AbilityContextInfo&, const RouterPageInfoNG&);
205     using DrawCommandSendHandleFunc = void (*)();
206     using LayoutDoneHandleFunc = void (*)();
207     using NavDestinationSwitchHandleFunc = void (*)(const AbilityContextInfo&, NavDestinationSwitchInfo&);
208     using WillClickHandleFunc = void (*)(
209         AbilityContextInfo&, const GestureEvent&, const ClickInfo&, const RefPtr<FrameNode>&);
210     using DidClickHandleFunc = void (*)(
211         AbilityContextInfo&, const GestureEvent&, const ClickInfo&, const RefPtr<FrameNode>&);
212     using PanGestureHandleFunc = void (*)(AbilityContextInfo&, const GestureEvent&,
213         const RefPtr<PanRecognizer>& current, const RefPtr<FrameNode>&, const NG::PanGestureInfo& panGestureInfo);
214     using GestureHandleFunc = void (*)(NG::GestureListenerType gestureListenerType,
215         const GestureEvent& gestureEventInfo, const RefPtr<NG::NGGestureRecognizer>& current,
216         const RefPtr<NG::FrameNode>& frameNode, NG::GestureActionPhase phase);
217     using TabContentStateHandleFunc = void (*)(const TabContentInfo&);
218     NavDestinationSwitchHandleFunc GetHandleNavDestinationSwitchFunc();
219     void SetHandleNavigationChangeFunc(NavigationHandleFunc func);
220     void SetHandleScrollEventChangeFunc(ScrollEventHandleFunc func);
221     void SetHandleRouterPageChangeFunc(RouterPageHandleFunc func);
222     using DensityHandleFunc = void (*)(AbilityContextInfo&, double);
223     using DensityHandleFuncForAni = std::function<void(AbilityContextInfo&, double)>;
224     void SetHandleDensityChangeFunc(DensityHandleFunc func);
225     void SetHandleDensityChangeFuncForAni(DensityHandleFuncForAni func);
226     void SetLayoutDoneHandleFunc(DrawCommandSendHandleFunc func);
227     void HandleLayoutDoneCallBack();
228     void SetDrawCommandSendHandleFunc(LayoutDoneHandleFunc func);
229     void HandleDrawCommandSendCallBack();
230     void SetHandleNavDestinationSwitchFunc(NavDestinationSwitchHandleFunc func);
231     void SetWillClickFunc(WillClickHandleFunc func);
232     void SetDidClickFunc(DidClickHandleFunc func);
233     void SetPanGestureHandleFunc(PanGestureHandleFunc func);
234     void SetHandleTabContentStateUpdateFunc(TabContentStateHandleFunc func);
235     void SetHandleGestureHandleFunc(GestureHandleFunc func);
236 private:
237     NavigationHandleFunc navigationHandleFunc_ = nullptr;
238     ScrollEventHandleFunc scrollEventHandleFunc_ = nullptr;
239     RouterPageHandleFunc routerPageHandleFunc_ = nullptr;
240     LayoutDoneHandleFunc layoutDoneHandleFunc_ = nullptr;
241     DrawCommandSendHandleFunc drawCommandSendHandleFunc_ = nullptr;
242     DensityHandleFunc densityHandleFunc_ = nullptr;
243     DensityHandleFuncForAni densityHandleFuncForAni_ = nullptr;
244     NavDestinationSwitchHandleFunc navDestinationSwitchHandleFunc_ = nullptr;
245     WillClickHandleFunc willClickHandleFunc_ = nullptr;
246     DidClickHandleFunc didClickHandleFunc_ = nullptr;
247     PanGestureHandleFunc panGestureHandleFunc_ = nullptr;
248     TabContentStateHandleFunc tabContentStateHandleFunc_ = nullptr;
249     GestureHandleFunc gestureHandleFunc_ = nullptr;
250 
251     napi_value GetUIContextValue();
252 };
253 } // namespace OHOS::Ace::NG
254 
255 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_OBSERVER_HANDLER_H
256