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