Files
qbz/crates/qbz-slint/ui/primitives/QbzToggle.slint
T
vicrodh b4aa514653 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
2026-06-20 15:50:58 -06:00

65 lines
1.9 KiB
Plaintext

// Switch toggle primitive — the standard on/off control for settings.
//
// The toggle does not flip itself: it emits `toggled(new-value)` and the
// caller updates the bound state. This keeps a single source of truth in
// the SettingsState global.
import { Theme } from "../foundation/semantic-colors.slint";
export component QbzToggle inherits Rectangle {
in property <bool> checked: false;
in property <bool> enabled: true;
callback toggled(bool);
width: 40px;
height: 22px;
border-radius: 11px;
background: root.checked ? Theme.accent : Theme.surface-elevated;
opacity: root.enabled ? 1.0 : 0.4;
Rectangle {
width: 16px;
height: 16px;
border-radius: 8px;
background: Theme.text-primary;
y: 3px;
x: root.checked ? root.width - self.width - 3px : 3px;
animate x { duration: 120ms; easing: ease-out; }
}
TouchArea {
mouse-cursor: root.enabled ? pointer : default;
clicked => {
if (root.enabled) {
root.toggled(!root.checked);
}
}
}
// 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;
}
}