refactor(plugins): Use signals to update UI (#7030)

The plugin UI manually updated itself whenever the user changed the
state by clicking a button (e.g. enabling a plugin). This gets more
complicated when other parts of Chatterino can also load/toggle plugins.
To help with this, I added a signal for the current list of plugins. The
UI also reacts to changes to the enabled plugins (a setting).

Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
Nerixyz
2026-06-30 11:50:01 +00:00
committed by GitHub
parent e6931b1b14
commit 305149a795
3 changed files with 36 additions and 7 deletions
@@ -312,6 +312,7 @@ void PluginController::load(const QFileInfo &index, const QDir &pluginDir,
auto plugin = std::make_unique<Plugin>(pluginName, l, meta, pluginDir);
auto *temp = plugin.get();
this->plugins_.insert({pluginName, std::move(plugin)});
this->queueChangeNotification();
if (getApp()->getArgs().safeMode)
{
@@ -361,6 +362,7 @@ bool PluginController::reload(const QString &id)
QDir loadDir = it->second->loadDirectory_;
// Since Plugin owns the state, it will clean up everything related to it
this->plugins_.erase(id);
this->queueChangeNotification();
this->tryLoadFromDir(loadDir);
return true;
}
@@ -488,5 +490,21 @@ WebSocketPool &PluginController::webSocketPool()
return this->webSocketPool_;
}
void PluginController::queueChangeNotification()
{
if (this->changeNotificationQueued)
{
return;
}
this->changeNotificationQueued = true;
QMetaObject::invokeMethod(
qApp,
[this] {
this->changeNotificationQueued = false;
this->onPluginsUpdated.invoke();
},
Qt::QueuedConnection);
}
} // namespace chatterino
#endif
@@ -69,6 +69,7 @@ public:
WebSocketPool &webSocketPool();
pajlada::Signals::Signal<Plugin *> onPluginLoaded;
pajlada::Signals::NoArgSignal onPluginsUpdated;
private:
void loadPlugins();
@@ -82,6 +83,9 @@ private:
static void loadChatterinoLib(lua_State *l);
bool tryLoadFromDir(const QDir &pluginDir);
void queueChangeNotification();
std::map<QString, std::unique_ptr<Plugin>> plugins_;
WebSocketPool webSocketPool_;
@@ -89,6 +93,8 @@ private:
std::pair<std::string, std::function<sol::object(sol::state_view)>>>
loaders_;
bool changeNotificationQueued = false;
// This is for tests, pay no attention
friend class PluginControllerAccess;
};
+12 -7
View File
@@ -98,7 +98,15 @@ PluginsPage::PluginsPage()
}
}
this->rebuildContent();
this->managedConnections_.managedConnect(
getApp()->getPlugins()->onPluginsUpdated, [this] {
this->rebuildContent();
});
getSettings()->enabledPlugins.connect(
[this] {
this->rebuildContent();
},
this->managedConnections_);
}
void PluginsPage::rebuildContent()
@@ -231,17 +239,14 @@ void PluginsPage::rebuildContent()
}
getSettings()->enabledPlugins.setValue(val);
getApp()->getPlugins()->reload(name);
this->rebuildContent();
});
pluginEntry->addRow(toggleButton);
}
auto *reloadButton = new QPushButton("Reload", this->dataFrame_);
QObject::connect(reloadButton, &QPushButton::pressed,
[name = id, this]() {
getApp()->getPlugins()->reload(name);
this->rebuildContent();
});
QObject::connect(reloadButton, &QPushButton::pressed, [name = id]() {
getApp()->getPlugins()->reload(name);
});
pluginEntry->addRow(reloadButton);
if (getApp()->getArgs().safeMode)
{