feat(slint-a11y): add keyboard focus rings to shared interactive primitives

Give SecondaryButton, ToggleButton, CircleAction, QbzCheckbox, QbzToggle
and QbzSelect a keyboard FocusScope (Enter/Space trigger the same action
as a click) plus a non-layout focus-ring overlay: a sibling Rectangle
with a transparent-default 2px border that turns Theme.focus-ring only
when the scope has keyboard focus, with its radius matched to the control
(circle for CircleAction) so focus never reflows the layout.

The existing TouchArea stays the pointer source on every control; the
FocusScope only adds key activation and focusability. Universal: the ring
is a per-theme token and only appears on keyboard focus, so it stays
visually neutral at rest across all 33 themes (WCAG 2.4.7 focus-visible).

cc
This commit is contained in:
vicrodh committed 2026-06-20 15:50:58 -06:00
1 parent 9e9135e3d6
commit b4aa514653
6 files changed
+157

No files matched your search

@@ -62,6 +62,32 @@ export component CircleAction inherits Rectangle {
y: Math.round((parent.height - self.height) / 2 / 1px) * 1px;
}
// Keyboard activation: Enter/Space fire the same click. The TouchArea below
// stays the pointer source; this only adds key activation + focusability.
fs := FocusScope {
enabled: root.enabled;
key-pressed(event) => {
if (event.text == Key.Return || event.text == " ") {
root.clicked();
return accept;
}
return reject;
}
}
// Non-layout focus ring — transparent-bordered overlay shown only on
// keyboard focus, circular to match the button so it never reflows.
Rectangle {
x: 0;
y: 0;
width: parent.width;
height: parent.height;
background: transparent;
border-width: 2px;
border-color: fs.has-focus ? Theme.focus-ring : transparent;
border-radius: root.diameter / 2;
}
ta := TouchArea {
mouse-cursor: root.enabled ? pointer : default;
clicked => {
@@ -31,4 +31,30 @@ export component QbzCheckbox inherits Rectangle {
root.toggled();
}
}
// Keyboard activation: Enter/Space toggle the box, same as a click. The
// TouchArea stays the pointer source; this only adds key activation + focus.
fs := FocusScope {
key-pressed(event) => {
if (event.text == Key.Return || event.text == " ") {
root.checked = !root.checked;
root.toggled();
return accept;
}
return reject;
}
}
// Non-layout focus ring — transparent-bordered overlay shown only on
// keyboard focus, so it never reflows the checkbox.
Rectangle {
x: 0;
y: 0;
width: parent.width;
height: parent.height;
background: transparent;
border-width: 2px;
border-color: fs.has-focus ? Theme.focus-ring : transparent;
border-radius: parent.border-radius;
}
}
@@ -155,6 +155,34 @@ export component QbzSelect inherits Rectangle {
}
}
// Keyboard activation: Enter/Space open the option list, same as a click.
// The TouchArea stays the pointer source; this only adds key activation +
// focusability on the collapsed control.
fs := FocusScope {
enabled: root.enabled;
key-pressed(event) => {
if (event.text == Key.Return || event.text == " ") {
root.filter = "";
popup.show();
return accept;
}
return reject;
}
}
// Non-layout focus ring — transparent-bordered overlay shown only on
// keyboard focus, matching the control's radius so it never reflows.
Rectangle {
x: 0;
y: 0;
width: parent.width;
height: parent.height;
background: transparent;
border-width: 2px;
border-color: fs.has-focus ? Theme.focus-ring : transparent;
border-radius: parent.border-radius;
}
popup := PopupWindow {
// Right-anchored so a list wider than the control grows leftward
// and stays on-screen.
@@ -35,4 +35,30 @@ export component QbzToggle inherits Rectangle {
}
}
}
// Keyboard activation: Enter/Space flip the switch, same as a click. The
// TouchArea stays the pointer source; this only adds key activation + focus.
fs := FocusScope {
enabled: root.enabled;
key-pressed(event) => {
if (event.text == Key.Return || event.text == " ") {
root.toggled(!root.checked);
return accept;
}
return reject;
}
}
// Non-layout focus ring — transparent-bordered overlay shown only on
// keyboard focus, matching the switch's pill radius so it never reflows.
Rectangle {
x: 0;
y: 0;
width: parent.width;
height: parent.height;
background: transparent;
border-width: 2px;
border-color: fs.has-focus ? Theme.focus-ring : transparent;
border-radius: parent.border-radius;
}
}
@@ -47,4 +47,30 @@ export component SecondaryButton inherits Rectangle {
}
}
}
// Keyboard activation: Enter/Space fire the same click. The TouchArea stays
// the pointer source; this only adds key activation + focusability.
fs := FocusScope {
enabled: root.enabled;
key-pressed(event) => {
if (event.text == Key.Return || event.text == " ") {
root.clicked();
return accept;
}
return reject;
}
}
// Non-layout focus ring — transparent-bordered overlay shown only on
// keyboard focus, so it never reflows the button.
Rectangle {
x: 0;
y: 0;
width: parent.width;
height: parent.height;
background: transparent;
border-width: 2px;
border-color: fs.has-focus ? Theme.focus-ring : transparent;
border-radius: parent.border-radius;
}
}
@@ -36,4 +36,29 @@ export component ToggleButton inherits Rectangle {
root.clicked();
}
}
// Keyboard activation: Enter/Space fire the same click. The TouchArea stays
// the pointer source; this only adds key activation + focusability.
fs := FocusScope {
key-pressed(event) => {
if (event.text == Key.Return || event.text == " ") {
root.clicked();
return accept;
}
return reject;
}
}
// Non-layout focus ring — transparent-bordered overlay shown only on
// keyboard focus, so it never reflows the toggle.
Rectangle {
x: 0;
y: 0;
width: parent.width;
height: parent.height;
background: transparent;
border-width: 2px;
border-color: fs.has-focus ? Theme.focus-ring : transparent;
border-radius: parent.border-radius;
}
}