changeset 170:c8375765f0fc

window: make threading somewhat sane
author Paper <mrpapersonic@gmail.com>
date Tue, 21 Nov 2023 11:04:13 -0500
parents e44b7c428d7c
children 03b444cbe55f
files include/gui/window.h src/gui/window.cc
diffstat 2 files changed, 36 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/include/gui/window.h	Sun Nov 19 17:30:38 2023 -0500
+++ b/include/gui/window.h	Tue Nov 21 11:04:13 2023 -0500
@@ -10,8 +10,24 @@
 #include <QWidget>
 #include <QStackedWidget>
 #include <QCloseEvent>
+#include <QThread>
 #include "gui/widgets/sidebar.h"
 
+Q_DECLARE_METATYPE(std::vector<std::string>);
+
+class PlayingThread : public QThread {
+		Q_OBJECT
+
+	public:
+		PlayingThread(QObject* object = nullptr) : QThread(object) {}
+
+	private:
+		void run() override;
+
+	signals:
+		void Done(const std::vector<std::string>& files);
+};
+
 class MainWindow final : public QMainWindow {
 		Q_OBJECT
 
@@ -30,6 +46,8 @@
 		std::unique_ptr<QWidget> main_widget = nullptr;
 		std::unique_ptr<QStackedWidget> stack = nullptr;
 		std::unique_ptr<SideBar> sidebar = nullptr;
+
+        std::unique_ptr<PlayingThread> thread = nullptr;
 };
 
 #endif // __window_h
--- a/src/gui/window.cc	Sun Nov 19 17:30:38 2023 -0500
+++ b/src/gui/window.cc	Tue Nov 21 11:04:13 2023 -0500
@@ -42,25 +42,6 @@
 #	include "sys/win32/dark_theme.h"
 #endif
 
-Q_DECLARE_METATYPE(std::vector<std::string>);
-
-class Thread : public QThread {
-		Q_OBJECT
-
-	public:
-		Thread(QObject* object = nullptr) : QThread(object) {}
-
-	private:
-		void run() override {
-			std::vector<std::string> files;
-			Track::Media::GetCurrentlyPlaying(files);
-			emit Done(files);
-		}
-
-	signals:
-		void Done(const std::vector<std::string>& files);
-};
-
 enum class Pages {
 	NOW_PLAYING,
 
@@ -73,6 +54,12 @@
 	TORRENTS
 };
 
+void PlayingThread::run() {
+	std::vector<std::string> files;
+	Track::Media::GetCurrentlyPlaying(files);
+	emit Done(files);
+}
+
 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
 	setWindowIcon(QIcon(":/favicon.png"));
 
@@ -89,13 +76,15 @@
 
 	qRegisterMetaType<std::vector<std::string>>();
 
+	thread.reset(new PlayingThread(this));
+
 	QTimer* timer = new QTimer;
-	timer->start(5000);
 
 	connect(timer, &QTimer::timeout, this, [this, page] {
-		Thread* thread = new Thread(this);
-		connect(thread, &QThread::finished, thread, &QThread::deleteLater);
-		connect(thread, &Thread::Done, this, [page](const std::vector<std::string>& files) {
+		if (!thread.get() || thread->isRunning())
+			return;
+		connect(thread.get(), &QThread::finished, thread.get(), &QThread::deleteLater);
+		connect(thread.get(), &PlayingThread::Done, this, [page](const std::vector<std::string>& files) {
 			for (const auto& file : files) {
 				anitomy::Anitomy anitomy;
 				anitomy.Parse(Strings::ToWstring(file));
@@ -112,6 +101,9 @@
 		});
 		thread->start();
 	});
+
+	timer->start(5000);
+	timer->moveToThread(thread.get());
 }
 
 void MainWindow::AddMainWidgets() {
@@ -332,10 +324,10 @@
 
 			/* pain in my ass */
 			connect(sidebar.get(), &SideBar::CurrentItemChanged, this,
-			        [pages_group](int index) { pages_group->actions()[index]->setChecked(true); });
+					[pages_group](int index) { pages_group->actions()[index]->setChecked(true); });
 
 			connect(pages_group, &QActionGroup::triggered, this,
-			        [this, page_to_index_map](QAction* action) { sidebar->SetCurrentItem(page_to_index_map.at(action)); });
+					[this, page_to_index_map](QAction* action) { sidebar->SetCurrentItem(page_to_index_map.at(action)); });
 		}
 
 		menu->addSeparator();
@@ -380,7 +372,7 @@
 		/* Toolbar */
 		QToolBar* toolbar = new QToolBar(this);
 		toolbar->addAction(QIcon(":/icons/24x24/arrow-circle-double-135.png"), tr("&Synchronize"),
-		                   [this] { AsyncSynchronize(stack.get()); });
+						   [this] { AsyncSynchronize(stack.get()); });
 
 		toolbar->addSeparator();
 
@@ -486,4 +478,3 @@
 }
 
 #include "gui/moc_window.cpp"
-#include "gui/window.moc"