Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-03-12 16:58:58 +08:00
parent 2881874d7a
commit d0e19a2b1e
6 changed files with 175 additions and 65 deletions

View File

@ -25,6 +25,7 @@
#include "widget.h"
#include <QtCore/qdatetime.h>
#include <QtGui/qpainter.h>
#include <QtGui/qevent.h>
#include <QtWidgets/qboxlayout.h>
#include <QtWidgets/qlabel.h>
#include <QtWidgets/qpushbutton.h>
@ -87,7 +88,7 @@ Widget::~Widget() = default;
void Widget::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
initOnce();
initFramelessHelperOnce();
}
void Widget::timerEvent(QTimerEvent *event)
@ -140,13 +141,30 @@ void Widget::paintEvent(QPaintEvent *event)
#endif
}
void Widget::initOnce()
void Widget::mousePressEvent(QMouseEvent *event)
{
QWidget::mousePressEvent(event);
if (isInTitleBarDraggableArea(event->pos())) {
Utilities::startSystemMove(windowHandle());
}
}
void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
QWidget::mouseDoubleClickEvent(event);
if (isInTitleBarDraggableArea(event->pos())) {
if (m_maximizeButton) {
m_maximizeButton->click();
}
}
}
void Widget::initFramelessHelperOnce()
{
if (m_inited) {
return;
}
m_inited = true;
resetContentsMargins();
FramelessWindowsManager::addWindow(windowHandle());
connect(FramelessWindowsManager::instance(), &FramelessWindowsManager::themeChanged, this, [this](){
updateStyleSheet();
@ -233,9 +251,26 @@ void Widget::setupUi()
mainLayout->addLayout(contentLayout);
mainLayout->addStretch();
setLayout(mainLayout);
resetContentsMargins();
updateStyleSheet();
}
bool Widget::isInTitleBarDraggableArea(const QPoint &pos) const
{
Q_ASSERT(m_titleBarWidget);
Q_ASSERT(m_minimizeButton);
Q_ASSERT(m_maximizeButton);
Q_ASSERT(m_closeButton);
if (!m_titleBarWidget || !m_minimizeButton || !m_maximizeButton || !m_closeButton) {
return false;
}
QRegion draggableArea = {0, 0, m_titleBarWidget->width(), m_titleBarWidget->height()};
draggableArea -= m_minimizeButton->geometry();
draggableArea -= m_maximizeButton->geometry();
draggableArea -= m_closeButton->geometry();
return draggableArea.contains(pos);
}
void Widget::updateStyleSheet()
{
const bool active = isActiveWindow();

View File

@ -44,10 +44,13 @@ protected:
void timerEvent(QTimerEvent *event) override;
void changeEvent(QEvent *event) override;
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
private:
void initOnce();
void initFramelessHelperOnce();
void setupUi();
bool isInTitleBarDraggableArea(const QPoint &pos) const;
private Q_SLOTS:
void updateStyleSheet();

View File

@ -25,65 +25,10 @@
#include "framelesshelper.h"
#include <QtGui/qevent.h>
#include <QtGui/qwindow.h>
#include "framelesswindowsmanager.h"
#include "utilities.h"
FRAMELESSHELPER_BEGIN_NAMESPACE
static constexpr const int g_resizeBorderThickness = kDefaultResizeBorderThicknessAero;
[[nodiscard]] static inline Qt::CursorShape calculateCursorShape
(const QWindow * const window, const QPointF &pos)
{
Q_ASSERT(window);
if (!window) {
return Qt::ArrowCursor;
}
if (window->visibility() != QWindow::Windowed) {
return Qt::ArrowCursor;
}
if (((pos.x() < g_resizeBorderThickness) && (pos.y() < g_resizeBorderThickness))
|| ((pos.x() >= (window->width() - g_resizeBorderThickness)) && (pos.y() >= (window->height() - g_resizeBorderThickness)))) {
return Qt::SizeFDiagCursor;
}
if (((pos.x() >= (window->width() - g_resizeBorderThickness)) && (pos.y() < g_resizeBorderThickness))
|| ((pos.x() < g_resizeBorderThickness) && (pos.y() >= (window->height() - g_resizeBorderThickness)))) {
return Qt::SizeBDiagCursor;
}
if ((pos.x() < g_resizeBorderThickness) || (pos.x() >= (window->width() - g_resizeBorderThickness))) {
return Qt::SizeHorCursor;
}
if ((pos.y() < g_resizeBorderThickness) || (pos.y() >= (window->height() - g_resizeBorderThickness))) {
return Qt::SizeVerCursor;
}
return Qt::ArrowCursor;
}
[[nodiscard]] static inline Qt::Edges calculateWindowEdges
(const QWindow * const window, const QPointF &pos)
{
Q_ASSERT(window);
if (!window) {
return {};
}
if (window->visibility() != QWindow::Windowed) {
return {};
}
Qt::Edges edges = {};
if (pos.x() < g_resizeBorderThickness) {
edges |= Qt::LeftEdge;
}
if (pos.x() >= (window->width() - g_resizeBorderThickness)) {
edges |= Qt::RightEdge;
}
if (pos.y() < g_resizeBorderThickness) {
edges |= Qt::TopEdge;
}
if (pos.y() >= (window->height() - g_resizeBorderThickness)) {
edges |= Qt::BottomEdge;
}
return edges;
}
FramelessHelper::FramelessHelper(QObject *parent) : QObject(parent) {}
FramelessHelper::~FramelessHelper() = default;
@ -133,18 +78,16 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event)
const QPointF localPos = mouseEvent->windowPos();
#endif
if (type == QEvent::MouseMove) {
const Qt::CursorShape cs = calculateCursorShape(window, localPos);
const Qt::CursorShape cs = Utilities::calculateCursorShape(window, localPos);
if (cs == Qt::ArrowCursor) {
window->unsetCursor();
} else {
window->setCursor(cs);
}
} else if (type == QEvent::MouseButtonPress) {
const Qt::Edges edges = calculateWindowEdges(window, localPos);
const Qt::Edges edges = Utilities::calculateWindowEdges(window, localPos);
if (edges != Qt::Edges{}) {
if (!window->startSystemResize(edges)) {
qWarning() << "Current platform doesn't support \"QWindow::startSystemResize()\".";
}
Utilities::startSystemResize(window, edges);
}
}
return false;

View File

@ -26,4 +26,57 @@
FRAMELESSHELPER_BEGIN_NAMESPACE
static constexpr const int g_resizeBorderThickness = kDefaultResizeBorderThicknessAero;
Qt::CursorShape Utilities::calculateCursorShape(const QWindow *window, const QPointF &pos)
{
Q_ASSERT(window);
if (!window) {
return Qt::ArrowCursor;
}
if (window->visibility() != QWindow::Windowed) {
return Qt::ArrowCursor;
}
if (((pos.x() < g_resizeBorderThickness) && (pos.y() < g_resizeBorderThickness))
|| ((pos.x() >= (window->width() - g_resizeBorderThickness)) && (pos.y() >= (window->height() - g_resizeBorderThickness)))) {
return Qt::SizeFDiagCursor;
}
if (((pos.x() >= (window->width() - g_resizeBorderThickness)) && (pos.y() < g_resizeBorderThickness))
|| ((pos.x() < g_resizeBorderThickness) && (pos.y() >= (window->height() - g_resizeBorderThickness)))) {
return Qt::SizeBDiagCursor;
}
if ((pos.x() < g_resizeBorderThickness) || (pos.x() >= (window->width() - g_resizeBorderThickness))) {
return Qt::SizeHorCursor;
}
if ((pos.y() < g_resizeBorderThickness) || (pos.y() >= (window->height() - g_resizeBorderThickness))) {
return Qt::SizeVerCursor;
}
return Qt::ArrowCursor;
}
Qt::Edges Utilities::calculateWindowEdges(const QWindow *window, const QPointF &pos)
{
Q_ASSERT(window);
if (!window) {
return {};
}
if (window->visibility() != QWindow::Windowed) {
return {};
}
Qt::Edges edges = {};
if (pos.x() < g_resizeBorderThickness) {
edges |= Qt::LeftEdge;
}
if (pos.x() >= (window->width() - g_resizeBorderThickness)) {
edges |= Qt::RightEdge;
}
if (pos.y() < g_resizeBorderThickness) {
edges |= Qt::TopEdge;
}
if (pos.y() >= (window->height() - g_resizeBorderThickness)) {
edges |= Qt::BottomEdge;
}
return edges;
}
FRAMELESSHELPER_END_NAMESPACE

View File

@ -32,6 +32,11 @@ FRAMELESSHELPER_BEGIN_NAMESPACE
namespace Utilities
{
[[nodiscard]] FRAMELESSHELPER_API Qt::CursorShape calculateCursorShape(const QWindow *window, const QPointF &pos);
[[nodiscard]] FRAMELESSHELPER_API Qt::Edges calculateWindowEdges(const QWindow *window, const QPointF &pos);
FRAMELESSHELPER_API void startSystemMove(QWindow *window);
FRAMELESSHELPER_API void startSystemResize(QWindow *window, const Qt::Edges edges);
#ifdef Q_OS_WINDOWS
[[nodiscard]] FRAMELESSHELPER_API bool isWin8OrGreater();
[[nodiscard]] FRAMELESSHELPER_API bool isWin8Point1OrGreater();

View File

@ -115,6 +115,34 @@ FRAMELESSHELPER_BEGIN_NAMESPACE
}
}
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
[[nodiscard]] static inline DWORD qtEdgesToWin32Orientation(const Qt::Edges edges)
{
if (edges == Qt::Edges{}) {
return 0;
}
if (edges == (Qt::LeftEdge)) {
return 0xF001; // SC_SIZELEFT
} else if (edges == (Qt::RightEdge)) {
return 0xF002; // SC_SIZERIGHT
} else if (edges == (Qt::TopEdge)) {
return 0xF003; // SC_SIZETOP
} else if (edges == (Qt::TopEdge | Qt::LeftEdge)) {
return 0xF004; // SC_SIZETOPLEFT
} else if (edges == (Qt::TopEdge | Qt::RightEdge)) {
return 0xF005; // SC_SIZETOPRIGHT
} else if (edges == (Qt::BottomEdge)) {
return 0xF006; // SC_SIZEBOTTOM
} else if (edges == (Qt::BottomEdge | Qt::LeftEdge)) {
return 0xF007; // SC_SIZEBOTTOMLEFT
} else if (edges == (Qt::BottomEdge | Qt::RightEdge)) {
return 0xF008; // SC_SIZEBOTTOMRIGHT
} else {
return 0xF000; // SC_SIZE
}
}
#endif
bool Utilities::isWin8OrGreater()
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
@ -766,4 +794,47 @@ void Utilities::fixupQtInternals(const WId winId)
}
}
void Utilities::startSystemMove(QWindow *window)
{
Q_ASSERT(window);
if (!window) {
return;
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
window->startSystemMove();
#else
if (ReleaseCapture() == FALSE) {
qWarning() << getSystemErrorMessage(QStringLiteral("ReleaseCapture"));
return;
}
const auto hwnd = reinterpret_cast<HWND>(window->winId());
if (PostMessageW(hwnd, WM_SYSCOMMAND, 0xF012 /*SC_DRAGMOVE*/, 0) == FALSE) {
qWarning() << getSystemErrorMessage(QStringLiteral("PostMessageW"));
}
#endif
}
void Utilities::startSystemResize(QWindow *window, const Qt::Edges edges)
{
Q_ASSERT(window);
if (!window) {
return;
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
window->startSystemResize(edges);
#else
if (edges == Qt::Edges{}) {
return;
}
if (ReleaseCapture() == FALSE) {
qWarning() << getSystemErrorMessage(QStringLiteral("ReleaseCapture"));
return;
}
const auto hwnd = reinterpret_cast<HWND>(window->winId());
if (PostMessageW(hwnd, WM_SYSCOMMAND, qtEdgesToWin32Orientation(edges), 0) == FALSE) {
qWarning() << getSystemErrorMessage(QStringLiteral("PostMessageW"));
}
#endif
}
FRAMELESSHELPER_END_NAMESPACE