comparison 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
comparison
equal deleted inserted replaced
167:31735c8592bc 168:79a2a24453fa
28 #include <QMenuBar> 28 #include <QMenuBar>
29 #include <QMessageBox> 29 #include <QMessageBox>
30 #include <QPlainTextEdit> 30 #include <QPlainTextEdit>
31 #include <QStackedWidget> 31 #include <QStackedWidget>
32 #include <QTextStream> 32 #include <QTextStream>
33 #include <QThread>
33 #include <QThreadPool> 34 #include <QThreadPool>
34 #include <QTimer> 35 #include <QTimer>
35 #include <QToolBar> 36 #include <QToolBar>
36 #include <QToolButton> 37 #include <QToolButton>
37 38
39 # include "sys/osx/dark_theme.h" 40 # include "sys/osx/dark_theme.h"
40 #elif defined(WIN32) 41 #elif defined(WIN32)
41 # include "sys/win32/dark_theme.h" 42 # include "sys/win32/dark_theme.h"
42 #endif 43 #endif
43 44
45 Q_DECLARE_METATYPE(std::vector<std::string>);
46
47 class Thread : public QThread {
48 Q_OBJECT
49
50 public:
51 Thread(QObject* object = nullptr) : QThread(object) {}
52
53 private:
54 void run() override {
55 std::vector<std::string> files;
56 Track::Media::GetCurrentlyPlaying(files);
57 emit Done(files);
58 }
59
60 signals:
61 void Done(const std::vector<std::string>& files);
62 };
63
44 enum class Pages { 64 enum class Pages {
45 NOW_PLAYING, 65 NOW_PLAYING,
46 66
47 ANIME_LIST, 67 ANIME_LIST,
48 HISTORY, 68 HISTORY,
63 83
64 setCentralWidget(main_widget.get()); 84 setCentralWidget(main_widget.get());
65 85
66 CreateBars(); 86 CreateBars();
67 87
68 QTimer* timer = new QTimer(this); 88 NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING)));
69 89
70 /* this is very very stinky */ 90 qRegisterMetaType<std::vector<std::string>>();
71 connect(timer, &QTimer::timeout, this, [this] { 91
72 bool success = false; 92 QTimer* timer = new QTimer;
73 93 timer->start(5000);
74 NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING))); 94
75 95 connect(timer, &QTimer::timeout, this, [this, page] {
76 std::vector<std::string> files; 96 Thread* thread = new Thread(this);
77 Track::Media::GetCurrentlyPlaying(files); 97 connect(thread, &QThread::finished, thread, &QThread::deleteLater);
78 98 connect(thread, &Thread::Done, this, [page](const std::vector<std::string>& files) {
79 /* this should really be more intertwined with anitomy */ 99 for (const auto& file : files) {
80 for (const auto& file : files) { 100 anitomy::Anitomy anitomy;
81 anitomy::Anitomy anitomy; 101 anitomy.Parse(Strings::ToWstring(file));
82 anitomy.Parse(Strings::ToWstring(file)); 102
83 103 const auto& elements = anitomy.elements();
84 const auto& elements = anitomy.elements(); 104
85 105 int id = Anime::db.GetAnimeFromTitle(Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle)));
86 int id = Anime::db.GetAnimeFromTitle(Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle))); 106 if (id <= 0)
87 if (id <= 0) 107 continue;
88 continue; 108
89 109 page->SetPlaying(Anime::db.items[id], elements);
90 qDebug() << id; 110 break;
91 111 }
92 page->SetPlaying(Anime::db.items[id], elements); 112 });
93 113 thread->start();
94 success = true;
95 }
96
97 if (!success)
98 page->SetDefault();
99 }); 114 });
100 timer->start(5000);
101 } 115 }
102 116
103 void MainWindow::AddMainWidgets() { 117 void MainWindow::AddMainWidgets() {
104 int page = static_cast<int>(Pages::ANIME_LIST); 118 int page = static_cast<int>(Pages::ANIME_LIST);
105 if (sidebar.get()) { 119 if (sidebar.get()) {
470 session.config.Save(); 484 session.config.Save();
471 event->accept(); 485 event->accept();
472 } 486 }
473 487
474 #include "gui/moc_window.cpp" 488 #include "gui/moc_window.cpp"
489 #include "gui/window.moc"