1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
/*
* Copyright © 2023 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Matthias Clasen <mclasen@redhat.com>
*/
#include "config.h"
#include "a11yoverlay.h"
#include "gtkwidget.h"
#include "gtkroot.h"
#include "gtknative.h"
#include "gtkwidgetprivate.h"
#include "gtkatcontextprivate.h"
#include "gtkaccessibleprivate.h"
#include "gtktypebuiltins.h"
struct _GtkA11yOverlay
{
GtkInspectorOverlay parent_instance;
GdkRGBA recommend_color;
GdkRGBA error_color;
GArray *context;
};
struct _GtkA11yOverlayClass
{
GtkInspectorOverlayClass parent_class;
};
G_DEFINE_TYPE (GtkA11yOverlay, gtk_a11y_overlay, GTK_TYPE_INSPECTOR_OVERLAY)
typedef enum
{
FIX_SEVERITY_GOOD,
FIX_SEVERITY_RECOMMENDATION,
FIX_SEVERITY_ERROR
} FixSeverity;
typedef enum
{
ATTRIBUTE_STATE,
ATTRIBUTE_PROPERTY,
ATTRIBUTE_RELATION
} AttributeType;
static struct {
GtkAccessibleRole role;
AttributeType type;
int id;
} required_attributes[] = {
{ GTK_ACCESSIBLE_ROLE_CHECKBOX, ATTRIBUTE_STATE, GTK_ACCESSIBLE_STATE_CHECKED },
{ GTK_ACCESSIBLE_ROLE_COMBO_BOX, ATTRIBUTE_STATE, GTK_ACCESSIBLE_STATE_EXPANDED },
{ GTK_ACCESSIBLE_ROLE_COMBO_BOX, ATTRIBUTE_RELATION, GTK_ACCESSIBLE_RELATION_CONTROLS },
{ GTK_ACCESSIBLE_ROLE_HEADING, ATTRIBUTE_PROPERTY, GTK_ACCESSIBLE_PROPERTY_LEVEL },
{ GTK_ACCESSIBLE_ROLE_SCROLLBAR, ATTRIBUTE_RELATION, GTK_ACCESSIBLE_RELATION_CONTROLS },
{ GTK_ACCESSIBLE_ROLE_SCROLLBAR, ATTRIBUTE_PROPERTY, GTK_ACCESSIBLE_PROPERTY_VALUE_NOW },
{ GTK_ACCESSIBLE_ROLE_SWITCH, ATTRIBUTE_STATE, GTK_ACCESSIBLE_STATE_CHECKED },
};
static struct {
GtkAccessibleRole role;
GtkAccessibleRole context;
} required_context[] = {
{ GTK_ACCESSIBLE_ROLE_CAPTION, GTK_ACCESSIBLE_ROLE_GRID },
{ GTK_ACCESSIBLE_ROLE_CAPTION, GTK_ACCESSIBLE_ROLE_TABLE },
{ GTK_ACCESSIBLE_ROLE_CAPTION, GTK_ACCESSIBLE_ROLE_TREE_GRID },
{ GTK_ACCESSIBLE_ROLE_CELL, GTK_ACCESSIBLE_ROLE_ROW },
{ GTK_ACCESSIBLE_ROLE_COLUMN_HEADER, GTK_ACCESSIBLE_ROLE_ROW },
{ GTK_ACCESSIBLE_ROLE_GRID_CELL, GTK_ACCESSIBLE_ROLE_ROW },
{ GTK_ACCESSIBLE_ROLE_LIST_ITEM, GTK_ACCESSIBLE_ROLE_LIST },
{ GTK_ACCESSIBLE_ROLE_MENU_ITEM, GTK_ACCESSIBLE_ROLE_GROUP },
{ GTK_ACCESSIBLE_ROLE_MENU_ITEM, GTK_ACCESSIBLE_ROLE_MENU },
{ GTK_ACCESSIBLE_ROLE_MENU_ITEM, GTK_ACCESSIBLE_ROLE_MENU_BAR },
{ GTK_ACCESSIBLE_ROLE_MENU_ITEM_CHECKBOX, GTK_ACCESSIBLE_ROLE_GROUP },
{ GTK_ACCESSIBLE_ROLE_MENU_ITEM_CHECKBOX, GTK_ACCESSIBLE_ROLE_MENU },
{ GTK_ACCESSIBLE_ROLE_MENU_ITEM_CHECKBOX, GTK_ACCESSIBLE_ROLE_MENU_BAR },
{ GTK_ACCESSIBLE_ROLE_MENU_ITEM_RADIO, GTK_ACCESSIBLE_ROLE_GROUP },
{ GTK_ACCESSIBLE_ROLE_MENU_ITEM_RADIO, GTK_ACCESSIBLE_ROLE_MENU },
{ GTK_ACCESSIBLE_ROLE_MENU_ITEM_RADIO, GTK_ACCESSIBLE_ROLE_MENU_BAR },
{ GTK_ACCESSIBLE_ROLE_OPTION, GTK_ACCESSIBLE_ROLE_GROUP },
{ GTK_ACCESSIBLE_ROLE_OPTION, GTK_ACCESSIBLE_ROLE_LIST_BOX },
{ GTK_ACCESSIBLE_ROLE_ROW, GTK_ACCESSIBLE_ROLE_GRID },
{ GTK_ACCESSIBLE_ROLE_ROW, GTK_ACCESSIBLE_ROLE_ROW_GROUP },
{ GTK_ACCESSIBLE_ROLE_ROW, GTK_ACCESSIBLE_ROLE_TABLE },
{ GTK_ACCESSIBLE_ROLE_ROW, GTK_ACCESSIBLE_ROLE_TREE_GRID },
{ GTK_ACCESSIBLE_ROLE_ROW_GROUP, GTK_ACCESSIBLE_ROLE_GRID },
{ GTK_ACCESSIBLE_ROLE_ROW_GROUP, GTK_ACCESSIBLE_ROLE_TABLE },
{ GTK_ACCESSIBLE_ROLE_ROW_GROUP, GTK_ACCESSIBLE_ROLE_TREE_GRID },
{ GTK_ACCESSIBLE_ROLE_ROW_HEADER, GTK_ACCESSIBLE_ROLE_ROW },
{ GTK_ACCESSIBLE_ROLE_TAB, GTK_ACCESSIBLE_ROLE_TAB_LIST },
{ GTK_ACCESSIBLE_ROLE_TREE_ITEM, GTK_ACCESSIBLE_ROLE_GROUP },
{ GTK_ACCESSIBLE_ROLE_TREE_ITEM, GTK_ACCESSIBLE_ROLE_TREE },
};
static FixSeverity
check_accessibility_errors (GtkATContext *context,
GtkAccessibleRole role,
GArray *context_elements,
char **hint)
{
gboolean label_set;
const char *role_name;
GEnumClass *states;
GEnumClass *properties;
GEnumClass *relations;
gboolean has_context;
*hint = NULL;
role_name = gtk_accessible_role_to_name (role, NULL);
if (!gtk_at_context_is_realized (context))
gtk_at_context_realize (context);
/* Check for abstract roles */
if (gtk_accessible_role_is_abstract (role))
{
*hint = g_strdup_printf ("%s is an abstract role", role_name);
return FIX_SEVERITY_ERROR;
}
/* Check for name and description */
label_set = gtk_at_context_has_accessible_property (context, GTK_ACCESSIBLE_PROPERTY_LABEL) ||
gtk_at_context_has_accessible_relation (context, GTK_ACCESSIBLE_RELATION_LABELLED_BY);
switch (gtk_accessible_role_get_naming (role))
{
case GTK_ACCESSIBLE_NAME_ALLOWED:
break;
case GTK_ACCESSIBLE_NAME_REQUIRED:
if (!label_set)
{
if (gtk_accessible_role_supports_name_from_author (role))
{
char *name = gtk_at_context_get_name (context);
if (strcmp (name, "") == 0)
{
g_free (name);
*hint = g_strdup_printf ("%s must have text content or label", role_name);
return FIX_SEVERITY_ERROR;
}
}
else
{
*hint = g_strdup_printf ("%s must have label", role_name);
return FIX_SEVERITY_ERROR;
}
}
break;
case GTK_ACCESSIBLE_NAME_PROHIBITED:
if (label_set)
{
*hint = g_strdup_printf ("%s can't have label", role_name);
return FIX_SEVERITY_ERROR;
}
break;
case GTK_ACCESSIBLE_NAME_RECOMMENDED:
if (!label_set)
{
*hint = g_strdup_printf ("label recommended for %s", role_name);
return FIX_SEVERITY_RECOMMENDATION;
}
break;
case GTK_ACCESSIBLE_NAME_NOT_RECOMMENDED:
if (label_set)
{
*hint = g_strdup_printf ("label not recommended for %s", role_name);
return FIX_SEVERITY_RECOMMENDATION;
}
break;
default:
g_assert_not_reached ();
}
/* Check for required attributes */
states = g_type_class_peek (GTK_TYPE_ACCESSIBLE_STATE);
properties = g_type_class_peek (GTK_TYPE_ACCESSIBLE_PROPERTY);
relations = g_type_class_peek (GTK_TYPE_ACCESSIBLE_RELATION);
for (unsigned int i = 0; i < G_N_ELEMENTS (required_attributes); i++)
{
if (role == required_attributes[i].role)
{
switch (required_attributes[i].type)
{
case ATTRIBUTE_STATE:
if (!gtk_at_context_has_accessible_state (context, required_attributes[i].id))
{
*hint = g_strdup_printf ("%s must have state %s", role_name, g_enum_get_value (states, required_attributes[i].id)->value_nick);
return FIX_SEVERITY_ERROR;
}
break;
case ATTRIBUTE_PROPERTY:
if (!gtk_at_context_has_accessible_property (context, required_attributes[i].id))
{
*hint = g_strdup_printf ("%s must have property %s", role_name, g_enum_get_value (properties, required_attributes[i].id)->value_nick);
return FIX_SEVERITY_ERROR;
}
break;
case ATTRIBUTE_RELATION:
if (!gtk_at_context_has_accessible_relation (context, required_attributes[i].id))
{
*hint = g_strdup_printf ("%s must have relation %s", role_name, g_enum_get_value (relations, required_attributes[i].id)->value_nick);
return FIX_SEVERITY_ERROR;
}
break;
default:
g_assert_not_reached ();
}
}
}
/* Check for required context */
has_context = TRUE;
for (unsigned int i = 0; i < G_N_ELEMENTS (required_context); i++)
{
if (required_context[i].role != role)
continue;
has_context = FALSE;
for (unsigned int j = 0; j < context_elements->len; j++)
{
GtkAccessibleRole elt = g_array_index (context_elements, GtkAccessibleRole, j);
if (required_context[i].context == elt)
{
has_context = TRUE;
break;
}
}
if (has_context)
break;
}
if (!has_context)
{
GString *s = g_string_new ("");
for (unsigned int i = 0; i < G_N_ELEMENTS (required_context); i++)
{
if (required_context[i].role != role)
continue;
if (s->len > 0)
g_string_append (s, ", ");
g_string_append (s, gtk_accessible_role_to_name (required_context[i].context, NULL));
}
*hint = g_strdup_printf ("%s requires context: %s", role_name, s->str);
g_string_free (s, TRUE);
return FIX_SEVERITY_ERROR;
}
if (role == GTK_ACCESSIBLE_ROLE_BUTTON)
{
GtkAccessible *accessible = gtk_at_context_get_accessible (context);
if (GTK_IS_WIDGET (accessible))
{
int width = gtk_widget_get_width (GTK_WIDGET (accessible));
int height = gtk_widget_get_height (GTK_WIDGET (accessible));
if (width < 24 || height < 24)
{
*hint = g_strdup_printf ("Button is too small: %dx%d", width, height);
return FIX_SEVERITY_ERROR;
}
}
}
return FIX_SEVERITY_GOOD;
}
static FixSeverity
check_widget_accessibility_errors (GtkWidget *widget,
GArray *context_elements,
char **hint)
{
GtkAccessibleRole role;
GtkATContext *context;
FixSeverity ret;
role = gtk_accessible_get_accessible_role (GTK_ACCESSIBLE (widget));
context = gtk_accessible_get_at_context (GTK_ACCESSIBLE (widget));
ret = check_accessibility_errors (context, role, context_elements, hint);
g_object_unref (context);
return ret;
}
static void
center_over_within (graphene_rect_t *rect,
const graphene_rect_t *over,
const graphene_rect_t *within)
{
graphene_point_t center;
graphene_rect_get_center (over, ¢er);
rect->origin.x = CLAMP (center.x - 0.5 * rect->size.width, within->origin.x, within->origin.x + within->size.width - rect->size.width);
rect->origin.y = CLAMP (center.y - 0.5 * rect->size.height, within->origin.y, within->origin.y + within->size.height - rect->size.height);
}
static void
recurse_child_widgets (GtkA11yOverlay *self,
GtkWidget *widget,
GtkSnapshot *snapshot)
{
GtkWidget *child;
char *hint;
FixSeverity severity;
GtkAccessibleRole role;
if (!gtk_widget_get_mapped (widget))
return;
severity = check_widget_accessibility_errors (widget, self->context, &hint);
if (severity != FIX_SEVERITY_GOOD)
{
int width, height;
GdkRGBA color;
width = gtk_widget_get_width (widget);
height = gtk_widget_get_height (widget);
if (severity == FIX_SEVERITY_ERROR)
color = self->error_color;
else
color = self->recommend_color;
gtk_snapshot_save (snapshot);
gtk_snapshot_push_debug (snapshot, "Widget a11y debugging");
gtk_snapshot_append_color (snapshot, &color,
&GRAPHENE_RECT_INIT (0, 0, width, height));
if (hint)
{
int toplevel_width, toplevel_height;
PangoLayout *layout;
PangoRectangle extents;
GdkRGBA black = { 0, 0, 0, 1 };
float widths[4] = { 1, 1, 1, 1 };
GdkRGBA colors[4] = {
{ 0, 0, 0, 1 },
{ 0, 0, 0, 1 },
{ 0, 0, 0, 1 },
{ 0, 0, 0, 1 },
};
GtkNative *native;
graphene_rect_t over, within, bounds;
gboolean ret G_GNUC_UNUSED;
native = gtk_widget_get_native (widget);
toplevel_width = gtk_widget_get_width (GTK_WIDGET (native));
toplevel_height = gtk_widget_get_height (GTK_WIDGET (native));
gtk_snapshot_save (snapshot);
layout = gtk_widget_create_pango_layout (widget, hint);
pango_layout_set_width (layout, toplevel_width * PANGO_SCALE);
pango_layout_get_pixel_extents (layout, NULL, &extents);
extents.x -= 5;
extents.y -= 5;
extents.width += 10;
extents.height += 10;
ret = gtk_widget_compute_point (widget, GTK_WIDGET (native), &GRAPHENE_POINT_INIT (0, 0), &over.origin);
over.size.width = width;
over.size.height = height;
graphene_rect_init (&within, 0, 0, toplevel_width, toplevel_height);
graphene_rect_init (&bounds, 0, 0, extents.width, extents.height);
center_over_within (&bounds, &over, &within);
color.alpha = 0.8f;
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (bounds.origin.x - over.origin.x,
bounds.origin.y - over.origin.y));
gtk_snapshot_append_border (snapshot,
&GSK_ROUNDED_RECT_INIT (0, 0,
extents.width, extents.height),
widths, colors);
gtk_snapshot_append_color (snapshot, &color,
&GRAPHENE_RECT_INIT (0, 0,
extents.width, extents.height));
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (5, 5));
gtk_snapshot_append_layout (snapshot, layout, &black);
g_object_unref (layout);
gtk_snapshot_restore (snapshot);
}
gtk_snapshot_pop (snapshot);
gtk_snapshot_restore (snapshot);
}
g_free (hint);
/* Recurse into child widgets */
role = gtk_accessible_get_accessible_role (GTK_ACCESSIBLE (widget));
g_array_append_val (self->context, role);
for (child = gtk_widget_get_first_child (widget);
child != NULL;
child = gtk_widget_get_next_sibling (child))
{
gtk_snapshot_save (snapshot);
gtk_snapshot_transform (snapshot, child->priv->transform);
recurse_child_widgets (self, child, snapshot);
gtk_snapshot_restore (snapshot);
}
g_array_remove_index (self->context, self->context->len - 1);
}
static void
gtk_a11y_overlay_snapshot (GtkInspectorOverlay *overlay,
GtkSnapshot *snapshot,
GskRenderNode *node,
GtkWidget *widget)
{
GtkA11yOverlay *self = GTK_A11Y_OVERLAY (overlay);
g_assert (self->context->len == 0);
recurse_child_widgets (self, widget, snapshot);
g_assert (self->context->len == 0);
}
static void
gtk_a11y_overlay_finalize (GObject *object)
{
GtkA11yOverlay *self = GTK_A11Y_OVERLAY (object);
g_array_free (self->context, TRUE);
G_OBJECT_CLASS (gtk_a11y_overlay_parent_class)->finalize (object);
}
static void
gtk_a11y_overlay_class_init (GtkA11yOverlayClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkInspectorOverlayClass *overlay_class = GTK_INSPECTOR_OVERLAY_CLASS (klass);
object_class->finalize = gtk_a11y_overlay_finalize;
overlay_class->snapshot = gtk_a11y_overlay_snapshot;
}
static void
gtk_a11y_overlay_init (GtkA11yOverlay *self)
{
self->recommend_color = (GdkRGBA) { 0.0, 0.5, 1.0, 0.2 };
self->error_color = (GdkRGBA) { 1.0, 0.0, 0.0, 0.2 };
self->context = g_array_new (FALSE, FALSE, sizeof (GtkAccessibleRole));
}
GtkInspectorOverlay *
gtk_a11y_overlay_new (void)
{
GtkA11yOverlay *self;
self = g_object_new (GTK_TYPE_A11Y_OVERLAY, NULL);
return GTK_INSPECTOR_OVERLAY (self);
}
|