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