Better window handling on macOS

This commit is contained in:
Victor Tran 2021-02-04 21:32:24 +11:00
parent 4e9d0f0d41
commit a5a4ae336a
4 changed files with 40 additions and 21 deletions

View file

@ -92,11 +92,7 @@ bool tCsdGlobal::csdsEnabled() {
}
bool tCsdGlobal::recommendCsdForPlatform() {
#ifdef Q_OS_MAC
return false;
#else
return true;
#endif
}
tCsdTools::tCsdTools(QObject* parent) : QObject(parent) {
@ -188,6 +184,12 @@ void tCsdTools::installResizeAction(QWidget* widget) {
connect(widget, &QWidget::destroyed, this, QOverload<QObject*>::of(&tCsdTools::removeResizeAction));
widget->installEventFilter(this);
ResizeWidget* resize = new ResizeWidget();
resize->widget = widget;
#ifdef Q_OS_MAC
macInstallResizeAction(widget);
#else
if (tCsdGlobal::csdsEnabled()) {
int border = CsdSizeGrip::borderWidth();
widget->setContentsMargins(border, border, border, border);
@ -196,15 +198,13 @@ void tCsdTools::installResizeAction(QWidget* widget) {
widget->setAttribute(Qt::WA_TranslucentBackground);
}
ResizeWidget* resize = new ResizeWidget();
resize->widget = widget;
for (int i = 0; i < 4; i++) {
CsdSizeGrip* grip = new CsdSizeGrip(i, widget);
resize->sizeGrips[i] = grip;
}
d->resizeWidgets.append(resize);
#endif
}
void tCsdTools::removeResizeAction(QWidget* widget) {
@ -214,9 +214,13 @@ void tCsdTools::removeResizeAction(QWidget* widget) {
widget->removeEventFilter(this);
disconnect(widget, &QWidget::destroyed, this, QOverload<QObject*>::of(&tCsdTools::removeResizeAction));
#ifdef Q_OS_MAC
macRemoveResizeAction(widget);
#else
for (int i = 0; i < 4; i++) {
rw->sizeGrips[i]->deleteLater();
}
#endif
d->resizeWidgets.removeOne(rw);
delete rw;
@ -345,7 +349,7 @@ bool tCsdTools::eventFilter(QObject* watched, QEvent* event) {
//Stop moving the window
widget->setProperty("tcsdtools_action", "none");
}
} else if (event->type() == QEvent::Resize) {
} else if (event->type() == QEvent::Resize || event->type() == QEvent::WindowStateChange) {
QWidget* widget = qobject_cast<QWidget*>(watched);
Q_ASSERT(widget);

View file

@ -88,6 +88,8 @@ class THELIBSSHARED_EXPORT tCsdTools : public QObject {
#ifdef Q_OS_MAC
void macHandleDrag(QPoint screenPos, QWidget* dragWindow);
void macInstallResizeAction(QWidget* widget);
void macRemoveResizeAction(QWidget* widget);
#endif
bool eventFilter(QObject* watched, QEvent* event) override;

View file

@ -26,12 +26,11 @@
#include "tcsdtools.h"
#ifdef HAVE_X11
#include <QX11Info>
#include <X11/Xlib.h>
#include <QX11Info>
#include <X11/Xlib.h>
#endif
CsdSizeGrip::CsdSizeGrip(int side, QWidget *parent) : QWidget(parent)
{
CsdSizeGrip::CsdSizeGrip(int side, QWidget* parent) : QWidget(parent) {
connect(tCsdGlobal::instance(), &tCsdGlobal::csdsEnabledChanged, this, &CsdSizeGrip::csdsEnabledChanged);
this->side = side;
this->parentWidget = parent;
@ -68,14 +67,14 @@ void CsdSizeGrip::resizeGrip() {
}
}
bool CsdSizeGrip::eventFilter(QObject *watched, QEvent *event) {
bool CsdSizeGrip::eventFilter(QObject* watched, QEvent* event) {
if (event->type() == QEvent::Resize || event->type() == QEvent::WindowStateChange) {
resizeGrip();
}
return false;
}
void CsdSizeGrip::paintEvent(QPaintEvent *event) {
void CsdSizeGrip::paintEvent(QPaintEvent* event) {
QPainter painter(this);
painter.setBrush(Qt::transparent);
painter.setBrush(this->palette().color(QPalette::WindowText));
@ -94,10 +93,10 @@ void CsdSizeGrip::paintEvent(QPaintEvent *event) {
}
}
void CsdSizeGrip::mousePressEvent(QMouseEvent *e) {
void CsdSizeGrip::mousePressEvent(QMouseEvent* e) {
if (e->button() == Qt::LeftButton) {
//Resize this window
#ifdef HAVE_X11
#ifdef HAVE_X11
if (QX11Info::isPlatformX11()) {
XEvent event;
event.xclient.type = ClientMessage;
@ -141,15 +140,15 @@ void CsdSizeGrip::mousePressEvent(QMouseEvent *e) {
XSendEvent(QX11Info::display(), QX11Info::appRootWindow(), False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
return;
}
#endif
#endif
startPoint = e->pos();
moving = true;
qWarning() << "No method to initiate a window move.";
qWarning() << "No method to initiate a window resize.";
}
}
void CsdSizeGrip::mouseMoveEvent(QMouseEvent *e) {
void CsdSizeGrip::mouseMoveEvent(QMouseEvent* e) {
if (moving) {
QRect geometry = parentWidget->window()->geometry();
switch (hitTest(startPoint)) {
@ -202,7 +201,7 @@ void CsdSizeGrip::mouseMoveEvent(QMouseEvent *e) {
}
}
void CsdSizeGrip::mouseReleaseEvent(QMouseEvent *e) {
void CsdSizeGrip::mouseReleaseEvent(QMouseEvent* e) {
moving = false;
}

View file

@ -7,7 +7,21 @@ void tCsdTools::macHandleDrag(QPoint screenPos, QWidget* dragWindow) {
CGEventRef cgEvent = CGEventCreateMouseEvent(nullptr, kCGEventLeftMouseDown, screenPos.toCGPoint(), kCGMouseButtonLeft);
NSEvent* nsEvent = [NSEvent eventWithCGEvent:cgEvent];
NSView *view = reinterpret_cast<NSView *>(dragWindow->winId());
NSView* view = reinterpret_cast<NSView*>(dragWindow->winId());
[view.window performWindowDragWithEvent:nsEvent];
CFRelease(cgEvent);
}
void tCsdTools::macInstallResizeAction(QWidget* widget) {
NSView* view = reinterpret_cast<NSView*>(widget->winId());
NSWindowStyleMask styleMask = NSWindowStyleMaskFullSizeContentView | NSWindowStyleMaskUnifiedTitleAndToolbar | NSWindowStyleMaskResizable | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable;
[view.window setStyleMask:styleMask];
}
void tCsdTools::macRemoveResizeAction(QWidget* widget) {
NSView* view = reinterpret_cast<NSView*>(widget->winId());
NSWindowStyleMask styleMask = NSWindowStyleMaskResizable | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable;
[view.window setStyleMask:styleMask];
}