• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 <unistd.h>
20 #include <vector>
21 #include "ability_info.h"
22 #include "window_transition_info.h"
23 #include "wm_common.h"
24 #include "wm_common_inner.h"
25 #include "wm_math.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 class WindowHelper {
30 public:
IsMainWindow(WindowType type)31     static inline bool IsMainWindow(WindowType type)
32     {
33         return (type >= WindowType::APP_MAIN_WINDOW_BASE && type < WindowType::APP_MAIN_WINDOW_END);
34     }
35 
IsSubWindow(WindowType type)36     static inline bool IsSubWindow(WindowType type)
37     {
38         return (type >= WindowType::APP_SUB_WINDOW_BASE && type < WindowType::APP_SUB_WINDOW_END);
39     }
40 
IsAppWindow(WindowType type)41     static inline bool IsAppWindow(WindowType type)
42     {
43         return (IsMainWindow(type) || IsSubWindow(type));
44     }
45 
IsAppFloatingWindow(WindowType type)46     static inline bool IsAppFloatingWindow(WindowType type)
47     {
48         return (type == WindowType::WINDOW_TYPE_FLOAT) || (type == WindowType::WINDOW_TYPE_FLOAT_CAMERA);
49     }
50 
IsBelowSystemWindow(WindowType type)51     static inline bool IsBelowSystemWindow(WindowType type)
52     {
53         return (type >= WindowType::BELOW_APP_SYSTEM_WINDOW_BASE && type < WindowType::BELOW_APP_SYSTEM_WINDOW_END);
54     }
55 
IsAboveSystemWindow(WindowType type)56     static inline bool IsAboveSystemWindow(WindowType type)
57     {
58         return (type >= WindowType::ABOVE_APP_SYSTEM_WINDOW_BASE && type < WindowType::ABOVE_APP_SYSTEM_WINDOW_END);
59     }
60 
IsSystemSubWindow(WindowType type)61     static inline bool IsSystemSubWindow(WindowType type)
62     {
63         return (type >= WindowType::SYSTEM_SUB_WINDOW_BASE && type < WindowType::SYSTEM_SUB_WINDOW_END);
64     }
65 
IsSystemMainWindow(WindowType type)66     static inline bool IsSystemMainWindow(WindowType type)
67     {
68         return IsBelowSystemWindow(type) || IsAboveSystemWindow(type);
69     }
70 
IsSystemWindow(WindowType type)71     static inline bool IsSystemWindow(WindowType type)
72     {
73         return (IsBelowSystemWindow(type) || IsAboveSystemWindow(type) || IsSystemSubWindow(type));
74     }
75 
IsUIExtensionWindow(WindowType type)76     static inline bool IsUIExtensionWindow(WindowType type)
77     {
78         return (type == WindowType::WINDOW_TYPE_UI_EXTENSION);
79     }
80 
IsAppComponentWindow(WindowType type)81     static inline bool IsAppComponentWindow(WindowType type)
82     {
83         return (type == WindowType::WINDOW_TYPE_APP_COMPONENT);
84     }
85 
IsMainFloatingWindow(WindowType type,WindowMode mode)86     static inline bool IsMainFloatingWindow(WindowType type, WindowMode mode)
87     {
88         return ((IsMainWindow(type)) && (mode == WindowMode::WINDOW_MODE_FLOATING));
89     }
90 
IsMainFullScreenWindow(WindowType type,WindowMode mode)91     static inline bool IsMainFullScreenWindow(WindowType type, WindowMode mode)
92     {
93         return ((IsMainWindow(type)) && (mode == WindowMode::WINDOW_MODE_FULLSCREEN));
94     }
95 
IsFloatingWindow(WindowMode mode)96     static inline bool IsFloatingWindow(WindowMode mode)
97     {
98         return mode == WindowMode::WINDOW_MODE_FLOATING;
99     }
100 
IsSystemBarWindow(WindowType type)101     static inline bool IsSystemBarWindow(WindowType type)
102     {
103         return (type == WindowType::WINDOW_TYPE_STATUS_BAR || type == WindowType::WINDOW_TYPE_NAVIGATION_BAR);
104     }
105 
IsOverlayWindow(WindowType type)106     static inline bool IsOverlayWindow(WindowType type)
107     {
108         return (type == WindowType::WINDOW_TYPE_STATUS_BAR ||
109             type == WindowType::WINDOW_TYPE_NAVIGATION_BAR ||
110             type == WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT);
111     }
112 
IsRotatableWindow(WindowType type,WindowMode mode)113     static inline bool IsRotatableWindow(WindowType type, WindowMode mode)
114     {
115         return WindowHelper::IsMainFullScreenWindow(type, mode) || type == WindowType::WINDOW_TYPE_KEYGUARD ||
116             type == WindowType::WINDOW_TYPE_DESKTOP ||
117             ((type == WindowType::WINDOW_TYPE_LAUNCHER_RECENT) && (mode == WindowMode::WINDOW_MODE_FULLSCREEN));
118     }
119 
IsFullScreenWindow(WindowMode mode)120     static inline bool IsFullScreenWindow(WindowMode mode)
121     {
122         return mode == WindowMode::WINDOW_MODE_FULLSCREEN;
123     }
124 
IsSplitWindowMode(WindowMode mode)125     static inline bool IsSplitWindowMode(WindowMode mode)
126     {
127         return mode == WindowMode::WINDOW_MODE_SPLIT_PRIMARY || mode == WindowMode::WINDOW_MODE_SPLIT_SECONDARY;
128     }
129 
IsAppFullOrSplitWindow(WindowType type,WindowMode mode)130     static inline bool IsAppFullOrSplitWindow(WindowType type, WindowMode mode)
131     {
132         if (!IsAppWindow(type)) {
133             return false;
134         }
135         return IsFullScreenWindow(mode) || IsSplitWindowMode(mode);
136     }
137 
IsValidWindowMode(WindowMode mode)138     static inline bool IsValidWindowMode(WindowMode mode)
139     {
140         return mode == WindowMode::WINDOW_MODE_FULLSCREEN || mode == WindowMode::WINDOW_MODE_SPLIT_PRIMARY ||
141             mode == WindowMode::WINDOW_MODE_SPLIT_SECONDARY || mode == WindowMode::WINDOW_MODE_FLOATING ||
142             mode == WindowMode::WINDOW_MODE_PIP;
143     }
144 
IsEmptyRect(const Rect & r)145     static inline bool IsEmptyRect(const Rect& r)
146     {
147         return (r.posX_ == 0 && r.posY_ == 0 && r.width_ == 0 && r.height_ == 0);
148     }
149 
IsLandscapeRect(const Rect & r)150     static inline bool IsLandscapeRect(const Rect& r)
151     {
152         return r.width_ > r.height_;
153     }
154 
IsShowWhenLocked(uint32_t flags)155     static inline bool IsShowWhenLocked(uint32_t flags)
156     {
157         return flags & static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED);
158     }
159 
GetOverlap(const Rect & rect1,const Rect & rect2,const int offsetX,const int offsetY)160     static Rect GetOverlap(const Rect& rect1, const Rect& rect2, const int offsetX, const int offsetY)
161     {
162         int32_t x_begin = std::max(rect1.posX_, rect2.posX_);
163         int32_t x_end = std::min(rect1.posX_ + static_cast<int32_t>(rect1.width_),
164             rect2.posX_ + static_cast<int32_t>(rect2.width_));
165         int32_t y_begin = std::max(rect1.posY_, rect2.posY_);
166         int32_t y_end = std::min(rect1.posY_ + static_cast<int32_t>(rect1.height_),
167             rect2.posY_ + static_cast<int32_t>(rect2.height_));
168         if (y_begin >= y_end || x_begin >= x_end) {
169             return { 0, 0, 0, 0 };
170         }
171         return { x_begin - offsetX, y_begin - offsetY,
172             static_cast<uint32_t>(x_end - x_begin), static_cast<uint32_t>(y_end - y_begin) };
173     }
174 
IsWindowModeSupported(uint32_t modeSupportInfo,WindowMode mode)175     static bool IsWindowModeSupported(uint32_t modeSupportInfo, WindowMode mode)
176     {
177         switch (mode) {
178             case WindowMode::WINDOW_MODE_FULLSCREEN:
179                 return WindowModeSupport::WINDOW_MODE_SUPPORT_FULLSCREEN & modeSupportInfo;
180             case WindowMode::WINDOW_MODE_FLOATING:
181                 return WindowModeSupport::WINDOW_MODE_SUPPORT_FLOATING & modeSupportInfo;
182             case WindowMode::WINDOW_MODE_SPLIT_PRIMARY:
183                 return WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_PRIMARY & modeSupportInfo;
184             case WindowMode::WINDOW_MODE_SPLIT_SECONDARY:
185                 return WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_SECONDARY & modeSupportInfo;
186             case WindowMode::WINDOW_MODE_PIP:
187                 return WindowModeSupport::WINDOW_MODE_SUPPORT_PIP & modeSupportInfo;
188             case WindowMode::WINDOW_MODE_UNDEFINED:
189                 return false;
190             default:
191                 return true;
192         }
193     }
194 
GetWindowModeFromModeSupportInfo(uint32_t modeSupportInfo)195     static WindowMode GetWindowModeFromModeSupportInfo(uint32_t modeSupportInfo)
196     {
197         // get the binary number consists of the last 1 and 0 behind it
198         uint32_t windowModeSupport = modeSupportInfo & (~modeSupportInfo + 1);
199 
200         switch (windowModeSupport) {
201             case WindowModeSupport::WINDOW_MODE_SUPPORT_FULLSCREEN:
202                 return WindowMode::WINDOW_MODE_FULLSCREEN;
203             case WindowModeSupport::WINDOW_MODE_SUPPORT_FLOATING:
204                 return WindowMode::WINDOW_MODE_FLOATING;
205             case WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_PRIMARY:
206                 return WindowMode::WINDOW_MODE_SPLIT_PRIMARY;
207             case WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_SECONDARY:
208                 return WindowMode::WINDOW_MODE_SPLIT_SECONDARY;
209             case WindowModeSupport::WINDOW_MODE_SUPPORT_PIP:
210                 return WindowMode::WINDOW_MODE_PIP;
211             default:
212                 return WindowMode::WINDOW_MODE_UNDEFINED;
213         }
214     }
215 
ConvertSupportModesToSupportInfo(const std::vector<AppExecFwk::SupportWindowMode> & supportModes)216     static uint32_t ConvertSupportModesToSupportInfo(const std::vector<AppExecFwk::SupportWindowMode>& supportModes)
217     {
218         uint32_t modeSupportInfo = 0;
219         for (auto& mode : supportModes) {
220             if (mode == AppExecFwk::SupportWindowMode::FULLSCREEN) {
221                 modeSupportInfo |= WindowModeSupport::WINDOW_MODE_SUPPORT_FULLSCREEN;
222             } else if (mode == AppExecFwk::SupportWindowMode::SPLIT) {
223                 modeSupportInfo |= (WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_PRIMARY |
224                                     WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_SECONDARY);
225             } else if (mode == AppExecFwk::SupportWindowMode::FLOATING) {
226                 modeSupportInfo |= WindowModeSupport::WINDOW_MODE_SUPPORT_FLOATING;
227             }
228         }
229         return modeSupportInfo;
230     }
231 
IsPointInTargetRect(int32_t pointPosX,int32_t pointPosY,const Rect & targetRect)232     static bool IsPointInTargetRect(int32_t pointPosX, int32_t pointPosY, const Rect& targetRect)
233     {
234         if ((pointPosX > targetRect.posX_) &&
235             (pointPosX < (targetRect.posX_ + static_cast<int32_t>(targetRect.width_)) - 1) &&
236             (pointPosY > targetRect.posY_) &&
237             (pointPosY < (targetRect.posY_ + static_cast<int32_t>(targetRect.height_)) - 1)) {
238             return true;
239         }
240         return false;
241     }
242 
IsPointInTargetRectWithBound(int32_t pointPosX,int32_t pointPosY,const Rect & targetRect)243     static bool IsPointInTargetRectWithBound(int32_t pointPosX, int32_t pointPosY, const Rect& targetRect)
244     {
245         if ((pointPosX >= targetRect.posX_) &&
246             (pointPosX < (targetRect.posX_ + static_cast<int32_t>(targetRect.width_))) &&
247             (pointPosY >= targetRect.posY_) &&
248             (pointPosY < (targetRect.posY_ + static_cast<int32_t>(targetRect.height_)))) {
249             return true;
250         }
251         return false;
252     }
253 
IsPointInWindowExceptCorner(int32_t pointPosX,int32_t pointPosY,const Rect & rectExceptCorner)254     static bool IsPointInWindowExceptCorner(int32_t pointPosX, int32_t pointPosY, const Rect& rectExceptCorner)
255     {
256         if ((pointPosX > rectExceptCorner.posX_ &&
257             pointPosX < (rectExceptCorner.posX_ + static_cast<int32_t>(rectExceptCorner.width_)) - 1) ||
258             (pointPosY > rectExceptCorner.posY_ &&
259             pointPosY < (rectExceptCorner.posY_ + static_cast<int32_t>(rectExceptCorner.height_)) - 1)) {
260             return true;
261         }
262         return false;
263     }
264 
IsSwitchCascadeReason(WindowUpdateReason reason)265     static inline bool IsSwitchCascadeReason(WindowUpdateReason reason)
266     {
267         return (reason >= WindowUpdateReason::NEED_SWITCH_CASCADE_BASE) &&
268             (reason < WindowUpdateReason::NEED_SWITCH_CASCADE_END);
269     }
270 
GetAvoidPosType(const Rect & rect,const Rect & displayRect)271     static AvoidPosType GetAvoidPosType(const Rect& rect, const Rect& displayRect)
272     {
273         if (rect.width_ ==  displayRect.width_) {
274             if (rect.posY_ == displayRect.posY_) {
275                 return AvoidPosType::AVOID_POS_TOP;
276             } else {
277                 return AvoidPosType::AVOID_POS_BOTTOM;
278             }
279         } else if (rect.height_ ==  displayRect.height_) {
280             if (rect.posX_ == displayRect.posX_) {
281                 return AvoidPosType::AVOID_POS_LEFT;
282             } else {
283                 return AvoidPosType::AVOID_POS_RIGHT;
284             }
285         }
286 
287         return AvoidPosType::AVOID_POS_UNKNOWN;
288     }
289 
IsNumber(std::string str)290     static inline bool IsNumber(std::string str)
291     {
292         if (str.size() == 0) {
293             return false;
294         }
295         for (int32_t i = 0; i < static_cast<int32_t>(str.size()); i++) {
296             if (str.at(i) < '0' || str.at(i) > '9') {
297                 return false;
298             }
299         }
300         return true;
301     }
302 
303     static bool IsFloatingNumber(std::string str, bool allowNeg = false)
304     {
305         if (str.size() == 0) {
306             return false;
307         }
308 
309         int32_t i = 0;
310         if (allowNeg && str.at(i) == '-') {
311             i++;
312         }
313 
314         for (; i < static_cast<int32_t>(str.size()); i++) {
315             if ((str.at(i) < '0' || str.at(i) > '9') &&
316                 (str.at(i) != '.' || std::count(str.begin(), str.end(), '.') > 1)) {
317                 return false;
318             }
319         }
320         return true;
321     }
322 
Split(std::string str,std::string pattern)323     static std::vector<std::string> Split(std::string str, std::string pattern)
324     {
325         int32_t position;
326         std::vector<std::string> result;
327         str += pattern;
328         int32_t length = static_cast<int32_t>(str.size());
329         for (int32_t i = 0; i < length; i++) {
330             position = static_cast<int32_t>(str.find(pattern, i));
331             if (position < length) {
332                 std::string tmp = str.substr(i, position - i);
333                 result.push_back(tmp);
334                 i = position + static_cast<int32_t>(pattern.size()) - 1;
335             }
336         }
337         return result;
338     }
339 
CalculateOriginPosition(const Rect & rOrigin,const Rect & rActial,const PointInfo & pos)340     static PointInfo CalculateOriginPosition(const Rect& rOrigin, const Rect& rActial, const PointInfo& pos)
341     {
342         PointInfo ret = pos;
343         ret.x += rActial.posX_ - pos.x;
344         ret.y += rActial.posY_ - pos.y;
345         ret.x += rOrigin.posX_ - rActial.posX_;
346         ret.y += rOrigin.posY_ - rActial.posY_;
347         if (rActial.width_ && rActial.height_) {
348             ret.x += (pos.x - rActial.posX_) * rOrigin.width_ / rActial.width_;
349             ret.y += (pos.y - rActial.posY_) * rOrigin.height_ / rActial.height_;
350         }
351         return ret;
352     }
353 
354     // Transform a point at screen to its oringin position in 3D world and project to xy plane
CalculateOriginPosition(const TransformHelper::Matrix4 & transformMat,const PointInfo & pointPos)355     static PointInfo CalculateOriginPosition(const TransformHelper::Matrix4& transformMat, const PointInfo& pointPos)
356     {
357         TransformHelper::Vector2 p(static_cast<float>(pointPos.x), static_cast<float>(pointPos.y));
358         TransformHelper::Vector2 originPos = TransformHelper::GetOriginScreenPoint(p, transformMat);
359         return PointInfo { static_cast<uint32_t>(originPos.x_), static_cast<uint32_t>(originPos.y_) };
360     }
361 
362     // This method is used to update transform when rect changed, but world transform matrix should not change.
GetTransformFromWorldMat4(const TransformHelper::Matrix4 & inWorldMat,const Rect & rect,Transform & transform)363     static void GetTransformFromWorldMat4(const TransformHelper::Matrix4& inWorldMat, const Rect& rect,
364         Transform& transform)
365     {
366         TransformHelper::Vector3 pivotPos = { rect.posX_ + transform.pivotX_ * rect.width_,
367             rect.posY_ + transform.pivotY_ * rect.height_, 0 };
368         TransformHelper::Matrix4 worldMat = TransformHelper::CreateTranslation(pivotPos) * inWorldMat *
369                         TransformHelper::CreateTranslation(-pivotPos);
370         auto scale = worldMat.GetScale();
371         auto translation = worldMat.GetTranslation();
372         transform.scaleX_ = scale.x_;
373         transform.scaleY_ = scale.y_;
374         transform.scaleZ_ = scale.z_;
375         transform.translateX_ = translation.x_;
376         transform.translateY_ = translation.y_;
377         transform.translateZ_ = translation.z_;
378     }
379 
ComputeWorldTransformMat4(const Transform & transform)380     static TransformHelper::Matrix4 ComputeWorldTransformMat4(const Transform& transform)
381     {
382         TransformHelper::Matrix4 ret = TransformHelper::Matrix4::Identity;
383         // set scale
384         if ((transform.scaleX_ - 1) || (transform.scaleY_ - 1) || (transform.scaleY_ - 1)) {
385             ret *= TransformHelper::CreateScale(transform.scaleX_, transform.scaleY_, transform.scaleZ_);
386         }
387         // set rotation
388         if (transform.rotationX_) {
389             ret *= TransformHelper::CreateRotationX(MathHelper::ToRadians(transform.rotationX_));
390         }
391         if (transform.rotationY_) {
392             ret *= TransformHelper::CreateRotationY(MathHelper::ToRadians(transform.rotationY_));
393         }
394         if (transform.rotationZ_) {
395             ret *= TransformHelper::CreateRotationZ(MathHelper::ToRadians(transform.rotationZ_));
396         }
397         // set translation
398         if (transform.translateX_ || transform.translateY_ || transform.translateZ_) {
399             ret *= TransformHelper::CreateTranslation(TransformHelper::Vector3(transform.translateX_,
400                 transform.translateY_, transform.translateZ_));
401         }
402         return ret;
403     }
404 
405     // Transform rect by matrix and get the circumscribed rect
TransformRect(const TransformHelper::Matrix4 & transformMat,const Rect & rect)406     static Rect TransformRect(const TransformHelper::Matrix4& transformMat, const Rect& rect)
407     {
408         TransformHelper::Vector3 a = TransformHelper::TransformWithPerspDiv(
409             TransformHelper::Vector3(rect.posX_, rect.posY_, 0), transformMat);
410         TransformHelper::Vector3 b = TransformHelper::TransformWithPerspDiv(
411             TransformHelper::Vector3(rect.posX_ + rect.width_, rect.posY_, 0), transformMat);
412         TransformHelper::Vector3 c = TransformHelper::TransformWithPerspDiv(
413             TransformHelper::Vector3(rect.posX_, rect.posY_ + rect.height_, 0), transformMat);
414         TransformHelper::Vector3 d = TransformHelper::TransformWithPerspDiv(
415             TransformHelper::Vector3(rect.posX_ + rect.width_, rect.posY_ + rect.height_, 0), transformMat);
416         // Return smallest rect involve transformed rect(abcd)
417         int32_t xmin = MathHelper::Min(a.x_, b.x_, c.x_, d.x_);
418         int32_t ymin = MathHelper::Min(a.y_, b.y_, c.y_, d.y_);
419         int32_t xmax = MathHelper::Max(a.x_, b.x_, c.x_, d.x_);
420         int32_t ymax = MathHelper::Max(a.y_, b.y_, c.y_, d.y_);
421         uint32_t w = static_cast<uint32_t>(xmax - xmin);
422         uint32_t h = static_cast<uint32_t>(ymax - ymin);
423         return Rect { xmin, ymin, w, h };
424     }
425 
CalculateHotZoneScale(const TransformHelper::Matrix4 & transformMat)426     static TransformHelper::Vector2 CalculateHotZoneScale(const TransformHelper::Matrix4& transformMat)
427     {
428         TransformHelper::Vector2 hotZoneScale;
429         TransformHelper::Vector3 a = TransformHelper::TransformWithPerspDiv(TransformHelper::Vector3(0, 0, 0),
430             transformMat);
431         TransformHelper::Vector3 b = TransformHelper::TransformWithPerspDiv(TransformHelper::Vector3(1, 0, 0),
432             transformMat);
433         TransformHelper::Vector3 c = TransformHelper::TransformWithPerspDiv(TransformHelper::Vector3(0, 1, 0),
434             transformMat);
435         TransformHelper::Vector2 axy(a.x_, a.y_);
436         TransformHelper::Vector2 bxy(b.x_, b.y_);
437         TransformHelper::Vector2 cxy(c.x_, c.y_);
438         hotZoneScale.x_ = (axy - bxy).Length();
439         hotZoneScale.y_ = (axy - cxy).Length();
440         if (std::isnan(hotZoneScale.x_) || std::isnan(hotZoneScale.y_) ||
441             MathHelper::NearZero(hotZoneScale.x_) || MathHelper::NearZero(hotZoneScale.y_)) {
442             return TransformHelper::Vector2(1, 1);
443         } else {
444             return hotZoneScale;
445         }
446     }
447 
CalculateTouchHotAreas(const Rect & windowRect,const std::vector<Rect> & requestRects,std::vector<Rect> & outRects)448     static bool CalculateTouchHotAreas(const Rect& windowRect, const std::vector<Rect>& requestRects,
449         std::vector<Rect>& outRects)
450     {
451         bool isOk = true;
452         for (const auto& rect : requestRects) {
453             if (rect.posX_ < 0 || rect.posY_ < 0 || rect.width_ == 0 || rect.height_ == 0) {
454                 return false;
455             }
456             Rect hotArea;
457             if (rect.posX_ >= static_cast<int32_t>(windowRect.width_) ||
458                 rect.posY_ >= static_cast<int32_t>(windowRect.height_)) {
459                 isOk = false;
460                 continue;
461             }
462             hotArea.posX_ = windowRect.posX_ + rect.posX_;
463             hotArea.posY_ = windowRect.posY_ + rect.posY_;
464             hotArea.width_ = static_cast<uint32_t>(std::min(hotArea.posX_ + rect.width_,
465                 windowRect.posX_ + windowRect.width_) - hotArea.posX_);
466             hotArea.height_ = static_cast<uint32_t>(std::min(hotArea.posY_ + rect.height_,
467                 windowRect.posY_ + windowRect.height_) - hotArea.posY_);
468             outRects.emplace_back(hotArea);
469         }
470         return isOk;
471     }
472 
IsRectSatisfiedWithSizeLimits(const Rect & rect,const WindowSizeLimits & sizeLimits)473     static bool IsRectSatisfiedWithSizeLimits(const Rect& rect, const WindowSizeLimits& sizeLimits)
474     {
475         if (rect.height_ == 0) {
476             return false;
477         }
478         auto curRatio = static_cast<float>(rect.width_) / static_cast<float>(rect.height_);
479         if (sizeLimits.minWidth_ <= rect.width_ && rect.width_ <= sizeLimits.maxWidth_ &&
480             sizeLimits.minHeight_ <= rect.height_ && rect.height_ <= sizeLimits.maxHeight_ &&
481             sizeLimits.minRatio_ <= curRatio && curRatio <= sizeLimits.maxRatio_) {
482             return true;
483         }
484         return false;
485     }
486 
IsOnlySupportSplitAndShowWhenLocked(bool isShowWhenLocked,uint32_t modeSupportInfo)487     static bool IsOnlySupportSplitAndShowWhenLocked(bool isShowWhenLocked, uint32_t modeSupportInfo)
488     {
489         uint32_t splitModeInfo = (WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_PRIMARY |
490                                   WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_SECONDARY);
491         if (isShowWhenLocked && (splitModeInfo == modeSupportInfo)) {
492             return true;
493         }
494         return false;
495     }
496 
IsInvalidWindowInTileLayoutMode(uint32_t supportModeInfo,WindowLayoutMode layoutMode)497     static bool IsInvalidWindowInTileLayoutMode(uint32_t supportModeInfo, WindowLayoutMode layoutMode)
498     {
499         if ((!IsWindowModeSupported(supportModeInfo, WindowMode::WINDOW_MODE_FLOATING)) &&
500             (layoutMode == WindowLayoutMode::TILE)) {
501             return true;
502         }
503         return false;
504     }
505 
CheckSupportWindowMode(WindowMode winMode,uint32_t modeSupportInfo,const sptr<WindowTransitionInfo> & info)506     static bool CheckSupportWindowMode(WindowMode winMode, uint32_t modeSupportInfo,
507         const sptr<WindowTransitionInfo>& info)
508     {
509         if (!WindowHelper::IsMainWindow(info->GetWindowType())) {
510             return true;
511         }
512 
513         if ((!IsWindowModeSupported(modeSupportInfo, winMode)) ||
514             (IsOnlySupportSplitAndShowWhenLocked(info->GetShowFlagWhenLocked(), modeSupportInfo))) {
515             return false;
516         }
517         return true;
518     }
519 
IsAspectRatioSatisfiedWithSizeLimits(const WindowSizeLimits & sizeLimits,float ratio,float vpr)520     static bool IsAspectRatioSatisfiedWithSizeLimits(const WindowSizeLimits& sizeLimits, float ratio, float vpr)
521     {
522         /*
523          * 1) Usually the size limits won't be empty after show window.
524          *    In case of SetAspectRatio is called befor show (size limits may be empty at that time) or the
525          *    sizeLimits is empty, there is no need to check ratio (layout will check), return true directly.
526          * 2) ratio : 0.0 means reset aspect ratio
527          */
528         if (sizeLimits.IsEmpty() || MathHelper::NearZero(ratio)) {
529             return true;
530         }
531 
532         uint32_t winFrameW = static_cast<uint32_t>(WINDOW_FRAME_WIDTH * vpr) * 2; // 2 mean double decor width
533         uint32_t winFrameH = static_cast<uint32_t>(WINDOW_FRAME_WIDTH * vpr) +
534             static_cast<uint32_t>(WINDOW_TITLE_BAR_HEIGHT * vpr); // decor height
535         uint32_t maxWidth = sizeLimits.maxWidth_ - winFrameW;
536         uint32_t minWidth = sizeLimits.minWidth_ - winFrameW;
537         uint32_t maxHeight = sizeLimits.maxHeight_ - winFrameH;
538         uint32_t minHeight = sizeLimits.minHeight_ - winFrameH;
539         float maxRatio = static_cast<float>(maxWidth) / static_cast<float>(minHeight);
540         float minRatio = static_cast<float>(minWidth) / static_cast<float>(maxHeight);
541         if (maxRatio < ratio || ratio < minRatio) {
542             return false;
543         }
544         return true;
545     }
546 
IsWindowFollowParent(WindowType type)547     static bool IsWindowFollowParent(WindowType type)
548     {
549         if (type == WindowType::WINDOW_TYPE_DIALOG || type == WindowType::WINDOW_TYPE_APP_SUB_WINDOW) {
550             return true;
551         }
552         return false;
553     }
554 
555 private:
556     WindowHelper() = default;
557     ~WindowHelper() = default;
558 };
559 } // namespace OHOS
560 } // namespace Rosen
561 #endif // OHOS_WM_INCLUDE_WM_HELPER_H
562