Sine Striker
2023-12-25 29901fc2a97eedd3c914f807d1819c9ea7e69973
examples/mainwindow/mainwindow.cpp
@@ -8,13 +8,21 @@
#include <QtWidgets/QApplication>
#include <QtWidgets/QStyle>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QActionGroup>
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#  include <QtGui/QActionGroup>
#else
#  include <QtWidgets/QActionGroup>
#endif
#include <QWKCore/styleagent.h>
#include <QWKWidgets/widgetwindowagent.h>
#include <QWKStyleSupport/styleagent.h>
#include <widgetframe/windowbar.h>
#include <widgetframe/windowbutton.h>
#ifdef Q_OS_WINDOWS
#  include <QWKCore/qwindowkit_windows.h>
#endif
class ClockWidget : public QLabel {
public:
@@ -33,8 +41,7 @@
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    installWindowAgent();
    styleAgent = new QWK::StyleAgent(this);
    installStyleAgent();
    auto clockWidget = new ClockWidget();
    clockWidget->setObjectName(QStringLiteral("clock-widget"));
@@ -112,6 +119,16 @@
    windowAgent = new QWK::WidgetWindowAgent(this);
    windowAgent->setup(this);
#ifdef Q_OS_WIN
    if (QWK::IsWindows10OrGreater_Real() && !QWK::IsWindows11OrGreater_Real()) {
        // windowAgent->setWindowAttribute(QStringLiteral("dark-mode"), true);
        // https://github.com/microsoft/terminal/blob/71a6f26e6ece656084e87de1a528c4a8072eeabd/src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp#L940
        // Must call DWM API to extend top frame to client area
        windowAgent->setWindowAttribute(QStringLiteral("extra-margins"), true);
    }
#endif
    // 2. Construct your title bar
    auto menuBar = [this]() {
        auto menuBar = new QMenuBar();
@@ -139,36 +156,40 @@
#ifdef Q_OS_WIN
        auto dwmBlurAction = new QAction(tr("Enable DWM blur"), menuBar);
        dwmBlurAction->setCheckable(true);
        connect(dwmBlurAction, &QAction::triggered, this, [this](bool checked){
            QWindow *w = windowHandle();
            styleAgent->setWindowAttribute(w, QStringLiteral("dwm-blur"), checked);
        connect(dwmBlurAction, &QAction::triggered, this, [this](bool checked) {
            if (!windowAgent->setWindowAttribute(QStringLiteral("dwm-blur"), checked)) {
                return;
            }
            setProperty("custom-style", checked);
            style()->polish(this);
        });
        auto acrylicAction = new QAction(tr("Enable acrylic material"), menuBar);
        acrylicAction->setCheckable(true);
        connect(acrylicAction, &QAction::triggered, this, [this](bool checked){
            QWindow *w = windowHandle();
            styleAgent->setWindowAttribute(w, QStringLiteral("acrylic-material"), QColor());
        connect(acrylicAction, &QAction::triggered, this, [this](bool checked) {
            if (!windowAgent->setWindowAttribute(QStringLiteral("acrylic-material"), true)) {
                return;
            }
            setProperty("custom-style", checked);
            style()->polish(this);
        });
        auto micaAction = new QAction(tr("Enable mica"), menuBar);
        micaAction->setCheckable(true);
        connect(micaAction, &QAction::triggered, this, [this](bool checked){
            QWindow *w = windowHandle();
            styleAgent->setWindowAttribute(w, QStringLiteral("mica"), checked);
        connect(micaAction, &QAction::triggered, this, [this](bool checked) {
            if (!windowAgent->setWindowAttribute(QStringLiteral("mica"), checked)) {
                return;
            }
            setProperty("custom-style", checked);
            style()->polish(this);
        });
        auto micaAltAction = new QAction(tr("Enable mica alt"), menuBar);
        micaAltAction->setCheckable(true);
        connect(micaAltAction, &QAction::triggered, this, [this](bool checked){
            QWindow *w = windowHandle();
            styleAgent->setWindowAttribute(w, QStringLiteral("mica-alt"), checked);
        connect(micaAltAction, &QAction::triggered, this, [this](bool checked) {
            if (!windowAgent->setWindowAttribute(QStringLiteral("mica-alt"), checked)) {
                return;
            }
            setProperty("custom-style", checked);
            style()->polish(this);
        });
@@ -283,10 +304,15 @@
#endif
}
void MainWindow::installStyleAgent() {
    styleAgent = new QWK::StyleAgent(this);
}
void MainWindow::loadStyleSheet(Theme theme) {
    if (!styleSheet().isEmpty() && theme == currentTheme)
        return;
    currentTheme = theme;
    if (QFile qss(theme == Dark ? QStringLiteral(":/dark-style.qss")
                                : QStringLiteral(":/light-style.qss"));
        qss.open(QIODevice::ReadOnly | QIODevice::Text)) {