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 SetOriginalValue(int32_t pointerId, int32_t pointerType,
55         int32_t pointerPosX, int32_t pointerPosY, const WSRect& winRect);
56     void SetAspectRatio(float ratio);
57     bool ConsumeMoveEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, const WSRect& originalRect);
58     bool ConsumeDragEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, const WSRect& originalRect,
59         const sptr<WindowSessionProperty> property, const SystemSessionConfig& sysConfig);
60     void CalcFirstMoveTargetRect(const WSRect& windowRect, bool isFullToFloating);
61     WSRect GetFullScreenToFloatingRect(const WSRect& originalRect, const WSRect& windowRect);
62     int32_t GetOriginalPointerPosX();
63     int32_t GetOriginalPointerPosY();
64     void SetWindowDragHotAreaFunc(const NotifyWindowDragHotAreaFunc& func);
65     void UpdateGravityWhenDrag(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
66         const std::shared_ptr<RSSurfaceNode>& surfaceNode);
67     void OnLostFocus();
68 
69 private:
70     struct MoveDragProperty {
71         int32_t pointerId_ = -1;
72         int32_t pointerType_ = -1;
73         int32_t originalPointerPosX_ = -1;
74         int32_t originalPointerPosY_ = -1;
75         WSRect originalRect_ = { 0, 0, 0, 0 };
76         WSRect targetRect_ = { 0, 0, 0, 0 };
77 
isEmptyMoveDragProperty78         bool isEmpty() const
79         {
80             return (pointerId_ == -1 && originalPointerPosX_ == -1 && originalPointerPosY_ == -1);
81         }
82     };
83 
84     struct MoveTempProperty {
85         int32_t pointerId_ = -1;
86         int32_t pointerType_ = -1;
87         int32_t lastDownPointerPosX_ = -1;
88         int32_t lastDownPointerPosY_ = -1;
89         int32_t lastDownPointerWindowX_ = -1;
90         int32_t lastDownPointerWindowY_ = -1;
91         int32_t lastMovePointerPosX_ = -1;
92         int32_t lastMovePointerPosY_ = -1;
93 
isEmptyMoveTempProperty94         bool isEmpty() const
95         {
96             return (pointerId_ == -1 && lastDownPointerPosX_ == -1 && lastDownPointerPosY_ == -1);
97         }
98     };
99 
100     enum AxisType { UNDEFINED, X_AXIS, Y_AXIS };
101     constexpr static float NEAR_ZERO = 0.001f;
102 
103     bool CalcMoveTargetRect(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, const WSRect& originalRect);
104     bool EventDownInit(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, const WSRect& originalRect,
105         const sptr<WindowSessionProperty> property, const SystemSessionConfig& sysConfig);
106     AreaType GetAreaType(int32_t pointWinX, int32_t pointWinY, int32_t sourceType, const WSRect& rect);
107     WSRect CalcFreeformTargetRect(AreaType type, int32_t tranX, int32_t tranY, WSRect originalRect);
108     WSRect CalcFixedAspectRatioTargetRect(AreaType type, int32_t tranX, int32_t tranY, float aspectRatio,
109         WSRect originalRect);
110     void CalcFreeformTranslateLimits(AreaType type);
111     void CalcFixedAspectRatioTranslateLimits(AreaType type, AxisType axis);
112     void FixTranslateByLimits(int32_t& tranX, int32_t& tranY);
113     bool InitMainAxis(AreaType type, int32_t tranX, int32_t tranY);
114     void ConvertXYByAspectRatio(int32_t& tx, int32_t& ty, float aspectRatio);
115     void ProcessSessionRectChange(const SizeChangeReason& reason);
116     void InitDecorValue(const sptr<WindowSessionProperty> property, const SystemSessionConfig& sysConfig);
117 
118     float GetVirtualPixelRatio() const;
119     void UpdateDragType(int32_t startPointPosX, int32_t startPointPosY);
120     bool IsPointInDragHotZone(int32_t startPointPosX, int32_t startPointPosY,
121         int32_t sourceType, const WSRect& winRect);
122     void CalculateStartRectExceptHotZone(float vpr, const WSRect& winRect);
123     WSError UpdateMoveTempProperty(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
124     bool CheckDragEventLegal(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
125         const sptr<WindowSessionProperty> property);
126     void ResSchedReportData(int32_t type, bool onOffTag);
127     void NotifyWindowInputPidChange(bool isServerPid);
128 
129     bool isStartMove_ = false;
130     bool isStartDrag_ = false;
131     bool isDecorEnable_ = true;
132     bool hasPointDown_ = false;
133     float aspectRatio_ = 0.0f;
134     float vpr_ = 1.0f;
135     int32_t minTranX_ = INT32_MIN;
136     int32_t minTranY_ = INT32_MIN;
137     int32_t maxTranX_ = INT32_MAX;
138     int32_t maxTranY_ = INT32_MAX;
139     AreaType type_ = AreaType::UNDEFINED;
140     AxisType mainMoveAxis_ = AxisType::UNDEFINED;
141     WindowLimits limits_;
142     MoveDragProperty moveDragProperty_;
143     MoveDragCallback moveDragCallback_;
144     int32_t persistentId_;
145 
146     enum class DragType : uint32_t {
147         DRAG_UNDEFINED,
148         DRAG_LEFT_OR_RIGHT,
149         DRAG_BOTTOM_OR_TOP,
150         DRAG_LEFT_TOP_CORNER,
151         DRAG_RIGHT_TOP_CORNER,
152     };
153     const std::map<DragType, uint32_t> STYLEID_MAP = {
154         {DragType::DRAG_UNDEFINED,        MMI::MOUSE_ICON::DEFAULT},
155         {DragType::DRAG_BOTTOM_OR_TOP,    MMI::MOUSE_ICON::NORTH_SOUTH},
156         {DragType::DRAG_LEFT_OR_RIGHT,    MMI::MOUSE_ICON::WEST_EAST},
157         {DragType::DRAG_LEFT_TOP_CORNER,  MMI::MOUSE_ICON::NORTH_WEST_SOUTH_EAST},
158         {DragType::DRAG_RIGHT_TOP_CORNER, MMI::MOUSE_ICON::NORTH_EAST_SOUTH_WEST}
159     };
160     Rect rectExceptFrame_ { 0, 0, 0, 0 };
161     Rect rectExceptCorner_ { 0, 0, 0, 0 };
162     uint32_t mouseStyleID_ = 0;
163     DragType dragType_ = DragType::DRAG_UNDEFINED;
164     MoveTempProperty moveTempProperty_;
165 
166     void UpdateHotAreaType(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
167     void ProcessWindowDragHotAreaFunc(bool flag, const SizeChangeReason& reason);
168     uint32_t windowDragHotAreaType_ = WINDOW_HOT_AREA_TYPE_UNDEFINED;
169     NotifyWindowDragHotAreaFunc windowDragHotAreaFunc_;
170     NotifyWindowPidChangeCallback pidChangeCallback_;
171 
172     const std::map<AreaType, Gravity> GRAVITY_MAP = {
173         {AreaType::LEFT,          Gravity::RIGHT},
174         {AreaType::TOP,           Gravity::BOTTOM},
175         {AreaType::RIGHT,         Gravity::LEFT},
176         {AreaType::BOTTOM,        Gravity::TOP},
177         {AreaType::LEFT_TOP,      Gravity::BOTTOM_RIGHT},
178         {AreaType::RIGHT_TOP,     Gravity::BOTTOM_LEFT},
179         {AreaType::RIGHT_BOTTOM,  Gravity::TOP_LEFT},
180         {AreaType::LEFT_BOTTOM,   Gravity::TOP_RIGHT}
181     };
182 };
183 } // namespace OHOS::Rosen
184 #endif // OHOS_ROSEN_WINDOW_SCENE_MOVE_DRAG_CONTROLLER_H
185