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