Skip to content

Commit

Permalink
refactor(instwin): update code
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed Aug 14, 2023
1 parent e65f442 commit 9fe2ad1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
11 changes: 6 additions & 5 deletions packages/instwin/include/instwin/core/installer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ enum InstallResult {
Warning = 3,
};

class InstallerWorker : public QObject {
class InstallerWorker : public QThread {
Q_OBJECT

public slots:
void run();
public:
InstallerWorker(QObject *parent);

signals:
void onLog(std::string s);
void onProgress(int progress);
void onResult(InstallResult result);

private:
void run() override;

std::string timeString();
};

class Installer : public QObject {
Q_OBJECT

public:
Installer();
Installer(QObject *parent);

public slots:
void start();
Expand All @@ -46,7 +48,6 @@ public slots:
void onResult(InstallResult result);

private:
QThread thread;
InstallerWorker worker;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/instwin/include/instwin/windows/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class MainWindow : public QMainWindow {
~MainWindow();

public slots:
void navigateToProgressPage();
void navigateToFinishPage();
[[maybe_unused]] void navigateToProgressPage();
[[maybe_unused]] void navigateToFinishPage();
void appendProgressLog(std::string s);

private:
Expand Down
9 changes: 5 additions & 4 deletions packages/instwin/src/core/installer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace InstWin {

InstallerWorker::InstallerWorker(QObject *parent) : QThread(parent) {
}

void InstallerWorker::run() {
emit onProgress(0);
emit onLog(std::format("Installation started at {}.", timeString()));
Expand All @@ -14,16 +17,14 @@ std::string InstallerWorker::timeString() {
return std::format("{:%Y-%m-%d %H:%M:%OS}", std::chrono::system_clock::now());
}

Installer::Installer() {
worker.moveToThread(&thread);
connect(this, &Installer::start, &worker, &InstallerWorker::run);
Installer::Installer(QObject *parent) : QObject(parent), worker(this) {
connect(&worker, &InstallerWorker::onLog, this, &Installer::onLog);
connect(&worker, &InstallerWorker::onProgress, this, &Installer::onProgress);
connect(&worker, &InstallerWorker::onResult, this, &Installer::onResult);
}

void Installer::start() {
thread.start();
worker.start();
}

} // namespace InstWin
21 changes: 14 additions & 7 deletions packages/instwin/src/windows/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "instwin/windows/mainwindow.hpp"
#include "ui_mainwindow.h"

MainWindow::MainWindow() : QMainWindow(nullptr), ui(new Ui::MainWindow) {
MainWindow::MainWindow()
: QMainWindow(nullptr), ui(new Ui::MainWindow), installer(this) {
// Setup UI
ui->setupUi(this);

Expand All @@ -10,6 +11,11 @@ MainWindow::MainWindow() : QMainWindow(nullptr), ui(new Ui::MainWindow) {
&InstWin::Installer::onLog,
this,
&MainWindow::appendProgressLog);
connect(
ui->installButton,
&QPushButton::clicked,
&installer,
&InstWin::Installer::start);

initializeWindowStyle();
}
Expand Down Expand Up @@ -47,11 +53,11 @@ void MainWindow::initializeWindowStyle() {
int dwmUseDarkMode = 1;
DwmSetWindowAttribute(
hWnd,
supports >= 2 ? 20
: // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE
// = 20 (starting from 18985)
19, // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE
// = 19 (before 18985)
supports >= 2 ? 20 // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE
: // = 20 (starting from 18985)
// DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE
19, // = 19 (before 18985)

&dwmUseDarkMode,
sizeof(dwmUseDarkMode));
unsigned int dwmCornerPreference =
Expand Down Expand Up @@ -79,7 +85,6 @@ void MainWindow::initializeWindowStyle() {

void MainWindow::navigateToProgressPage() {
ui->centralWidget->setCurrentIndex(1);
installer.start();
}

void MainWindow::navigateToFinishPage() {
Expand All @@ -89,6 +94,8 @@ void MainWindow::navigateToFinishPage() {
void MainWindow::appendProgressLog(std::string s) {
ui->progressPageLog->moveCursor(QTextCursor::End);
ui->progressPageLog->insertPlainText(s.c_str());
ui->progressPageLog->moveCursor(QTextCursor::End);
ui->progressPageLog->insertPlainText("\n");
QScrollBar *verticalScrollBar = ui->progressPageLog->verticalScrollBar();
verticalScrollBar->setValue(verticalScrollBar->maximum());
}

0 comments on commit 9fe2ad1

Please sign in to comment.