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_FRAMEWORKS_CORE_COMMON_SUBWINDOW_SUBWINDOW_MANAGER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_SUBWINDOW_SUBWINDOW_MANAGER_H 18 19 #include <mutex> 20 #include <set> 21 #include <unordered_map> 22 23 #include "base/memory/referenced.h" 24 #include "base/subwindow/subwindow.h" 25 #include "base/utils/macros.h" 26 #include "base/utils/noncopyable.h" 27 #include "core/components/dialog/dialog_properties.h" 28 #include "core/components_ng/base/frame_node.h" 29 #include "core/components_ng/pattern/overlay/overlay_manager.h" 30 31 namespace OHOS::Ace { 32 33 using SubwindowMap = std::unordered_map<int32_t, RefPtr<Subwindow>>; 34 35 class ACE_FORCE_EXPORT SubwindowManager final : public NonCopyable { 36 public: 37 // Get the instance 38 static std::shared_ptr<SubwindowManager> GetInstance(); 39 40 void AddContainerId(uint32_t windowId, int32_t containerId); 41 void RemoveContainerId(uint32_t windowId); 42 int32_t GetContainerId(uint32_t windowId); 43 44 void AddParentContainerId(int32_t containerId, int32_t parentContainerId); 45 void RemoveParentContainerId(int32_t containerId); 46 int32_t GetParentContainerId(int32_t containerId); 47 48 void AddSubwindow(int32_t instanceId, RefPtr<Subwindow>); 49 void RemoveSubwindow(int32_t instanceId); 50 51 // Get the subwindow of instance, return the window or nullptr. 52 const RefPtr<Subwindow> GetSubwindow(int32_t instanceId); 53 54 void HideCurrentSubwindow(); 55 56 void SetCurrentSubwindowName(const std::string& currentSubwindow); 57 std::string GetCurrentSubWindowName(); 58 59 void SetCurrentSubwindow(const RefPtr<Subwindow>& subwindow); 60 61 const RefPtr<Subwindow>& GetCurrentWindow(); 62 Rect GetParentWindowRect(); 63 64 RefPtr<Subwindow> ShowPreviewNG(); 65 void HidePreviewNG(); 66 void ShowMenu(const RefPtr<Component>& newComponent); 67 void ShowMenuNG( 68 const RefPtr<NG::FrameNode>& menuNode, int32_t targetId, const NG::OffsetF& offset, bool isAboveApps = false); 69 void HideMenuNG(const RefPtr<NG::FrameNode>& menu, int32_t targetId); 70 void HideMenuNG(bool showPreviewAnimation = true, bool startDrag = false); 71 void UpdateHideMenuOffsetNG(const NG::OffsetF& offset = NG::OffsetF(0.0f, 0.0f)); 72 void ShowPopup(const RefPtr<Component>& newComponent, bool disableTouchEvent = true); 73 void ShowPopupNG(int32_t targetId, const NG::PopupInfo& popupInfo); 74 void HidePopupNG(int32_t targetId, int32_t instanceId = -1); 75 bool CancelPopup(const std::string& id); 76 void CloseMenu(); 77 void ClearMenu(); 78 void ClearMenuNG(int32_t instanceId = -1, bool inWindow = true, bool showAnimation = false); 79 void ClearPopupInSubwindow(int32_t instanceId = -1); 80 RefPtr<NG::FrameNode> ShowDialogNG(const DialogProperties& dialogProps, std::function<void()>&& buildFunc); 81 void CloseDialogNG(const RefPtr<NG::FrameNode>& dialogNode); 82 void HideSubWindowNG(); 83 void HideDialogSubWindow(int32_t instanceId); 84 void SetDialogHotAreas(const std::vector<Rect>& rects, int32_t overlayId, int32_t instanceId); 85 void SetHotAreas(const std::vector<Rect>& rects, int32_t overlayId = -1, int32_t instanceId = -1); GetDialogSubWindowId()86 int32_t GetDialogSubWindowId() 87 { 88 return dialogSubWindowId_; 89 } SetDialogSubWindowId(int32_t dialogSubWindowId)90 void SetDialogSubWindowId(int32_t dialogSubWindowId) 91 { 92 dialogSubWindowId_ = dialogSubWindowId; 93 } 94 void AddDialogSubwindow(int32_t instanceId, const RefPtr<Subwindow>& subwindow); 95 // Get the dialog subwindow of instance, return the window or nullptr. 96 int32_t GetDialogSubwindowInstanceId(int32_t SubwindowId); 97 const RefPtr<Subwindow> GetDialogSubwindow(int32_t instanceId); 98 void SetCurrentDialogSubwindow(const RefPtr<Subwindow>& subwindow); 99 const RefPtr<Subwindow>& GetCurrentDialogWindow(); 100 void DeleteHotAreas(int32_t subwindowid, int32_t overlayid); 101 102 void ClearToastInSubwindow(); 103 void ShowToast( 104 const std::string& message, int32_t duration, const std::string& bottom, const NG::ToastShowMode& showMode); 105 void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons, 106 bool autoCancel, std::function<void(int32_t, int32_t)>&& napiCallback, 107 const std::set<std::string>& dialogCallbacks); 108 void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons, 109 std::function<void(int32_t, int32_t)>&& napiCallback, const std::set<std::string>& dialogCallbacks); 110 void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 111 std::function<void(int32_t, int32_t)>&& callback); 112 void CloseDialog(int32_t instanceId); 113 void RequestFocusSubwindow(int32_t instanceId); 114 void OpenCustomDialog(const PromptDialogAttr &dialogAttr, std::function<void(int32_t)> &&callback); 115 void CloseCustomDialog(const int32_t dialogId); 116 117 bool GetShown(); 118 void ResizeWindowForFoldStatus(); 119 private: 120 RefPtr<Subwindow> GetOrCreateSubWindow(); 121 122 static std::mutex instanceMutex_; 123 static std::shared_ptr<SubwindowManager> instance_; 124 125 std::mutex mutex_; 126 std::unordered_map<uint32_t, int32_t> containerMap_; 127 128 std::mutex parentMutex_; 129 std::unordered_map<int32_t, int32_t> parentContainerMap_; 130 131 // Used to save the relationship between container and subwindow, it is 1:1 132 std::mutex subwindowMutex_; 133 SubwindowMap subwindowMap_; 134 int32_t dialogSubWindowId_; 135 std::mutex currentSubwindowMutex_; 136 std::string currentSubwindowName_; 137 138 RefPtr<Subwindow> currentSubwindow_; 139 140 // Used to save the relationship between container and dialog subwindow, it is 1:1 141 std::mutex dialogSubwindowMutex_; 142 SubwindowMap dialogSubwindowMap_; 143 std::mutex currentDialogSubwindowMutex_; 144 RefPtr<Subwindow> currentDialogSubwindow_; 145 }; 146 147 } // namespace OHOS::Ace 148 149 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_SUBWINDOW_SUBWINDOW_MANAGER_H 150