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