Merge branch 'camelCaseNick/a11y-text-contents-at' into 'main'

a11y: implement get_contents_at for Gtk.Text, Gtk.TextView, Gtk.Inscription

See merge request GNOME/gtk!6971
This commit is contained in:
Matthias Clasen 2024-03-03 23:03:13 +00:00
commit 448287bd58
7 changed files with 114 additions and 16 deletions

View File

@ -1363,6 +1363,19 @@ gtk_inscription_accessible_text_get_contents (GtkAccessibleText *self,
return g_bytes_new_take (string, size);
}
static GBytes *
gtk_inscription_accessible_text_get_contents_at (GtkAccessibleText *self,
unsigned int offset,
GtkAccessibleTextGranularity granularity,
unsigned int *start,
unsigned int *end)
{
PangoLayout *layout = gtk_inscription_get_layout (GTK_INSCRIPTION (self));
char *string = gtk_pango_get_string_at (layout, offset, granularity, start, end);
return g_bytes_new_take (string, strlen (string));
}
static unsigned int
gtk_inscription_accessible_text_get_caret_position (GtkAccessibleText *self)
{
@ -1412,6 +1425,7 @@ static void
gtk_inscription_accessible_text_init (GtkAccessibleTextInterface *iface)
{
iface->get_contents = gtk_inscription_accessible_text_get_contents;
iface->get_contents_at = gtk_inscription_accessible_text_get_contents_at;
iface->get_caret_position = gtk_inscription_accessible_text_get_caret_position;
iface->get_selection = gtk_inscription_accessible_text_get_selection;
iface->get_attributes = gtk_inscription_accessible_text_get_attributes;

View File

@ -7472,6 +7472,19 @@ gtk_text_accessible_text_get_contents (GtkAccessibleText *self,
return g_bytes_new_take (string, size);
}
static GBytes *
gtk_text_accessible_text_get_contents_at (GtkAccessibleText *self,
unsigned int offset,
GtkAccessibleTextGranularity granularity,
unsigned int *start,
unsigned int *end)
{
PangoLayout *layout = gtk_text_get_layout (GTK_TEXT (self));
char *string = gtk_pango_get_string_at (layout, offset, granularity, start, end);
return g_bytes_new_take (string, strlen (string));
}
static unsigned int
gtk_text_accessible_text_get_caret_position (GtkAccessibleText *self)
{
@ -7531,6 +7544,7 @@ static void
gtk_text_accessible_text_init (GtkAccessibleTextInterface *iface)
{
iface->get_contents = gtk_text_accessible_text_get_contents;
iface->get_contents_at = gtk_text_accessible_text_get_contents_at;
iface->get_caret_position = gtk_text_accessible_text_get_caret_position;
iface->get_selection = gtk_text_accessible_text_get_selection;
iface->get_attributes = gtk_text_accessible_text_get_attributes;

View File

@ -10383,6 +10383,39 @@ gtk_text_view_accessible_text_get_contents (GtkAccessibleText *self,
return g_bytes_new_take (string, strlen (string) + 1);
}
static GBytes *
gtk_text_view_accessible_text_get_contents_at (GtkAccessibleText *self,
unsigned int offset,
GtkAccessibleTextGranularity granularity,
unsigned int *start,
unsigned int *end)
{
GtkTextViewPrivate *priv = GTK_TEXT_VIEW (self)->priv;
GtkTextLayout *text_layout = priv->layout;
GtkTextBuffer *text_buffer;
GtkTextIter iter;
GtkTextLine *line;
PangoLayout *line_layout;
char *string;
unsigned int line_start, line_end, line_offset;
text_buffer = gtk_text_layout_get_buffer (text_layout);
gtk_text_buffer_get_iter_at_offset (text_buffer, &iter, offset);
line = _gtk_text_iter_get_text_line (&iter);
line_offset = gtk_text_iter_get_offset (&iter) - gtk_text_iter_get_line_offset (&iter);
line_layout = gtk_text_layout_get_line_display (text_layout, line, FALSE)->layout;
string = gtk_pango_get_string_at (line_layout, offset - line_offset, granularity, &line_start, &line_end);
if (start != NULL)
*start = line_offset + line_start;
if (end != NULL)
*end = line_offset + line_end;
return g_bytes_new_take (string, strlen (string));
}
static unsigned int
gtk_text_view_accessible_text_get_caret_position (GtkAccessibleText *self)
{
@ -10472,6 +10505,7 @@ static void
gtk_text_view_accessible_text_init (GtkAccessibleTextInterface *iface)
{
iface->get_contents = gtk_text_view_accessible_text_get_contents;
iface->get_contents_at = gtk_text_view_accessible_text_get_contents_at;
iface->get_caret_position = gtk_text_view_accessible_text_get_caret_position;
iface->get_selection = gtk_text_view_accessible_text_get_selection;
iface->get_attributes = gtk_text_view_accessible_text_get_attributes;

View File

@ -13,15 +13,24 @@ inscription_text_interface (void)
GtkAccessibleTextRange *ranges = NULL;
char **attr_names, **attr_values;
const char *string;
unsigned int start, end;
g_object_ref_sink (inscription);
gtk_inscription_set_markup (GTK_INSCRIPTION (inscription), "<markup>a<span overline='single'>b</span>c</markup>");
gtk_inscription_set_markup (GTK_INSCRIPTION (inscription), "<markup>a<span overline='single'>b</span>c</markup> def");
bytes = gtk_accessible_text_get_contents (GTK_ACCESSIBLE_TEXT (inscription), 0, G_MAXINT);
string = g_bytes_get_data (bytes, &len);
g_assert_cmpint (len, ==, 4);
g_assert_cmpstr (string, ==, "abc");
g_assert_cmpint (len, ==, 8);
g_assert_cmpstr (string, ==, "abc def");
g_bytes_unref (bytes);
bytes = gtk_accessible_text_get_contents_at (GTK_ACCESSIBLE_TEXT (inscription), 1, GTK_ACCESSIBLE_TEXT_GRANULARITY_WORD, &start, &end);
string = g_bytes_get_data (bytes, &len);
g_assert_cmpint (len, ==, 5);
g_assert_cmpint (start, ==, 0);
g_assert_cmpint (end, ==, 4);
g_assert_cmpstr (string, ==, "abc ");
g_bytes_unref (bytes);
g_assert_cmpint (gtk_accessible_text_get_caret_position (GTK_ACCESSIBLE_TEXT (inscription)), ==, 0);

View File

@ -69,17 +69,26 @@ label_text_interface (void)
GtkAccessibleTextRange *ranges = NULL;
char **attr_names, **attr_values;
const char *string;
unsigned int start, end;
g_object_ref_sink (label);
gtk_label_set_markup (GTK_LABEL (label), "<markup>a<span underline='single'>b</span>c</markup>");
gtk_label_set_markup (GTK_LABEL (label), "<markup>a<span underline='single'>b</span>c def</markup>");
gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_label_select_region (GTK_LABEL (label), 1, 2);
bytes = gtk_accessible_text_get_contents (GTK_ACCESSIBLE_TEXT (label), 0, G_MAXINT);
string = g_bytes_get_data (bytes, &len);
g_assert_cmpint (len, ==, 4);
g_assert_cmpstr (string, ==, "abc");
g_assert_cmpint (len, ==, 8);
g_assert_cmpstr (string, ==, "abc def");
g_bytes_unref (bytes);
bytes = gtk_accessible_text_get_contents_at (GTK_ACCESSIBLE_TEXT (label), 1, GTK_ACCESSIBLE_TEXT_GRANULARITY_WORD, &start, &end);
string = g_bytes_get_data (bytes, &len);
g_assert_cmpint (len, ==, 5);
g_assert_cmpint (start, ==, 0);
g_assert_cmpint (end, ==, 4);
g_assert_cmpstr (string, ==, "abc ");
g_bytes_unref (bytes);
g_assert_cmpint (gtk_accessible_text_get_caret_position (GTK_ACCESSIBLE_TEXT (label)), ==, 2);

View File

@ -87,6 +87,7 @@ test_text_accessible_text (void)
PangoAttribute *attr;
GtkATContext *context;
TestData td = { 0, };
unsigned int start, end;
g_object_ref_sink (text);
@ -99,13 +100,13 @@ test_text_accessible_text (void)
test_data_clear (&td);
gtk_editable_set_text (GTK_EDITABLE (text), "abc");
gtk_editable_set_text (GTK_EDITABLE (text), "abc def");
g_assert_cmpuint (td.update_text_contents_count, ==, 1);
g_assert_cmpuint (td.change, ==, GTK_ACCESSIBLE_TEXT_CONTENT_CHANGE_INSERT);
g_assert_cmpuint (td.start, ==, 0);
g_assert_cmpuint (td.end, ==, 3);
g_assert_cmpstr ("abc", ==, g_bytes_get_data (td.contents, NULL));
g_assert_cmpuint (td.end, ==, 7);
g_assert_cmpstr ("abc def", ==, g_bytes_get_data (td.contents, NULL));
attrs = pango_attr_list_new ();
attr = pango_attr_underline_new (PANGO_UNDERLINE_DOUBLE);
@ -124,8 +125,16 @@ test_text_accessible_text (void)
bytes = gtk_accessible_text_get_contents (GTK_ACCESSIBLE_TEXT (text), 0, G_MAXINT);
string = g_bytes_get_data (bytes, &len);
g_assert_cmpint (len, ==, 4);
g_assert_cmpstr (string, ==, "abc");
g_assert_cmpint (len, ==, 8);
g_assert_cmpstr (string, ==, "abc def");
g_bytes_unref (bytes);
bytes = gtk_accessible_text_get_contents_at (GTK_ACCESSIBLE_TEXT (text), 1, GTK_ACCESSIBLE_TEXT_GRANULARITY_WORD, &start, &end);
string = g_bytes_get_data (bytes, &len);
g_assert_cmpint (len, ==, 5);
g_assert_cmpint (start, ==, 0);
g_assert_cmpint (end, ==, 4);
g_assert_cmpstr (string, ==, "abc ");
g_bytes_unref (bytes);
g_assert_cmpint (gtk_accessible_text_get_caret_position (GTK_ACCESSIBLE_TEXT (text)), ==, 2);

View File

@ -115,6 +115,7 @@ textview_accessible_text (void)
GtkTextBuffer *buffer;
GtkTextTag *tag;
GtkTextIter start, end;
unsigned int start_index, end_index;
g_object_ref_sink (widget);
@ -129,13 +130,13 @@ textview_accessible_text (void)
test_data_clear (&td);
gtk_text_buffer_set_text (buffer, "abc", -1);
gtk_text_buffer_set_text (buffer, "abc def", -1);
g_assert_cmpuint (td.update_text_contents_count, ==, 1);
g_assert_cmpuint (td.change, ==, GTK_ACCESSIBLE_TEXT_CONTENT_CHANGE_INSERT);
g_assert_cmpuint (td.start, ==, 0);
g_assert_cmpuint (td.end, ==, 3);
g_assert_cmpstr ("abc", ==, g_bytes_get_data (td.contents, NULL));
g_assert_cmpuint (td.end, ==, 7);
g_assert_cmpstr ("abc def", ==, g_bytes_get_data (td.contents, NULL));
tag = gtk_text_tag_new ("uline");
g_object_set (tag, "underline", PANGO_UNDERLINE_DOUBLE, NULL);
@ -157,8 +158,16 @@ textview_accessible_text (void)
bytes = gtk_accessible_text_get_contents (GTK_ACCESSIBLE_TEXT (widget), 0, G_MAXINT);
string = g_bytes_get_data (bytes, &len);
g_assert_cmpint (len, ==, 4);
g_assert_cmpstr (string, ==, "abc");
g_assert_cmpint (len, ==, 8);
g_assert_cmpstr (string, ==, "abc def");
g_bytes_unref (bytes);
bytes = gtk_accessible_text_get_contents_at (GTK_ACCESSIBLE_TEXT (widget), 1, GTK_ACCESSIBLE_TEXT_GRANULARITY_WORD, &start_index, &end_index);
string = g_bytes_get_data (bytes, &len);
g_assert_cmpint (len, ==, 5);
g_assert_cmpint (start_index, ==, 0);
g_assert_cmpint (end_index, ==, 4);
g_assert_cmpstr (string, ==, "abc ");
g_bytes_unref (bytes);
res = gtk_accessible_text_get_selection (GTK_ACCESSIBLE_TEXT (widget), &n_ranges, &ranges);