[gnome-builder/wip/gtk4-port: 1502/1774] plugins/shellcmd: delete new command if cancelled




commit 0807a10a253da6771923e8c341bbfceb7ca7b25f
Author: Christian Hergert <chergert redhat com>
Date:   Tue Jun 14 11:29:07 2022 -0700

    plugins/shellcmd: delete new command if cancelled
    
    We do insert the command immediately so that we can deal with it in
    GSettings so that means we need to delete it if the editing is cancelled.
    
    Not totally ideal from an atomicity aspect, but good enough since
    everything has GSettings property bindings.

 src/plugins/shellcmd/gbp-shellcmd-command-dialog.c | 48 +++++++++++++++++++++-
 src/plugins/shellcmd/gbp-shellcmd-command-dialog.h |  3 +-
 .../shellcmd/gbp-shellcmd-command-dialog.ui        |  5 ++-
 .../shellcmd/gbp-shellcmd-preferences-addin.c      |  4 +-
 4 files changed, 53 insertions(+), 7 deletions(-)
---
diff --git a/src/plugins/shellcmd/gbp-shellcmd-command-dialog.c 
b/src/plugins/shellcmd/gbp-shellcmd-command-dialog.c
index 019d65c03..b31ab6d13 100644
--- a/src/plugins/shellcmd/gbp-shellcmd-command-dialog.c
+++ b/src/plugins/shellcmd/gbp-shellcmd-command-dialog.c
@@ -40,13 +40,17 @@ struct _GbpShellcmdCommandDialog
   GtkStringList         *envvars;
   GtkListBox            *envvars_list_box;
   GtkLabel              *shortcut_label;
+  GtkButton             *save;
 
   char                  *accel;
+
+  guint                  delete_on_cancel : 1;
 };
 
 enum {
   PROP_0,
   PROP_COMMAND,
+  PROP_DELETE_ON_CANCEL,
   N_PROPS
 };
 
@@ -289,6 +293,25 @@ command_delete_action (GtkWidget  *widget,
   IDE_EXIT;
 }
 
+static void
+command_cancel_action (GtkWidget  *widget,
+                       const char *action_name,
+                       GVariant   *param)
+{
+  GbpShellcmdCommandDialog *self = (GbpShellcmdCommandDialog *)widget;
+
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_SHELLCMD_COMMAND_DIALOG (self));
+
+  if (self->delete_on_cancel)
+    gbp_shellcmd_run_command_delete (self->command);
+
+  gtk_window_destroy (GTK_WINDOW (self));
+
+  IDE_EXIT;
+}
+
 static void
 command_save_action (GtkWidget  *widget,
                      const char *action_name,
@@ -343,6 +366,10 @@ gbp_shellcmd_command_dialog_get_property (GObject    *object,
       g_value_set_object (value, self->command);
       break;
 
+    case PROP_DELETE_ON_CANCEL:
+      g_value_set_boolean (value, self->delete_on_cancel);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -362,6 +389,15 @@ gbp_shellcmd_command_dialog_set_property (GObject      *object,
       gbp_shellcmd_command_dialog_set_command (self, g_value_get_object (value));
       break;
 
+    case PROP_DELETE_ON_CANCEL:
+      self->delete_on_cancel = g_value_get_boolean (value);
+      if (self->delete_on_cancel)
+        {
+          gtk_window_set_title (GTK_WINDOW (self), _("Create Command"));
+          gtk_button_set_label (self->save, _("Cre_ate"));
+        }
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -384,10 +420,17 @@ gbp_shellcmd_command_dialog_class_init (GbpShellcmdCommandDialogClass *klass)
                           G_PARAM_CONSTRUCT_ONLY |
                           G_PARAM_STATIC_STRINGS));
 
+  properties [PROP_DELETE_ON_CANCEL] =
+    g_param_spec_boolean ("delete-on-cancel", NULL, NULL, FALSE,
+                         (G_PARAM_READWRITE |
+                          G_PARAM_CONSTRUCT_ONLY |
+                          G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_properties (object_class, N_PROPS, properties);
 
   gtk_widget_class_install_action (widget_class, "command.save", NULL, command_save_action);
   gtk_widget_class_install_action (widget_class, "command.delete", NULL, command_delete_action);
+  gtk_widget_class_install_action (widget_class, "command.cancel", NULL, command_cancel_action);
 
   gtk_widget_class_set_template_from_resource (widget_class, 
"/plugins/shellcmd/gbp-shellcmd-command-dialog.ui");
   gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandDialog, argv);
@@ -395,6 +438,7 @@ gbp_shellcmd_command_dialog_class_init (GbpShellcmdCommandDialogClass *klass)
   gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandDialog, envvars_list_box);
   gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandDialog, location);
   gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandDialog, name);
