1 /* 2 * Copyright (c) 2022 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_ADAPTER_OHOS_ENTRANCE_SUBWINDOW_OHOS_H 17 #define FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_SUBWINDOW_OHOS_H 18 19 #include <unordered_map> 20 21 #include "event_handler.h" 22 #include "event_runner.h" 23 #include "resource_manager.h" 24 #include "wm/window.h" 25 26 #include "adapter/ohos/entrance/ace_container.h" 27 #include "adapter/ohos/entrance/platform_event_callback.h" 28 #include "base/resource/asset_manager.h" 29 #include "base/subwindow/subwindow.h" 30 #include "base/subwindow/subwindow_manager.h" 31 #include "base/thread/task_executor.h" 32 #include "core/common/ace_view.h" 33 #include "core/common/js_message_dispatcher.h" 34 #include "core/components/dialog/dialog_properties.h" 35 #include "core/components/select_popup/select_popup_component.h" 36 #include "core/components/stack/stack_element.h" 37 #include "core/components/tween/tween_component.h" 38 #include "core/components_ng/base/frame_node.h" 39 #include "core/components_ng/pattern/overlay/overlay_manager.h" 40 #include "core/pipeline/pipeline_base.h" 41 #include "core/pipeline_ng/pipeline_context.h" 42 43 namespace OHOS::Rosen { 44 class Window; 45 class WindowOption; 46 class RSUIDirector; 47 } // namespace OHOS::Rosen 48 49 namespace OHOS::Ace { 50 51 class SubwindowOhos : public Subwindow { 52 DECLARE_ACE_TYPE(SubwindowOhos, Subwindow); 53 54 public: 55 explicit SubwindowOhos(int32_t instanceId); 56 ~SubwindowOhos() = default; 57 58 void InitContainer() override; 59 void ResizeWindow() override; 60 void ResizeWindowForMenu() override; 61 NG::RectF GetRect() override; 62 void SetRect(const NG::RectF& rect) override; 63 void ShowMenu(const RefPtr<Component>& newComponent) override; 64 void ShowMenuNG(const RefPtr<NG::FrameNode> customNode, const NG::MenuParam& menuParam, 65 const RefPtr<NG::FrameNode>& targetNode, const NG::OffsetF& offset) override; 66 void ShowMenuNG(std::function<void()>&& buildFunc, std::function<void()>&& previewBuildFunc, 67 const NG::MenuParam& menuParam, const RefPtr<NG::FrameNode>& targetNode, const NG::OffsetF& offset) override; 68 bool ShowPreviewNG(bool isStartDraggingFromSubWindow = false) override; SetWindowTouchable(bool touchable)69 void SetWindowTouchable(bool touchable) override 70 { 71 CHECK_NULL_VOID(window_); 72 window_->SetTouchable(touchable); 73 } 74 void HidePreviewNG() override; 75 void HideMenuNG(const RefPtr<NG::FrameNode>& menu, int32_t targetId) override; 76 void HideMenuNG(bool showPreviewAnimation, bool startDrag) override; 77 void UpdateHideMenuOffsetNG( 78 const NG::OffsetF& offset, float menuScale, bool isRedragStart, int32_t menuWrapperId = -1) override; 79 void UpdatePreviewPosition() override; 80 bool GetMenuPreviewCenter(NG::OffsetF& offset) override; 81 void ContextMenuSwitchDragPreviewAnimationtNG(const RefPtr<NG::FrameNode>& dragPreviewNode, 82 const NG::OffsetF& offset) override; 83 void ShowPopup(const RefPtr<Component>& newComponent, bool disableTouchEvent = true) override; 84 void ShowPopupNG(int32_t targetId, const NG::PopupInfo& popupInfo, 85 const std::function<void(int32_t)>&& onWillDismiss = nullptr, bool interactiveDismiss = true) override; 86 void ShowTipsNG(int32_t targetId, const NG::PopupInfo& popupInfo, int32_t appearingTime, 87 int32_t appearingTimeWithContinuousOperation, bool isSubwindow) override; 88 void HideTipsNG(int32_t targetId, int32_t disappearingTime) override; 89 void HidePopupNG(int32_t targetId) override; 90 void GetPopupInfoNG(int32_t targetId, NG::PopupInfo& popupInfo) override; 91 bool CancelPopup(const std::string& id) override; 92 void CloseMenu() override; 93 void ClearMenu() override; 94 void ClearMenuNG(int32_t targetId, bool inWindow, bool showAnimation = false) override; 95 void ClearPopupNG() override; 96 void ClearPopupNG(bool isForceClear) override; 97 RefPtr<NG::FrameNode> ShowDialogNG(const DialogProperties& dialogProps, std::function<void()>&& buildFunc) override; 98 RefPtr<NG::FrameNode> ShowDialogNGWithNode(const DialogProperties& dialogProps, 99 const RefPtr<NG::UINode>& customNode) override; 100 void CloseDialogNG(const RefPtr<NG::FrameNode>& dialogNode) override; 101 void OpenCustomDialogNG(const DialogProperties& dialogProps, std::function<void(int32_t)>&& callback) override; 102 void CloseCustomDialogNG(int32_t dialogId) override; 103 void CloseCustomDialogNG(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)>&& callback) override; 104 void UpdateCustomDialogNG(const WeakPtr<NG::UINode>& node, const DialogProperties& dialogProps, 105 std::function<void(int32_t)>&& callback) override; 106 void HideSubWindowNG() override; GetShown()107 bool GetShown() override 108 { 109 return isShowed_; 110 } 111 112 void SetHotAreas(const std::vector<Rect>& rects, int32_t nodeId) override; 113 void DeleteHotAreas(int32_t nodeId) override; 114 void ClearToast() override; 115 void ShowToast(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback) override; 116 void CloseToast(const int32_t toastId, std::function<void(int32_t)>&& callback) override; 117 void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons, 118 bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, 119 const std::set<std::string>& callbacks) override; 120 void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons, 121 std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks) override; 122 void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 123 std::function<void(int32_t, int32_t)>&& callback) override; 124 void CloseDialog(int32_t instanceId) override; 125 void OpenCustomDialog(const PromptDialogAttr& dialogAttr, std::function<void(int32_t)>&& callback) override; 126 void CloseCustomDialog(const int32_t dialogId) override; 127 void CloseCustomDialog(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)>&& callback) override; 128 const RefPtr<NG::OverlayManager> GetOverlayManager() override; 129 GetChildContainerId()130 int32_t GetChildContainerId() const override 131 { 132 return childContainerId_; 133 } GetDialogWindow()134 sptr<OHOS::Rosen::Window> GetDialogWindow() const 135 { 136 return dialogWindow_; 137 } IsToastWindow()138 bool IsToastWindow() const 139 { 140 return isToastWindow_; 141 } SetIsToastWindow(bool isToastWindow)142 void SetIsToastWindow(bool isToastWindow) 143 { 144 isToastWindow_ = isToastWindow; 145 } 146 147 void UpdateAceView(int32_t width, int32_t height, float density, int32_t containerId); 148 149 // Gets parent window's size and offset 150 Rect GetParentWindowRect() const override; 151 Rect GetUIExtensionHostWindowRect() const override; 152 Rect GetFoldExpandAvailableRect() const override; 153 NG::RectF GetWindowRect() const override; 154 bool CheckHostWindowStatus() const override; 155 bool IsFreeMultiWindow() const override; 156 void OnFreeMultiWindowSwitch(bool enable) override; 157 int32_t RegisterFreeMultiWindowSwitchCallback(std::function<void(bool)>&& callback) override; 158 void UnRegisterFreeMultiWindowSwitchCallback(int32_t callbackId) override; 159 NeedAvoidKeyboard()160 bool NeedAvoidKeyboard() override 161 { 162 return needAvoidKeyboard_; 163 } 164 165 bool IsFocused() override; 166 void RequestFocus() override; GetSubWindow()167 const sptr<OHOS::Rosen::Window>& GetSubWindow() const 168 { 169 return window_; 170 } 171 void ResizeWindowForFoldStatus() override; 172 void ResizeWindowForFoldStatus(int32_t parentContainerId) override; 173 void MarkDirtyDialogSafeArea() override; 174 175 bool Close() override; 176 bool IsToastSubWindow() override; 177 void DestroyWindow() override; 178 179 void ResizeDialogSubwindow() override; 180 uint64_t GetDisplayId() override; 181 bool IsSameDisplayWithParentWindow(bool useInitializedId = false) override; 182 183 void InitializeSafeArea(); SetFollowParentWindowLayoutEnabled(bool enable)184 bool SetFollowParentWindowLayoutEnabled(bool enable) override 185 { 186 CHECK_NULL_RETURN(window_, false); 187 OHOS::Rosen::WMError ret = window_->SetFollowParentWindowLayoutEnabled(enable); 188 return ret == OHOS::Rosen::WMError::WM_OK; 189 } 190 bool ShowSelectOverlay(const RefPtr<NG::FrameNode>& overlayNode) override; 191 GetAttachState()192 MenuWindowState GetAttachState() override 193 { 194 return attachState_; 195 } 196 GetDetachState()197 MenuWindowState GetDetachState() override 198 { 199 return detachState_; 200 } 201 SetAttachState(MenuWindowState t)202 void SetAttachState(MenuWindowState t) 203 { 204 attachState_ = t; 205 } 206 SetDetachState(MenuWindowState t)207 void SetDetachState(MenuWindowState t) 208 { 209 detachState_ = t; 210 } 211 void ShowBindSheetNG(bool isShow, std::function<void(const std::string&)>&& callback, 212 std::function<RefPtr<NG::UINode>()>&& buildNodeFunc, std::function<RefPtr<NG::UINode>()>&& buildtitleNodeFunc, 213 NG::SheetStyle& sheetStyle, std::function<void()>&& onAppear, std::function<void()>&& onDisappear, 214 std::function<void()>&& shouldDismiss, std::function<void(const int32_t)>&& onWillDismiss, 215 std::function<void()>&& onWillAppear, std::function<void()>&& onWillDisappear, 216 std::function<void(const float)>&& onHeightDidChange, 217 std::function<void(const float)>&& onDetentsDidChange, 218 std::function<void(const float)>&& onWidthDidChange, 219 std::function<void(const float)>&& onTypeDidChange, 220 std::function<void()>&& sheetSpringBack, const RefPtr<NG::FrameNode>& targetNode) override; 221 void SwitchFollowParentWindowLayout(bool freeMultiWindowEnable) override; NeedFollowParentWindowLayout()222 bool NeedFollowParentWindowLayout() override 223 { 224 return !followParentWindowLayoutNodeIds_.empty(); 225 } 226 void AddFollowParentWindowLayoutNode(int32_t nodeId) override; 227 void RemoveFollowParentWindowLayoutNode(int32_t nodeId) override; SetNodeId(int32_t nodeId)228 void SetNodeId(int32_t nodeId) override 229 { 230 nodeId_ = nodeId; 231 } GetNodeId()232 int32_t GetNodeId() const override 233 { 234 return nodeId_; 235 } 236 void SetWindowAnchorInfo(const NG::OffsetF& offset, SubwindowType type, int32_t nodeId) override; 237 Rosen::WindowAnchorInfo WindowAnchorInfoConverter(const NG::OffsetF& offset, SubwindowType type); 238 239 #if defined(ACE_STATIC) 240 void ShowToastStatic(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback) override; 241 void CloseToastStatic(const int32_t toastId, std::function<void(int32_t)>&& callback) override; 242 void ShowDialogStatic(DialogProperties& dialogProps, std::function<void(int32_t, int32_t)>&& callback) override; 243 void ShowActionMenuStatic(DialogProperties& dialogProps, 244 std::function<void(int32_t, int32_t)>&& callback) override; 245 void OpenCustomDialogStatic(DialogProperties& dialogProps, 246 std::function<void(int32_t)>&& callback) override; 247 #endif 248 private: 249 RefPtr<StackElement> GetStack(); 250 void AddMenu(const RefPtr<Component>& newComponent); 251 void ShowWindow(bool needFocus = true); 252 void HideWindow(); 253 254 // Convert Rect to Rosen::Rect 255 void RectConverter(const Rect& rect, Rosen::Rect& rosenRect); 256 257 bool CreateEventRunner(); 258 void GetToastDialogWindowProperty( 259 int32_t& width, int32_t& height, int32_t& posX, int32_t& posY, float& density) const; 260 bool InitToastDialogWindow(int32_t& width, int32_t& height, int32_t posX, int32_t posY, bool isToast = false); 261 bool InitToastDialogView(int32_t width, int32_t height, float density); 262 bool InitToastServiceConfig(); 263 void ShowToastForAbility(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback); 264 void ShowToastForService(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback); 265 void ShowDialogForAbility(const std::string& title, const std::string& message, 266 const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, 267 const std::set<std::string>& callbacks); 268 void ShowDialogForService(const std::string& title, const std::string& message, 269 const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, 270 const std::set<std::string>& callbacks); 271 void ShowDialogForAbility(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons, 272 std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks); 273 void ShowDialogForService(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons, 274 std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks); 275 void OpenCustomDialogForAbility(const PromptDialogAttr& dialogAttr, std::function<void(int32_t)>&& callback); 276 void OpenCustomDialogForService(const PromptDialogAttr& dialogAttr, std::function<void(int32_t)>&& callback); 277 void ShowActionMenuForAbility(const std::string& title, const std::vector<ButtonInfo>& button, 278 std::function<void(int32_t, int32_t)>&& callback); 279 void ShowActionMenuForService(const std::string& title, const std::vector<ButtonInfo>& button, 280 std::function<void(int32_t, int32_t)>&& callback); 281 282 RefPtr<PipelineBase> GetChildPipelineContext() const; 283 std::function<void()> GetInitToastDelayTask(const NG::ToastInfo& toastInfo, 284 std::function<void(int32_t)>&& callback); 285 void ContainerModalUnFocus(); 286 287 void HidePixelMap(bool startDrag = false, double x = 0, double y = 0, bool showAnimation = true); 288 void HideEventColumn(); 289 Rosen::WindowType GetToastRosenType(bool IsSceneBoardEnabled); 290 291 void SetToastWindowOption(RefPtr<Platform::AceContainer>& parentContainer, 292 OHOS::sptr<OHOS::Rosen::WindowOption>& windowOption, 293 const Rosen::WindowType& toastWindowType, uint32_t mainWindowId); 294 295 void InitDialogWindowRSUIDirector(const RefPtr<Platform::AceContainer>& container); 296 void InitWindowRSUIDirector(const RefPtr<Platform::AceContainer>& container); 297 298 static int32_t id_; 299 int32_t windowId_ = 0; 300 int32_t parentContainerId_ = -1; 301 int32_t childContainerId_ = -1; 302 uint64_t defaultDisplayId_ = 0; 303 int32_t nodeId_ = -1; 304 std::shared_ptr<OHOS::Rosen::RSUIDirector> rsUiDirector; 305 sptr<OHOS::Rosen::Window> window_ = nullptr; 306 RefPtr<SelectPopupComponent> popup_; 307 std::unordered_map<int32_t, std::vector<Rosen::Rect>> hotAreasMap_; 308 309 sptr<OHOS::Rosen::Window> dialogWindow_; 310 std::shared_ptr<AppExecFwk::EventRunner> eventLoop_; 311 std::shared_ptr<AppExecFwk::EventHandler> handler_; 312 bool isToastWindow_ = false; 313 int32_t popupTargetId_ = -1; 314 bool haveDialog_ = false; 315 bool isShowed_ = false; 316 bool isClosing_ = false; 317 bool needAvoidKeyboard_ = false; 318 bool ifNeedSetCurrentWindow_ = true; 319 sptr<OHOS::Rosen::Window> parentWindow_ = nullptr; 320 int32_t callbackId_ = 0; 321 sptr<OHOS::Rosen::ISwitchFreeMultiWindowListener> freeMultiWindowListener_ = nullptr; 322 std::unordered_map<int32_t, std::function<void(bool)>> freeMultiWindowSwitchCallbackMap_; 323 NG::RectF windowRect_; 324 std::mutex eventRunnerMutex_; 325 MenuWindowState attachState_ = MenuWindowState::DEFAULT; 326 MenuWindowState detachState_ = MenuWindowState::DEFAULT; 327 std::list<int32_t> followParentWindowLayoutNodeIds_; 328 #if defined(ACE_STATIC) 329 void ShowDialogForAbilityStatic(DialogProperties& dialogProps, std::function<void(int32_t, int32_t)>&& callback); 330 void ShowDialogForServiceStatic(DialogProperties& dialogProps, std::function<void(int32_t, int32_t)>&& callback); 331 void ShowActionMenuForAbilityStatic(DialogProperties& dialogProps, 332 std::function<void(int32_t, int32_t)>&& callback); 333 void ShowActionMenuForServiceStatic(DialogProperties& dialogProps, 334 std::function<void(int32_t, int32_t)>&& callback); 335 void OpenCustomDialogForAbilityStatic(DialogProperties& dialogProps, 336 std::function<void(int32_t)>&& callback); 337 void OpenCustomDialogForServiceStatic(DialogProperties& dialogProps, 338 std::function<void(int32_t)>&& callback); 339 #endif 340 }; 341 342 class MenuWindowSceneListener : public OHOS::Rosen::IWindowAttachStateChangeListner { 343 public: MenuWindowSceneListener(WeakPtr<SubwindowOhos> sub)344 explicit MenuWindowSceneListener(WeakPtr<SubwindowOhos> sub) : sub_(sub) {} 345 ~MenuWindowSceneListener() = default; AfterAttached()346 void AfterAttached() 347 { 348 TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "receive callback: AfterAttachToFrameNode"); 349 auto sub = sub_.Upgrade(); 350 CHECK_NULL_VOID(sub); 351 sub->SetAttachState(MenuWindowState::ATTACHED); 352 } 353 AfterDetached()354 void AfterDetached() 355 { 356 TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "receive callback: AfterDetachToFrameNode"); 357 auto sub = sub_.Upgrade(); 358 CHECK_NULL_VOID(sub); 359 sub->SetDetachState(MenuWindowState::DETACHED); 360 } 361 private: 362 WeakPtr<SubwindowOhos> sub_; 363 }; 364 365 } // namespace OHOS::Ace 366 367 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_SUBWINDOW_OHOS_H 368