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