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