+  gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandDialog, save);
   gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandDialog, shortcut_label);
   gtk_widget_class_bind_template_callback (widget_class, on_env_entry_changed_cb);
   gtk_widget_class_bind_template_callback (widget_class, on_env_entry_activate_cb);
@@ -419,11 +463,13 @@ gbp_shellcmd_command_dialog_init (GbpShellcmdCommandDialog *self)
 }
 
 GbpShellcmdCommandDialog *
-gbp_shellcmd_command_dialog_new (GbpShellcmdRunCommand *command)
+gbp_shellcmd_command_dialog_new (GbpShellcmdRunCommand *command,
+                                 gboolean               delete_on_cancel)
 {
   g_return_val_if_fail (GBP_IS_SHELLCMD_RUN_COMMAND (command), NULL);
 
   return g_object_new (GBP_TYPE_SHELLCMD_COMMAND_DIALOG,
                        "command", command,
+                       "delete-on-cancel", delete_on_cancel,
                        NULL);
 }
diff --git a/src/plugins/shellcmd/gbp-shellcmd-command-dialog.h 
b/src/plugins/shellcmd/gbp-shellcmd-command-dialog.h
index f7765bae8..abefa6af3 100644
--- a/src/plugins/shellcmd/gbp-shellcmd-command-dialog.h
+++ b/src/plugins/shellcmd/gbp-shellcmd-command-dialog.h
@@ -32,6 +32,7 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GbpShellcmdCommandDialog, gbp_shellcmd_command_dialog, GBP, SHELLCMD_COMMAND_DIALOG, 
AdwWindow)
 
-GbpShellcmdCommandDialog *gbp_shellcmd_command_dialog_new (GbpShellcmdRunCommand *command);
+GbpShellcmdCommandDialog *gbp_shellcmd_command_dialog_new (GbpShellcmdRunCommand *command,
+                                                           gboolean               delete_on_cancel);
 
 G_END_DECLS
diff --git a/src/plugins/shellcmd/gbp-shellcmd-command-dialog.ui 
b/src/plugins/shellcmd/gbp-shellcmd-command-dialog.ui
index c77a2c7ab..61c23af1b 100644
--- a/src/plugins/shellcmd/gbp-shellcmd-command-dialog.ui
+++ b/src/plugins/shellcmd/gbp-shellcmd-command-dialog.ui
@@ -3,6 +3,7 @@
   <template class="GbpShellcmdCommandDialog" parent="AdwWindow">
     <property name="default-width">650</property>
     <property name="default-height">650</property>
+    <property name="title" translatable="yes">Edit Command</property>
     <child>
       <object class="GtkBox">
         <property name="orientation">vertical</property>
@@ -14,12 +15,12 @@
               <object class="GtkButton" id="cancel">
                 <property name="label" translatable="yes">_Cancel</property>
                 <property name="use-underline">true</property>
-                <property name="action-name">window.close</property>
+                <property name="action-name">command.cancel</property>
               </object>
             </child>
             <child type="end">
               <object class="GtkButton" id="save">
-                <property name="label" translatable="yes">_Apply</property>
+                <property name="label" translatable="yes">S_ave</property>
                 <property name="action-name">command.save</property>
                 <property name="use-underline">true</property>
                 <style>
diff --git a/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.c 
b/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.c
index 936b9f948..64f8ea1e7 100644
--- a/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.c
+++ b/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.c
@@ -108,9 +108,7 @@ on_row_activated_cb (GtkListBox           *list_box,
       command = new_command;
     }
 
-  dialog = gbp_shellcmd_command_dialog_new (command);
-  gtk_window_set_title (GTK_WINDOW (dialog),
-                        new_command ? _("Create Command") : _("Edit Command"));
+  dialog = gbp_shellcmd_command_dialog_new (command, new_command != NULL);
   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window));
   gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
   gtk_window_present (GTK_WINDOW (dialog));


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