1/*
2 * Copyright (c) 2022-2024 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 OHOS_ROSEN_WINDOW_IMPL_H
17#define OHOS_ROSEN_WINDOW_IMPL_H
18
19#include <map>
20
21#include <ui_content.h>
22#include "context.h"
23#include "ui/rs_surface_node.h"
24#include "vsync_station.h"
25#include "window.h"
26#include "window_property.h"
27
28namespace OHOS::AbilityRuntime {
29class Context;
30}
31
32namespace OHOS {
33namespace Rosen {
34union ColorParam {
35#if BIG_ENDIANNESS
36    struct {
37        uint8_t alpha;
38        uint8_t red;
39        uint8_t green;
40        uint8_t blue;
41    } argb;
42#else
43    struct {
44        uint8_t blue;
45        uint8_t green;
46        uint8_t red;
47        uint8_t alpha;
48    } argb;
49#endif
50    uint32_t value;
51};
52
53class WindowImpl : public Window {
54public:
55    explicit WindowImpl(const sptr<WindowOption>& option);
56    ~WindowImpl();
57    static sptr<Window> Find(const std::string& id);
58    static sptr<Window> GetTopWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context = nullptr);
59    static sptr<Window> GetTopWindowWithId(uint32_t mainWinId);
60    static std::vector<sptr<Window>> GetSubWindow(uint32_t parantId);
61    static void UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration);
62    virtual std::shared_ptr<RSSurfaceNode> GetSurfaceNode() const override;
63    virtual Rect GetRect() const override;
64    virtual Rect GetRequestRect() const override;
65    virtual WindowType GetType() const override;
66    virtual WindowMode GetMode() const override;
67    virtual float GetAlpha() const override;
68    virtual WindowState GetWindowState() const override;
69    virtual WMError SetFocusable(bool isFocusable) override;
70    virtual bool GetFocusable() const override;
71    virtual WMError SetTouchable(bool isTouchable) override;
72    virtual bool GetTouchable() const override;
73    virtual const std::string& GetWindowName() const override;
74    virtual uint32_t GetWindowId() const override;
75    uint64_t GetDisplayId() const override;
76    virtual uint32_t GetWindowFlags() const override;
77    uint32_t GetRequestModeSupportInfo() const override;
78    bool IsMainHandlerAvailable() const override;
79    virtual SystemBarProperty GetSystemBarPropertyByType(WindowType type) const override;
80    virtual bool IsFullScreen() const override;
81    virtual bool IsLayoutFullScreen() const override;
82    virtual WMError SetWindowType(WindowType type) override;
83    virtual WMError SetWindowMode(WindowMode mode) override;
84    virtual WMError SetAlpha(float alpha) override;
85    virtual WMError SetTransform(const Transform& trans) override;
86    virtual WMError AddWindowFlag(WindowFlag flag) override;
87    virtual WMError RemoveWindowFlag(WindowFlag flag) override;
88    virtual WMError SetWindowFlags(uint32_t flags) override;
89    virtual WMError SetSystemBarProperty(WindowType type, const SystemBarProperty& property) override;
90    virtual WMError SetSpecificBarProperty(WindowType type, const SystemBarProperty& property) override;
91    virtual WMError SetSystemBarProperties(const std::map<WindowType, SystemBarProperty>& properties,
92        const std::map<WindowType, SystemBarPropertyFlag>& propertyFlags) override;
93    virtual WMError GetSystemBarProperties(std::map<WindowType, SystemBarProperty>& properties) override;
94    virtual WMError SetLayoutFullScreen(bool status) override;
95    virtual WMError SetFullScreen(bool status) override;
96    virtual const Transform& GetTransform() const override;
97    virtual WMError UpdateSurfaceNodeAfterCustomAnimation(bool isAdd) override;
98    virtual WMError GetAvoidAreaByType(AvoidAreaType type, AvoidArea& avoidArea) override;
99
100    WMError Create(uint32_t parentId,
101        const std::shared_ptr<AbilityRuntime::Context>& context = nullptr);
102    virtual WMError Destroy() override;
103    virtual WMError Show(uint32_t reason = 0, bool withAnimation = false, bool withFocus = true) override;
104    virtual WMError Hide(uint32_t reason = 0, bool withAnimation = false, bool isFromInnerkits = true) override;
105    virtual WMError MoveTo(int32_t x, int32_t y, bool isMoveToGlobal = false) override;
106    virtual WMError Resize(uint32_t width, uint32_t height) override;
107    virtual WMError SetWindowGravity(WindowGravity gravity, uint32_t percent) override;
108    virtual WMError SetKeepScreenOn(bool keepScreenOn) override;
109    virtual bool IsKeepScreenOn() const override;
110    virtual WMError SetTurnScreenOn(bool turnScreenOn) override;
111    virtual bool IsTurnScreenOn() const override;
112    virtual WMError SetBackgroundColor(const std::string& color) override;
113    virtual WMError SetTransparent(bool isTransparent) override;
114    virtual bool IsTransparent() const override;
115    virtual WMError SetBrightness(float brightness) override;
116    virtual float GetBrightness() const override;
117    virtual WMError SetCallingWindow(uint32_t windowId) override;
118    virtual WMError SetPrivacyMode(bool isPrivacyMode) override;
119    virtual bool IsPrivacyMode() const override;
120    virtual void SetSystemPrivacyMode(bool isSystemPrivacyMode) override;
121    virtual WMError DisableAppWindowDecor() override;
122    virtual WMError BindDialogTarget(sptr<IRemoteObject> targetToken) override;
123    virtual WMError SetDialogBackGestureEnabled(bool isEnabled) override;
124    virtual WMError SetSnapshotSkip(bool isSkip) override;
125
126    // window effect
127    virtual WMError SetCornerRadius(float cornerRadius) override;
128    virtual WMError SetShadowRadius(float radius) override;
129    virtual WMError SetShadowColor(std::string color) override;
130    virtual WMError SetShadowOffsetX(float offsetX) override;
131    virtual WMError SetShadowOffsetY(float offsetY) override;
132    virtual WMError SetBlur(float radius) override;
133    virtual WMError SetBackdropBlur(float radius) override;
134    virtual WMError SetBackdropBlurStyle(WindowBlurStyle blurStyle) override;
135
136    virtual WMError Maximize() override;
137    virtual WMError Minimize() override;
138    virtual WMError Recover() override;
139    virtual WMError Close() override;
140    virtual void StartMove() override;
141
142    virtual WMError RequestFocus() const override;
143    virtual bool IsFocused() const override;
144    virtual void SetInputEventConsumer(const std::shared_ptr<IInputEventConsumer>& inputEventConsumer) override;
145
146    virtual WMError RegisterLifeCycleListener(const sptr<IWindowLifeCycle>& listener) override;
147    virtual WMError RegisterWindowChangeListener(const sptr<IWindowChangeListener>& listener) override;
148    virtual WMError UnregisterLifeCycleListener(const sptr<IWindowLifeCycle>& listener) override;
149    virtual WMError UnregisterWindowChangeListener(const sptr<IWindowChangeListener>& listener) override;
150    virtual WMError RegisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener>& listener) override;
151    virtual WMError UnregisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener>& listener) override;
152    virtual WMError RegisterDragListener(const sptr<IWindowDragListener>& listener) override;
153    virtual WMError UnregisterDragListener(const sptr<IWindowDragListener>& listener) override;
154    virtual WMError RegisterDisplayMoveListener(sptr<IDisplayMoveListener>& listener) override;
155    virtual WMError UnregisterDisplayMoveListener(sptr<IDisplayMoveListener>& listener) override;
156    virtual void RegisterWindowDestroyedListener(const NotifyNativeWinDestroyFunc& func) override;
157    virtual WMError RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener) override;
158    virtual WMError UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener) override;
159    virtual WMError RegisterTouchOutsideListener(const sptr<ITouchOutsideListener>& listener) override;
160    virtual WMError UnregisterTouchOutsideListener(const sptr<ITouchOutsideListener>& listener) override;
161    virtual WMError RegisterAnimationTransitionController(
162        const sptr<IAnimationTransitionController>& listener) override;
163    virtual WMError RegisterScreenshotListener(const sptr<IScreenshotListener>& listener) override;
164    virtual WMError UnregisterScreenshotListener(const sptr<IScreenshotListener>& listener) override;
165    virtual WMError RegisterDialogTargetTouchListener(const sptr<IDialogTargetTouchListener>& listener) override;
166    virtual WMError UnregisterDialogTargetTouchListener(const sptr<IDialogTargetTouchListener>& listener) override;
167    virtual void RegisterDialogDeathRecipientListener(const sptr<IDialogDeathRecipientListener>& listener) override;
168    virtual void UnregisterDialogDeathRecipientListener(const sptr<IDialogDeathRecipientListener>& listener) override;
169    virtual void SetAceAbilityHandler(const sptr<IAceAbilityHandler>& handler) override;
170    virtual void SetRequestModeSupportInfo(uint32_t modeSupportInfo) override;
171    virtual void ConsumeKeyEvent(const std::shared_ptr<MMI::KeyEvent>& inputEvent) override;
172    virtual void ConsumePointerEvent(const std::shared_ptr<MMI::PointerEvent>& inputEvent) override;
173    virtual void RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback) override;
174    virtual int64_t GetVSyncPeriod() override;
175    virtual void UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration) override;
176    void UpdateAvoidArea(const sptr<AvoidArea>& avoidArea, AvoidAreaType type) override;
177    void NotifyTouchDialogTarget(int32_t posX = 0, int32_t posY = 0) override;
178
179    virtual WMError NapiSetUIContent(const std::string& contentInfo, napi_env env, napi_value storage,
180        BackupAndRestoreType type, sptr<IRemoteObject> token, AppExecFwk::Ability* ability) override;
181    virtual std::string GetContentInfo(BackupAndRestoreType type = BackupAndRestoreType::CONTINUATION) override;
182    virtual const std::shared_ptr<AbilityRuntime::Context> GetContext() const override;
183    virtual Ace::UIContent* GetUIContent() const override;
184    virtual void OnNewWant(const AAFwk::Want& want) override;
185    virtual void SetRequestedOrientation(Orientation) override;
186    virtual Orientation GetRequestedOrientation() override;
187    virtual void SetNeedRemoveWindowInputChannel(bool needRemoveWindowInputChannel) override;
188    virtual WMError SetTouchHotAreas(const std::vector<Rect>& rects) override;
189    virtual void GetRequestedTouchHotAreas(std::vector<Rect>& rects) const override;
190    virtual WMError SetAPPWindowLabel(const std::string& label) override;
191    virtual WMError SetAPPWindowIcon(const std::shared_ptr<Media::PixelMap>& icon) override;
192
193    // colorspace, gamut
194    virtual bool IsSupportWideGamut() override;
195    virtual void SetColorSpace(ColorSpace colorSpace) override;
196    virtual ColorSpace GetColorSpace() override;
197
198    virtual void DumpInfo(const std::vector<std::string>& params, std::vector<std::string>& info) override;
199    virtual std::shared_ptr<Media::PixelMap> Snapshot() override;
200    virtual WMError NotifyMemoryLevel(int32_t level) override;
201    virtual bool IsAllowHaveSystemSubWindow() override;
202    WMError RaiseToAppTop() override;
203    virtual WMError SetAspectRatio(float ratio) override;
204    virtual WMError ResetAspectRatio() override;
205    virtual KeyboardAnimationConfig GetKeyboardAnimationConfig() override;
206
207    virtual void SetNeedDefaultAnimation(bool needDefaultAnimation) override;
208
209    virtual void SetViewportConfig(const Ace::ViewportConfig& config) override;
210    virtual void UpdateViewportConfig() override;
211    virtual void SetOrientation(Orientation orientation) override;
212    virtual void SetSize(int32_t width, int32_t height) override;
213    virtual void SetDensity(float density) override;
214
215    virtual void CreateSurfaceNode(const std::string name, const SendRenderDataCallback& callback) override;
216    virtual void SetContentInfoCallback(const ContentInfoCallback& callback) override;
217    virtual WMError SetResizeByDragEnabled(bool dragEnabled) override;
218    virtual WMError SetRaiseByClickEnabled(bool raiseEnabled) override;
219    virtual WMError RaiseAboveTarget(int32_t subWindowId) override;
220    virtual WMError HideNonSystemFloatingWindows(bool shouldHide) override;
221    virtual WMError RegisterWindowVisibilityChangeListener(const WindowVisibilityListenerSptr& listener) override;
222    virtual WMError UnregisterWindowVisibilityChangeListener(const WindowVisibilityListenerSptr& listener) override;
223    virtual WMError RegisterSystemBarEnableListener(const sptr<IWindowSystemBarEnableListener>& listener) override;
224    virtual WMError UnRegisterSystemBarEnableListener(const sptr<IWindowSystemBarEnableListener>& listener) override;
225    virtual WMError RegisterIgnoreViewSafeAreaListener(const sptr<IIgnoreViewSafeAreaListener>& listener) override;
226    virtual WMError UnRegisterIgnoreViewSafeAreaListener(const sptr<IIgnoreViewSafeAreaListener>& listener) override;
227    virtual WmErrorCode KeepKeyboardOnFocus(bool keepKeyboardFlag) override;
228    virtual WMError SetSingleFrameComposerEnabled(bool enable) override;
229    virtual WMError SetLandscapeMultiWindow(bool isLandscapeMultiWindow) override;
230    virtual void SetUiDvsyncSwitch(bool dvsyncSwitch) override;
231    virtual WMError UpdateSystemBarProperty(bool status);
232    virtual WMError SetImmersiveModeEnabledState(bool enable) override;
233    virtual bool GetImmersiveModeEnabledState() const override;
234
235private:
236    static sptr<Window> FindWindowById(uint32_t windowId);
237    template<typename T1, typename T2, typename Ret>
238    using EnableIfSame = typename std::enable_if<std::is_same_v<T1, T2>, Ret>::type;
239    template<typename T> WMError RegisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener);
240    template<typename T> WMError UnregisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener);
241    template<typename T>
242    inline EnableIfSame<T, IWindowSystemBarEnableListener, std::vector<sptr<IWindowSystemBarEnableListener>>>
243        GetListeners()
244    {
245        std::vector<sptr<IWindowSystemBarEnableListener>> systemBarEnableListeners;
246        {
247            std::lock_guard<std::mutex> lock(globalMutex_);
248            for (auto& listener : systemBarEnableListeners_[GetWindowId()]) {
249                systemBarEnableListeners.push_back(listener);
250            }
251        }
252        return systemBarEnableListeners;
253    }
254    template<typename T>
255    inline EnableIfSame<T, IIgnoreViewSafeAreaListener, std::vector<sptr<IIgnoreViewSafeAreaListener>>> GetListeners()
256    {
257        std::vector<sptr<IIgnoreViewSafeAreaListener>> ignoreSafeAreaListeners;
258        {
259            std::lock_guard<std::mutex> lock(globalMutex_);
260            for (auto& listener : ignoreSafeAreaListeners_[GetWindowId()]) {
261                ignoreSafeAreaListeners.push_back(listener);
262            }
263        }
264        return ignoreSafeAreaListeners;
265    }
266    template<typename T>
267    inline EnableIfSame<T, IAvoidAreaChangedListener, std::vector<sptr<IAvoidAreaChangedListener>>> GetListeners()
268    {
269        std::vector<sptr<IAvoidAreaChangedListener>> avoidAreaChangeListeners;
270        {
271            std::lock_guard<std::mutex> lock(globalMutex_);
272            for (auto& listener : avoidAreaChangeListeners_[GetWindowId()]) {
273                avoidAreaChangeListeners.push_back(listener);
274            }
275        }
276        return avoidAreaChangeListeners;
277    }
278    void ClearListenersById(uint32_t winId);
279    void NotifySystemBarChange(WindowType type, const SystemBarProperty& property);
280    void NotifySetIgnoreSafeArea(bool value);
281    void NotifyAvoidAreaChange(const sptr<AvoidArea>& avoidArea, AvoidAreaType type);
282    static std::mutex globalMutex_;
283    static std::map<std::string, std::pair<uint32_t, sptr<Window>>> windowMap_;
284    static std::map<uint32_t, std::vector<sptr<WindowImpl>>> subWindowMap_;
285    static std::map<uint32_t, std::vector<sptr<IWindowSystemBarEnableListener>>> systemBarEnableListeners_;
286    static std::map<uint32_t, std::vector<sptr<IIgnoreViewSafeAreaListener>>> ignoreSafeAreaListeners_;
287    static std::map<uint32_t, std::vector<sptr<IAvoidAreaChangedListener>>> avoidAreaChangeListeners_;
288    WindowState state_ { WindowState::STATE_INITIAL };
289    std::shared_ptr<RSSurfaceNode> surfaceNode_;
290    std::shared_ptr<VsyncStation> vsyncStation_ = nullptr;
291    std::shared_ptr<AbilityRuntime::Context> context_;
292    std::string name_;
293    std::unique_ptr<Ace::UIContent> uiContent_;
294    KeyboardAnimationConfig keyboardAnimationConfig_;
295    bool needRemoveWindowInputChannel_ = false;
296    ContentInfoCallback contentInfoCallback_;
297    Transform transform_;
298    int32_t width_ = 0;
299    int32_t height_ = 0;
300    int32_t orientation_ = 0;
301    float density_ = 1.0f;
302    bool isIgnoreSafeArea_ = false;
303    uint32_t windowId_ = 0;
304    WindowMode windowMode_ = WindowMode::WINDOW_MODE_FULLSCREEN;
305    sptr<WindowProperty> property_;
306    mutable std::mutex mutex_;
307    std::unordered_map<WindowType, SystemBarProperty> sysBarPropMap_ {
308        { WindowType::WINDOW_TYPE_STATUS_BAR,           SystemBarProperty(true, 0x00FFFFFF, 0xFF000000) },
309        { WindowType::WINDOW_TYPE_NAVIGATION_BAR,       SystemBarProperty(true, 0x00FFFFFF, 0xFF000000) },
310        { WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR, SystemBarProperty(true, 0x00FFFFFF, 0xFF000000) },
311    };
312    std::unordered_map<AvoidAreaType, sptr<AvoidArea>> avoidAreaMap_ {
313        { AvoidAreaType::TYPE_SYSTEM,               new AvoidArea() },
314        { AvoidAreaType::TYPE_CUTOUT,               new AvoidArea() },
315        { AvoidAreaType::TYPE_SYSTEM_GESTURE,       new AvoidArea() },
316        { AvoidAreaType::TYPE_KEYBOARD,             new AvoidArea() },
317        { AvoidAreaType::TYPE_NAVIGATION_INDICATOR, new AvoidArea() },
318    };
319};
320} // namespace Rosen
321} // namespace OHOS
322#endif // OHOS_ROSEN_WINDOW_IMPL_H
323