[gnome-text-editor/wip/exalm/message-dialog: 1/2] Port to AdwMessageDialog




commit 4a1e901babbae41849dc43fe4341f7d7cec85122
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Mon Jun 27 22:56:59 2022 +0400

    Port to AdwMessageDialog

 src/editor-page.c                | 14 ++----
 src/editor-save-changes-dialog.c | 58 +++++++++++--------------
 src/editor-window-actions.c      | 92 ++++++++++++++--------------------------
 3 files changed, 59 insertions(+), 105 deletions(-)
---
diff --git a/src/editor-page.c b/src/editor-page.c
index bd0e428..1167bbd 100644
--- a/src/editor-page.c
+++ b/src/editor-page.c
@@ -1072,17 +1072,9 @@ editor_page_save_cb (GObject      *object,
   g_warning ("Failed to save document: %s", error->message);
 
   parent = gtk_widget_get_native (GTK_WIDGET (self));
-  dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
-                                   GTK_DIALOG_MODAL | GTK_DIALOG_USE_HEADER_BAR,
-                                   GTK_MESSAGE_WARNING,
-                                   GTK_BUTTONS_CLOSE,
-                                   "%s",
-                                   _("Failed to save document"));
-  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
-  g_signal_connect (dialog,
-                    "response",
-                    G_CALLBACK (gtk_window_destroy),
-                    NULL);
+  dialog = adw_message_dialog_new (GTK_WINDOW (parent), _("Failed to save document"), NULL);
+  adw_message_dialog_add_response (ADW_MESSAGE_DIALOG (dialog), "close", _("_Close"));
+  adw_message_dialog_format_body (ADW_MESSAGE_DIALOG (dialog), "%s", error->message);
   gtk_window_present (GTK_WINDOW (dialog));
 }
 
diff --git a/src/editor-save-changes-dialog.c b/src/editor-save-changes-dialog.c
index 644bfc8..ddebaea 100644
--- a/src/editor-save-changes-dialog.c
+++ b/src/editor-save-changes-dialog.c
@@ -45,7 +45,7 @@ typedef struct
   GtkCheckButton *check;
 
   /* Duplicated backpointer because lazy */
-  GtkMessageDialog *dialog;
+  AdwMessageDialog *dialog;
 } SaveRequest;
 
 static void
@@ -63,7 +63,7 @@ static void
 editor_save_changes_dialog_remove (GArray *requests,
                                    guint   index)
 {
-  g_autoptr(GtkMessageDialog) dialog = NULL;
+  g_autoptr(AdwMessageDialog) dialog = NULL;
 
   g_assert (requests != NULL);
   g_assert (index < requests->len);
@@ -81,7 +81,6 @@ editor_save_changes_dialog_remove (GArray *requests,
 
       task = g_object_get_data (G_OBJECT (dialog), "TASK");
       g_task_return_boolean (task, TRUE);
-      gtk_window_destroy (GTK_WINDOW (dialog));
     }
 }
 
