[gtk/matthiasc/for-master: 1/7] colorswatch: Export some functions for a11y




commit 3805e1d5079e1eba4abfc4b69d72dc59413791ee
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Oct 21 23:08:04 2020 -0400

    colorswatch: Export some functions for a11y
    
    Make simple functions to activate, select or customize
    a GtkColorSwatch. These will be exported by a11y as
    actions.

 gtk/gtkcolorswatch.c        | 67 +++++++++++++++++++++++++++++++++------------
 gtk/gtkcolorswatchprivate.h |  4 +++
 2 files changed, 53 insertions(+), 18 deletions(-)
---
diff --git a/gtk/gtkcolorswatch.c b/gtk/gtkcolorswatch.c
index cc185553cc..7addf913d3 100644
--- a/gtk/gtkcolorswatch.c
+++ b/gtk/gtkcolorswatch.c
@@ -144,8 +144,8 @@ swatch_drag_drop (GtkDropTarget  *dest,
   return TRUE;
 }
 
-static void
-activate_color (GtkColorSwatch *swatch)
+void
+gtk_color_swatch_activate (GtkColorSwatch *swatch)
 {
   double red, green, blue, alpha;
 
@@ -158,8 +158,8 @@ activate_color (GtkColorSwatch *swatch)
                               "color.select", "(dddd)", red, green, blue, alpha);
 }
 
-static void
-customize_color (GtkColorSwatch *swatch)
+void
+gtk_color_swatch_customize (GtkColorSwatch *swatch)
 {
   double red, green, blue, alpha;
 
@@ -172,6 +172,18 @@ customize_color (GtkColorSwatch *swatch)
                               "color.customize", "(dddd)", red, green, blue, alpha);
 }
 
+void
+gtk_color_swatch_select (GtkColorSwatch *swatch)
+{
+  gtk_widget_set_state_flags (GTK_WIDGET (swatch), GTK_STATE_FLAG_SELECTED, FALSE);
+}
+
+static gboolean
+gtk_color_swatch_is_selected (GtkColorSwatch *swatch)
+{
+  return (gtk_widget_get_state_flags (GTK_WIDGET (swatch)) & GTK_STATE_FLAG_SELECTED) != 0;
+}
+
 static gboolean
 key_controller_key_pressed (GtkEventControllerKey *controller,
                             guint                  keyval,
@@ -189,10 +201,10 @@ key_controller_key_pressed (GtkEventControllerKey *controller,
     {
       if (swatch->has_color &&
           swatch->selectable &&
-          (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_SELECTED) == 0)
-        gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_SELECTED, FALSE);
+          !gtk_color_swatch_is_selected (swatch))
+        gtk_color_swatch_select (swatch);
       else
-        customize_color (swatch);
+        gtk_color_swatch_customize (swatch);
 
       return TRUE;
     }
@@ -245,19 +257,15 @@ do_popup (GtkColorSwatch *swatch)
 static gboolean
 swatch_primary_action (GtkColorSwatch *swatch)
 {
-  GtkWidget *widget = (GtkWidget *)swatch;
-  GtkStateFlags flags;
-
-  flags = gtk_widget_get_state_flags (widget);
   if (!swatch->has_color)
     {
-      customize_color (swatch);
+      gtk_color_swatch_customize (swatch);
       return TRUE;
     }
   else if (swatch->selectable &&
-           (flags & GTK_STATE_FLAG_SELECTED) == 0)
+           !gtk_color_swatch_is_selected (swatch))
     {
-      gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_SELECTED, FALSE);
+      gtk_color_swatch_select (swatch);
       return TRUE;
     }
 
@@ -290,7 +298,7 @@ tap_action (GtkGestureClick *gesture,
       if (n_press == 1)
         swatch_primary_action (swatch);
       else if (n_press > 1)
-        activate_color (swatch);
+        gtk_color_swatch_activate (swatch);
     }
   else if (button == GDK_BUTTON_SECONDARY)
     {
@@ -360,12 +368,30 @@ update_icon (GtkColorSwatch *swatch)
 
   if (swatch->icon)
     gtk_image_set_from_icon_name (image, swatch->icon);
-  else if (gtk_widget_get_state_flags (GTK_WIDGET (swatch)) & GTK_STATE_FLAG_SELECTED)
+  else if (gtk_color_swatch_is_selected (swatch))
     gtk_image_set_from_icon_name (image, "object-select-symbolic");
   else
     gtk_image_clear (image);
 }
 
+static void
+update_accessible_properties (GtkColorSwatch *swatch)
+{
+  if (swatch->selectable)
+    {
+      gboolean selected = gtk_color_swatch_is_selected (swatch);
+
+      gtk_accessible_update_state (GTK_ACCESSIBLE (swatch),
+                                   GTK_ACCESSIBLE_STATE_CHECKED, selected,
+                                   -1);
+    }
+  else
+    {
+      gtk_accessible_reset_state (GTK_ACCESSIBLE (swatch),
+                                  GTK_ACCESSIBLE_STATE_CHECKED);
+    }
+}
+
 static void
 swatch_state_flags_changed (GtkWidget     *widget,
                             GtkStateFlags  previous_state)
@@ -373,6 +399,7 @@ swatch_state_flags_changed (GtkWidget     *widget,
   GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
 
   update_icon (swatch);
+  update_accessible_properties (swatch);
 
   GTK_WIDGET_CLASS (gtk_color_swatch_parent_class)->state_flags_changed (widget, previous_state);
 }
@@ -504,6 +531,7 @@ gtk_color_swatch_class_init (GtkColorSwatchClass *class)
                                        NULL);
 
   gtk_widget_class_set_css_name (widget_class, I_("colorswatch"));
+  gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_RADIO);
 }
 
 static void
@@ -544,8 +572,9 @@ gtk_color_swatch_init (GtkColorSwatch *swatch)
   gtk_widget_add_css_class (GTK_WIDGET (swatch), "activatable");
 
   swatch->overlay_widget = g_object_new (GTK_TYPE_IMAGE,
-                                               "css-name", "overlay",
-                                               NULL);
+                                         "accessible-role", GTK_ACCESSIBLE_ROLE_NONE,
+                                         "css-name", "overlay",
+                                         NULL);
   gtk_widget_set_parent (swatch->overlay_widget, GTK_WIDGET (swatch));
 }
 
@@ -669,6 +698,8 @@ gtk_color_swatch_set_selectable (GtkColorSwatch *swatch,
     return;
 
   swatch->selectable = selectable;
+
+  update_accessible_properties (swatch);
   g_object_notify (G_OBJECT (swatch), "selectable");
 }
 
diff --git a/gtk/gtkcolorswatchprivate.h b/gtk/gtkcolorswatchprivate.h
index 87031a6b32..745f2c1d5e 100644
--- a/gtk/gtkcolorswatchprivate.h
+++ b/gtk/gtkcolorswatchprivate.h
@@ -50,6 +50,10 @@ void        gtk_color_swatch_set_selectable   (GtkColorSwatch *swatch,
                                                gboolean        selectable);
 gboolean    gtk_color_swatch_get_selectable   (GtkColorSwatch *swatch);
 
+void        gtk_color_swatch_select    (GtkColorSwatch *swatch);
+void        gtk_color_swatch_activate  (GtkColorSwatch *swatch);
+void        gtk_color_swatch_customize (GtkColorSwatch *swatch);
+
 G_END_DECLS
 
 #endif /* __GTK_COLOR_SWATCH_PRIVATE_H__ */


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