diff src/gui/window.cc @ 168:79a2a24453fa

window: improve performance when getting running files now we don't block the whole app! :) this is especially noticeable on X11, where window walking takes ABSURDLY long
author paper@DavesDouble.local
date Sun, 19 Nov 2023 05:36:41 -0500
parents d43d68408d3c
children c8375765f0fc
line wrap: on
line diff
--- a/src/gui/window.cc	Sun Nov 19 05:32:06 2023 -0500
+++ b/src/gui/window.cc	Sun Nov 19 05:36:41 2023 -0500
@@ -30,6 +30,7 @@
 #include <QPlainTextEdit>
 #include <QStackedWidget>
 #include <QTextStream>
+#include <QThread>
 #include <QThreadPool>
 #include <QTimer>
 #include <QToolBar>
@@ -41,6 +42,25 @@
 #	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,
 
@@ -65,39 +85,33 @@
 
 	CreateBars();
 
-	QTimer* timer = new QTimer(this);
-
-	/* this is very very stinky */
-	connect(timer, &QTimer::timeout, this, [this] {
-		bool success = false;
-
-		NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING)));
+	NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING)));
 
-		std::vector<std::string> files;
-		Track::Media::GetCurrentlyPlaying(files);
+	qRegisterMetaType<std::vector<std::string>>();
 
-		/* this should really be more intertwined with anitomy */
-		for (const auto& file : files) {
-			anitomy::Anitomy anitomy;
-			anitomy.Parse(Strings::ToWstring(file));
+	QTimer* timer = new QTimer;
+	timer->start(5000);
 
-			const auto& elements = anitomy.elements();
+	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) {
+			for (const auto& file : files) {
+				anitomy::Anitomy anitomy;
+				anitomy.Parse(Strings::ToWstring(file));
 
-			int id = Anime::db.GetAnimeFromTitle(Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle)));
-			if (id <= 0)
-				continue;
-
-			qDebug() << id;
+				const auto& elements = anitomy.elements();
 
-			page->SetPlaying(Anime::db.items[id], elements);
-
-			success = true;
-		}
+				int id = Anime::db.GetAnimeFromTitle(Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle)));
+				if (id <= 0)
+					continue;
 
-		if (!success)
-			page->SetDefault();
+				page->SetPlaying(Anime::db.items[id], elements);
+				break;
+			}
+		});
+		thread->start();
 	});
-	timer->start(5000);
 }
 
 void MainWindow::AddMainWidgets() {
@@ -472,3 +486,4 @@
 }
 
 #include "gui/moc_window.cpp"
+#include "gui/window.moc"