Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-03-11 17:59:53 +08:00
parent ae65733b2d
commit 0ce352ff22
8 changed files with 39 additions and 142 deletions

View File

@ -96,5 +96,5 @@ target_include_directories(${PROJECT_NAME} PUBLIC
) )
if(BUILD_EXAMPLES) if(BUILD_EXAMPLES)
#add_subdirectory(examples) add_subdirectory(examples)
endif() endif()

View File

@ -1,11 +1,11 @@
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets) find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Quick)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Quick)
if(TARGET Qt${QT_VERSION_MAJOR}::Widgets) if(TARGET Qt${QT_VERSION_MAJOR}::Widgets)
add_subdirectory(widget) add_subdirectory(widget)
add_subdirectory(mainwindow) #add_subdirectory(mainwindow)
endif() endif()
if(TARGET Qt${QT_VERSION_MAJOR}::Quick) if(TARGET Qt${QT_VERSION_MAJOR}::Quick)
add_subdirectory(quick) #add_subdirectory(quick)
endif() endif()

View File

@ -27,7 +27,6 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

View File

@ -23,14 +23,12 @@
*/ */
#include "widget.h" #include "widget.h"
#include <QtCore/qdebug.h>
#include <QtCore/qdatetime.h> #include <QtCore/qdatetime.h>
#include <QtGui/qpainter.h> #include <QtGui/qpainter.h>
#include <QtWidgets/qboxlayout.h> #include <QtWidgets/qboxlayout.h>
#include <QtWidgets/qlabel.h> #include <QtWidgets/qlabel.h>
#include <QtWidgets/qpushbutton.h> #include <QtWidgets/qpushbutton.h>
#include "../../utilities.h" #include <framelesswindowsmanager.h>
#include "../../framelesswindowsmanager.h"
FRAMELESSHELPER_USE_NAMESPACE FRAMELESSHELPER_USE_NAMESPACE
@ -80,8 +78,8 @@ Widget::Widget(QWidget *parent) : QWidget(parent)
{ {
setAttribute(Qt::WA_DontCreateNativeAncestors); setAttribute(Qt::WA_DontCreateNativeAncestors);
createWinId(); createWinId();
setupUi(); //setupUi();
startTimer(500); //startTimer(500);
} }
Widget::~Widget() = default; Widget::~Widget() = default;
@ -92,18 +90,7 @@ void Widget::showEvent(QShowEvent *event)
static bool inited = false; static bool inited = false;
if (!inited) { if (!inited) {
inited = true; inited = true;
QWindow *win = windowHandle(); FramelessWindowsManager::addWindow(windowHandle());
Q_ASSERT(win);
if (!win) {
qFatal("Failed to retrieve the window handle.");
return;
}
FramelessWindowsManager::addWindow(win);
FramelessWindowsManager::setHitTestVisible(win, m_minimizeButton, true);
FramelessWindowsManager::setHitTestVisible(win, m_maximizeButton, true);
FramelessWindowsManager::setHitTestVisible(win, m_closeButton, true);
const auto margin = static_cast<int>(qRound(frameBorderThickness()));
setContentsMargins(margin, margin, margin, margin);
} }
} }
@ -118,6 +105,7 @@ void Widget::timerEvent(QTimerEvent *event)
void Widget::changeEvent(QEvent *event) void Widget::changeEvent(QEvent *event)
{ {
QWidget::changeEvent(event); QWidget::changeEvent(event);
#if 0
bool shouldUpdate = false; bool shouldUpdate = false;
if (event->type() == QEvent::WindowStateChange) { if (event->type() == QEvent::WindowStateChange) {
if (isMaximized() || isFullScreen()) { if (isMaximized() || isFullScreen()) {
@ -135,45 +123,17 @@ void Widget::changeEvent(QEvent *event)
if (shouldUpdate) { if (shouldUpdate) {
updateStyleSheet(); updateStyleSheet();
} }
#endif
} }
void Widget::paintEvent(QPaintEvent *event) void Widget::paintEvent(QPaintEvent *event)
{ {
QWidget::paintEvent(event); QWidget::paintEvent(event);
if ((windowState() == Qt::WindowNoState)
#ifdef Q_OS_WINDOWS
&& !Utilities::isWin11OrGreater()
#endif
) {
const qreal borderThickness = frameBorderThickness();
const auto w = static_cast<qreal>(width());
const auto h = static_cast<qreal>(height());
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
using BorderLines = QList<QLineF>;
#else
using BorderLines = QVector<QLineF>;
#endif
const BorderLines lines = {
{0, 0, w, 0},
{w - borderThickness, 0, w - borderThickness, h},
{w, h - borderThickness, 0, h - borderThickness},
{0, h, 0, 0}
};
const ColorizationArea area = Utilities::getColorizationArea();
const bool colorizedBorder = ((area == ColorizationArea::TitleBar_WindowBorder)
|| (area == ColorizationArea::All));
const QColor borderColor = (isActiveWindow() ? (colorizedBorder ? Utilities::getColorizationColor() : Qt::black) : Qt::darkGray);
QPainter painter(this);
painter.save();
painter.setRenderHint(QPainter::Antialiasing, false);
painter.setPen({borderColor, borderThickness});
painter.drawLines(lines);
painter.restore();
}
} }
void Widget::setupUi() void Widget::setupUi()
{ {
#if 0
setObjectName(QStringLiteral("MainWidget")); setObjectName(QStringLiteral("MainWidget"));
setWindowTitle(tr("Hello, World!")); setWindowTitle(tr("Hello, World!"));
resize(800, 600); resize(800, 600);
@ -205,7 +165,6 @@ void Widget::setupUi()
m_closeButton->setObjectName(QStringLiteral("CloseButton")); m_closeButton->setObjectName(QStringLiteral("CloseButton"));
connect(m_closeButton, &QPushButton::clicked, this, &Widget::close); connect(m_closeButton, &QPushButton::clicked, this, &Widget::close);
updateSystemButtonIcons(); updateSystemButtonIcons();
updateTitleBarSize();
const auto titleBarLayout = new QHBoxLayout(m_titleBarWidget); const auto titleBarLayout = new QHBoxLayout(m_titleBarWidget);
titleBarLayout->setContentsMargins(0, 0, 0, 0); titleBarLayout->setContentsMargins(0, 0, 0, 0);
titleBarLayout->setSpacing(0); titleBarLayout->setSpacing(0);
@ -238,66 +197,16 @@ void Widget::setupUi()
mainLayout->addStretch(); mainLayout->addStretch();
setLayout(mainLayout); setLayout(mainLayout);
updateStyleSheet(); updateStyleSheet();
#endif
} }
void Widget::updateStyleSheet() void Widget::updateStyleSheet()
{ {
const bool active = isActiveWindow();
const bool dark = Utilities::shouldAppsUseDarkMode();
const ColorizationArea area = Utilities::getColorizationArea();
const bool colorizedTitleBar = ((area == ColorizationArea::TitleBar_WindowBorder)
|| (area == ColorizationArea::All));
const QColor colorizationColor = Utilities::getColorizationColor();
const QColor mainWidgetBackgroundColor = (dark ? systemDarkColor : systemLightColor);
const QColor titleBarWidgetBackgroundColor = [active, colorizedTitleBar, &colorizationColor, dark]{
if (active) {
if (colorizedTitleBar) {
return colorizationColor;
} else {
if (dark) {
return QColor(Qt::black);
} else {
return QColor(Qt::white);
}
}
} else {
if (dark) {
return systemDarkColor;
} else {
return QColor(Qt::white);
}
}
}();
const QColor windowTitleLabelTextColor = (active ? (dark ? Qt::white : Qt::black) : Qt::darkGray);
const QColor clockLabelTextColor = (dark ? Qt::white : Qt::black);
setStyleSheet(QString::fromUtf8(mainStyleSheet)
.arg(mainWidgetBackgroundColor.name(),
titleBarWidgetBackgroundColor.name(),
windowTitleLabelTextColor.name(),
clockLabelTextColor.name()));
update();
}
void Widget::updateTitleBarSize()
{
const QWindow *win = windowHandle();
Q_ASSERT(win);
if (!win) {
return;
}
const int titleBarHeight = Utilities::getSystemMetric(win, SystemMetric::TitleBarHeight, false);
const QSize systemButtonSize = {qRound(static_cast<qreal>(titleBarHeight) * 1.5), titleBarHeight};
m_minimizeButton->setFixedSize(systemButtonSize);
m_minimizeButton->setIconSize(systemButtonSize);
m_maximizeButton->setFixedSize(systemButtonSize);
m_maximizeButton->setIconSize(systemButtonSize);
m_closeButton->setFixedSize(systemButtonSize);
m_closeButton->setIconSize(systemButtonSize);
m_titleBarWidget->setFixedHeight(titleBarHeight);
} }
void Widget::updateSystemButtonIcons() void Widget::updateSystemButtonIcons()
{ {
#if 0
Q_ASSERT(m_minimizeButton); Q_ASSERT(m_minimizeButton);
Q_ASSERT(m_maximizeButton); Q_ASSERT(m_maximizeButton);
Q_ASSERT(m_closeButton); Q_ASSERT(m_closeButton);
@ -312,11 +221,7 @@ void Widget::updateSystemButtonIcons()
m_maximizeButton->setIcon(QIcon(QStringLiteral(":/images/button_maximize_%1.svg").arg(suffix))); m_maximizeButton->setIcon(QIcon(QStringLiteral(":/images/button_maximize_%1.svg").arg(suffix)));
} }
m_closeButton->setIcon(QIcon(QStringLiteral(":/images/button_close_%1.svg").arg(suffix))); m_closeButton->setIcon(QIcon(QStringLiteral(":/images/button_close_%1.svg").arg(suffix)));
} #endif
qreal Widget::frameBorderThickness() const
{
return (static_cast<qreal>(Utilities::getWindowVisibleFrameBorderThickness(winId())) / devicePixelRatioF());
} }
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
@ -325,20 +230,5 @@ bool Widget::nativeEvent(const QByteArray &eventType, void *message, qintptr *re
bool Widget::nativeEvent(const QByteArray &eventType, void *message, long *result) bool Widget::nativeEvent(const QByteArray &eventType, void *message, long *result)
#endif #endif
{ {
if (message) {
if (Utilities::isThemeChanged(message)) {
updateStyleSheet();
updateSystemButtonIcons();
return true;
}
QPointF pos = {};
if (Utilities::isSystemMenuRequested(message, &pos)) {
if (Utilities::showSystemMenu(winId(), pos)) {
return true;
} else {
qWarning() << "Failed to display the system menu.";
}
}
}
return QWidget::nativeEvent(eventType, message, result); return QWidget::nativeEvent(eventType, message, result);
} }

View File

@ -27,8 +27,8 @@
#include <QtWidgets/qwidget.h> #include <QtWidgets/qwidget.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
QT_FORWARD_DECLARE_CLASS(QLabel) class QLabel;
QT_FORWARD_DECLARE_CLASS(QPushButton) class QPushButton;
QT_END_NAMESPACE QT_END_NAMESPACE
class Widget : public QWidget class Widget : public QWidget
@ -53,9 +53,7 @@ protected:
private: private:
void setupUi(); void setupUi();
void updateStyleSheet(); void updateStyleSheet();
void updateTitleBarSize();
void updateSystemButtonIcons(); void updateSystemButtonIcons();
qreal frameBorderThickness() const;
private: private:
QWidget *m_titleBarWidget = nullptr; QWidget *m_titleBarWidget = nullptr;

View File

@ -69,6 +69,8 @@ void FramelessHelperWin::addWindow(QWindow *window)
Utilities::fixupQtInternals(winId); Utilities::fixupQtInternals(winId);
Utilities::updateInternalWindowFrameMargins(window, true); Utilities::updateInternalWindowFrameMargins(window, true);
Utilities::updateWindowFrameMargins(winId, false); Utilities::updateWindowFrameMargins(winId, false);
const bool dark = Utilities::shouldAppsUseDarkMode();
Utilities::updateWindowFrameBorderColor(winId, dark);
} }
void FramelessHelperWin::removeWindow(QWindow *window) void FramelessHelperWin::removeWindow(QWindow *window)

View File

@ -62,21 +62,26 @@ void FramelessWindowsManager::addWindow(QWindow *window)
if (!window) { if (!window) {
return; return;
} }
QMutexLocker locker(&Private::g_manager()->mutex); Private::g_manager()->mutex.lock();
if (Private::g_manager()->qwindow.contains(window)) { if (Private::g_manager()->qwindow.contains(window)) {
Private::g_manager()->mutex.unlock();
return; return;
} }
QVariantHash data = {}; QVariantHash data = {};
data.insert(kWindow, QVariant::fromValue(window)); data.insert(kWindow, QVariant::fromValue(window));
auto qtFramelessHelper = new FramelessHelper(window);
if (g_usePureQtImplementation) { if (g_usePureQtImplementation) {
const auto qtFramelessHelper = new FramelessHelper(window);
qtFramelessHelper->addWindow(window);
data.insert(kFramelessHelper, QVariant::fromValue(qtFramelessHelper)); data.insert(kFramelessHelper, QVariant::fromValue(qtFramelessHelper));
} else {
delete qtFramelessHelper;
qtFramelessHelper = nullptr;
} }
const QUuid uuid = QUuid::createUuid();
Private::g_manager()->data.insert(uuid, data);
Private::g_manager()->qwindow.insert(window, uuid);
Private::g_manager()->winId.insert(window->winId(), uuid);
Private::g_manager()->mutex.unlock();
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
else {
FramelessHelperWin::addWindow(window);
}
// Work-around Win32 multi-monitor artifacts. // Work-around Win32 multi-monitor artifacts.
QObject::connect(window, &QWindow::screenChanged, window, [window](QScreen *screen){ QObject::connect(window, &QWindow::screenChanged, window, [window](QScreen *screen){
Q_UNUSED(screen); Q_UNUSED(screen);
@ -90,10 +95,14 @@ void FramelessWindowsManager::addWindow(QWindow *window)
window->resize(window->size()); window->resize(window->size());
}); });
#endif #endif
const QUuid uuid = QUuid::createUuid(); if (g_usePureQtImplementation && qtFramelessHelper) {
Private::g_manager()->qwindow.insert(window, uuid); qtFramelessHelper->addWindow(window);
Private::g_manager()->winId.insert(window->winId(), uuid); }
Private::g_manager()->data.insert(uuid, data); #ifdef Q_OS_WINDOWS
if (!g_usePureQtImplementation) {
FramelessHelperWin::addWindow(window);
}
#endif
} }
void FramelessWindowsManager::removeWindow(QWindow *window) void FramelessWindowsManager::removeWindow(QWindow *window)

View File

@ -728,8 +728,7 @@ void Utilities::updateWindowFrameBorderColor(const WId winId, const bool dark)
const BOOL value = (dark ? TRUE : FALSE); const BOOL value = (dark ? TRUE : FALSE);
HRESULT hr = pDwmSetWindowAttribute(hwnd, _DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, &value, sizeof(value)); HRESULT hr = pDwmSetWindowAttribute(hwnd, _DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, &value, sizeof(value));
if (FAILED(hr)) { if (FAILED(hr)) {
qWarning() << __getSystemErrorMessage(QStringLiteral("DwmSetWindowAttribute"), hr); // Just eat this error, because it only works on systems before Win10 20H1.
//return;
} }
hr = pDwmSetWindowAttribute(hwnd, _DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value)); hr = pDwmSetWindowAttribute(hwnd, _DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value));
if (FAILED(hr)) { if (FAILED(hr)) {