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/platform_event_callback.h"
27 #include "base/resource/asset_manager.h"
28 #include "base/subwindow/subwindow.h"
29 #include "base/subwindow/subwindow_manager.h"
30 #include "base/thread/task_executor.h"
31 #include "core/common/ace_view.h"
32 #include "core/common/js_message_dispatcher.h"
33 #include "core/components/dialog/dialog_properties.h"
34 #include "core/components/select_popup/select_popup_component.h"
35 #include "core/components/stack/stack_element.h"
36 #include "core/components/tween/tween_component.h"
37 #include "core/components_ng/base/frame_node.h"
38 #include "core/components_ng/pattern/overlay/overlay_manager.h"
39 #include "core/pipeline/pipeline_base.h"
40 #include "core/pipeline_ng/pipeline_context.h"
41 
42 namespace OHOS::Rosen {
43 class Window;
44 class WindowOption;
45 class RSUIDirector;
46 } // namespace OHOS::Rosen
47 
48 namespace OHOS::Ace {
49 
50 class SubwindowOhos : public Subwindow {
51     DECLARE_ACE_TYPE(SubwindowOhos, Subwindow)
52 
53 public:
54     explicit SubwindowOhos(int32_t instanceId);
55     ~SubwindowOhos() = default;
56 
57     bool InitContainer() override;
58     void ResizeWindow() override;
59     NG::RectF GetRect() override;
60     void SetRect(const NG::RectF& rect) override;
61     void ShowMenu(const RefPtr<Component>& newComponent) override;
62     void ShowMenuNG(const RefPtr<NG::FrameNode> customNode, const NG::MenuParam& menuParam,
63         const RefPtr<NG::FrameNode>& targetNode, const NG::OffsetF& offset) override;
64     void ShowMenuNG(std::function<void()>&& buildFunc, std::function<void()>&& previewBuildFunc,
65         const NG::MenuParam& menuParam, const RefPtr<NG::FrameNode>& targetNode, const NG::OffsetF& offset) override;
66     bool ShowPreviewNG(bool isStartDraggingFromSubWindow = false) override;
67     void HidePreviewNG() override;
68     void HideMenuNG(const RefPtr<NG::FrameNode>& menu, int32_t targetId) override;
69     void HideMenuNG(bool showPreviewAnimation, bool startDrag) override;
70     void UpdateHideMenuOffsetNG(
71         const NG::OffsetF& offset, float menuScale, bool isRedragStart, int32_t menuWrapperId = -1) override;
72     void ContextMenuSwitchDragPreviewAnimationtNG(const RefPtr<NG::FrameNode>& dragPreviewNode,
73         const NG::OffsetF& offset) override;
74     void UpdatePreviewPosition() override;
75     bool GetMenuPreviewCenter(NG::OffsetF& offset) override;
76     void ShowPopup(const RefPtr<Component>& newComponent, bool disableTouchEvent = true) override;
77     void ShowPopupNG(int32_t targetId, const NG::PopupInfo& popupInfo,
78         const std::function<void(int32_t)>&& onWillDismiss = nullptr, bool interactiveDismiss = true) override;
79     void HidePopupNG(int32_t targetId) override;
80     void GetPopupInfoNG(int32_t targetId, NG::PopupInfo& popupInfo) override;
81     bool CancelPopup(const std::string& id) override;
82     void CloseMenu() override;
83     void ClearMenu() override;
84     void ClearMenuNG(int32_t targetId, bool inWindow, bool showAnimation = false) override;
85     void ClearPopupNG() override;
86     RefPtr<NG::FrameNode> ShowDialogNG(const DialogProperties& dialogProps, std::function<void()>&& buildFunc) override;
87     RefPtr<NG::FrameNode> ShowDialogNGWithNode(const DialogProperties& dialogProps,
88         const RefPtr<NG::UINode>& customNode) override;
89     void CloseDialogNG(const RefPtr<NG::FrameNode>& dialogNode) override;
90     void OpenCustomDialogNG(const DialogProperties& dialogProps, std::function<void(int32_t)>&& callback) override;
91     void CloseCustomDialogNG(int32_t dialogId) override;
92     void CloseCustomDialogNG(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)>&& callback) override;
93     void UpdateCustomDialogNG(const WeakPtr<NG::UINode>& node, const DialogProperties& dialogProps,
94         std::function<void(int32_t)>&& callback) override;
95     void HideSubWindowNG() override;
96     bool GetShown() override
97     {
98         return isShowed_;
99     }
100 
101     void SetHotAreas(const std::vector<Rect>& rects, int32_t nodeId) override;
102     void DeleteHotAreas(int32_t nodeId) override;
103     void ClearToast() override;
104     void ShowToast(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback) override;
105     void CloseToast(const int32_t toastId, std::function<void(int32_t)>&& callback) override;
106     void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons,
107         bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
108         const std::set<std::string>& callbacks) override;
109     void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
110         std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks) override;
111     void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
112         std::function<void(int32_t, int32_t)>&& callback) override;
113     void CloseDialog(int32_t instanceId) override;
114     void OpenCustomDialog(const PromptDialogAttr& dialogAttr, std::function<void(int32_t)>&& callback) override;
115     void CloseCustomDialog(const int32_t dialogId) override;
116     void CloseCustomDialog(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)>&& callback) override;
117     const RefPtr<NG::OverlayManager> GetOverlayManager() override;
118 
119     int32_t GetChildContainerId() const override
120     {
121         return childContainerId_;
122     }
GetDialogWindow() const123     sptr<OHOS::Rosen::Window> GetDialogWindow() const
124     {
125         return dialogWindow_;
126     }
IsToastWindow() const127     bool IsToastWindow() const
128     {
129         return isToastWindow_;
130     }
SetIsToastWindow(bool isToastWindow)131     void SetIsToastWindow(bool isToastWindow)
132     {
133         isToastWindow_ = isToastWindow;
134     }
135 
136     void UpdateAceView(int32_t width, int32_t height, float density, int32_t containerId);
137 
138     // Gets parent window's size and offset
139     Rect GetParentWindowRect() const override;
140     Rect GetUIExtensionHostWindowRect() const override;
141     NG::RectF GetWindowRect() const override;
142     bool IsFreeMultiWindow() const override;
143     void OnFreeMultiWindowSwitch(bool enable) override;
144     int32_t RegisterFreeMultiWindowSwitchCallback(std::function<void(bool)>&& callback) override;
145     void UnRegisterFreeMultiWindowSwitchCallback(int32_t callbackId) override;
146 
147     bool IsFocused() override;
148     void RequestFocus() override;
GetSubWindow() const149     const sptr<OHOS::Rosen::Window>& GetSubWindow() const
150     {
151         return window_;
152     }
153     void ResizeWindowForFoldStatus() override;
154     void ResizeWindowForFoldStatus(int32_t parentContainerId) override;
155     void MarkDirtyDialogSafeArea() override;
156 
157     bool Close() override;
158     bool IsToastSubWindow() override;
159     void DestroyWindow() override;
160 
161 private:
162     RefPtr<StackElement> GetStack();
163     void AddMenu(const RefPtr<Component>& newComponent);
164     void ShowWindow(bool needFocus = true);
165     void HideWindow();
166 
167     // Convert Rect to Rosen::Rect
168     void RectConverter(const Rect& rect, Rosen::Rect& rosenRect);
169 
170     bool CreateEventRunner();
171     void GetToastDialogWindowProperty(
172         int32_t& width, int32_t& height, int32_t& posX, int32_t& posY, float& density) const;
173     bool InitToastDialogWindow(int32_t width, int32_t height, int32_t posX, int32_t posY, bool isToast = false);
174     bool InitToastDialogView(int32_t width, int32_t height, float density);
175     bool InitToastServiceConfig();
176     void ShowToastForAbility(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback);
177     void ShowToastForService(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback);
178     void ShowDialogForAbility(const std::string& title, const std::string& message,
179         const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
180         const std::set<std::string>& callbacks);
181     void ShowDialogForService(const std::string& title, const std::string& message,
182         const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
183         const std::set<std::string>& callbacks);
184     void ShowDialogForAbility(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
185         std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks);
186     void ShowDialogForService(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
187         std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks);
188     void OpenCustomDialogForAbility(const PromptDialogAttr& dialogAttr, std::function<void(int32_t)>&& callback);
189     void OpenCustomDialogForService(const PromptDialogAttr& dialogAttr, std::function<void(int32_t)>&& callback);
190     void ShowActionMenuForAbility(const std::string& title, const std::vector<ButtonInfo>& button,
191         std::function<void(int32_t, int32_t)>&& callback);
192     void ShowActionMenuForService(const std::string& title, const std::vector<ButtonInfo>& button,
193         std::function<void(int32_t, int32_t)>&& callback);
194     RefPtr<PipelineBase> GetChildPipelineContext() const;
195     std::function<void()> GetInitToastDelayTask(const NG::ToastInfo& toastInfo,
196         std::function<void(int32_t)>&& callback);
197     void ContainerModalUnFocus();
198 
199     void HideFilter(bool isInSubWindow);
200     void HidePixelMap(bool startDrag = false, double x = 0, double y = 0, bool showAnimation = true);
201     void HideEventColumn();
202     Rosen::WindowType GetToastRosenType(bool IsSceneBoardEnabled);
203 
204     static int32_t id_;
205     int32_t windowId_ = 0;
206     int32_t parentContainerId_ = -1;
207     int32_t childContainerId_ = -1;
208     std::shared_ptr<OHOS::Rosen::RSUIDirector> rsUiDirector;
209     sptr<OHOS::Rosen::Window> window_ = nullptr;
210     RefPtr<SelectPopupComponent> popup_;
211     std::unordered_map<int32_t, std::vector<Rosen::Rect>> hotAreasMap_;
212 
213     sptr<OHOS::Rosen::Window> dialogWindow_;
214     std::shared_ptr<AppExecFwk::EventRunner> eventLoop_;
215     std::shared_ptr<AppExecFwk::EventHandler> handler_;
216     int32_t targetId_ = -1;
217     bool isToastWindow_ = false;
218     int32_t popupTargetId_ = -1;
219     bool haveDialog_ = false;
220     bool isShowed_ = false;
221     sptr<OHOS::Rosen::Window> parentWindow_ = nullptr;
222     int32_t callbackId_ = 0;
223     sptr<OHOS::Rosen::ISwitchFreeMultiWindowListener> freeMultiWindowListener_ = nullptr;
224     std::unordered_map<int32_t, std::function<void(bool)>> freeMultiWindowSwitchCallbackMap_;
225     NG::RectF windowRect_;
226 };
227 
228 } // namespace OHOS::Ace
229 
230 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_SUBWINDOW_OHOS_H
231