Zhao Yuhang
2024-09-15 f645ae48cda10b80ffe9d83ba9487671c7f535c0
update demo code
6个文件已修改
145 ■■■■■ 已修改文件
examples/mainwindow/main.cpp 26 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/mainwindow/mainwindow.cpp 37 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/mainwindow/mainwindow.h 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/qml/QWKButton.qml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/qml/main.cpp 24 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/qml/main.qml 55 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/mainwindow/main.cpp
@@ -9,34 +9,22 @@
int main(int argc, char *argv[]) {
    qputenv("QT_WIN_DEBUG_CONSOLE", "attach");
    qputenv("QSG_INFO", "1");
#if 0
    qputenv("QT_WIDGETS_RHI", "1");
    qputenv("QSG_RHI_BACKEND", "d3d12");
    qputenv("QSG_RHI_HDR", "scrgb");
    qputenv("QT_QPA_DISABLE_REDIRECTION_SURFACE", "1");
    //qputenv("QT_WIDGETS_HIGHDPI_DOWNSCALE", "1");
    //qputenv("QT_WIDGETS_RHI", "1");
    //qputenv("QSG_RHI_BACKEND", "d3d12");
    //qputenv("QSG_RHI_HDR", "scrgb");
    //qputenv("QT_QPA_DISABLE_REDIRECTION_SURFACE", "1");
    
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(
        Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
    QGuiApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
    QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
    QApplication a(argc, argv);
#if 0 && defined(Q_OS_WINDOWS) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QApplication::setFont([]() {
        QFont f("Microsoft YaHei");
        f.setStyleStrategy(QFont::PreferAntialias);
        f.setPixelSize(15);
        return f;
    }());
#endif
    MainWindow w;
    w.show();
#if 0
    QMainWindow w2;
    w2.show();
#endif
    return a.exec();
}
examples/mainwindow/mainwindow.cpp
@@ -37,11 +37,14 @@
protected:
    void timerEvent(QTimerEvent *event) override {
        QLabel::timerEvent(event);
        setText(QTime::currentTime().toString(QStringLiteral("hh:mm:ss")));
    }
};
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    setAttribute(Qt::WA_DontCreateNativeAncestors);
    installWindowAgent();
#if 1
@@ -128,14 +131,6 @@
    return QMainWindow::event(event);
}
void MainWindow::closeEvent(QCloseEvent *event) {
    // if (!(qApp->keyboardModifiers() & Qt::ControlModifier)) {
    //     QTimer::singleShot(1000, this, &QWidget::show);
    // }
    event->accept();
}
void MainWindow::installWindowAgent() {
    // 1. Setup window agent
    windowAgent = new QWK::WidgetWindowAgent(this);
@@ -166,6 +161,11 @@
        });
#ifdef Q_OS_WIN
        auto noneAction = new QAction(tr("None"), menuBar);
        noneAction->setData(QStringLiteral("none"));
        noneAction->setCheckable(true);
        noneAction->setChecked(true);
        auto dwmBlurAction = new QAction(tr("Enable DWM blur"), menuBar);
        dwmBlurAction->setData(QStringLiteral("dwm-blur"));
        dwmBlurAction->setCheckable(true);
