[gnome-builder/wip/gtk4-port] plugins/buildui: add dialog to select default runnable



commit 3a76c85c9044c4a189ce9adf66eaa780c7d1e2db
Author: Christian Hergert <chergert redhat com>
Date:   Sat May 21 00:19:47 2022 -0700

    plugins/buildui: add dialog to select default runnable

 src/plugins/buildui/buildui.gresource.xml          |   1 +
 src/plugins/buildui/gbp-buildui-runnables-dialog.c | 217 +++++++++++++++++++++
 src/plugins/buildui/gbp-buildui-runnables-dialog.h |  35 ++++
 .../buildui/gbp-buildui-runnables-dialog.ui        |  57 ++++++
 src/plugins/buildui/gbp-buildui-workspace-addin.c  |  23 +++
 src/plugins/buildui/gtk/menus.ui                   |   7 +-
 src/plugins/buildui/meson.build                    |   1 +
 7 files changed, 336 insertions(+), 5 deletions(-)
---
diff --git a/src/plugins/buildui/buildui.gresource.xml b/src/plugins/buildui/buildui.gresource.xml
index 2402b9803..8eb6d16ee 100644
--- a/src/plugins/buildui/buildui.gresource.xml
+++ b/src/plugins/buildui/buildui.gresource.xml
@@ -6,6 +6,7 @@
     <file preprocess="xml-stripblanks">gbp-buildui-log-pane.ui</file>
     <file preprocess="xml-stripblanks">gbp-buildui-omni-bar-section.ui</file>
     <file preprocess="xml-stripblanks">gbp-buildui-pane.ui</file>
+    <file preprocess="xml-stripblanks">gbp-buildui-runnables-dialog.ui</file>
     <file preprocess="xml-stripblanks">gbp-buildui-stage-row.ui</file>
     <file preprocess="xml-stripblanks">gbp-buildui-status-indicator.ui</file>
     <file preprocess="xml-stripblanks">gbp-buildui-status-popover.ui</file>
