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 #include <array>
23 
24 #include "base/memory/referenced.h"
25 #include "base/subwindow/subwindow.h"
26 #include "base/utils/macros.h"
27 #include "base/utils/noncopyable.h"
28 #include "core/components/dialog/dialog_properties.h"
29 #include "core/components_ng/base/frame_node.h"
30 #include "core/components_ng/pattern/overlay/overlay_manager.h"
31 
32 namespace OHOS::Ace {
33 
34 using SubwindowMap = std::unordered_map<int32_t, RefPtr<Subwindow>>;
35 
36 class ACE_FORCE_EXPORT SubwindowManager final : public NonCopyable {
37 public:
38     // Get the instance
39     static std::shared_ptr<SubwindowManager> GetInstance();
40 
41     void AddContainerId(uint32_t windowId, int32_t containerId);
42     void RemoveContainerId(uint32_t windowId);
43     int32_t GetContainerId(uint32_t windowId);
44 
45     void AddParentContainerId(int32_t containerId, int32_t parentContainerId);
46     void RemoveParentContainerId(int32_t containerId);
47     int32_t GetParentContainerId(int32_t containerId);
48     int32_t GetSubContainerId(int32_t parentContainerId);
49 
50     void AddSubwindow(int32_t instanceId, RefPtr<Subwindow>);
51     void RemoveSubwindow(int32_t instanceId);
52 
53     // Get the subwindow of instance, return the window or nullptr.
54     const RefPtr<Subwindow> GetSubwindow(int32_t instanceId);
55 
56     void HideCurrentSubwindow();
57 
58     void SetCurrentSubwindowName(const std::string& currentSubwindow);
59     std::string GetCurrentSubWindowName();
60 
61     void SetCurrentSubwindow(const RefPtr<Subwindow>& subwindow);
62 
63     const RefPtr<Subwindow>& GetCurrentWindow();
64     Rect GetParentWindowRect();
65 
66     RefPtr<Subwindow> ShowPreviewNG(bool isStartDraggingFromSubWindow);
67     void HidePreviewNG();
68     void ShowMenu(const RefPtr<Component>& newComponent);
69     void ShowMenuNG(const RefPtr<NG::FrameNode>& menuNode, const NG::MenuParam& menuParam,
70         const RefPtr<NG::FrameNode>& targetNode, const NG::OffsetF& offset);
71     void ShowMenuNG(std::function<void()>&& buildFunc, std::function<void()>&& previewBuildFunc,
72         const NG::MenuParam& menuParam, const RefPtr<NG::FrameNode>& targetNode, const NG::OffsetF& offset);
73     void HideMenuNG(const RefPtr<NG::FrameNode>& menu, int32_t targetId);
74     void HideMenuNG(bool showPreviewAnimation = true, bool startDrag = false);
75     void UpdateHideMenuOffsetNG(const NG::OffsetF& offset = NG::OffsetF(0.0f, 0.0f), float menuScale = 1.0f,
76         bool isRedragStart = false, int32_t menuWrapperId = -1);
77     void UpdatePreviewPosition();
78     bool GetMenuPreviewCenter(NG::OffsetF& offset);
79     void ContextMenuSwitchDragPreviewAnimation(const RefPtr<NG::FrameNode>& dragPreviewNode,
80         const NG::OffsetF& offset = NG::OffsetF(0.0f, 0.0f));
81     void ShowPopup(const RefPtr<Component>& newComponent, bool disableTouchEvent = true);
82     void ShowPopupNG(const RefPtr<NG::FrameNode>& targetNode, const NG::PopupInfo& popupInfo,
83         const std::function<void(int32_t)>&& onWillDismiss = nullptr, bool interactiveDismiss = true);
84     void HidePopupNG(int32_t targetId, int32_t instanceId = -1);
85     bool CancelPopup(const std::string& id);
86     void CloseMenu();
87     void ClearMenu();
88     void ClearMenuNG(int32_t instanceId = -1, int32_t targetId = -1, bool inWindow = true, bool showAnimation = false);
89     void ClearPopupInSubwindow(int32_t instanceId = -1);
90     RefPtr<NG::FrameNode> ShowDialogNG(const DialogProperties& dialogProps, std::function<void()>&& buildFunc);
91     RefPtr<NG::FrameNode> ShowDialogNGWithNode(const DialogProperties& dialogProps,
92         const RefPtr<NG::UINode>& customNode);
93     void CloseDialogNG(const RefPtr<NG::FrameNode>& dialogNode);
94     void OpenCustomDialogNG(const DialogProperties& dialogProps, std::function<void(int32_t)>&& callback);
95     void CloseCustomDialogNG(int32_t dialogId);
96     void CloseCustomDialogNG(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)>&& callback);
97     void UpdateCustomDialogNG(const WeakPtr<NG::UINode>& node, const PromptDialogAttr &dialogAttr,
98         std::function<void(int32_t)>&& callback);
99     void HideSubWindowNG();
100     void HideDialogSubWindow(int32_t instanceId);
101     void SetHotAreas(const std::vector<Rect>& rects, int32_t nodeId = -1, int32_t instanceId = -1);
GetDialogSubWindowId()102     int32_t GetDialogSubWindowId()
103     {
104         return dialogSubWindowId_;
105     }
SetDialogSubWindowId(int32_t dialogSubWindowId)106     void SetDialogSubWindowId(int32_t dialogSubWindowId)
107     {
108         dialogSubWindowId_ = dialogSubWindowId;
109     }
110     void AddDialogSubwindow(int32_t instanceId, const RefPtr<Subwindow>& subwindow);
111     // Get the dialog subwindow of instance, return the window or nullptr.
112     int32_t GetDialogSubwindowInstanceId(int32_t SubwindowId);
113     const RefPtr<Subwindow> GetDialogSubwindow(int32_t instanceId);
114     void SetCurrentDialogSubwindow(const RefPtr<Subwindow>& subwindow);
115     const RefPtr<Subwindow>& GetCurrentDialogWindow();
116     void DeleteHotAreas(int32_t subwindowId, int32_t nodeId);
117 
118     void ClearToastInSubwindow();
119     void ShowToast(const NG::ToastInfo& toastInfo);
120     void ShowToastNG(const NG::ToastInfo& toastInfo);
121     const RefPtr<Subwindow> GetToastSubwindow(int32_t instanceId);
122     void AddToastSubwindow(int32_t instanceId, RefPtr<Subwindow> subwindow);
123     void HideToastSubWindowNG();
124     ToastWindowType GetToastWindowType(int32_t instanceId);
125     void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons,
126         bool autoCancel, std::function<void(int32_t, int32_t)>&& napiCallback,
127         const std::set<std::string>& dialogCallbacks);
128     void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
129         std::function<void(int32_t, int32_t)>&& napiCallback, const std::set<std::string>& dialogCallbacks);
130     void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
131         std::function<void(int32_t, int32_t)>&& callback);
132     void CloseDialog(int32_t instanceId);
133     void RequestFocusSubwindow(int32_t instanceId);
134     void OpenCustomDialog(const PromptDialogAttr &dialogAttr, std::function<void(int32_t)> &&callback);
135     void CloseCustomDialog(const int32_t dialogId);
136     void CloseCustomDialog(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)> &&callback);
137 
138     bool GetShown();
139     void ResizeWindowForFoldStatus(int32_t parentContainerId);
140     void MarkDirtyDialogSafeArea();
141     void OnWindowSizeChanged(int32_t instanceId, Rect windowRect, WindowSizeChangeReason reason);
142     void HideSystemTopMostWindow();
143     const RefPtr<Subwindow> GetSystemToastWindow(int32_t instanceId);
144     void AddSystemToastWindow(int32_t instanceId, RefPtr<Subwindow> subwindow);
145     void ClearToastInSystemSubwindow();
146     bool IsSubwindowExist(RefPtr<Subwindow> subwindow);
147     bool IsFreeMultiWindow(int32_t instanceId) const;
148 
149     RefPtr<NG::FrameNode> GetSubwindowDialogNodeWithExistContent(const RefPtr<NG::UINode>& node);
150 
151 private:
152     RefPtr<Subwindow> GetOrCreateSubWindow(bool isDialog = false);
153     RefPtr<Subwindow> GetOrCreateSystemSubWindow(int32_t containerId);
154     RefPtr<Subwindow> GetOrCreateToastWindow(int32_t containerId, const NG::ToastShowMode& showMode);
155     RefPtr<Subwindow> GetOrCreateToastWindowNG(int32_t containerId, const ToastWindowType& windowType,
156         uint32_t mainWindowId);
157     static std::mutex instanceMutex_;
158     static std::shared_ptr<SubwindowManager> instance_;
159 
160     std::mutex mutex_;
161     std::unordered_map<uint32_t, int32_t> containerMap_;
162 
163     std::mutex parentMutex_;
164     std::unordered_map<int32_t, int32_t> parentContainerMap_;
165 
166     // Used to save the relationship between container and subwindow, it is 1:1
167     std::mutex subwindowMutex_;
168     SubwindowMap subwindowMap_;
169     int32_t dialogSubWindowId_;
170     std::mutex currentSubwindowMutex_;
171     std::string currentSubwindowName_;
172 
173     RefPtr<Subwindow> currentSubwindow_;
174 
175     std::mutex toastMutex_;
176     SubwindowMap toastWindowMap_;
177     // Used to save the relationship between container and dialog subwindow, it is 1:1
178     std::mutex dialogSubwindowMutex_;
179     SubwindowMap dialogSubwindowMap_;
180     std::mutex currentDialogSubwindowMutex_;
181     RefPtr<Subwindow> currentDialogSubwindow_;
182     Rect uiExtensionWindowRect_;
183     std::mutex systemToastMutex_;
184     SubwindowMap systemToastWindowMap_;
185 };
186 
187 } // namespace OHOS::Ace
188 
189 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_SUBWINDOW_SUBWINDOW_MANAGER_H
190