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 constexpr int32_t MIN_SUBCONTAINER_ID = 1000000; 34 constexpr int32_t MIN_PA_SERVICE_ID = 100000; 35 36 using SubwindowMap = std::unordered_map<int32_t, RefPtr<Subwindow>>; 37 38 class ACE_FORCE_EXPORT SubwindowManager final : public NonCopyable { 39 public: 40 // Get the instance 41 static std::shared_ptr<SubwindowManager> GetInstance(); 42 43 void AddContainerId(uint32_t windowId, int32_t containerId); 44 void RemoveContainerId(uint32_t windowId); 45 int32_t GetContainerId(uint32_t windowId); 46 47 void AddParentContainerId(int32_t containerId, int32_t parentContainerId); 48 void RemoveParentContainerId(int32_t containerId); 49 int32_t GetParentContainerId(int32_t containerId); 50 51 void AddSubwindow(int32_t instanceId, RefPtr<Subwindow>); 52 void RemoveSubwindow(int32_t instanceId); 53 54 // Get the subwindow of instance, return the window or nullptr. 55 const RefPtr<Subwindow> GetSubwindow(int32_t instanceId); 56 57 void HideCurrentSubwindow(); 58 59 void SetCurrentSubwindowName(const std::string& currentSubwindow); 60 std::string GetCurrentSubWindowName(); 61 62 void SetCurrentSubwindow(const RefPtr<Subwindow>& subwindow); 63 64 const RefPtr<Subwindow>& GetCurrentWindow(); 65 Rect GetParentWindowRect(); 66 67 void ShowMenu(const RefPtr<Component>& newComponent); 68 void ShowMenuNG(const RefPtr<NG::FrameNode>& menuNode, int32_t targetId, 69 const NG::OffsetF& offset, bool isAboveApps = false); 70 void HideMenuNG(const RefPtr<NG::FrameNode>& menu, int32_t targetId); 71 void HideMenuNG(); 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); 79 RefPtr<NG::FrameNode> ShowDialogNG(const DialogProperties& dialogProps, std::function<void()>&& buildFunc); 80 void HideSubWindowNG(); 81 82 void SetHotAreas(const std::vector<Rect>& rects, int32_t overlayId = -1, int32_t instanceId = -1); 83 84 void AddDialogSubwindow(int32_t instanceId, const RefPtr<Subwindow>& subwindow); 85 // Get the dialog subwindow of instance, return the window or nullptr. 86 const RefPtr<Subwindow> GetDialogSubwindow(int32_t instanceId); 87 void SetCurrentDialogSubwindow(const RefPtr<Subwindow>& subwindow); 88 const RefPtr<Subwindow>& GetCurrentDialogWindow(); 89 90 void ShowToast(const std::string& message, int32_t duration, const std::string& bottom); 91 void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons, 92 bool autoCancel, std::function<void(int32_t, int32_t)>&& napiCallback, 93 const std::set<std::string>& dialogCallbacks); 94 void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons, 95 std::function<void(int32_t, int32_t)>&& napiCallback, const std::set<std::string>& dialogCallbacks); 96 void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 97 std::function<void(int32_t, int32_t)>&& callback); 98 void CloseDialog(int32_t instanceId); 99 void RegisterOnShowMenu(const std::function<void()>& callback); 100 void RegisterOnHideMenu(const std::function<void()>& callback); 101 void RequestFocusSubwindow(int32_t instanceId); 102 103 private: 104 RefPtr<Subwindow> GetOrCreateSubWindow(); 105 106 static std::mutex instanceMutex_; 107 static std::shared_ptr<SubwindowManager> instance_; 108 109 std::mutex mutex_; 110 std::unordered_map<uint32_t, int32_t> containerMap_; 111 112 std::mutex parentMutex_; 113 std::unordered_map<int32_t, int32_t> parentContainerMap_; 114 115 // Used to save the relationship between container and subwindow, it is 1:1 116 std::mutex subwindowMutex_; 117 SubwindowMap subwindowMap_; 118 119 std::mutex currentSubwindowMutex_; 120 std::string currentSubwindowName_; 121 122 RefPtr<Subwindow> currentSubwindow_; 123 124 // Used to save the relationship between container and dialog subwindow, it is 1:1 125 std::mutex dialogSubwindowMutex_; 126 SubwindowMap dialogSubwindowMap_; 127 std::mutex currentDialogSubwindowMutex_; 128 RefPtr<Subwindow> currentDialogSubwindow_; 129 std::function<void()> onShowMenuCallback_; 130 std::function<void()> onHideMenuCallback_; 131 }; 132 133 } // namespace OHOS::Ace 134 135 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_SUBWINDOW_SUBWINDOW_MANAGER_H 136