summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Sparber <julian@sparber.net>2022-04-14 12:14:17 +0200
committerJulian Sparber <julian@sparber.net>2022-04-14 12:48:20 +0200
commite3320633a63e706c0681b20deee50dfb25ee2f97 (patch)
tree93d575b9531d096c53a6e962335aa519ae35b453
parenta2599c2bb938b0b1afa9bed2f762d18772aa5fc0 (diff)
downloadgtk+-e3320633a63e706c0681b20deee50dfb25ee2f97.tar.gz
label: Move focus out of widget after last link
This allows the user to navigate via tab the links in a label and exits the widget after the last link, when moving forward, and first link, when moving backward. This also ensures that ellipsised links arn't focused. Fixes: https://gitlab.gnome.org/GNOME/gtk/-/issues/4681
-rw-r--r--gtk/gtklabel.c26
1 files changed, 18 insertions, 8 deletions
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index 7560e551fe..04ae996c20 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -1793,6 +1793,7 @@ gtk_label_focus (GtkWidget *widget,
{
info->selection_anchor = focus_link->start;
info->selection_end = focus_link->start;
+ break;
}
}
}
@@ -1856,7 +1857,6 @@ gtk_label_focus (GtkWidget *widget,
{
int focus_link_index;
int new_index = -1;
- int i;
if (info->n_links == 0)
goto out;
@@ -1870,29 +1870,39 @@ gtk_label_focus (GtkWidget *widget,
{
case GTK_DIR_TAB_FORWARD:
if (focus_link)
- new_index = (focus_link_index + 1) % info->n_links;
+ new_index = focus_link_index + 1;
else
new_index = 0;
- for (i = new_index; i < info->n_links; i++)
+ if (new_index >= info->n_links)
+ goto out;
+
+ while (new_index < info->n_links)
{
- const GtkLabelLink *link = &info->links[i];
+ const GtkLabelLink *link = &info->links[new_index];
if (!range_is_in_ellipsis (self, link->start, link->end))
break;
+
+ new_index++;
}
break;
case GTK_DIR_TAB_BACKWARD:
if (focus_link)
- new_index = focus_link_index == 0 ? info->n_links - 1 : focus_link_index - 1;
+ new_index = focus_link_index - 1;
else
new_index = info->n_links - 1;
- for (i = new_index; i >= 0; i--)
+ if (new_index < 0)
+ goto out;
+
+ while (new_index >= 0)
{
- const GtkLabelLink *link = &info->links[i];
+ const GtkLabelLink *link = &info->links[new_index];
if (!range_is_in_ellipsis (self, link->start, link->end))
break;
+
+ new_index--;
}
break;
@@ -1904,7 +1914,7 @@ gtk_label_focus (GtkWidget *widget,
goto out;
}
- if (new_index != -1)
+ if (new_index != -1 && new_index < info->n_links)
{
focus_link = &info->links[new_index];
info->selection_anchor = focus_link->start;