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