Yuhang Zhao
2023-12-05 d02f3b0bc32f711bf33a7fd288185963e9e96189
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "mainwindow.h"
 
#include <QtCore/QDebug>
#include <QtCore/QTime>
#include <QtWidgets/QPushButton>
 
#include <QWKWidgets/widgetwindowagent.h>
 
#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);
}