diff --git a/src/plugins/buildui/gbp-buildui-runnables-dialog.c 
b/src/plugins/buildui/gbp-buildui-runnables-dialog.c
new file mode 100644
index 000000000..766b0d6d2
--- /dev/null
+++ b/src/plugins/buildui/gbp-buildui-runnables-dialog.c
@@ -0,0 +1,217 @@
+/*
+ * gbp-buildui-runnables-dialog.c
+ *
+ * Copyright 2022 Christian Hergert <>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-buildui-runnables-dialog"
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+#include <libpeas/peas.h>
+
+#include <libide-gui.h>
+
+#include "gbp-buildui-runnables-dialog.h"
+
+struct _GbpBuilduiRunnablesDialog
+{
+  AdwWindow   parent_instance;
+  GtkListBox *list_box;
+  GtkSpinner *spinner;
+  guint       busy : 1;
+};
+
+G_DEFINE_FINAL_TYPE (GbpBuilduiRunnablesDialog, gbp_buildui_runnables_dialog, ADW_TYPE_WINDOW)
+
+enum {
+  PROP_0,
+  PROP_BUSY,
+  PROP_CONTEXT,
+  N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
+
+static GtkWidget *
+create_run_command_row (gpointer item,
+                   gpointer user_data)
+{
+  IdeRunCommand *run_command = item;
+  g_autoptr(GVariant) idv = NULL;
+  AdwActionRow *row;
+  const char *id;
+  GtkWidget *check;
+
+  g_assert (IDE_IS_RUN_COMMAND (run_command));
+
+  id = ide_run_command_get_id (run_command);
+  idv = g_variant_take_ref (g_variant_new_string (id ? id : ""));
+
+  check = g_object_new (GTK_TYPE_CHECK_BUTTON,
+                        "action-name", "run-manager.default-run-command",
+                        "css-classes", IDE_STRV_INIT ("checkimage"),
+                        "action-target", idv,
+                        "valign", GTK_ALIGN_CENTER,
+                        "can-focus", FALSE,
+                        NULL);
+  row = g_object_new (ADW_TYPE_ACTION_ROW,
+                      "title", ide_run_command_get_display_name (item),
+                      "activatable-widget", check,
+                      NULL);
+  adw_action_row_add_suffix (row, check);
+
+  return GTK_WIDGET (row);
+}
+
+static void
+gbp_buildui_runnables_dialog_list_commands_cb (GObject      *object,
+                                               GAsyncResult *result,
+                                               gpointer      user_data)
+{
+  IdeRunManager *run_manager = (IdeRunManager *)object;
+  g_autoptr(GbpBuilduiRunnablesDialog) self = user_data;
+  g_autoptr(GListModel) model = NULL;
+  g_autoptr(GError) error = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_RUN_MANAGER (run_manager));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (GBP_IS_BUILDUI_RUNNABLES_DIALOG (self));
+
+  self->busy = FALSE;
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_BUSY]);
+
+  if (!(model = ide_run_manager_list_commands_finish (run_manager, result, &error)))
+    {
+      if (!ide_error_ignore (error))
+        ide_object_warning (run_manager,
+                            /* translators: %s is replaced with the error message */
+                            _("Failed to list run commands: %s"),
+                            error->message);
+      IDE_EXIT;
+    }
+
+  gtk_list_box_bind_model (self->list_box, model, create_run_command_row, NULL, NULL);
+
+  IDE_EXIT;
+}
+
+static void
+gbp_buildui_runnables_dialog_set_context (GbpBuilduiRunnablesDialog *self,
+                                          IdeContext                *context)
+{
+  IdeRunManager *run_manager;
+
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_BUILDUI_RUNNABLES_DIALOG (self));
+  g_assert (!context || IDE_IS_CONTEXT (context));
+
+  if (context == NULL)
+    IDE_EXIT;
+
+  self->busy = TRUE;
+
+  run_manager = ide_run_manager_from_context (context);
+  gtk_widget_insert_action_group (GTK_WIDGET (self),
+                                  "run-manager",
+                                  G_ACTION_GROUP (run_manager));
+  ide_run_manager_list_commands_async (run_manager,
+                                       NULL,
+                                       gbp_buildui_runnables_dialog_list_commands_cb,
+                                       g_object_ref (self));
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_BUSY]);
+
+  IDE_EXIT;
+}
+
+static void
+gbp_buildui_runnables_dialog_get_property (GObject    *object,
+                                           guint       prop_id,
+                                           GValue     *value,
+                                           GParamSpec *pspec)
+{
+  GbpBuilduiRunnablesDialog *self = GBP_BUILDUI_RUNNABLES_DIALOG (object);
+
+  switch (prop_id)
+    {
+    case PROP_BUSY:
+      g_value_set_boolean (value, self->busy);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gbp_buildui_runnables_dialog_set_property (GObject      *object,
+                                           guint         prop_id,
+                                           const GValue *value,
+                                           GParamSpec   *pspec)
+{
+  GbpBuilduiRunnablesDialog *self = GBP_BUILDUI_RUNNABLES_DIALOG (object);
+
+  switch (prop_id)
+    {
+    case PROP_CONTEXT:
+      gbp_buildui_runnables_dialog_set_context (self, g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gbp_buildui_runnables_dialog_class_init (GbpBuilduiRunnablesDialogClass *klass)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->get_property = gbp_buildui_runnables_dialog_get_property;
+  object_class->set_property = gbp_buildui_runnables_dialog_set_property;
+
+  properties [PROP_BUSY] =
+    g_param_spec_boolean ("busy", NULL, NULL,
+                          FALSE,
+                          (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_CONTEXT] =
+    g_param_spec_object ("context", NULL, NULL,
+                         IDE_TYPE_CONTEXT,
+                         (G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_Escape, 0, "window.close", NULL);
+
+  gtk_widget_class_set_template_from_resource (widget_class, 
"/plugins/buildui/gbp-buildui-runnables-dialog.ui");
+  gtk_widget_class_bind_template_child (widget_class, GbpBuilduiRunnablesDialog, list_box);
+  gtk_widget_class_bind_template_child (widget_class, GbpBuilduiRunnablesDialog, spinner);
+}
+
+static void
+gbp_buildui_runnables_dialog_init (GbpBuilduiRunnablesDialog *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+}
diff --git a/src/plugins/buildui/gbp-buildui-runnables-dialog.h 
b/src/plugins/buildui/gbp-buildui-runnables-dialog.h
new file mode 100644
index 000000000..1c3b9b69e
--- /dev/null
+++ b/src/plugins/buildui/gbp-buildui-runnables-dialog.h
@@ -0,0 +1,35 @@
+/* gbp-buildui-runnables-dialog.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <adwaita.h>
+
+#include <libide-foundry.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_BUILDUI_RUNNABLES_DIALOG (gbp_buildui_runnables_dialog_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpBuilduiRunnablesDialog, gbp_buildui_runnables_dialog, GBP, 
BUILDUI_RUNNABLES_DIALOG, AdwWindow)
+
+GtkWidget *gbp_buildui_runnables_dialog_new (IdeContext *context);
+
+G_END_DECLS
diff --git a/src/plugins/buildui/gbp-buildui-runnables-dialog.ui 
b/src/plugins/buildui/gbp-buildui-runnables-dialog.ui
new file mode 100644
index 000000000..d5906c0c3
--- /dev/null
+++ b/src/plugins/buildui/gbp-buildui-runnables-dialog.ui
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GbpBuilduiRunnablesDialog" parent="AdwWindow">
+    <property name="title" translatable="yes">Select Run Command</property>
+    <property name="default-width">700</property>
+    <property name="default-height">550</property>
+    <child type="content">
+      <object class="GtkBox">
+        <property name="orientation">vertical</property>
+        <child>
+          <object class="AdwHeaderBar">
+          </object>
+        </child>
+        <child>
+          <object class="AdwPreferencesPage">
+            <child>
+              <object class="AdwPreferencesGroup">
+                <property name="title" translatable="yes">Available Run Commands</property>
+                <property name="description" translatable="yes">Some run commands may not be available until 
the project has been configured.</property>
+                <property name="header-suffix">
+                  <object class="GtkSpinner" id="spinner">
+                    <property name="spinning" bind-source="GbpBuilduiRunnablesDialog" bind-property="busy" 
bind-flags="sync-create"/>
+                  </object>
+                </property>
+                <child>
+                  <object class="AdwActionRow">
+                    <property name="title" translatable="yes">Project Default</property>
+                    <property name="subtitle" translatable="yes">This selection allows the build system to 
choose the best default candidate.</property>
+                    <property name="activatable-widget">default_runnable</property>
+                    <child type="suffix">
+                      <object class="GtkCheckButton" id="default_runnable">
+                        <property name="action-name">run-manager.default-run-command</property>
+                        <property name="action-target">''</property>
+                        <style>
+                          <class name="checkimage"/>
+                        </style>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+                <child>
+                  <object class="GtkListBox" id="list_box">
+                    <property name="margin-top">12</property>
+                    <property name="selection-mode">none</property>
+                    <style>
+                      <class name="boxed-list"/>
+                    </style>
+                  </object>
+                </child>
+              </object>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/plugins/buildui/gbp-buildui-workspace-addin.c 
b/src/plugins/buildui/gbp-buildui-workspace-addin.c
index 3aea73dac..bf9c8d7fe 100644
--- a/src/plugins/buildui/gbp-buildui-workspace-addin.c
+++ b/src/plugins/buildui/gbp-buildui-workspace-addin.c
@@ -30,6 +30,7 @@
 #include "gbp-buildui-log-pane.h"
 #include "gbp-buildui-omni-bar-section.h"
 #include "gbp-buildui-pane.h"
+#include "gbp-buildui-runnables-dialog.h"
 #include "gbp-buildui-status-indicator.h"
 #include "gbp-buildui-status-popover.h"
 #include "gbp-buildui-targets-dialog.h"
@@ -203,6 +204,27 @@ select_build_target_action (GSimpleAction *action,
   gtk_window_present (GTK_WINDOW (dialog));
 }
 
+static void
+select_run_command_action (GSimpleAction *action,
+                           GVariant      *param,
+                           gpointer       user_data)
+{
+  GbpBuilduiWorkspaceAddin *self = user_data;
+  GbpBuilduiRunnablesDialog *dialog;
+  IdeContext *context;
+
+  g_assert (G_IS_SIMPLE_ACTION (action));
+
+  context = ide_workspace_get_context (self->workspace);
+  dialog = g_object_new (GBP_TYPE_BUILDUI_RUNNABLES_DIALOG,
+                         "context", context,
+                         "transient-for", self->workspace,
+                         "modal", TRUE,
+                         NULL);
+
+  gtk_window_present (GTK_WINDOW (dialog));
+}
+
 static void
 show_status_popover (GSimpleAction *action,
                      GVariant      *param,
@@ -228,6 +250,7 @@ show_status_popover (GSimpleAction *action,
 static const GActionEntry actions[] = {
   { "show-build-log", on_view_output_cb },
   { "select-build-target", select_build_target_action },
+  { "select-run-command", select_run_command_action },
   { "show-build-status-popover", show_status_popover, "s" },
 };
 
diff --git a/src/plugins/buildui/gtk/menus.ui b/src/plugins/buildui/gtk/menus.ui
index a045d2219..6cdfccd89 100644
--- a/src/plugins/buildui/gtk/menus.ui
+++ b/src/plugins/buildui/gtk/menus.ui
@@ -111,12 +111,9 @@
   </menu>
   <menu id="run-menu">
     <section id="run-command-section">
-      <submenu id="run-commands">
-        <attribute name="label" translatable="yes">Run Command</attribute>
-      </submenu>
       <item>
-        <attribute name="label" translatable="yes">Custom Command…</attribute>
-        <attribute name="action">runui.custom</attribute>
+        <attribute name="label" translatable="yes">Select Run Command…</attribute>
+        <attribute name="action">win.select-run-command</attribute>
       </item>
     </section>
     <section id="run-menu-observation-section">
diff --git a/src/plugins/buildui/meson.build b/src/plugins/buildui/meson.build
index 0cb5fed4b..0020afbaf 100644
--- a/src/plugins/buildui/meson.build
+++ b/src/plugins/buildui/meson.build
@@ -6,6 +6,7 @@ plugins_sources += files([
   'gbp-buildui-omni-bar-section.c',
   'gbp-buildui-pane.c',
   'gbp-buildui-preferences-addin.c',
+  'gbp-buildui-runnables-dialog.c',
   'gbp-buildui-runtime-categories.c',
   'gbp-buildui-runtime-row.c',
   'gbp-buildui-stage-row.c',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]