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