From 9b1599d82e86d2d0463b60fe6fa61f94be50f983 Mon Sep 17 00:00:00 2001
From: Yuhang Zhao <zhaoyuhang@rankyee.com>
Date: 周二, 05 12月 2023 14:48:52 +0800
Subject: [PATCH] port some more code (for os < win10)

---
 examples/mainwindow/mainwindow.cpp |   95 +++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 91 insertions(+), 4 deletions(-)

diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp
index f551a7f..7246fdd 100644
--- a/examples/mainwindow/mainwindow.cpp
+++ b/examples/mainwindow/mainwindow.cpp
@@ -1,15 +1,102 @@
 #include "mainwindow.h"
 
 #include <QtCore/QDebug>
+#include <QtCore/QTime>
+#include <QtWidgets/QPushButton>
 
 #include <QWKWidgets/widgetwindowagent.h>
 
-MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
-    auto agent = new QWK::WidgetWindowAgent(this);
-    if (!agent->setup(this)) {
-        qDebug() << "Frameless handle failed to initialize.";
+#include <widgetframe/windowbar.h>
+
+class ClockWidget : public QPushButton {
+public:
+    explicit ClockWidget(QWidget *parent = nullptr) : QPushButton(parent) {
+        startTimer(100);
     }
+
+    ~ClockWidget() override = default;
+
+protected:
+    void timerEvent(QTimerEvent *event) override {
+        setText(QTime::currentTime().toString(QStringLiteral("hh:mm:ss")));
+    }
+};
+
+MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
+    installWindowAgent();
 }
 
 MainWindow::~MainWindow() {
 }
+
+void MainWindow::installWindowAgent() {
+    auto agent = new QWK::WidgetWindowAgent(this);
+    if (!agent->setup(this)) {
+        qDebug() << "Frameless handle failed to initialize.";
+        return;
+    }
+
+    auto titleLabel = new QLabel();
+    titleLabel->setAlignment(Qt::AlignCenter);
+
+    auto menuBar = []() {
+        auto menuBar = new QMenuBar();
+        auto file = new QMenu("File(&F)");
+        file->addAction(new QAction("New(&N)"));
+        file->addAction(new QAction("Open(&O)"));
+
+        auto edit = new QMenu("Edit(&E)");
+        edit->addAction(new QAction("Undo(&U)"));
+        edit->addAction(new QAction("Redo(&R)"));
+
+        menuBar->addMenu(file);
+        menuBar->addMenu(edit);
+        return menuBar;
+    }();
+
+    static const auto buttonStyleSheet = QLatin1String{ "QPushButton{color:black;};QPushButton:hover{background-color:black;color:white;}" };
+
+    auto iconButton = new QPushButton("I");
+    iconButton->setStyleSheet(buttonStyleSheet);
+    auto minButton = new QPushButton("鈥�");
+    minButton->setStyleSheet(buttonStyleSheet);
+    auto maxButton = new QPushButton("馃棖");
+    maxButton->setStyleSheet(buttonStyleSheet);
+    maxButton->setCheckable(true);
+    auto closeButton = new QPushButton("鉁�");
+    closeButton->setStyleSheet(buttonStyleSheet);
+
+    auto windowBar = new QWK::WindowBar();
+    windowBar->setIconButton(iconButton);
+    windowBar->setMinButton(minButton);
+    windowBar->setMaxButton(maxButton);
+    windowBar->setCloseButton(closeButton);
+    windowBar->setMenuBar(menuBar);
+    windowBar->setTitleLabel(titleLabel);
+    windowBar->setHostWidget(this);
+
+    agent->setTitleBar(windowBar);
+    agent->setSystemButton(QWK::CoreWindowAgent::WindowIcon, iconButton);
+    agent->setSystemButton(QWK::CoreWindowAgent::Minimize, minButton);
+    agent->setSystemButton(QWK::CoreWindowAgent::Maximize, maxButton);
+    agent->setSystemButton(QWK::CoreWindowAgent::Close, closeButton);
+    agent->setHitTestVisible(menuBar, true);
+
+    connect(windowBar, &QWK::WindowBar::minimizeRequested, this, &QWidget::showMinimized);
+    connect(windowBar, &QWK::WindowBar::maximizeRequested, this, [this](bool max) {
+        if (max) {
+            showMaximized();
+        } else {
+            showNormal();
+        }
+    });
+    connect(windowBar, &QWK::WindowBar::closeRequested, this, &QWidget::close);
+
+    auto clockWidget = new ClockWidget();
+    clockWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+
+    setMenuWidget(windowBar);
+    setCentralWidget(clockWidget);
+    setWindowTitle("Example MainWindow");
+    resize(1024, 768);
+}

--
Gitblit v1.9.1