@@ -183,19 +183,27 @@
        micaAltAction->setCheckable(true);
        auto winStyleGroup = new QActionGroup(menuBar);
        // At most one action can be checked at any one time. The actions can also be all unchecked.
        winStyleGroup->setExclusionPolicy(QActionGroup::ExclusionPolicy::ExclusiveOptional);
        winStyleGroup->addAction(noneAction);
        winStyleGroup->addAction(dwmBlurAction);
        winStyleGroup->addAction(acrylicAction);
        winStyleGroup->addAction(micaAction);
        winStyleGroup->addAction(micaAltAction);
        connect(winStyleGroup, &QActionGroup::triggered, this, [this, winStyleGroup](QAction *action) {
            // Unset all custom style attributes first, otherwise the style will not display correctly
            for (auto _act : winStyleGroup->actions()) {
                windowAgent->setWindowAttribute(_act->data().toString(), false);
            for (const QAction* _act : winStyleGroup->actions()) {
                const QString data = _act->data().toString();
                if (data.isEmpty() || data == QStringLiteral("none")) {
                    continue;
            }
            windowAgent->setWindowAttribute(action->data().toString(), action->isChecked());
            setProperty("custom-style", action->isChecked());
                windowAgent->setWindowAttribute(data, false);
            }
            const QString data = action->data().toString();
            if (data == QStringLiteral("none")) {
                setProperty("custom-style", false);
            } else if (!data.isEmpty()) {
                windowAgent->setWindowAttribute(data, true);
                setProperty("custom-style", true);
            }
            style()->polish(this);
        });
@@ -248,6 +256,7 @@
#ifdef Q_OS_WIN
        settings->addSeparator();
        settings->addAction(noneAction);
        settings->addAction(dwmBlurAction);
        settings->addAction(acrylicAction);
        settings->addAction(micaAction);
examples/mainwindow/mainwindow.h
@@ -30,8 +30,6 @@
protected:
    bool event(QEvent *event) override;
    void closeEvent(QCloseEvent *event) override;
private:
    void installWindowAgent();
    void loadStyleSheet(Theme theme);
examples/qml/QWKButton.qml
@@ -20,6 +20,7 @@
            mipmap: true
            width: 12
            height: 12
            fillMode: Image.PreserveAspectFit
        }
    }
    background: Rectangle {
examples/qml/main.cpp
@@ -4,22 +4,32 @@
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlContext>
#include <QtQuick/QQuickWindow>
#include <QWKQuick/qwkquickglobal.h>
#ifdef Q_OS_WIN
// Indicates to hybrid graphics systems to prefer the discrete part by default.
extern "C" {
    Q_DECL_EXPORT unsigned long NvOptimusEnablement = 0x00000001;
    Q_DECL_EXPORT int AmdPowerXpressRequestHighPerformance = 1;
}
#endif
int main(int argc, char *argv[]) {
    qputenv("QT_WIN_DEBUG_CONSOLE", "attach");
    qputenv("QT_WIN_DEBUG_CONSOLE", "attach"); // or "new": create a separate console window
    qputenv("QSG_INFO", "1");
    qputenv("QSG_NO_VSYNC", "1");
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    qputenv("QT_QUICK_CONTROLS_STYLE", "Basic");
#else
    qputenv("QT_QUICK_CONTROLS_STYLE", "Default");
#endif
#if 0
    qputenv("QSG_RHI_BACKEND", "opengl");
    //qputenv("QSG_RHI_HDR", "scrgb");
    //qputenv("QSG_RHI_BACKEND", "opengl"); // other options: d3d11, d3d12, vulkan
    //qputenv("QSG_RHI_HDR", "scrgb"); // other options: hdr10, p3
    //qputenv("QT_QPA_DISABLE_REDIRECTION_SURFACE", "1");
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(
        Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
@@ -27,6 +37,12 @@
    // Make sure alpha channel is requested, our special effects on Windows depends on it.
    QQuickWindow::setDefaultAlphaBuffer(true);
    QQmlApplicationEngine engine;
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
    const bool curveRenderingAvailable = true;
#else
    const bool curveRenderingAvailable = false;
#endif
    engine.rootContext()->setContextProperty(QStringLiteral("$curveRenderingAvailable"), QVariant(curveRenderingAvailable));
    QWK::registerTypes(&engine);
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    return application.exec();
examples/qml/main.qml
@@ -12,6 +12,7 @@
    title: qsTr("Hello, world!")
    Component.onCompleted: {
        windowAgent.setup(window)
        windowAgent.setWindowAttribute("dark-mode", true)
        window.visible = true
    }
@@ -35,10 +36,9 @@
        id: windowAgent
    }
    MouseArea {
        anchors.fill: parent
    TapHandler {
        acceptedButtons: Qt.RightButton
        onClicked: contextMenu.open()
        onTapped: contextMenu.open()
    }
    Rectangle {
@@ -63,6 +63,7 @@
            height: 18
            mipmap: true
            source: "qrc:///app/example.png"
            fillMode: Image.PreserveAspectFit
            Component.onCompleted: windowAgent.setSystemButton(WindowAgent.WindowIcon, iconButton)
        }
@@ -140,6 +141,11 @@
            bold: true
        }
        color: "#FEFEFE"
        Component.onCompleted: {
            if ($curveRenderingAvailable) {
                timeLabel.renderType = Text.CurveRendering
            }
        }
    }
    Menu {
@@ -163,6 +169,7 @@
            MenuItem {
                text: qsTr("Dark")
                checkable: true
                checked: true
                onTriggered: windowAgent.setWindowAttribute("dark-mode", true)
            }
        }
@@ -178,11 +185,28 @@
            MenuItem {
                enabled: Qt.platform.os === "windows"
                text: qsTr("None")
                checkable: true
                checked: true
                onTriggered: {
                    window.color = darkStyle.windowBackgroundColor
                    windowAgent.setWindowAttribute("dwm-blur", false)
                    windowAgent.setWindowAttribute("acrylic-material", false)
                    windowAgent.setWindowAttribute("mica", false)
                    windowAgent.setWindowAttribute("mica-alt", false)
                }
            }
            MenuItem {
                enabled: Qt.platform.os === "windows"
                text: qsTr("DWM blur")
                checkable: true
                onTriggered: {
                    window.color = checked ? "transparent" : darkStyle.windowBackgroundColor
                    windowAgent.setWindowAttribute("dwm-blur", checked)
                    window.color = "transparent"
                    windowAgent.setWindowAttribute("acrylic-material", false)
                    windowAgent.setWindowAttribute("mica", false)
                    windowAgent.setWindowAttribute("mica-alt", false)
                    windowAgent.setWindowAttribute("dwm-blur", true)
                }
            }
@@ -191,8 +215,11 @@
                text: qsTr("Acrylic material")
                checkable: true
                onTriggered: {
                    window.color = checked ? "transparent" : darkStyle.windowBackgroundColor
                    windowAgent.setWindowAttribute("acrylic-material", checked)
                    window.color = "transparent"
                    windowAgent.setWindowAttribute("dwm-blur", false)
                    windowAgent.setWindowAttribute("mica", false)
                    windowAgent.setWindowAttribute("mica-alt", false)
                    windowAgent.setWindowAttribute("acrylic-material", true)
                }
            }
@@ -201,8 +228,11 @@
                text: qsTr("Mica")
                checkable: true
                onTriggered: {
                    window.color = checked ? "transparent" : darkStyle.windowBackgroundColor
                    windowAgent.setWindowAttribute("mica", checked)
                    window.color = "transparent"
                    windowAgent.setWindowAttribute("dwm-blur", false)
                    windowAgent.setWindowAttribute("acrylic-material", false)
                    windowAgent.setWindowAttribute("mica-alt", false)
                    windowAgent.setWindowAttribute("mica", true)
                }
            }
@@ -211,8 +241,11 @@
                text: qsTr("Mica Alt")
                checkable: true
                onTriggered: {
                    window.color = checked ? "transparent" : darkStyle.windowBackgroundColor
                    windowAgent.setWindowAttribute("mica-alt", checked)
                    window.color = "transparent"
                    windowAgent.setWindowAttribute("dwm-blur", false)
                    windowAgent.setWindowAttribute("acrylic-material", false)
                    windowAgent.setWindowAttribute("mica", false)
                    windowAgent.setWindowAttribute("mica-alt", true)
                }
            }
        }