gplugin/gplugin

Implement enable switch in GTK4 viewer

2021-12-16, Elliott Sales de Andrade
f03eee6f9596
Parents ba6874e1ad57
Children c97d5cc32a14
Implement enable switch in GTK4 viewer

This adds a `state-set` signal on the plugin row, which merely re-broadcasts the signal from the switch. This enables the view to watch for the signal and pass along the desired state to the manager.

The plugin row sets the display of the enable switch whenever the plugin state refreshes. I'm not entirely sure if all states are displayed in the way we want.

Testing Done:
Start viewer and see that auto-loaded plugins are actually enabled, and that others can be enabled/disabled.

Reviewed at https://reviews.imfreedom.org/r/1193/
--- a/gplugin-gtk4/data/plugin-row.ui Wed Dec 08 02:23:36 2021 -0600
+++ b/gplugin-gtk4/data/plugin-row.ui Thu Dec 16 21:11:01 2021 -0600
@@ -27,6 +27,7 @@
<child>
<object class="GtkSwitch" id="enable">
<property name="valign">center</property>
+ <signal name="state-set" handler="gplugin_gtk_plugin_row_enable_state_set_cb"/>
</object>
</child>
<child>
--- a/gplugin-gtk4/gplugin-gtk-plugin-row.c Wed Dec 08 02:23:36 2021 -0600
+++ b/gplugin-gtk4/gplugin-gtk-plugin-row.c Thu Dec 16 21:11:01 2021 -0600
@@ -72,6 +72,14 @@
NULL,
};
+enum {
+ SIG_PLUGIN_STATE_SET,
+ N_SIGNALS,
+};
+static guint signals[N_SIGNALS] = {
+ 0,
+};
+
/******************************************************************************
* Helpers
*****************************************************************************/
@@ -79,6 +87,7 @@
_gplugin_gtk_plugin_row_refresh(GPluginGtkPluginRow *row)
{
GtkWidget *widget = NULL;
+ GPluginPluginState state = GPLUGIN_PLUGIN_STATE_UNKNOWN;
GError *error = NULL;
gchar *name = NULL, *version = NULL, *website = NULL;
gchar *summary = NULL, *description = NULL, *id = NULL, *abi_version = NULL;
@@ -107,6 +116,7 @@
filename = gplugin_plugin_get_filename(row->plugin);
error = gplugin_plugin_get_error(row->plugin);
+ state = gplugin_plugin_get_state(row->plugin);
if(GPLUGIN_IS_LOADER(plugin_loader)) {
loader = g_strdup(G_OBJECT_TYPE_NAME(plugin_loader));
@@ -153,6 +163,33 @@
g_free(basename);
}
+ /* Set state of enable switch. */
+ switch(state) {
+ case GPLUGIN_PLUGIN_STATE_QUERIED:
+ case GPLUGIN_PLUGIN_STATE_REQUERY:
+ gtk_switch_set_state(GTK_SWITCH(row->enable), FALSE);
+ gtk_widget_set_sensitive(row->enable, TRUE);
+ break;
+
+ case GPLUGIN_PLUGIN_STATE_LOADED:
+ gtk_switch_set_state(GTK_SWITCH(row->enable), TRUE);
+ gtk_widget_set_sensitive(row->enable, TRUE);
+ break;
+
+ case GPLUGIN_PLUGIN_STATE_UNLOAD_FAILED:
+ gtk_switch_set_state(GTK_SWITCH(row->enable), TRUE);
+ gtk_widget_set_sensitive(row->enable, FALSE);
+ break;
+
+ case GPLUGIN_PLUGIN_STATE_ERROR:
+ case GPLUGIN_PLUGIN_STATE_LOAD_FAILED:
+ case GPLUGIN_PLUGIN_STATE_UNKNOWN:
+ default:
+ gtk_switch_set_state(GTK_SWITCH(row->enable), FALSE);
+ gtk_widget_set_sensitive(row->enable, FALSE);
+ break;
+ }
+
gtk_label_set_text(GTK_LABEL(row->title), name);
gtk_label_set_text(GTK_LABEL(row->version), version);
gtk_label_set_markup(GTK_LABEL(row->website), website);
@@ -214,6 +251,8 @@
g_free(name);
g_free(version);
g_free(website);
+
+ gtk_list_box_row_changed(GTK_LIST_BOX_ROW(row));
}
/******************************************************************************
@@ -228,6 +267,19 @@
_gplugin_gtk_plugin_row_refresh(GPLUGIN_GTK_PLUGIN_ROW(data));
}
+static gboolean
+gplugin_gtk_plugin_row_enable_state_set_cb(
+ G_GNUC_UNUSED GtkSwitch *widget,
+ gboolean state,
+ gpointer data)
+{
+ GPluginGtkPluginRow *row = GPLUGIN_GTK_PLUGIN_ROW(data);
+
+ g_signal_emit(G_OBJECT(row), signals[SIG_PLUGIN_STATE_SET], 0, state);
+
+ return TRUE;
+}
+
/******************************************************************************
* GObject Implementation
*****************************************************************************/
@@ -323,6 +375,7 @@
"The GPluginPlugin whose info should be displayed",
G_TYPE_OBJECT,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
+
properties[PROP_EXPANDED] = g_param_spec_boolean(
"expanded",
"expanded",
@@ -330,6 +383,29 @@
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
+ /* signals */
+
+ /**
+ * GPluginGtkPluginRow::plugin-state-set:
+ * @row: The [class@GPluginGtk.PluginRow] instance.
+ * @enabled: Whether the plugin was requested to be enabled or disabled.
+ *
+ * Emitted when the plugin row enable switch is toggled.
+ *
+ * Since: 0.38.0
+ */
+ signals[SIG_PLUGIN_STATE_SET] = g_signal_new_class_handler(
+ "plugin-state-set",
+ G_OBJECT_CLASS_TYPE(klass),
+ G_SIGNAL_RUN_LAST,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ G_TYPE_NONE,
+ 1,
+ G_TYPE_BOOLEAN);
+
/* template stuff */
gtk_widget_class_set_template_from_resource(
widget_class,
@@ -360,6 +436,9 @@
widget_class,
GPluginGtkPluginRow,
revealer);
+ gtk_widget_class_bind_template_callback(
+ widget_class,
+ gplugin_gtk_plugin_row_enable_state_set_cb);
/* Details */
gtk_widget_class_bind_template_child(
--- a/gplugin-gtk4/gplugin-gtk-view.c Wed Dec 08 02:23:36 2021 -0600
+++ b/gplugin-gtk4/gplugin-gtk-view.c Thu Dec 16 21:11:01 2021 -0600
@@ -71,6 +71,35 @@
}
static void
+gplugin_gtk_view_plugin_state_set_cb(
+ GPluginGtkPluginRow *row,
+ gboolean state,
+ gpointer data)
+{
+ GPluginGtkView *view = GPLUGIN_GTK_VIEW(data);
+ GPluginPlugin *plugin = gplugin_gtk_plugin_row_get_plugin(row);
+ GError *error = NULL;
+
+ if(state) {
+ gplugin_manager_load_plugin(view->manager, plugin, &error);
+
+ if(error != NULL) {
+ g_warning("Failed to load plugin: %s", error->message);
+
+ g_error_free(error);
+ }
+ } else {
+ gplugin_manager_unload_plugin(view->manager, plugin, &error);
+
+ if(error != NULL) {
+ g_warning("Failed to unload plugin: %s", error->message);
+
+ g_error_free(error);
+ }
+ }
+}
+
+static void
gplugin_gtk_view_add_plugins_to_list(
G_GNUC_UNUSED const gchar *id,
GSList *plugins,
@@ -83,6 +112,11 @@
gplugin_gtk_plugin_row_set_plugin(
GPLUGIN_GTK_PLUGIN_ROW(row),
GPLUGIN_PLUGIN(plugins->data));
+ g_signal_connect(
+ row,
+ "plugin-state-set",
+ G_CALLBACK(gplugin_gtk_view_plugin_state_set_cb),
+ view);
gtk_list_box_append(GTK_LIST_BOX(view->list_box), row);
}
}