[gnome-builder/wip/chergert/shortcut-editing] plugins/shortcutui: wire up dialog to set shortcut



commit 7ecee6c7b8d6acad5d89675e83941ffb0610e485
Author: Christian Hergert <chergert redhat com>
Date:   Tue Oct 4 22:25:01 2022 -0700

    plugins/shortcutui: wire up dialog to set shortcut
    
    This doesn't actually apply the shortcut yet, but it does get things into
    place to accept input from the user with the shortcut dialog.

 src/plugins/shortcutui/gbp-shortcutui-action.c |   6 ++
 src/plugins/shortcutui/gbp-shortcutui-action.h |  13 +--
 src/plugins/shortcutui/gbp-shortcutui-dialog.c | 114 ++++++++++++++++++++++---
 src/plugins/shortcutui/gbp-shortcutui-row.c    |   8 ++
 src/plugins/shortcutui/gbp-shortcutui-row.h    |   5 +-
 5 files changed, 124 insertions(+), 22 deletions(-)
---
diff --git a/src/plugins/shortcutui/gbp-shortcutui-action.c b/src/plugins/shortcutui/gbp-shortcutui-action.c
index 89d3dfc87..8472379be 100644
--- a/src/plugins/shortcutui/gbp-shortcutui-action.c
+++ b/src/plugins/shortcutui/gbp-shortcutui-action.c
@@ -282,6 +282,12 @@ gbp_shortcutui_action_compare (const GbpShortcutuiAction *a,
   return 0;
 }
 
+const char *
+gbp_shortcutui_action_get_accelerator (const GbpShortcutuiAction *self)
+{
+  return self->accelerator;
+}
+
 gboolean
 gbp_shortcutui_action_is_same_group (const GbpShortcutuiAction *a,
                                      const GbpShortcutuiAction *b)
diff --git a/src/plugins/shortcutui/gbp-shortcutui-action.h b/src/plugins/shortcutui/gbp-shortcutui-action.h
index 9f3e08f8e..1f0d8ae41 100644
--- a/src/plugins/shortcutui/gbp-shortcutui-action.h
+++ b/src/plugins/shortcutui/gbp-shortcutui-action.h
@@ -28,11 +28,12 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GbpShortcutuiAction, gbp_shortcutui_action, GBP, SHORTCUTUI_ACTION, GObject)
 
-const char *gbp_shortcutui_action_get_page      (const GbpShortcutuiAction *self);
-const char *gbp_shortcutui_action_get_group     (const GbpShortcutuiAction *self);
-int         gbp_shortcutui_action_compare       (const GbpShortcutuiAction *a,
-                                                 const GbpShortcutuiAction *b);
-gboolean    gbp_shortcutui_action_is_same_group (const GbpShortcutuiAction *a,
-                                                 const GbpShortcutuiAction *b);
+const char *gbp_shortcutui_action_get_accelerator (const GbpShortcutuiAction *self);
+const char *gbp_shortcutui_action_get_page        (const GbpShortcutuiAction *self);
+const char *gbp_shortcutui_action_get_group       (const GbpShortcutuiAction *self);
+int         gbp_shortcutui_action_compare         (const GbpShortcutuiAction *a,
+                                                   const GbpShortcutuiAction *b);
+gboolean    gbp_shortcutui_action_is_same_group   (const GbpShortcutuiAction *a,
+                                                   const GbpShortcutuiAction *b);
 
 G_END_DECLS
diff --git a/src/plugins/shortcutui/gbp-shortcutui-dialog.c b/src/plugins/shortcutui/gbp-shortcutui-dialog.c
index 4778c3eda..db09bcf72 100644
--- a/src/plugins/shortcutui/gbp-shortcutui-dialog.c
+++ b/src/plugins/shortcutui/gbp-shortcutui-dialog.c
@@ -22,6 +22,8 @@
 
 #include "config.h"
 
+#include <glib/gi18n.h>
+
 #include <libide-gui.h>
 
 #include "gbp-shortcutui-action.h"
@@ -56,19 +58,6 @@ gbp_shortcutui_dialog_update_header_cb (GtkListBoxRow *row,
                                     GBP_SHORTCUTUI_ROW (before));
 }
 
-static GtkWidget *
-gbp_shortcutui_dialog_create_row_cb (gpointer item,
-                                     gpointer user_data)
-{
-  GbpShortcutuiAction *action = item;
-
-  g_assert (GBP_IS_SHORTCUTUI_ACTION (action));
-
-  return g_object_new (GBP_TYPE_SHORTCUTUI_ROW,
-                       "action", action,
-                       NULL);
-}
-
 static gboolean
 gbp_shortcutui_dialog_update_visible (gpointer user_data)
 {
@@ -144,6 +133,97 @@ gbp_shortcutui_dialog_group_header_cb (GtkListBoxRow *row,
     }
 }
 
