• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_ROSEN_SESSION_HELPER_H
17 #define OHOS_ROSEN_SESSION_HELPER_H
18 
19 #include <pointer_event.h>
20 
21 #include <string>
22 #include "ws_common.h"
23 #include "ws_common_inner.h"
24 #include "wm_common.h"
25 #include "wm_common_inner.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 class SessionHelper {
30 public:
GetOverlap(const WSRect & rect1,const WSRect & rect2,int offsetX,int offsetY)31     static WSRect GetOverlap(const WSRect& rect1, const WSRect& rect2, int offsetX, int offsetY)
32     {
33         int32_t x_begin = std::max(rect1.posX_, rect2.posX_);
34         int32_t x_end = std::min(rect1.posX_ + static_cast<int32_t>(rect1.width_),
35             rect2.posX_ + static_cast<int32_t>(rect2.width_));
36         int32_t y_begin = std::max(rect1.posY_, rect2.posY_);
37         int32_t y_end = std::min(rect1.posY_ + static_cast<int32_t>(rect1.height_),
38             rect2.posY_ + static_cast<int32_t>(rect2.height_));
39         if (y_begin >= y_end || x_begin >= x_end) {
40             return { 0, 0, 0, 0 };
41         }
42         return { x_begin - offsetX, y_begin - offsetY,
43             static_cast<uint32_t>(x_end - x_begin), static_cast<uint32_t>(y_end - y_begin) };
44     }
45 
IsEmptyRect(const WSRect & r)46     static inline bool IsEmptyRect(const WSRect& r)
47     {
48         return (r.posX_ == 0 && r.posY_ == 0 && r.width_ == 0 && r.height_ == 0);
49     }
50 
IsPointInRect(int32_t pointPosX,int32_t pointPosY,const Rect & rect)51     static bool IsPointInRect(int32_t pointPosX, int32_t pointPosY, const Rect& rect)
52     {
53         if ((pointPosX >= rect.posX_) &&
54             (pointPosX < (rect.posX_ + static_cast<int32_t>(rect.width_)) - 1) &&
55             (pointPosY >= rect.posY_) &&
56             (pointPosY < (rect.posY_ + static_cast<int32_t>(rect.height_)) - 1)) {
57             return true;
58         }
59         return false;
60     }
61 
TransferToWSRect(const Rect & rect)62     static inline WSRect TransferToWSRect(const Rect& rect)
63     {
64         WSRect r;
65         r.height_ = rect.height_;
66         r.width_ = rect.width_;
67         r.posX_ = rect.posX_;
68         r.posY_ = rect.posY_;
69         return r;
70     }
71 
TransferToRect(const WSRect & rect)72     static inline Rect TransferToRect(const WSRect& rect)
73     {
74         Rect r;
75         r.height_ = rect.height_;
76         r.width_ = rect.width_;
77         r.posX_ = rect.posX_;
78         r.posY_ = rect.posY_;
79         return r;
80     }
81 
IsBelowSystemWindow(WindowType type)82     static inline bool IsBelowSystemWindow(WindowType type)
83     {
84         return (type >= WindowType::BELOW_APP_SYSTEM_WINDOW_BASE && type < WindowType::BELOW_APP_SYSTEM_WINDOW_END);
85     }
86 
IsAboveSystemWindow(WindowType type)87     static inline bool IsAboveSystemWindow(WindowType type)
88     {
89         return (type >= WindowType::ABOVE_APP_SYSTEM_WINDOW_BASE && type < WindowType::ABOVE_APP_SYSTEM_WINDOW_END);
90     }
91 
IsSystemSubWindow(WindowType type)92     static inline bool IsSystemSubWindow(WindowType type)
93     {
94         return (type >= WindowType::SYSTEM_SUB_WINDOW_BASE && type < WindowType::SYSTEM_SUB_WINDOW_END);
95     }
96 
IsSystemWindow(WindowType type)97     static inline bool IsSystemWindow(WindowType type)
98     {
99         return (IsBelowSystemWindow(type) || IsAboveSystemWindow(type) || IsSystemSubWindow(type));
100     }
101 
IsMainWindow(WindowType type)102     static inline bool IsMainWindow(WindowType type)
103     {
104         return (type >= WindowType::APP_MAIN_WINDOW_BASE && type < WindowType::APP_MAIN_WINDOW_END);
105     }
106 
IsSubWindow(WindowType type)107     static inline bool IsSubWindow(WindowType type)
108     {
109         return (type >= WindowType::APP_SUB_WINDOW_BASE && type < WindowType::APP_SUB_WINDOW_END);
110     }
111 
IsNonSecureToUIExtension(WindowType type)112     static inline bool IsNonSecureToUIExtension(WindowType type)
113     {
114         return IsSubWindow(type) || type == WindowType::WINDOW_TYPE_DIALOG;
115     }
116 
IsSecureUIExtension(UIExtensionUsage usage)117     static inline bool IsSecureUIExtension(UIExtensionUsage usage)
118     {
119         return usage == UIExtensionUsage::CONSTRAINED_EMBEDDED || usage == UIExtensionUsage::PREVIEW_EMBEDDED;
120     }
121 
IsNeedSACalling(WindowType type)122     static inline bool IsNeedSACalling(WindowType type)
123     {
124         return type == WindowType::WINDOW_TYPE_MAGNIFICATION || type == WindowType::WINDOW_TYPE_MAGNIFICATION_MENU ||
125             type == WindowType::WINDOW_TYPE_SELECTION;
126     }
127 
GetAreaType(int32_t pointWinX,int32_t pointWinY,int32_t sourceType,int outside,float vpr,const WSRect & rect)128     static AreaType GetAreaType(int32_t pointWinX, int32_t pointWinY,
129         int32_t sourceType, int outside, float vpr, const WSRect& rect)
130     {
131         int32_t insideCorner = WINDOW_FRAME_CORNER_WIDTH * vpr;
132         int32_t insideEdge = WINDOW_FRAME_WIDTH * vpr;
133         int32_t leftOut = -outside;
134         int32_t leftIn = insideEdge;
135         int32_t leftCorner = insideCorner;
136         int32_t rightCorner = rect.width_ - insideCorner;
137         int32_t rightIn = rect.width_ - insideEdge;
138         int32_t rightOut = rect.width_ + outside;
139         int32_t topOut = -outside;
140         int32_t topIn = insideEdge;
141         int32_t topCorner = insideCorner;
142         int32_t bottomCorner = rect.height_ - insideCorner;
143         int32_t bottomIn = rect.height_ - insideEdge;
144         int32_t bottomOut = rect.height_ + outside;
145 
146         auto isInRange = [](int32_t min, int32_t max, int32_t value) { return min <= value && value <= max; };
147 
148         AreaType type;
149         if (isInRange(leftOut, leftCorner, pointWinX) && isInRange(topOut, topCorner, pointWinY)) {
150             type = AreaType::LEFT_TOP;
151         } else if (isInRange(rightCorner, rightOut, pointWinX) && isInRange(topOut, topCorner, pointWinY)) {
152             type = AreaType::RIGHT_TOP;
153         } else if (isInRange(rightCorner, rightOut, pointWinX) && isInRange(bottomCorner, bottomOut, pointWinY)) {
154             type = AreaType::RIGHT_BOTTOM;
155         } else if (isInRange(leftOut, leftCorner, pointWinX) && isInRange(bottomCorner, bottomOut, pointWinY)) {
156             type = AreaType::LEFT_BOTTOM;
157         } else if (isInRange(leftOut, leftIn, pointWinX)) {
158             type = AreaType::LEFT;
159         } else if (isInRange(topOut, topIn, pointWinY)) {
160             type = AreaType::TOP;
161         } else if (isInRange(rightIn, rightOut, pointWinX)) {
162             type = AreaType::RIGHT;
163         } else if (isInRange(bottomIn, bottomOut, pointWinY)) {
164             type = AreaType::BOTTOM;
165         } else {
166             type = AreaType::UNDEFINED;
167         }
168         return type;
169     }
170 
GetAreaTypeForScaleResize(int32_t pointWinX,int32_t pointWinY,int outside,const WSRect & rect)171     static AreaType GetAreaTypeForScaleResize(int32_t pointWinX, int32_t pointWinY, int outside, const WSRect& rect)
172     {
173         constexpr uint32_t HALF = 2;
174         int32_t leftOut = -outside;
175         int32_t rightOut = rect.width_ + outside;
176         int32_t topOut = -outside;
177         int32_t bottomOut = rect.height_ + outside;
178 
179         auto isInRange = [](int32_t min, int32_t max, int32_t value) { return min <= value && value <= max; };
180 
181         AreaType type;
182         if (isInRange(leftOut, rightOut / HALF, pointWinX) &&
183             isInRange(topOut, bottomOut / HALF, pointWinY)) {
184             type = AreaType::LEFT_TOP;
185         } else if (isInRange(rightOut / HALF, rightOut, pointWinX) &&
186             isInRange(topOut, bottomOut / HALF, pointWinY)) {
187             type = AreaType::RIGHT_TOP;
188         } else if (isInRange(rightOut / HALF, rightOut, pointWinX) &&
189             isInRange(bottomOut / HALF, bottomOut, pointWinY)) {
190             type = AreaType::RIGHT_BOTTOM;
191         } else if (isInRange(leftOut, rightOut / HALF, pointWinX) &&
192             isInRange(bottomOut / HALF, bottomOut, pointWinY)) {
193             type = AreaType::LEFT_BOTTOM;
194         } else {
195             type = AreaType::UNDEFINED;
196         }
197         return type;
198     }
199 
ConvertDisplayOrientationToFloat(DisplayOrientation sensorOrientation)200     static float ConvertDisplayOrientationToFloat(DisplayOrientation sensorOrientation)
201     {
202         float rotation = 0.f;
203         switch (sensorOrientation) {
204             case DisplayOrientation::LANDSCAPE:
205                 rotation = 90.f; // degree 90
206                 break;
207             case DisplayOrientation::PORTRAIT_INVERTED:
208                 rotation = 180.f; // degree 180
209                 break;
210             case DisplayOrientation::LANDSCAPE_INVERTED:
211                 rotation = 270.f; // degree 270
212                 break;
213             default:
214                 break;
215         }
216         return rotation;
217     }
218 };
219 } // Rosen
220 } // OHOS
221 #endif // OHOS_ROSEN_SESSION_HELPER_H