• 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_WINDOW_SCENE_MOVE_DRAG_CONTROLLER_H
17 #define OHOS_ROSEN_WINDOW_SCENE_MOVE_DRAG_CONTROLLER_H
18 
19 #include <refbase.h>
20 #include <struct_multimodal.h>
21 
22 #include "common/include/window_session_property.h"
23 #include "property/rs_properties_def.h"
24 #include "window.h"
25 #include "ws_common_inner.h"
26 
27 namespace OHOS::MMI {
28 class PointerEvent;
29 } // namespace MMI
30 
31 namespace OHOS::Rosen {
32 
33 using MoveDragCallback = std::function<void(const SizeChangeReason&)>;
34 
35 using NotifyWindowDragHotAreaFunc = std::function<void(uint32_t type, const SizeChangeReason& reason)>;
36 
37 using NotifyWindowPidChangeCallback = std::function<void(int32_t windowId, bool startMoving)>;
38 
39 const uint32_t WINDOW_HOT_AREA_TYPE_UNDEFINED = 0;
40 
41 class MoveDragController : public RefBase {
42 public:
43     MoveDragController(int32_t persistentId);
44     ~MoveDragController() = default;
45 
46     void RegisterMoveDragCallback(const MoveDragCallback& callBack);
47     void SetStartMoveFlag(bool flag);
48     bool GetStartMoveFlag() const;
49     bool GetStartDragFlag() const;
50     bool HasPointDown();
51     void SetNotifyWindowPidChangeCallback(const NotifyWindowPidChangeCallback& callback);
52     WSRect GetTargetRect() const;
53     void InitMoveDragProperty();
54     void SetOriginalMoveDragPos(int32_t pointerId, int32_t pointerType, int32_t pointerPosX,
55         int32_t pointerPosY, int32_t pointerWindowX, int32_t pointerWindowY,
56         const WSRect& winRect);
57     void SetAspectRatio(float ratio);
58     bool ConsumeMoveEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, const WSRect& originalRect);
59     bool ConsumeDragEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, const WSRect& originalRect,
60         const sptr<WindowSessionProperty> property, const SystemSessionConfig& sysConfig);
61     void CalcFirstMoveTargetRect(const WSRect& windowRect, bool isFullToFloating);
62     WSRect GetFullScreenToFloatingRect(const WSRect& originalRect, const WSRect& windowRect);
63     int32_t GetOriginalPointerPosX();
64     int32_t GetOriginalPointerPosY();
65     void SetWindowDragHotAreaFunc(const NotifyWindowDragHotAreaFunc& func);
66     void UpdateGravityWhenDrag(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
67         const std::shared_ptr<RSSurfaceNode>& surfaceNode);
68     void OnLostFocus();
69     bool GetMoveInputBarFlag();
70     DisplayId GetMoveInputBarStartDisplayId();
71     void SetMoveInputBarStartDisplayId(DisplayId displayId);
72     void SetMoveAvailableArea(const DMRect& area);
73     void SetMoveInputBarFlag(bool moveInputBarFlag);
74 
75     /*
76      * PC Window Layout
77      */
78     void HandleStartMovingWithCoordinate(int32_t offsetX, int32_t offsetY,
79         int32_t pointerPosX, int32_t pointerPosY, const WSRect& winRect);
80     void StopMoving();
81 
82 private:
83     struct MoveDragProperty {
84         int32_t pointerId_ = -1;
85         int32_t pointerType_ = -1;
86         int32_t originalPointerPosX_ = -1;
87         int32_t originalPointerPosY_ = -1;
88         // the x coordinate of the pointer related to the active window
89         int32_t originalPointerWindowX_ = -1;
90         // the y coordinate of the pointer related to the active window
91         int32_t originalPointerWindowY_ = -1;
92         WSRect originalRect_ = { 0, 0, 0, 0 };
93         WSRect targetRect_ = { 0, 0, 0, 0 };
94 
isEmptyMoveDragProperty95         bool isEmpty() const
96         {
97             return (pointerId_ == -1 && originalPointerPosX_ == -1 && originalPointerPosY_ == -1);
98         }
99     };
100 
101     struct MoveTempProperty {
102         int32_t pointerId_ = -1;
103         int32_t pointerType_ = -1;
104         int32_t lastDownPointerPosX_ = -1;
105         int32_t lastDownPointerPosY_ = -1;
106         int32_t lastDownPointerWindowX_ = -1;
107         int32_t lastDownPointerWindowY_ = -1;
108         int32_t lastMovePointerPosX_ = -1;
109         int32_t lastMovePointerPosY_ = -1;
110 
isEmptyMoveTempProperty111         bool isEmpty() const
112         {
113             return (pointerId_ == -1 && lastDownPointerPosX_ == -1 && lastDownPointerPosY_ == -1);
114         }
115     };
116 
117     enum AxisType { UNDEFINED, X_AXIS, Y_AXIS };
118     constexpr static float NEAR_ZERO = 0.001f;
119 
120     bool CalcMoveTargetRect(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, const WSRect& originalRect);
121     bool CalcMoveInputBarRect(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, const WSRect& originalRect);
122     void AdjustTargetPositionByAvailableArea(int32_t& moveDragFinalX, int32_t& moveDragFinalY);
123     void InitializeMoveDragPropertyNotValid(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
124                                             const WSRect& originalRect);
125     bool CheckAndInitializeMoveDragProperty(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
126                                             const WSRect& originalRect);
127     void CalcMoveForSameDisplay(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
128                                 int32_t& moveDragFinalX, int32_t& moveDragFinalY);
129     bool EventDownInit(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, const WSRect& originalRect,
130         const sptr<WindowSessionProperty> property, const SystemSessionConfig& sysConfig);
131     AreaType GetAreaType(int32_t pointWinX, int32_t pointWinY, int32_t sourceType, const WSRect& rect);
132     WSRect CalcFreeformTargetRect(AreaType type, int32_t tranX, int32_t tranY, WSRect originalRect);
133     WSRect CalcFixedAspectRatioTargetRect(AreaType type, int32_t tranX, int32_t tranY, float aspectRatio,
134         WSRect originalRect);
135     void CalcFreeformTranslateLimits(AreaType type);
136     void CalcFixedAspectRatioTranslateLimits(AreaType type, AxisType axis);
137     void FixTranslateByLimits(int32_t& tranX, int32_t& tranY);
138     bool InitMainAxis(AreaType type, int32_t tranX, int32_t tranY);
139     void ConvertXYByAspectRatio(int32_t& tx, int32_t& ty, float aspectRatio);
140     void ProcessSessionRectChange(const SizeChangeReason& reason);
141     void InitDecorValue(const sptr<WindowSessionProperty> property, const SystemSessionConfig& sysConfig);
142 
143     float GetVirtualPixelRatio() const;
144     void UpdateDragType(int32_t startPointPosX, int32_t startPointPosY);
145     bool IsPointInDragHotZone(int32_t startPointPosX, int32_t startPointPosY,
146         int32_t sourceType, const WSRect& winRect);
147     void CalculateStartRectExceptHotZone(float vpr, const WSRect& winRect);
148     WSError UpdateMoveTempProperty(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
149     bool CheckDragEventLegal(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
150         const sptr<WindowSessionProperty> property);
151     void ResSchedReportData(int32_t type, bool onOffTag);
152     void NotifyWindowInputPidChange(bool isServerPid);
153 
154     bool isStartMove_ = false;
155     bool isStartDrag_ = false;
156     bool isDecorEnable_ = true;
157     bool hasPointDown_ = false;
158     float aspectRatio_ = 0.0f;
159     float vpr_ = 1.0f;
160     int32_t minTranX_ = INT32_MIN;
161     int32_t minTranY_ = INT32_MIN;
162     int32_t maxTranX_ = INT32_MAX;
163     int32_t maxTranY_ = INT32_MAX;
164     AreaType type_ = AreaType::UNDEFINED;
165     AxisType mainMoveAxis_ = AxisType::UNDEFINED;
166     WindowLimits limits_;
167     MoveDragProperty moveDragProperty_;
168     MoveDragCallback moveDragCallback_;
169     int32_t persistentId_;
170 
171     enum class DragType : uint32_t {
172         DRAG_UNDEFINED,
173         DRAG_LEFT_OR_RIGHT,
174         DRAG_BOTTOM_OR_TOP,
175         DRAG_LEFT_TOP_CORNER,
176         DRAG_RIGHT_TOP_CORNER,
177     };
178     const std::map<DragType, uint32_t> STYLEID_MAP = {
179         {DragType::DRAG_UNDEFINED,        MMI::MOUSE_ICON::DEFAULT},
180         {DragType::DRAG_BOTTOM_OR_TOP,    MMI::MOUSE_ICON::NORTH_SOUTH},
181         {DragType::DRAG_LEFT_OR_RIGHT,    MMI::MOUSE_ICON::WEST_EAST},
182         {DragType::DRAG_LEFT_TOP_CORNER,  MMI::MOUSE_ICON::NORTH_WEST_SOUTH_EAST},
183         {DragType::DRAG_RIGHT_TOP_CORNER, MMI::MOUSE_ICON::NORTH_EAST_SOUTH_WEST}
184     };
185     Rect rectExceptFrame_ { 0, 0, 0, 0 };
186     Rect rectExceptCorner_ { 0, 0, 0, 0 };
187     uint32_t mouseStyleID_ = 0;
188     DragType dragType_ = DragType::DRAG_UNDEFINED;
189     MoveTempProperty moveTempProperty_;
190     DMRect moveAvailableArea_ = {0, 0, 0, 0};
191     DisplayId moveInputBarStartDisplayId_ = DISPLAY_ID_INVALID;
192     bool moveInputBarFlag_ = false;
193 
194     void UpdateHotAreaType(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
195     void ProcessWindowDragHotAreaFunc(bool flag, const SizeChangeReason& reason);
196     uint32_t windowDragHotAreaType_ = WINDOW_HOT_AREA_TYPE_UNDEFINED;
197     NotifyWindowDragHotAreaFunc windowDragHotAreaFunc_;
198     NotifyWindowPidChangeCallback pidChangeCallback_;
199 
200     const std::map<AreaType, Gravity> GRAVITY_MAP = {
201         {AreaType::LEFT,          Gravity::RIGHT},
202         {AreaType::TOP,           Gravity::BOTTOM},
203         {AreaType::RIGHT,         Gravity::LEFT},
204         {AreaType::BOTTOM,        Gravity::TOP},
205         {AreaType::LEFT_TOP,      Gravity::BOTTOM_RIGHT},
206         {AreaType::RIGHT_TOP,     Gravity::BOTTOM_LEFT},
207         {AreaType::RIGHT_BOTTOM,  Gravity::TOP_LEFT},
208         {AreaType::LEFT_BOTTOM,   Gravity::TOP_RIGHT}
209     };
210 };
211 } // namespace OHOS::Rosen
212 #endif // OHOS_ROSEN_WINDOW_SCENE_MOVE_DRAG_CONTROLLER_H
213