• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ace_container.h"
27 #include "adapter/ohos/entrance/platform_event_callback.h"
28 #include "base/resource/asset_manager.h"
29 #include "base/subwindow/subwindow.h"
30 #include "base/subwindow/subwindow_manager.h"
31 #include "base/thread/task_executor.h"
32 #include "core/common/ace_view.h"
33 #include "core/common/js_message_dispatcher.h"
34 #include "core/components/dialog/dialog_properties.h"
35 #include "core/components/select_popup/select_popup_component.h"
36 #include "core/components/stack/stack_element.h"
37 #include "core/components/tween/tween_component.h"
38 #include "core/components_ng/base/frame_node.h"
39 #include "core/components_ng/pattern/overlay/overlay_manager.h"
40 #include "core/pipeline/pipeline_base.h"
41 #include "core/pipeline_ng/pipeline_context.h"
42 
43 namespace OHOS::Rosen {
44 class Window;
45 class WindowOption;
46 class RSUIDirector;
47 } // namespace OHOS::Rosen
48 
49 namespace OHOS::Ace {
50 
51 class SubwindowOhos : public Subwindow {
52     DECLARE_ACE_TYPE(SubwindowOhos, Subwindow)
53 
54 public:
55     explicit SubwindowOhos(int32_t instanceId);
56     ~SubwindowOhos() = default;
57 
58     void InitContainer() override;
59     void ResizeWindow() override;
60     void ResizeWindowForMenu() override;
61     NG::RectF GetRect() override;
62     void SetRect(const NG::RectF& rect) override;
63     void ShowMenu(const RefPtr<Component>& newComponent) override;
64     void ShowMenuNG(const RefPtr<NG::FrameNode> customNode, const NG::MenuParam& menuParam,
65         const RefPtr<NG::FrameNode>& targetNode, const NG::OffsetF& offset) override;
66     void ShowMenuNG(std::function<void()>&& buildFunc, std::function<void()>&& previewBuildFunc,
67         const NG::MenuParam& menuParam, const RefPtr<NG::FrameNode>& targetNode, const NG::OffsetF& offset) override;
68     bool ShowPreviewNG(bool isStartDraggingFromSubWindow = false) override;
SetWindowTouchable(bool touchable)69     void SetWindowTouchable(bool touchable) override
70     {
71         CHECK_NULL_VOID(window_);
72         window_->SetTouchable(touchable);
73     }
74     void HidePreviewNG() override;
75     void HideMenuNG(const RefPtr<NG::FrameNode>& menu, int32_t targetId) override;
76     void HideMenuNG(bool showPreviewAnimation, bool startDrag) override;
77     void UpdateHideMenuOffsetNG(
78         const NG::OffsetF& offset, float menuScale, bool isRedragStart, int32_t menuWrapperId = -1) override;
79     void ContextMenuSwitchDragPreviewAnimationtNG(const RefPtr<NG::FrameNode>& dragPreviewNode,
80         const NG::OffsetF& offset) override;
81     void UpdatePreviewPosition() override;
82     bool GetMenuPreviewCenter(NG::OffsetF& offset) override;
83     void ShowPopup(const RefPtr<Component>& newComponent, bool disableTouchEvent = true) override;
84     void ShowPopupNG(int32_t targetId, const NG::PopupInfo& popupInfo,
85         const std::function<void(int32_t)>&& onWillDismiss = nullptr, bool interactiveDismiss = true) override;
86     void ShowTipsNG(int32_t targetId, const NG::PopupInfo& popupInfo, int32_t appearingTime,
87         int32_t appearingTimeWithContinuousOperation) override;
88     void HideTipsNG(int32_t targetId, int32_t disappearingTime) override;
89     void HidePopupNG(int32_t targetId) override;
90     void GetPopupInfoNG(int32_t targetId, NG::PopupInfo& popupInfo) override;
91     bool CancelPopup(const std::string& id) override;
92     void CloseMenu() override;
93     void ClearMenu() override;
94     void ClearMenuNG(int32_t targetId, bool inWindow, bool showAnimation = false) override;
95     void ClearPopupNG() override;
96     void ClearPopupNG(bool isForceClear) override;
97     RefPtr<NG::FrameNode> ShowDialogNG(const DialogProperties& dialogProps, std::function<void()>&& buildFunc) override;
98     RefPtr<NG::FrameNode> ShowDialogNGWithNode(const DialogProperties& dialogProps,
99         const RefPtr<NG::UINode>& customNode) override;
100     void CloseDialogNG(const RefPtr<NG::FrameNode>& dialogNode) override;
101     void OpenCustomDialogNG(const DialogProperties& dialogProps, std::function<void(int32_t)>&& callback) override;
102     void CloseCustomDialogNG(int32_t dialogId) override;
103     void CloseCustomDialogNG(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)>&& callback) override;
104     void UpdateCustomDialogNG(const WeakPtr<NG::UINode>& node, const DialogProperties& dialogProps,
105         std::function<void(int32_t)>&& callback) override;
106     void HideSubWindowNG() override;
GetShown()107     bool GetShown() override
108     {
109         return isShowed_;
110     }
111 
112     void SetHotAreas(const std::vector<Rect>& rects, int32_t nodeId) override;
113     void DeleteHotAreas(int32_t nodeId) override;
114     void ClearToast() override;
115     void ShowToast(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback) override;
116     void CloseToast(const int32_t toastId, std::function<void(int32_t)>&& callback) override;
117     void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons,
118         bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
119         const std::set<std::string>& callbacks) override;
120     void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
121         std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks) override;
122     void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
123         std::function<void(int32_t, int32_t)>&& callback) override;
124     void CloseDialog(int32_t instanceId) override;
125     void OpenCustomDialog(const PromptDialogAttr& dialogAttr, std::function<void(int32_t)>&& callback) override;
126     void CloseCustomDialog(const int32_t dialogId) override;
127     void CloseCustomDialog(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)>&& callback) override;
128     const RefPtr<NG::OverlayManager> GetOverlayManager() override;
129 
GetChildContainerId()130     int32_t GetChildContainerId() const override
131     {
132         return childContainerId_;
133     }
GetDialogWindow()134     sptr<OHOS::Rosen::Window> GetDialogWindow() const
135     {
136         return dialogWindow_;
137     }
IsToastWindow()138     bool IsToastWindow() const
139     {
140         return isToastWindow_;
141     }
SetIsToastWindow(bool isToastWindow)142     void SetIsToastWindow(bool isToastWindow)
143     {
144         isToastWindow_ = isToastWindow;
145     }
146 
147     void UpdateAceView(int32_t width, int32_t height, float density, int32_t containerId);
148 
149     // Gets parent window's size and offset
150     Rect GetParentWindowRect() const override;
151     Rect GetUIExtensionHostWindowRect() const override;
152     Rect GetFoldExpandAvailableRect() const override;
153     NG::RectF GetWindowRect() const override;
154     bool CheckHostWindowStatus() const override;
155     bool IsFreeMultiWindow() const override;
156     void OnFreeMultiWindowSwitch(bool enable) override;
157     int32_t RegisterFreeMultiWindowSwitchCallback(std::function<void(bool)>&& callback) override;
158     void UnRegisterFreeMultiWindowSwitchCallback(int32_t callbackId) override;
159 
NeedAvoidKeyboard()160     bool NeedAvoidKeyboard() override
161     {
162         return needAvoidKeyboard_;
163     }
164 
165     bool IsFocused() override;
166     void RequestFocus() override;
GetSubWindow()167     const sptr<OHOS::Rosen::Window>& GetSubWindow() const
168     {
169         return window_;
170     }
171     void ResizeWindowForFoldStatus() override;
172     void ResizeWindowForFoldStatus(int32_t parentContainerId) override;
173     void MarkDirtyDialogSafeArea() override;
174 
175     bool Close() override;
176     bool IsToastSubWindow() override;
177     void DestroyWindow() override;
178 
179     void ResizeDialogSubwindow() override;
180     uint64_t GetDisplayId() override;
181     bool IsSameDisplayWithParentWindow(bool useInitializedId = false) override;
182 
183     void InitializeSafeArea();
184     bool ShowSelectOverlay(const RefPtr<NG::FrameNode>& overlayNode) override;
185 
186     void ShowBindSheetNG(bool isShow, std::function<void(const std::string&)>&& callback,
187         std::function<RefPtr<NG::UINode>()>&& buildNodeFunc, std::function<RefPtr<NG::UINode>()>&& buildtitleNodeFunc,
188         NG::SheetStyle& sheetStyle, std::function<void()>&& onAppear, std::function<void()>&& onDisappear,
189         std::function<void()>&& shouldDismiss, std::function<void(const int32_t)>&& onWillDismiss,
190         std::function<void()>&& onWillAppear, std::function<void()>&& onWillDisappear,
191         std::function<void(const float)>&& onHeightDidChange,
192         std::function<void(const float)>&& onDetentsDidChange,
193         std::function<void(const float)>&& onWidthDidChange,
194         std::function<void(const float)>&& onTypeDidChange,
195         std::function<void()>&& sheetSpringBack, const RefPtr<NG::FrameNode>& targetNode) override;
196 
GetAttachState()197     MenuWindowState GetAttachState() override
198     {
199         return attachState_;
200     }
201 
GetDetachState()202     MenuWindowState GetDetachState() override
203     {
204         return detachState_;
205     }
206 
SetAttachState(MenuWindowState t)207     void SetAttachState(MenuWindowState t)
208     {
209         attachState_ = t;
210     }
211 
SetDetachState(MenuWindowState t)212     void SetDetachState(MenuWindowState t)
213     {
214         detachState_ = t;
215     }
216 
217 private:
218     RefPtr<StackElement> GetStack();
219     void AddMenu(const RefPtr<Component>& newComponent);
220     void ShowWindow(bool needFocus = true);
221     void HideWindow();
222 
223     // Convert Rect to Rosen::Rect
224     void RectConverter(const Rect& rect, Rosen::Rect& rosenRect);
225 
226     bool CreateEventRunner();
227     void GetToastDialogWindowProperty(
228         int32_t& width, int32_t& height, int32_t& posX, int32_t& posY, float& density) const;
229     bool InitToastDialogWindow(int32_t width, int32_t height, int32_t posX, int32_t posY, bool isToast = false);
230     bool InitToastDialogView(int32_t width, int32_t height, float density);
231     bool InitToastServiceConfig();
232     void ShowToastForAbility(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback);
233     void ShowToastForService(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback);
234     void ShowDialogForAbility(const std::string& title, const std::string& message,
235         const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
236         const std::set<std::string>& callbacks);
237     void ShowDialogForService(const std::string& title, const std::string& message,
238         const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
239         const std::set<std::string>& callbacks);
240     void ShowDialogForAbility(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
241         std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks);
242     void ShowDialogForService(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
243         std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks);
244     void OpenCustomDialogForAbility(const PromptDialogAttr& dialogAttr, std::function<void(int32_t)>&& callback);
245     void OpenCustomDialogForService(const PromptDialogAttr& dialogAttr, std::function<void(int32_t)>&& callback);
246     void ShowActionMenuForAbility(const std::string& title, const std::vector<ButtonInfo>& button,
247         std::function<void(int32_t, int32_t)>&& callback);
248     void ShowActionMenuForService(const std::string& title, const std::vector<ButtonInfo>& button,
249         std::function<void(int32_t, int32_t)>&& callback);
250     RefPtr<PipelineBase> GetChildPipelineContext() const;
251     std::function<void()> GetInitToastDelayTask(const NG::ToastInfo& toastInfo,
252         std::function<void(int32_t)>&& callback);
253     void ContainerModalUnFocus();
254 
255     void HideFilter(bool isInSubWindow);
256     void HidePixelMap(bool startDrag = false, double x = 0, double y = 0, bool showAnimation = true);
257     void HideEventColumn();
258     Rosen::WindowType GetToastRosenType(bool IsSceneBoardEnabled);
259 
260     void SetToastWindowOption(RefPtr<Platform::AceContainer>& parentContainer,
261         OHOS::sptr<OHOS::Rosen::WindowOption>& windowOption,
262         const Rosen::WindowType& toastWindowType, uint32_t mainWindowId);
263 
264     static int32_t id_;
265     int32_t windowId_ = 0;
266     int32_t parentContainerId_ = -1;
267     int32_t childContainerId_ = -1;
268     uint64_t defaultDisplayId_ = 0;
269     std::shared_ptr<OHOS::Rosen::RSUIDirector> rsUiDirector;
270     sptr<OHOS::Rosen::Window> window_ = nullptr;
271     RefPtr<SelectPopupComponent> popup_;
272     std::unordered_map<int32_t, std::vector<Rosen::Rect>> hotAreasMap_;
273 
274     sptr<OHOS::Rosen::Window> dialogWindow_;
275     std::shared_ptr<AppExecFwk::EventRunner> eventLoop_;
276     std::shared_ptr<AppExecFwk::EventHandler> handler_;
277     bool isToastWindow_ = false;
278     int32_t popupTargetId_ = -1;
279     bool haveDialog_ = false;
280     bool isShowed_ = false;
281     bool isClosing_ = false;
282     bool needAvoidKeyboard_ = false;
283     sptr<OHOS::Rosen::Window> parentWindow_ = nullptr;
284     int32_t callbackId_ = 0;
285     sptr<OHOS::Rosen::ISwitchFreeMultiWindowListener> freeMultiWindowListener_ = nullptr;
286     std::unordered_map<int32_t, std::function<void(bool)>> freeMultiWindowSwitchCallbackMap_;
287     NG::RectF windowRect_;
288     std::mutex eventRunnerMutex_;
289     MenuWindowState attachState_ = MenuWindowState::DEFAULT;
290     MenuWindowState detachState_ = MenuWindowState::DEFAULT;
291 };
292 
293 class MenuWindowSceneListener : public OHOS::Rosen::IWindowAttachStateChangeListner {
294 public:
MenuWindowSceneListener(WeakPtr<SubwindowOhos> sub)295     explicit MenuWindowSceneListener(WeakPtr<SubwindowOhos> sub) : sub_(sub) {}
296     ~MenuWindowSceneListener() = default;
AfterAttached()297     void AfterAttached()
298     {
299         TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "receive callback: AfterAttachToFrameNode");
300         auto sub = sub_.Upgrade();
301         CHECK_NULL_VOID(sub);
302         sub->SetAttachState(MenuWindowState::ATTACHED);
303     }
304 
AfterDetached()305     void AfterDetached()
306     {
307         TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "receive callback: AfterDetachToFrameNode");
308         auto sub = sub_.Upgrade();
309         CHECK_NULL_VOID(sub);
310         sub->SetDetachState(MenuWindowState::DETACHED);
311     }
312 private:
313     WeakPtr<SubwindowOhos> sub_;
314 };
315 
316 } // namespace OHOS::Ace
317 
318 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_SUBWINDOW_OHOS_H
319