From 13f55cd3e664b0fa7c14944572f2b41b73d64d16 Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Fri, 9 Aug 2024 18:30:24 +0200 Subject: [PATCH 1/2] a11y: Extract helper function to set GtkMessageDialog a11y name Extract the existing logic to set an accessible name for the `GtkMessageDialog` based on the message type from `setup_type` to a new helper function `update_accessible_name`. That helper function will be reused and extended in a follow-up commit. --- gtk/gtkmessagedialog.c | 68 +++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/gtk/gtkmessagedialog.c b/gtk/gtkmessagedialog.c index e70c82056c..1de3118e51 100644 --- a/gtk/gtkmessagedialog.c +++ b/gtk/gtkmessagedialog.c @@ -366,12 +366,50 @@ setup_primary_label_font (GtkMessageDialog *dialog) } } +static void +update_accessible_name (GtkMessageDialog *dialog) +{ + AtkObject *atk_obj = gtk_widget_get_accessible (GTK_WIDGET (dialog)); + if (!GTK_IS_ACCESSIBLE (atk_obj)) + return; + + const char *name = NULL; + + switch (dialog->priv->message_type) + { + case GTK_MESSAGE_INFO: + name = _("Information"); + break; + + case GTK_MESSAGE_QUESTION: + name = _("Question"); + break; + + case GTK_MESSAGE_WARNING: + name = _("Warning"); + break; + + case GTK_MESSAGE_ERROR: + name = _("Error"); + break; + + case GTK_MESSAGE_OTHER: + break; + + default: + g_warning ("Unknown GtkMessageType %u", dialog->priv->message_type); + break; + } + + if (name) + atk_object_set_name (atk_obj, name); +} + static void setup_type (GtkMessageDialog *dialog, GtkMessageType type) { GtkMessageDialogPrivate *priv = dialog->priv; - const gchar *name = NULL; AtkObject *atk_obj; if (priv->message_type == type) @@ -379,38 +417,12 @@ setup_type (GtkMessageDialog *dialog, priv->message_type = type; - switch (type) - { - case GTK_MESSAGE_INFO: - name = _("Information"); - break; - - case GTK_MESSAGE_QUESTION: - name = _("Question"); - break; - - case GTK_MESSAGE_WARNING: - name = _("Warning"); - break; - - case GTK_MESSAGE_ERROR: - name = _("Error"); - break; - - case GTK_MESSAGE_OTHER: - break; - - default: - g_warning ("Unknown GtkMessageType %u", type); - break; - } atk_obj = gtk_widget_get_accessible (GTK_WIDGET (dialog)); if (GTK_IS_ACCESSIBLE (atk_obj)) { atk_object_set_role (atk_obj, ATK_ROLE_ALERT); - if (name) - atk_object_set_name (atk_obj, name); + update_accessible_name (dialog); } g_object_notify (G_OBJECT (dialog), "message-type"); From 939737c3e72c2deaa0094f35838038df92f2a724 Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Fri, 9 Aug 2024 18:37:11 +0200 Subject: [PATCH 2/2] a11y: Use non-empty message dialog title as a11y name If a `GtkMessageDialog` has a non-empty title set, use that for the accessible name instead of a generic name indicating the type of the message dialog, as the window title is generally more informative, if set. It also better matches the information presented visually on screen (in the window title, task switchers,...) and is in line with the handling for non-message-dialog windows. This can easily be tested with the "Dialogs and Message Boxes" sample from gtk3-demo when setting a title for the message dialog in there like this: diff --git a/demos/gtk-demo/dialog.c b/demos/gtk-demo/dialog.c index 0eb1c62397..53fb7f8b0e 100644 --- a/demos/gtk-demo/dialog.c +++ b/demos/gtk-demo/dialog.c @@ -25,6 +25,8 @@ message_dialog_clicked (GtkButton *button, "number of times:"); gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%d", i); + gtk_window_set_title (GTK_WINDOW (dialog), "Some informative title"); + gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); i++; --- gtk/gtkmessagedialog.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/gtk/gtkmessagedialog.c b/gtk/gtkmessagedialog.c index 1de3118e51..ee35b261de 100644 --- a/gtk/gtkmessagedialog.c +++ b/gtk/gtkmessagedialog.c @@ -373,7 +373,12 @@ update_accessible_name (GtkMessageDialog *dialog) if (!GTK_IS_ACCESSIBLE (atk_obj)) return; - const char *name = NULL; + const char *name = gtk_window_get_title (GTK_WINDOW (dialog)); + if (name && name[0]) + { + atk_object_set_name (atk_obj, name); + return; + } switch (dialog->priv->message_type) { @@ -438,6 +443,8 @@ update_title (GObject *dialog, title = gtk_window_get_title (GTK_WINDOW (dialog)); gtk_label_set_label (GTK_LABEL (label), title); gtk_widget_set_visible (label, title && title[0]); + + update_accessible_name (GTK_MESSAGE_DIALOG (dialog)); } static void