@@ -121,7 +120,7 @@ static void
 editor_save_changes_dialog_discard (GtkMessageDialog *dialog,
                                     GArray           *requests)
 {
-  g_assert (GTK_IS_MESSAGE_DIALOG (dialog));
+  g_assert (ADW_IS_MESSAGE_DIALOG (dialog));
   g_assert (requests != NULL);
   g_assert (requests->len > 0);
 
@@ -185,7 +184,7 @@ static void
 editor_save_changes_dialog_save (GtkMessageDialog *dialog,
                                  GArray           *requests)
 {
-  g_assert (GTK_IS_MESSAGE_DIALOG (dialog));
+  g_assert (ADW_IS_MESSAGE_DIALOG (dialog));
   g_assert (requests != NULL);
   g_assert (requests->len > 0);
 
@@ -211,16 +210,16 @@ editor_save_changes_dialog_save (GtkMessageDialog *dialog,
 
 static void
 editor_save_changes_dialog_response (GtkMessageDialog *dialog,
-                                     int               response,
+                                     const char       *response,
                                      GArray           *requests)
 {
-  g_assert (GTK_IS_MESSAGE_DIALOG (dialog));
+  g_assert (ADW_IS_MESSAGE_DIALOG (dialog));
 
-  if (response == GTK_RESPONSE_NO)
+  if (!g_strcmp0 (response, "discard"))
     {
       editor_save_changes_dialog_discard (dialog, requests);
     }
-  else if (response == GTK_RESPONSE_YES)
+  else if (!g_strcmp0 (response, "save"))
     {
       editor_save_changes_dialog_save (dialog, requests);
     }
@@ -231,7 +230,6 @@ editor_save_changes_dialog_response (GtkMessageDialog *dialog,
                                G_IO_ERROR,
                                G_IO_ERROR_CANCELLED,
                                "The user cancelled the request");
-      gtk_window_destroy (GTK_WINDOW (dialog));
     }
 }
 
@@ -244,8 +242,6 @@ _editor_save_changes_dialog_new (GtkWindow *parent,
   PangoAttrList *smaller;
   GtkWidget *dialog;
   GtkWidget *group;
-  GtkWidget *area;
-  GtkWidget *button;
 
   g_return_val_if_fail (!parent || GTK_IS_WINDOW (parent), NULL);
   g_return_val_if_fail (pages != NULL, NULL);
@@ -257,28 +253,22 @@ _editor_save_changes_dialog_new (GtkWindow *parent,
 
   discard_label = g_dngettext (GETTEXT_PACKAGE, _("_Discard"), _("_Discard All"), pages->len);
 
-  dialog = gtk_message_dialog_new (parent,
-                                   GTK_DIALOG_MODAL | GTK_DIALOG_USE_HEADER_BAR,
-                                   GTK_MESSAGE_QUESTION,
-                                   GTK_BUTTONS_NONE,
-                                   _("Save Changes?"));
-  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
-                                            _("Open documents contain unsaved changes. Changes which are not 
saved will be permanently lost."));
-  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
-                          _("_Cancel"), GTK_RESPONSE_CANCEL,
-                          discard_label, GTK_RESPONSE_NO,
-                          _("_Save"), GTK_RESPONSE_YES,
-                          NULL);
-
-  button = gtk_dialog_get_widget_for_response (GTK_DIALOG (dialog), GTK_RESPONSE_NO);
-  gtk_widget_add_css_class (button, "destructive-action");
-
-  button = gtk_dialog_get_widget_for_response (GTK_DIALOG (dialog), GTK_RESPONSE_YES);
-  gtk_widget_add_css_class (button, "suggested-action");
-
-  area = gtk_message_dialog_get_message_area (GTK_MESSAGE_DIALOG (dialog));
+  dialog = adw_message_dialog_new (parent,
+                                   _("Save Changes?"),
+                                   _("Open documents contain unsaved changes. Changes which are not saved 
will be permanently lost."));
+
+  adw_message_dialog_add_responses (ADW_MESSAGE_DIALOG (dialog),
+                                    "cancel", _("_Cancel"),
+                                    "discard", discard_label,
+                                    "save", _("_Save"),
+                                    NULL);
+  adw_message_dialog_set_response_appearance (ADW_MESSAGE_DIALOG (dialog),
+                                              "discard", ADW_RESPONSE_DESTRUCTIVE);
+  adw_message_dialog_set_response_appearance (ADW_MESSAGE_DIALOG (dialog),
+                                              "save", ADW_RESPONSE_SUGGESTED);
+
   group = adw_preferences_group_new ();
-  gtk_box_append (GTK_BOX (area), group);
+  adw_message_dialog_set_extra_child (ADW_MESSAGE_DIALOG (dialog), group);
 
   smaller = pango_attr_list_new ();
   pango_attr_list_insert (smaller, pango_attr_scale_new (0.8333));
@@ -334,7 +324,7 @@ _editor_save_changes_dialog_new (GtkWindow *parent,
       sr.document = g_object_ref (document);
       sr.check = GTK_CHECK_BUTTON (check);
       sr.page = g_object_ref (page);
-      sr.dialog = g_object_ref (GTK_MESSAGE_DIALOG (dialog));
+      sr.dialog = g_object_ref (ADW_MESSAGE_DIALOG (dialog));
 
       /* Use NULL for the default file, otherwise set a file
        * so that we write to it instead of the draft.
diff --git a/src/editor-window-actions.c b/src/editor-window-actions.c
index d319aee..ff2548a 100644
--- a/src/editor-window-actions.c
+++ b/src/editor-window-actions.c
@@ -89,20 +89,6 @@ editor_window_actions_save_cb (GtkWidget  *widget,
     _editor_page_save (page);
 }
 
-static void
-editor_window_actions_confirm_save_response_cb (GtkMessageDialog *dialog,
-                                                int               response,
-                                                EditorPage       *page)
-{
-  g_assert (GTK_IS_MESSAGE_DIALOG (dialog));
-  g_assert (EDITOR_IS_PAGE (page));
-
-  if (response == GTK_RESPONSE_YES)
-    _editor_page_save (page);
-
-  gtk_window_destroy (GTK_WINDOW (dialog));
-}
-
 static void
 editor_window_actions_confirm_save_cb (GtkWidget  *widget,
                                        const char *action_name,
@@ -112,7 +98,6 @@ editor_window_actions_confirm_save_cb (GtkWidget  *widget,
   g_autofree gchar *title = NULL;
   EditorPage *page;
   GtkWidget *dialog;
-  GtkWidget *save;
 
   g_assert (EDITOR_IS_WINDOW (self));
 
@@ -120,24 +105,25 @@ editor_window_actions_confirm_save_cb (GtkWidget  *widget,
     return;
 
   title = editor_page_dup_title (page);
-  dialog = gtk_message_dialog_new (GTK_WINDOW (self),
-                                   GTK_DIALOG_MODAL,
-                                   GTK_MESSAGE_QUESTION,
-                                   GTK_BUTTONS_NONE,
-                                   /* translators: %s is replaced with the document title */
-                                   _("Save Changes to “%s”?"),
-                                   title);
-  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
-                                            _("Saving changes will replace the previously saved version."));
-  gtk_dialog_add_button (GTK_DIALOG (dialog), _("Cancel"), GTK_RESPONSE_CANCEL);
-  save = gtk_dialog_add_button (GTK_DIALOG (dialog), _("Save"), GTK_RESPONSE_YES);
-  gtk_widget_add_css_class (save, "destructive-action");
+  dialog = adw_message_dialog_new (GTK_WINDOW (self),
+                                   NULL,
+                                   _("Saving changes will replace the previously saved version."));
+  adw_message_dialog_format_heading (ADW_MESSAGE_DIALOG (dialog),
+                                     /* translators: %s is replaced with the document title */
+                                     _("Save Changes to “%s”?"),
+                                     title);
+  adw_message_dialog_add_responses (ADW_MESSAGE_DIALOG (dialog),
+                                    "cancel", _("_Cancel"),
+                                    "save", _("_Save"),
+                                    NULL);
+  adw_message_dialog_set_response_appearance (ADW_MESSAGE_DIALOG (dialog),
+                                              "save", ADW_RESPONSE_DESTRUCTIVE);
 
   g_signal_connect_object (dialog,
-                           "response",
-                           G_CALLBACK (editor_window_actions_confirm_save_response_cb),
+                           "response::save",
+                           G_CALLBACK (_editor_page_save),
                            page,
-                           0);
+                           G_CONNECT_SWAPPED);
 
   gtk_window_present (GTK_WINDOW (dialog));
 }
@@ -228,20 +214,6 @@ editor_window_actions_discard_changes_cb (GtkWidget  *widget,
     _editor_page_discard_changes (page);
 }
 
-static void
-editor_window_actions_confirm_discard_response_cb (GtkMessageDialog *dialog,
-                                                   int               response,
-                                                   EditorPage       *page)
-{
-  g_assert (GTK_IS_MESSAGE_DIALOG (dialog));
-  g_assert (EDITOR_IS_PAGE (page));
-
-  if (response == GTK_RESPONSE_YES)
-    _editor_page_discard_changes (page);
-
-  gtk_window_destroy (GTK_WINDOW (dialog));
-}
-
 static void
 editor_window_actions_confirm_discard_changes_cb (GtkWidget  *widget,
                                                   const char *action_name,
@@ -251,7 +223,6 @@ editor_window_actions_confirm_discard_changes_cb (GtkWidget  *widget,
   g_autofree gchar *title = NULL;
   EditorPage *page;
   GtkWidget *dialog;
-  GtkWidget *discard;
 
   g_assert (EDITOR_IS_WINDOW (self));
 
@@ -259,24 +230,25 @@ editor_window_actions_confirm_discard_changes_cb (GtkWidget  *widget,
     return;
 
   title = editor_page_dup_title (page);
-  dialog = gtk_message_dialog_new (GTK_WINDOW (self),
-                                   GTK_DIALOG_MODAL,
-                                   GTK_MESSAGE_QUESTION,
-                                   GTK_BUTTONS_NONE,
-                                   /* translators: %s is replaced with the document title */
-                                   _("Discard Changes to “%s”?"),
-                                   title);
-  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
-                                            _("Unsaved changes will be permanently lost."));
-  gtk_dialog_add_button (GTK_DIALOG (dialog), _("Cancel"), GTK_RESPONSE_CANCEL);
-  discard = gtk_dialog_add_button (GTK_DIALOG (dialog), _("Discard"), GTK_RESPONSE_YES);
-  gtk_widget_add_css_class (discard, "destructive-action");
+  dialog = adw_message_dialog_new (GTK_WINDOW (self),
+                                   NULL,
+                                   _("Unsaved changes will be permanently lost."));
+  adw_message_dialog_format_heading (ADW_MESSAGE_DIALOG (dialog),
+                                     /* translators: %s is replaced with the document title */
+                                     _("Discard Changes to “%s”?"),
+                                     title);
+  adw_message_dialog_add_responses (ADW_MESSAGE_DIALOG (dialog),
+                                    "cancel", _("_Cancel"),
+                                    "discard", _("_Discard"),
+                                    NULL);
+  adw_message_dialog_set_response_appearance (ADW_MESSAGE_DIALOG (dialog),
+                                              "discard", ADW_RESPONSE_DESTRUCTIVE);
 
   g_signal_connect_object (dialog,
-                           "response",
-                           G_CALLBACK (editor_window_actions_confirm_discard_response_cb),
+                           "response::discard",
+                           G_CALLBACK (_editor_page_discard_changes),
                            page,
-                           0);
+                           G_CONNECT_SWAPPED);
 
   gtk_window_present (GTK_WINDOW (dialog));
 }


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