• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 OHOS_WM_INCLUDE_WM_HELPER_H
17 #define OHOS_WM_INCLUDE_WM_HELPER_H
18 
19 #include <wm_common.h>
20 #include <wm_common_inner.h>
21 
22 namespace OHOS {
23 namespace Rosen {
24 class WindowHelper {
25 public:
IsMainWindow(WindowType type)26     static inline bool IsMainWindow(WindowType type)
27     {
28         return (type >= WindowType::APP_MAIN_WINDOW_BASE && type < WindowType::APP_MAIN_WINDOW_END);
29     }
30 
IsSubWindow(WindowType type)31     static inline bool IsSubWindow(WindowType type)
32     {
33         return (type >= WindowType::APP_SUB_WINDOW_BASE && type < WindowType::APP_SUB_WINDOW_END);
34     }
35 
IsAppWindow(WindowType type)36     static inline bool IsAppWindow(WindowType type)
37     {
38         return (IsMainWindow(type) || IsSubWindow(type));
39     }
40 
IsBelowSystemWindow(WindowType type)41     static inline bool IsBelowSystemWindow(WindowType type)
42     {
43         return (type >= WindowType::BELOW_APP_SYSTEM_WINDOW_BASE && type < WindowType::BELOW_APP_SYSTEM_WINDOW_END);
44     }
45 
IsAboveSystemWindow(WindowType type)46     static inline bool IsAboveSystemWindow(WindowType type)
47     {
48         return (type >= WindowType::ABOVE_APP_SYSTEM_WINDOW_BASE && type < WindowType::ABOVE_APP_SYSTEM_WINDOW_END);
49     }
50 
IsSystemWindow(WindowType type)51     static inline bool IsSystemWindow(WindowType type)
52     {
53         return (IsBelowSystemWindow(type) || IsAboveSystemWindow(type));
54     }
55 
IsMainFloatingWindow(WindowType type,WindowMode mode)56     static inline bool IsMainFloatingWindow(WindowType type, WindowMode mode)
57     {
58         return ((IsMainWindow(type)) && (mode == WindowMode::WINDOW_MODE_FLOATING));
59     }
60 
IsAvoidAreaWindow(WindowType type)61     static inline bool IsAvoidAreaWindow(WindowType type)
62     {
63         return (type == WindowType::WINDOW_TYPE_STATUS_BAR || type == WindowType::WINDOW_TYPE_NAVIGATION_BAR);
64     }
65 
IsSplitWindowMode(WindowMode mode)66     static inline bool IsSplitWindowMode(WindowMode mode)
67     {
68         return mode == WindowMode::WINDOW_MODE_SPLIT_PRIMARY || mode == WindowMode::WINDOW_MODE_SPLIT_SECONDARY;
69     }
70 
IsValidWindowMode(WindowMode mode)71     static inline bool IsValidWindowMode(WindowMode mode)
72     {
73         return mode == WindowMode::WINDOW_MODE_FULLSCREEN || mode == WindowMode::WINDOW_MODE_SPLIT_PRIMARY ||
74             mode == WindowMode::WINDOW_MODE_SPLIT_SECONDARY || mode == WindowMode::WINDOW_MODE_FLOATING ||
75             mode == WindowMode::WINDOW_MODE_PIP;
76     }
77 
IsValidWindowBlurLevel(WindowBlurLevel level)78     static inline bool IsValidWindowBlurLevel(WindowBlurLevel level)
79     {
80         return (level >= WindowBlurLevel::WINDOW_BLUR_OFF && level <= WindowBlurLevel::WINDOW_BLUR_HIGH);
81     }
82 
IsEmptyRect(const Rect & r)83     static inline bool IsEmptyRect(const Rect& r)
84     {
85         return (r.posX_ == 0 && r.posY_ == 0 && r.width_ == 0 && r.height_ == 0);
86     }
87 
GetFixedWindowRectByLimitSize(const Rect & oriDstRect,const Rect & lastRect,bool isVertical,float virtualPixelRatio)88     static Rect GetFixedWindowRectByLimitSize(const Rect& oriDstRect, const Rect& lastRect, bool isVertical,
89         float virtualPixelRatio)
90     {
91         uint32_t minVerticalFloatingW = static_cast<uint32_t>(MIN_VERTICAL_FLOATING_WIDTH * virtualPixelRatio);
92         uint32_t minVerticalFloatingH = static_cast<uint32_t>(MIN_VERTICAL_FLOATING_HEIGHT * virtualPixelRatio);
93         Rect dstRect = oriDstRect;
94         // fix minimum size
95         if (isVertical) {
96             dstRect.width_ = std::max(minVerticalFloatingW, oriDstRect.width_);
97             dstRect.height_ = std::max(minVerticalFloatingH, oriDstRect.height_);
98         } else {
99             dstRect.width_ = std::max(minVerticalFloatingH, oriDstRect.width_);
100             dstRect.height_ = std::max(minVerticalFloatingW, oriDstRect.height_);
101         }
102 
103         // fix maximum size
104         dstRect.width_ = std::min(static_cast<uint32_t>(MAX_FLOATING_SIZE * virtualPixelRatio), dstRect.width_);
105         dstRect.height_ = std::min(static_cast<uint32_t>(MAX_FLOATING_SIZE * virtualPixelRatio), dstRect.height_);
106 
107         // limit position by fixed width or height
108         if (oriDstRect.posX_ != lastRect.posX_) {
109             dstRect.posX_ = oriDstRect.posX_ + static_cast<int32_t>(oriDstRect.width_) -
110                 static_cast<int32_t>(dstRect.width_);
111         }
112         if (oriDstRect.posY_ != lastRect.posY_) {
113             dstRect.posY_ = oriDstRect.posY_ + static_cast<int32_t>(oriDstRect.height_) -
114                 static_cast<int32_t>(dstRect.height_);
115         }
116         return dstRect;
117     }
118 
GetFixedWindowRectByLimitPosition(const Rect & oriDstRect,const Rect & lastRect,float virtualPixelRatio,const Rect & displayLimitRect)119     static Rect GetFixedWindowRectByLimitPosition(const Rect& oriDstRect, const Rect& lastRect,
120         float virtualPixelRatio, const Rect& displayLimitRect)
121     {
122         Rect dstRect = oriDstRect;
123         uint32_t windowTitleBarH = static_cast<uint32_t>(WINDOW_TITLE_BAR_HEIGHT * virtualPixelRatio);
124         // minimum (x + width)
125         if (dstRect.posX_ < (displayLimitRect.posX_ + static_cast<int32_t>(windowTitleBarH - oriDstRect.width_))) {
126             if (oriDstRect.width_ != lastRect.width_) {
127                 dstRect.width_ = static_cast<uint32_t>(displayLimitRect.posX_  - oriDstRect.posX_) + windowTitleBarH;
128             }
129         }
130         // maximum position x
131         if (dstRect.posX_ > (displayLimitRect.posX_ +
132                              static_cast<int32_t>(displayLimitRect.width_ - windowTitleBarH))) {
133             dstRect.posX_ = displayLimitRect.posX_ + static_cast<int32_t>(displayLimitRect.width_ - windowTitleBarH);
134             if (oriDstRect.width_ != lastRect.width_) {
135                 dstRect.width_ = lastRect.width_;
136             }
137         }
138         // minimum position y
139         if (oriDstRect.posY_ < displayLimitRect.posY_) {
140             dstRect.posY_ = displayLimitRect.posY_;
141             if (oriDstRect.height_ != lastRect.height_) {
142                 dstRect.height_ = lastRect.height_;
143             }
144         }
145         // maximum position y
146         if (dstRect.posY_ > (displayLimitRect.posY_ +
147                              static_cast<int32_t>(displayLimitRect.height_ - windowTitleBarH))) {
148             dstRect.posY_ = displayLimitRect.posY_ + static_cast<int32_t>(displayLimitRect.height_ - windowTitleBarH);
149             if (oriDstRect.height_ != lastRect.height_) {
150                 dstRect.height_ = lastRect.height_;
151             }
152         }
153         return dstRect;
154     }
155 
IsPointInTargetRect(int32_t pointPosX,int32_t pointPosY,const Rect & targetRect)156     static bool IsPointInTargetRect(int32_t pointPosX, int32_t pointPosY, const Rect& targetRect)
157     {
158         if ((pointPosX > targetRect.posX_) &&
159             (pointPosX < (targetRect.posX_ + static_cast<int32_t>(targetRect.width_))) &&
160             (pointPosY > targetRect.posY_) &&
161             (pointPosY < (targetRect.posY_ + static_cast<int32_t>(targetRect.height_)))) {
162             return true;
163         }
164         return false;
165     }
166 
IsPointInWindowExceptCorner(int32_t pointPosX,int32_t pointPosY,const Rect & rectExceptCorner)167     static bool IsPointInWindowExceptCorner(int32_t pointPosX, int32_t pointPosY, const Rect& rectExceptCorner)
168     {
169         if ((pointPosX > rectExceptCorner.posX_ &&
170             pointPosX < (rectExceptCorner.posX_ + static_cast<int32_t>(rectExceptCorner.width_))) ||
171             (pointPosY > rectExceptCorner.posY_ &&
172             pointPosY < (rectExceptCorner.posY_ + static_cast<int32_t>(rectExceptCorner.height_)))) {
173             return true;
174         }
175         return false;
176     }
177 
IsSwitchCascadeReason(WindowUpdateReason reason)178     static inline bool IsSwitchCascadeReason(WindowUpdateReason reason)
179     {
180         return (reason >= WindowUpdateReason::NEED_SWITCH_CASCADE_BASE) &&
181             (reason < WindowUpdateReason::NEED_SWITCH_CASCADE_END);
182     }
183 
GetAvoidPosType(const Rect & rect,uint32_t displayWidth,uint32_t displayHeight)184     static AvoidPosType GetAvoidPosType(const Rect& rect, uint32_t displayWidth, uint32_t displayHeight)
185     {
186         if (rect.width_ ==  displayWidth) {
187             if (rect.posY_ == 0) {
188                 return AvoidPosType::AVOID_POS_TOP;
189             } else {
190                 return AvoidPosType::AVOID_POS_BOTTOM;
191             }
192         } else if (rect.height_ ==  displayHeight) {
193             if (rect.posX_ == 0) {
194                 return AvoidPosType::AVOID_POS_LEFT;
195             } else {
196                 return AvoidPosType::AVOID_POS_RIGHT;
197             }
198         }
199 
200         return AvoidPosType::AVOID_POS_UNKNOWN;
201     }
202 
203 private:
204     WindowHelper() = default;
205     ~WindowHelper() = default;
206 };
207 } // namespace OHOS
208 } // namespace Rosen
209 #endif // OHOS_WM_INCLUDE_WM_HELPER_H