From 3871bfc5d3aff45e498fa2944c27e6eb5d146c8e Mon Sep 17 00:00:00 2001
From: SineStriker <trueful@163.com>
Date: 周三, 20 12月 2023 19:56:32 +0800
Subject: [PATCH] Add mac hot-switch implementations

---
 examples/mainwindow/mainwindow.cpp |   51 +++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp
index 96736de..e289c3e 100644
--- a/examples/mainwindow/mainwindow.cpp
+++ b/examples/mainwindow/mainwindow.cpp
@@ -36,10 +36,7 @@
     clockWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
     setCentralWidget(clockWidget);
 
-    if (QFile qss(QStringLiteral(":/dark-style.qss"));
-        qss.open(QIODevice::ReadOnly | QIODevice::Text)) {
-        setStyleSheet(QString::fromUtf8(qss.readAll()));
-    }
+    loadStyleSheet(Dark);
 
     setWindowTitle(tr("Example MainWindow"));
     resize(800, 600);
@@ -108,23 +105,49 @@
 void MainWindow::installWindowAgent() {
     // 1. Setup window agent
     auto agent = new QWK::WidgetWindowAgent(this);
-    if (!agent->setup(this)) {
-        qFatal("QWK failed to initialize.");
-    }
+    agent->setup(this);
 
     // 2. Construct your title bar
-    auto menuBar = []() {
+    auto menuBar = [this, agent]() {
         auto menuBar = new QMenuBar();
+
+        // Virtual menu
         auto file = new QMenu(tr("File(&F)"), menuBar);
         file->addAction(new QAction(tr("New(&N)"), menuBar));
         file->addAction(new QAction(tr("Open(&O)"), menuBar));
+        file->addSeparator();
 
         auto edit = new QMenu(tr("Edit(&E)"), menuBar);
         edit->addAction(new QAction(tr("Undo(&U)"), menuBar));
         edit->addAction(new QAction(tr("Redo(&R)"), menuBar));
 
+        // Theme action
+        auto darkAction = new QAction(tr("Dark Theme"), menuBar);
+        darkAction->setCheckable(true);
+        connect(darkAction, &QAction::triggered, this, [this](bool checked) {
+            loadStyleSheet(checked ? Dark : Light); //
+        });
+        connect(this, &MainWindow::themeChanged, darkAction, [this, darkAction]() {
+            darkAction->setChecked(currentTheme == Dark); //
+        });
+
+        // Agent action
+        auto framelessOnAction = new QAction(tr("Enable Frameless"), menuBar);
+        framelessOnAction->setCheckable(true);
+        framelessOnAction->setChecked(true);
+        connect(framelessOnAction, &QAction::triggered, agent, &QWK::WindowAgentBase::setEnabled);
+        connect(agent, &QWK::WindowAgentBase::enabledChanged, framelessOnAction,
+                &QAction::setChecked);
+
+        // Real menu
+        auto settings = new QMenu(tr("Settings(&S)"), menuBar);
+        settings->addAction(darkAction);
+        settings->addSeparator();
+        settings->addAction(framelessOnAction);
+
         menuBar->addMenu(file);
         menuBar->addMenu(edit);
+        menuBar->addMenu(settings);
         return menuBar;
     }();
     menuBar->setObjectName(QStringLiteral("win-menu-bar"));
@@ -213,3 +236,15 @@
     connect(windowBar, &QWK::WindowBar::closeRequested, this, &QWidget::close);
 #endif
 }
+
+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)) {
+        setStyleSheet(QString::fromUtf8(qss.readAll()));
+        Q_EMIT themeChanged();
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.1