[gnome-builder/wip/gtk4-port] plugins/shellcmd: add subtitle/accelerator to rows



commit ddc5af4024665b2787e6076a4eaae3661f0a6866
Author: Christian Hergert <chergert redhat com>
Date:   Tue Jun 14 01:49:15 2022 -0700

    plugins/shellcmd: add subtitle/accelerator to rows

 .../shellcmd/gbp-shellcmd-preferences-addin.c      | 26 +++-----
 src/plugins/shellcmd/gbp-shellcmd-run-command.c    | 78 +++++++++++++++++++++-
 2 files changed, 86 insertions(+), 18 deletions(-)
---
diff --git a/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.c 
b/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.c
index c055ab0a4..936b9f948 100644
--- a/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.c
+++ b/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.c
@@ -38,24 +38,13 @@ struct _GbpShellcmdPreferencesAddin
   GSettings            *settings;
 };
 
-static gboolean
-argv_to_string (GBinding     *binding,
-                const GValue *from_value,
-                GValue       *to_value,
-                gpointer      user_data)
-{
-  const char * const *argv = g_value_get_boxed (from_value);
-  if (argv != NULL)
-    g_value_take_string (to_value, g_strjoinv (" ", (char **)argv));
-  return TRUE;
-}
-
 static GtkWidget *
 gbp_shellcmd_preferences_addin_create_row_cb (gpointer item,
                                               gpointer item_data)
 {
   GbpShellcmdRunCommand *command = item;
   AdwActionRow *row;
+  GtkLabel *accel;
 
   g_assert (GBP_IS_SHELLCMD_RUN_COMMAND (command));
 
@@ -64,9 +53,16 @@ gbp_shellcmd_preferences_addin_create_row_cb (gpointer item,
                       NULL);
   g_object_bind_property (command, "display-name", row, "title",
                           G_BINDING_SYNC_CREATE);
-  g_object_bind_property_full (command, "argv", row, "subtitle",
-                               G_BINDING_SYNC_CREATE,
-                               argv_to_string, NULL, NULL, NULL);
+  g_object_bind_property (command, "subtitle", row, "subtitle",
+                          G_BINDING_SYNC_CREATE);
+
+  accel = g_object_new (GTK_TYPE_LABEL,
+                        "margin-start", 6,
+                        "margin-end", 6,
+                        NULL);
+  g_object_bind_property (command, "accelerator-label", accel, "label",
+                          G_BINDING_SYNC_CREATE);
+  adw_action_row_add_suffix (row, GTK_WIDGET (accel));
   adw_action_row_add_suffix (row,
                              g_object_new (GTK_TYPE_IMAGE,
                                            "icon-name", "go-next-symbolic",
diff --git a/src/plugins/shellcmd/gbp-shellcmd-run-command.c b/src/plugins/shellcmd/gbp-shellcmd-run-command.c
index e2ea356c0..a1d2d5ab1 100644
--- a/src/plugins/shellcmd/gbp-shellcmd-run-command.c
+++ b/src/plugins/shellcmd/gbp-shellcmd-run-command.c
@@ -34,7 +34,9 @@ struct _GbpShellcmdRunCommand
 
 enum {
   PROP_0,
+  PROP_ACCELERATOR_LABEL,
   PROP_SETTINGS_PATH,
+  PROP_SUBTITLE,
   N_PROPS
 };
 
@@ -71,6 +73,59 @@ gbp_shellcmd_run_command_constructed (GObject *object)
   g_settings_bind (self->settings, "accelerator", self, "accelerator", G_SETTINGS_BIND_DEFAULT);
 }
 
+static void
+subtitle_changed_cb (GbpShellcmdRunCommand *self)
+{
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SUBTITLE]);
+}
+
+static char *
+get_subtitle (GbpShellcmdRunCommand *self)
+{
+  g_autofree char *joined = NULL;
+  const char * const *argv;
+  const char *cwd;
+
+  g_assert (GBP_IS_SHELLCMD_RUN_COMMAND (self));
+
+  argv = ide_run_command_get_argv (IDE_RUN_COMMAND (self));
+  cwd = ide_run_command_get_cwd (IDE_RUN_COMMAND (self));
+
+  if (argv != NULL)
+    joined = g_strjoinv (" ", (char **)argv);
+
+  if (joined && cwd)
+    /* something like a bash prompt */
+    return g_strdup_printf ("<tt>%s&gt; %s</tt>", cwd, joined);
+
+  if (cwd)
+    return g_strdup_printf ("%s&gt; ", cwd);
+
+  return g_steal_pointer (&joined);
+}
+
+static void
+accelerator_label_changed_cb (GbpShellcmdRunCommand *self)
+{
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACCELERATOR_LABEL]);
+}
+
+static char *
+get_accelerator_label (GbpShellcmdRunCommand *self)
+{
+  const char *accelerator = ide_run_command_get_accelerator (IDE_RUN_COMMAND (self));
+  GdkModifierType state;
+  guint keyval;
+
+  if (ide_str_empty0 (accelerator))
+    return NULL;
+
+  if (gtk_accelerator_parse (accelerator, &keyval, &state))
+    return gtk_accelerator_get_label (keyval, state);
+
+  return NULL;
+}
+
 static void
 gbp_shellcmd_run_command_dispose (GObject *object)
 {
@@ -93,10 +148,18 @@ gbp_shellcmd_run_command_get_property (GObject    *object,
 
   switch (prop_id)
     {
+    case PROP_ACCELERATOR_LABEL:
+      g_value_take_string (value, get_accelerator_label (self));
+      break;
+
     case PROP_SETTINGS_PATH:
       g_value_set_string (value, self->settings_path);
       break;
 
+    case PROP_SUBTITLE:
+      g_value_take_string (value, get_subtitle (self));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -131,11 +194,17 @@ gbp_shellcmd_run_command_class_init (GbpShellcmdRunCommandClass *klass)
   object_class->get_property = gbp_shellcmd_run_command_get_property;
   object_class->set_property = gbp_shellcmd_run_command_set_property;
 
+  properties [PROP_ACCELERATOR_LABEL] =
+    g_param_spec_string ("accelerator-label", NULL, NULL, NULL,
+                         (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
   properties [PROP_SETTINGS_PATH] =
     g_param_spec_string ("settings-path", NULL, NULL, NULL,
-                         (G_PARAM_READWRITE |
-                          G_PARAM_CONSTRUCT_ONLY |
-                          G_PARAM_STATIC_STRINGS));
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_SUBTITLE] =
+    g_param_spec_string ("subtitle", NULL, NULL, NULL,
+                         (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_properties (object_class, N_PROPS, properties);
 }
@@ -143,6 +212,9 @@ gbp_shellcmd_run_command_class_init (GbpShellcmdRunCommandClass *klass)
 static void
 gbp_shellcmd_run_command_init (GbpShellcmdRunCommand *self)
 {
+  g_signal_connect (self, "notify::accelerator", G_CALLBACK (accelerator_label_changed_cb), NULL);
+  g_signal_connect (self, "notify::cwd", G_CALLBACK (subtitle_changed_cb), NULL);
+  g_signal_connect (self, "notify::argv", G_CALLBACK (subtitle_changed_cb), NULL);
 }
 
 GbpShellcmdRunCommand *


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