[gnome-builder] gui: add helper to create cancellation action



commit 0b28d71c694f835dd0c2bd35b430542a4b6e009e
Author: Christian Hergert <chergert redhat com>
Date:   Tue May 4 20:21:25 2021 -0700

    gui: add helper to create cancellation action
    
    This just simplifies the process of creating an action tied to a
    GCancellable.

 src/libide/gui/ide-application-actions.c | 38 ++++++++++++++++++++++++++++++++
 src/libide/gui/ide-application.h         |  3 +++
 2 files changed, 41 insertions(+)
---
diff --git a/src/libide/gui/ide-application-actions.c b/src/libide/gui/ide-application-actions.c
index c06d8c76b..7fdc7cf3b 100644
--- a/src/libide/gui/ide-application-actions.c
+++ b/src/libide/gui/ide-application-actions.c
@@ -457,3 +457,41 @@ _ide_application_init_actions (IdeApplication *self)
                                    G_N_ELEMENTS (IdeApplicationActions),
                                    self);
 }
+
+static void
+cancellable_weak_notify (gpointer  data,
+                         GObject  *where_object_was)
+{
+  g_autofree char *name = data;
+  g_action_map_remove_action (G_ACTION_MAP (IDE_APPLICATION_DEFAULT), name);
+}
+
+char *
+ide_application_create_cancel_action (IdeApplication *self,
+                                      GCancellable   *cancellable)
+{
+  static guint cancel_count;
+  g_autofree char *action_name = NULL;
+  g_autofree char *detailed_action_name = NULL;
+  g_autoptr(GSimpleAction) action = NULL;
+  guint count;
+
+  g_return_val_if_fail (IDE_IS_APPLICATION (self), NULL);
+  g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), NULL);
+
+  count = ++cancel_count;
+  action_name = g_strdup_printf ("cancel_%u", count);
+  detailed_action_name = g_strdup_printf ("app.cancel_%u", count);
+  action = g_simple_action_new (action_name, NULL);
+  g_signal_connect_object (action,
+                           "activate",
+                           G_CALLBACK (g_cancellable_cancel),
+                           cancellable,
+                           G_CONNECT_SWAPPED);
+  g_object_weak_ref (G_OBJECT (cancellable),
+                     cancellable_weak_notify,
+                     g_steal_pointer (&action_name));
+  g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (action));
+
+  return g_steal_pointer (&detailed_action_name);
+}
diff --git a/src/libide/gui/ide-application.h b/src/libide/gui/ide-application.h
index 8dca6709e..2f5306ad2 100644
--- a/src/libide/gui/ide-application.h
+++ b/src/libide/gui/ide-application.h
@@ -92,5 +92,8 @@ IdeWorkbench  *ide_application_find_workbench_for_file  (IdeApplication
 IDE_AVAILABLE_IN_3_34
 gpointer       ide_application_find_addin_by_module_name (IdeApplication           *self,
                                                           const gchar              *module_name);
+IDE_AVAILABLE_IN_3_42
+char          *ide_application_create_cancel_action      (IdeApplication           *self,
+                                                          GCancellable             *cancellable);
 
 G_END_DECLS


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