button: Improve accessible setup
authorMatthias Clasen <mclasen@redhat.com>
Sat, 10 Jun 2023 14:25:02 +0000 (10:25 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 10 Jun 2023 15:29:12 +0000 (11:29 -0400)
With the current approach, we get duplicate labels
in the accessible name: _Cancel Cancel. Change things
around to always set the labelled-by accessible relation
if we have a label, and not the label accessible property.

gtk/gtkbutton.c
gtk/gtkfontchooserdialog.c
gtk/ui/gtkfontchooserwidget.ui
testsuite/a11y/button.c

index 763b9a50ce8d41173ee1091fb5c538f664bc94a0..8b92cac30138f564467b48cd68a3e71e68911512 100644 (file)
@@ -842,7 +842,7 @@ gtk_button_finish_activate (GtkButton *button,
  * This will also clear any previously set labels.
  */
 void
-gtk_button_set_label (GtkButton   *button,
+gtk_button_set_label (GtkButton  *button,
                       const char *label)
 {
   GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
@@ -853,13 +853,18 @@ gtk_button_set_label (GtkButton   *button,
   if (priv->child_type != LABEL_CHILD || priv->child == NULL)
     {
       child = gtk_label_new (NULL);
+      gtk_button_set_child (button,  child);
       if (priv->use_underline)
         {
           gtk_label_set_use_underline (GTK_LABEL (child), priv->use_underline);
           gtk_label_set_mnemonic_widget (GTK_LABEL (child), GTK_WIDGET (button));
         }
-
-      gtk_button_set_child (button,  child);
+      else
+        {
+          gtk_accessible_update_relation (GTK_ACCESSIBLE (button),
+                                          GTK_ACCESSIBLE_RELATION_LABELLED_BY, child, NULL,
+                                          -1);
+        }
     }
 
   gtk_label_set_label (GTK_LABEL (priv->child), label);
@@ -869,10 +874,6 @@ gtk_button_set_label (GtkButton   *button,
 
   gtk_button_set_child_type (button, LABEL_CHILD);
 
-  gtk_accessible_update_property (GTK_ACCESSIBLE (button),
-                                  GTK_ACCESSIBLE_PROPERTY_LABEL, label,
-                                  -1);
-
   g_object_notify_by_pspec (G_OBJECT (button), props[PROP_LABEL]);
 }
 
index 5d7f3fa157a756f29252e3359bfc3136dddf77b7..4c747a39370e8e733cb1b146eeccc8dcfe363c82 100644 (file)
@@ -36,6 +36,7 @@
 #include "gtkheaderbar.h"
 #include "gtkactionable.h"
 #include "gtkeventcontrollerkey.h"
+#include "gtkaccessible.h"
 
 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
 
@@ -189,6 +190,10 @@ setup_tweak_button (GtkFontChooserDialog *dialog)
       gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
       gtk_button_set_icon_name (GTK_BUTTON (button), "emblem-system-symbolic");
       gtk_widget_set_tooltip_text (button, _("Change Font Features"));
+      gtk_accessible_update_property (GTK_ACCESSIBLE (button),
+                                      GTK_ACCESSIBLE_PROPERTY_LABEL,
+                                      _("Change Font Features"),
+                                      -1);
 
       header = gtk_dialog_get_header_bar (GTK_DIALOG (dialog));
       gtk_header_bar_pack_end (GTK_HEADER_BAR (header), button);
index 9b0fc903135fff0bb4806d698ea13a0b92f63333..e1ccdd932c92722778d577265fa07beb6428fc1f 100644 (file)
                         <property name="activates-default">1</property>
                         <property name="placeholder-text" translatable="yes">Search font name</property>
                         <signal name="stop-search" handler="stop_search_cb" swapped="no"/>
+                        <accessibility>
+                          <property name="label" translatable="1">Search font name</property>
+                        </accessibility>
                       </object>
                     </child>
                     <child>
                       <object class="GtkMenuButton">
+                        <accessibility>
+                          <property name="label" translatable="1">Filters</property>
+                        </accessibility>
                         <property name="icon-name">pan-down-symbolic</property>
+                        <property name="tooltip-text" translatable="yes">Filters</property>
                         <property name="popover">
                           <object class="GtkPopover">
                             <child>
                               <object class="GtkBox">
                                 <property name="orientation">1</property>
                                 <property name="spacing">6</property>
+                                <accessibility>
+                                  <relation name="labelled-by">filter_by_label</relation>
+                                </accessibility>
                                 <child>
-                                  <object class="GtkLabel">
+                                  <object class="GtkLabel" id="filter_by_label">
                                     <property name="label" translatable="yes">Filter by</property>
                                     <property name="width-chars">20</property>
                                     <property name="margin-bottom">10</property>
index 105e424f0c129dbbf8516fcad8df3dd501f34f01..b32682f7555f162c921095b5d4a2331b7675fcde 100644 (file)
@@ -20,7 +20,8 @@ button_label (void)
   GtkWidget *button = gtk_button_new_with_label ("Hello");
   g_object_ref_sink (button);
 
-  gtk_test_accessible_assert_property (button, GTK_ACCESSIBLE_PROPERTY_LABEL, "Hello");
+  gtk_test_accessible_assert_relation (GTK_ACCESSIBLE (button),
+                                       GTK_ACCESSIBLE_RELATION_LABELLED_BY, gtk_widget_get_first_child (button), NULL);
 
   g_object_unref (button);
 }
@@ -58,7 +59,8 @@ linkbutton_label (void)
   GtkWidget *button = gtk_link_button_new ("Hello");
   g_object_ref_sink (button);
 
-  gtk_test_accessible_assert_property (button, GTK_ACCESSIBLE_PROPERTY_LABEL, "Hello");
+  gtk_test_accessible_assert_relation (GTK_ACCESSIBLE (button),
+                                       GTK_ACCESSIBLE_RELATION_LABELLED_BY, gtk_widget_get_first_child (button), NULL);
 
   g_object_unref (button);
 }