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 NavDestinationMode mode; 53 int32_t uniqueId; 54 55 NavDestinationInfo() = default; 56 NavDestinationInfoNavDestinationInfo57 NavDestinationInfo(std::string id, std::string name, NavDestinationState state) 58 : navigationId(std::move(id)), name(std::move(name)), state(state) 59 {} 60 NavDestinationInfoNavDestinationInfo61 NavDestinationInfo(std::string id, std::string name, NavDestinationState state, 62 int32_t index, napi_value param, std::string navDesId) 63 : navigationId(std::move(id)), name(std::move(name)), state(state), 64 index(index), param(param), navDestinationId(std::move(navDesId)) 65 {} 66 NavDestinationInfoNavDestinationInfo67 NavDestinationInfo(std::string id, std::string name, NavDestinationState state, 68 int32_t index, napi_value param, std::string navDesId, NavDestinationMode mode, int32_t uniqueId) 69 : navigationId(std::move(id)), name(std::move(name)), state(state), 70 index(index), param(param), navDestinationId(std::move(navDesId)), mode(mode), uniqueId(std::move(uniqueId)) 71 {} 72 }; 73 74 enum class ScrollEventType { 75 SCROLL_START = 0, 76 SCROLL_STOP = 1, 77 }; 78 79 struct ScrollEventInfo { 80 std::string id; 81 int32_t uniqueId; 82 ScrollEventType scrollEvent; 83 float offset; 84 ScrollEventInfoScrollEventInfo85 ScrollEventInfo(std::string id, int32_t uniqueId, ScrollEventType scrollEvent, float offset) 86 : id(std::move(id)), uniqueId(uniqueId), scrollEvent(scrollEvent), offset(offset) 87 {} 88 }; 89 90 struct NavDestinationSwitchInfo { 91 // UIContext 92 napi_value context; 93 std::optional<NavDestinationInfo> from; 94 std::optional<NavDestinationInfo> to; 95 NavigationOperation operation; 96 NavDestinationSwitchInfoNavDestinationSwitchInfo97 NavDestinationSwitchInfo(napi_value ctx, std::optional<NavDestinationInfo>&& fromInfo, 98 std::optional<NavDestinationInfo>&& toInfo, NavigationOperation op) 99 : context(ctx), from(std::forward<std::optional<NavDestinationInfo>>(fromInfo)), 100 to(std::forward<std::optional<NavDestinationInfo>>(toInfo)), operation(op) 101 {} 102 }; 103 104 struct RouterPageInfoNG { 105 napi_value context; 106 int32_t index; 107 std::string name; 108 std::string path; 109 RouterPageState state; 110 std::string pageId; 111 RouterPageInfoNGRouterPageInfoNG112 RouterPageInfoNG(napi_value context, int32_t index, std::string name, std::string path, RouterPageState state, 113 std::string pageId) 114 : context(context), index(index), name(std::move(name)), path(std::move(path)), state(state), 115 pageId(std::move(pageId)) 116 {} 117 }; 118 119 struct AbilityContextInfo { 120 std::string name = ""; 121 std::string bundleName = ""; 122 std::string moduleName = ""; 123 IsEqualAbilityContextInfo124 bool IsEqual(const AbilityContextInfo& info) const 125 { 126 return name == info.name && bundleName == info.bundleName && moduleName == info.moduleName; 127 } 128 }; 129 130 enum class TabContentState { 131 ON_SHOW = 0, 132 ON_HIDE = 1, 133 }; 134 135 struct TabContentInfo { 136 std::string tabContentId; 137 int32_t tabContentUniqueId = 0; 138 TabContentState state; 139 int32_t index = 0; 140 std::string id; 141 int32_t uniqueId = 0; 142 TabContentInfoTabContentInfo143 TabContentInfo(std::string tabContentId, int32_t tabContentUniqueId, TabContentState state, int32_t index, 144 std::string id, int32_t uniqueId) 145 : tabContentId(std::move(tabContentId)), tabContentUniqueId(tabContentUniqueId), state(state), index(index), 146 id(std::move(id)), uniqueId(uniqueId) 147 {} 148 }; 149 150 class ACE_FORCE_EXPORT UIObserverHandler { 151 public: 152 UIObserverHandler() = default; 153 ~UIObserverHandler() = default; 154 static UIObserverHandler& GetInstance(); 155 void NotifyNavigationStateChange(const WeakPtr<AceType>& weakPattern, NavDestinationState state); 156 void NotifyScrollEventStateChange(const WeakPtr<AceType>& weakPattern, ScrollEventType scrollEvent); 157 void NotifyRouterPageStateChange(const RefPtr<PageInfo>& pageInfo, RouterPageState state); 158 void NotifyDensityChange(double density); 159 void NotifyWillClick(const GestureEvent& gestureEventInfo, 160 const ClickInfo& clickInfo, const RefPtr<FrameNode>& frameNode); 161 void NotifyDidClick(const GestureEvent& gestureEventInfo, 162 const ClickInfo& clickInfo, const RefPtr<FrameNode>& frameNode); 163 void NotifyTabContentStateUpdate(const TabContentInfo& info); 164 std::shared_ptr<NavDestinationInfo> GetNavigationState(const RefPtr<AceType>& node); 165 std::shared_ptr<ScrollEventInfo> GetScrollEventState(const RefPtr<AceType>& node); 166 std::shared_ptr<RouterPageInfoNG> GetRouterPageState(const RefPtr<AceType>& node); 167 void NotifyNavDestinationSwitch(std::optional<NavDestinationInfo>&& from, 168 std::optional<NavDestinationInfo>&& to, NavigationOperation operation); 169 using NavigationHandleFunc = void (*)(const NavDestinationInfo& info); 170 using ScrollEventHandleFunc = void (*)(const std::string&, int32_t, ScrollEventType, float); 171 using RouterPageHandleFunc = void (*)(AbilityContextInfo&, const RouterPageInfoNG&); 172 using DrawCommandSendHandleFunc = void (*)(); 173 using LayoutDoneHandleFunc = void (*)(); 174 using NavDestinationSwitchHandleFunc = void (*)(const AbilityContextInfo&, NavDestinationSwitchInfo&); 175 using WillClickHandleFunc = void (*)( 176 AbilityContextInfo&, const GestureEvent&, const ClickInfo&, const RefPtr<FrameNode>&); 177 using DidClickHandleFunc = void (*)( 178 AbilityContextInfo&, const GestureEvent&, const ClickInfo&, const RefPtr<FrameNode>&); 179 using TabContentStateHandleFunc = void (*)(const TabContentInfo&); 180 NavDestinationSwitchHandleFunc GetHandleNavDestinationSwitchFunc(); 181 void SetHandleNavigationChangeFunc(NavigationHandleFunc func); 182 void SetHandleScrollEventChangeFunc(ScrollEventHandleFunc func); 183 void SetHandleRouterPageChangeFunc(RouterPageHandleFunc func); 184 using DensityHandleFunc = void (*)(AbilityContextInfo&, double); 185 void SetHandleDensityChangeFunc(DensityHandleFunc func); 186 void SetLayoutDoneHandleFunc(DrawCommandSendHandleFunc func); 187 void HandleLayoutDoneCallBack(); 188 void SetDrawCommandSendHandleFunc(LayoutDoneHandleFunc func); 189 void HandleDrawCommandSendCallBack(); 190 void SetHandleNavDestinationSwitchFunc(NavDestinationSwitchHandleFunc func); 191 void SetWillClickFunc(WillClickHandleFunc func); 192 void SetDidClickFunc(DidClickHandleFunc func); 193 void SetHandleTabContentStateUpdateFunc(TabContentStateHandleFunc func); 194 private: 195 NavigationHandleFunc navigationHandleFunc_ = nullptr; 196 ScrollEventHandleFunc scrollEventHandleFunc_ = nullptr; 197 RouterPageHandleFunc routerPageHandleFunc_ = nullptr; 198 LayoutDoneHandleFunc layoutDoneHandleFunc_ = nullptr; 199 DrawCommandSendHandleFunc drawCommandSendHandleFunc_ = nullptr; 200 DensityHandleFunc densityHandleFunc_ = nullptr; 201 NavDestinationSwitchHandleFunc navDestinationSwitchHandleFunc_ = nullptr; 202 WillClickHandleFunc willClickHandleFunc_ = nullptr; 203 DidClickHandleFunc didClickHandleFunc_ = nullptr; 204 TabContentStateHandleFunc tabContentStateHandleFunc_ = nullptr; 205 206 napi_value GetUIContextValue(); 207 }; 208 } // namespace OHOS::Ace::NG 209 210 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_OBSERVER_HANDLER_H 211