+static void
+set_accel (GbpShortcutuiDialog *self,
+           const char          *accel)
+{
+  g_assert (GBP_IS_SHORTCUTUI_DIALOG (self));
+
+  /* TODO: Set accel for dialog */
+  g_printerr ("Set accel to %s\n", accel);
+}
+
+static void
+shortcut_dialog_response_cb (GbpShortcutuiDialog    *self,
+                             int                     response_id,
+                             IdeShortcutAccelDialog *dialog)
+{
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_SHORTCUTUI_DIALOG (self));
+  g_assert (IDE_IS_SHORTCUT_ACCEL_DIALOG (dialog));
+
+  if (response_id == GTK_RESPONSE_ACCEPT)
+    {
+      const char *accel;
+
+      accel = ide_shortcut_accel_dialog_get_accelerator (dialog);
+      set_accel (self, accel);
+    }
+
+  gtk_window_destroy (GTK_WINDOW (dialog));
+
+  IDE_EXIT;
+}
+
+static void
+gbp_shortcutui_dialog_row_activated_cb (GbpShortcutuiDialog *self,
+                                        GbpShortcutuiRow    *row)
+{
+  IdeShortcutAccelDialog *dialog;
+  const char *accel;
+  const char *name;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_SHORTCUTUI_DIALOG (self));
+  g_assert (GBP_IS_SHORTCUTUI_ROW (row));
+
+  name = adw_preferences_row_get_title (ADW_PREFERENCES_ROW (row));
+  accel = gbp_shortcutui_row_get_accelerator (row);
+  dialog = g_object_new (IDE_TYPE_SHORTCUT_ACCEL_DIALOG,
+                         "accelerator", accel,
+                         "transient-for", self,
+                         "modal", TRUE,
+                         "shortcut-title", name,
+                         "title", _("Set Shortcut"),
+                         "use-header-bar", 1,
+                         NULL);
+  g_signal_connect_object (dialog,
+                           "response",
+                           G_CALLBACK (shortcut_dialog_response_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+  gtk_window_present (GTK_WINDOW (dialog));
+
+  IDE_EXIT;
+}
+
+static GtkWidget *
+gbp_shortcutui_dialog_create_row_cb (gpointer item,
+                                     gpointer user_data)
+{
+  GbpShortcutuiAction *action = item;
+  GbpShortcutuiDialog *self = user_data;
+  GtkWidget *row;
+
+  g_assert (GBP_IS_SHORTCUTUI_ACTION (action));
+  g_assert (GBP_IS_SHORTCUTUI_DIALOG (self));
+
+  row = g_object_new (GBP_TYPE_SHORTCUTUI_ROW,
+                      "activatable", TRUE,
+                      "action", action,
+                      NULL);
+  g_signal_connect_object (row,
+                           "activated",
+                           G_CALLBACK (gbp_shortcutui_dialog_row_activated_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+  return row;
+}
+
 void
 gbp_shortcutui_dialog_set_model (GbpShortcutuiDialog *self,
                                  GListModel          *model)
@@ -202,8 +282,14 @@ gbp_shortcutui_dialog_set_model (GbpShortcutuiDialog *self,
           GbpShortcutuiRow *row;
 
           row = g_object_new (GBP_TYPE_SHORTCUTUI_ROW,
+                              "activatable", TRUE,
                               "action", action,
                               NULL);
+          g_signal_connect_object (row,
+                                   "activated",
+                                   G_CALLBACK (gbp_shortcutui_dialog_row_activated_cb),
+                                   self,
+                                   G_CONNECT_SWAPPED);
           adw_expander_row_add_row (last_group_row, GTK_WIDGET (row));
         }
     }
@@ -246,5 +332,5 @@ gbp_shortcutui_dialog_init (GbpShortcutuiDialog *self)
   gtk_list_box_bind_model (self->results_list_box,
                            G_LIST_MODEL (self->filter_model),
                            gbp_shortcutui_dialog_create_row_cb,
-                           NULL, NULL);
+                           self, NULL);
 }
diff --git a/src/plugins/shortcutui/gbp-shortcutui-row.c b/src/plugins/shortcutui/gbp-shortcutui-row.c
index 8e78eedda..7fc8e683b 100644
--- a/src/plugins/shortcutui/gbp-shortcutui-row.c
+++ b/src/plugins/shortcutui/gbp-shortcutui-row.c
@@ -184,3 +184,11 @@ gbp_shortcutui_row_update_header (GbpShortcutuiRow *self,
 
   gtk_list_box_row_set_header (GTK_LIST_BOX_ROW (self), header);
 }
+
+const char *
+gbp_shortcutui_row_get_accelerator (GbpShortcutuiRow *self)
+{
+  g_return_val_if_fail (GBP_IS_SHORTCUTUI_ROW (self), NULL);
+
+  return gbp_shortcutui_action_get_accelerator (self->action);
+}
diff --git a/src/plugins/shortcutui/gbp-shortcutui-row.h b/src/plugins/shortcutui/gbp-shortcutui-row.h
index 5b58ce548..91e7f51db 100644
--- a/src/plugins/shortcutui/gbp-shortcutui-row.h
+++ b/src/plugins/shortcutui/gbp-shortcutui-row.h
@@ -28,7 +28,8 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GbpShortcutuiRow, gbp_shortcutui_row, GBP, SHORTCUTUI_ROW, AdwActionRow)
 
-void gbp_shortcutui_row_update_header (GbpShortcutuiRow *self,
-                                       GbpShortcutuiRow *before);
+const char *gbp_shortcutui_row_get_accelerator (GbpShortcutuiRow *self);
+void        gbp_shortcutui_row_update_header   (GbpShortcutuiRow *self,
+                                                GbpShortcutuiRow *before);
 
 G_END_DECLS


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