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