8#include <QtCore/QDebug>
13static constexpr const quint8 kDefaultResizeBorderThickness = 8;
15static Qt::CursorShape calculateCursorShape(
const QWindow *
window,
const QPoint &pos)
20 return Qt::ArrowCursor;
24 return Qt::ArrowCursor;
26 if (
window->visibility() != QWindow::Windowed) {
27 return Qt::ArrowCursor;
29 const int x = pos.x();
30 const int y = pos.y();
31 const int w =
window->width();
32 const int h =
window->height();
33 if (((x < kDefaultResizeBorderThickness) && (y < kDefaultResizeBorderThickness)) ||
34 ((x >= (w - kDefaultResizeBorderThickness)) && (y >= (h - kDefaultResizeBorderThickness)))) {
35 return Qt::SizeFDiagCursor;
37 if (((x >= (w - kDefaultResizeBorderThickness)) && (y < kDefaultResizeBorderThickness)) ||
38 ((x < kDefaultResizeBorderThickness) && (y >= (h - kDefaultResizeBorderThickness)))) {
39 return Qt::SizeBDiagCursor;
41 if ((x < kDefaultResizeBorderThickness) || (x >= (w - kDefaultResizeBorderThickness))) {
42 return Qt::SizeHorCursor;
44 if ((y < kDefaultResizeBorderThickness) || (y >= (h - kDefaultResizeBorderThickness))) {
45 return Qt::SizeVerCursor;
47 return Qt::ArrowCursor;
51static inline Qt::Edges calculateWindowEdges(
const QWindow *
window,
const QPoint &pos)
62 if (
window->visibility() != QWindow::Windowed) {
66 const int x = pos.x();
67 const int y = pos.y();
68 if (x < kDefaultResizeBorderThickness) {
69 edges |= Qt::LeftEdge;
71 if (x >= (
window->width() - kDefaultResizeBorderThickness)) {
72 edges |= Qt::RightEdge;
74 if (y < kDefaultResizeBorderThickness) {
77 if (y >= (
window->height() - kDefaultResizeBorderThickness)) {
78 edges |= Qt::BottomEdge;
84class QtWindowEventFilter :
public WindowSharedEventFilter
87 explicit QtWindowEventFilter(WindowContext *context);
88 ~QtWindowEventFilter()
override;
98 bool sharedEventFilter(QObject *
object, QEvent *event)
override;
100 WindowContext *m_context;
101 bool m_cursorShapeChanged;
102 WindowStatus m_windowStatus;
105QtWindowEventFilter::QtWindowEventFilter(WindowContext *context)
107 , m_cursorShapeChanged(false)
108 , m_windowStatus(Idle)
110 m_context->installSharedEventFilter(
this);
113QtWindowEventFilter::~QtWindowEventFilter() =
default;
115bool QtWindowEventFilter::sharedEventFilter(QObject *obj, QEvent *event)
119 auto host = m_context->host();
120 auto window = m_context->window();
121 auto delegate = m_context->delegate();
123 auto type =
event->type();
125 if (type == QEvent::WindowStateChange) {
126 Qt::WindowStates windowState = delegate->getWindowState(host);
127 if (windowState & Qt::WindowMaximized) {
129 window->setGeometry(
window->screen()->availableGeometry());
134 if (type < QEvent::MouseButtonPress || type > QEvent::MouseMove) {
137 auto me =
static_cast<const QMouseEvent *
>(event);
138 bool fixedSize = delegate->isHostSizeFixed(host);
143 bool inTitleBar = m_context->isInTitleBarDraggableArea(scenePos);
145 case QEvent::MouseButtonPress: {
146 switch (me->button()) {
147 case Qt::LeftButton: {
149 Qt::Edges edges = calculateWindowEdges(
window, scenePos);
150 if (edges != Qt::Edges()) {
151 m_windowStatus = Resizing;
152 startSystemResize(
window, edges);
161 m_windowStatus = PreparingMove;
167 case Qt::RightButton: {
168 m_context->showSystemMenu(globalPos);
174 m_windowStatus = WaitingRelease;
178 case QEvent::MouseButtonRelease: {
179 switch (m_windowStatus) {
183 m_windowStatus = Idle;
187 case WaitingRelease: {
188 m_windowStatus = Idle;
202 case QEvent::MouseMove: {
203 switch (m_windowStatus) {
207 case PreparingMove: {
208 m_windowStatus = Moving;
215 const Qt::CursorShape shape = calculateCursorShape(
window, scenePos);
216 if (shape == Qt::ArrowCursor) {
217 if (m_cursorShapeChanged) {
218 delegate->restoreCursorShape(host);
219 m_cursorShapeChanged =
false;
222 delegate->setCursorShape(host, shape);
223 m_cursorShapeChanged =
true;
234 case QEvent::MouseButtonDblClick: {
235 if (me->button() == Qt::LeftButton && inTitleBar && !fixedSize) {
236 Qt::WindowStates windowState = delegate->getWindowState(host);
237 if (!(windowState & Qt::WindowFullScreen)) {
238 if (windowState & Qt::WindowMaximized) {
239 delegate->setWindowState(host, windowState & ~Qt::WindowMaximized);
241 delegate->setWindowState(host, windowState | Qt::WindowMaximized);
256WindowContextQt::WindowContextQt()
266 return QStringLiteral(
"qt");
std::unique_ptr< WindowSharedEventFilter > qtWindowEventFilter
void virtual_hook(int id, void *data) override
QString key() const override
void winIdChanged() override
~WindowContextQt() override
virtual void virtual_hook(int id, void *data)
std::unique_ptr< WindowItemDelegate > m_delegate
QPoint getMouseEventGlobalPos(const QMouseEvent *event)
QPoint getMouseEventScenePos(const QMouseEvent *event)
#define QX_WINDOW_END_NAMESPACE
#define QX_WINDOW_BEGIN_NAMESPACE