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