Files
qbz/crates/qbz-slint/ui/primitives/QbzToggle.slint
T
vicrodh 88f1967d8a fix(slint): single click on button primitives (FocusScope stole the first press)
Slint hit-tests sibling children in reverse declaration order, so the
keyboard FocusScope declared after the pointer TouchArea was visited
first and, with the default focus-on-click, consumed the first Pressed
just to grab focus — aborting traversal before the TouchArea, so every
button needed two clicks. Set focus-on-click: false on the FocusScope in
QbzPrimaryButton, SecondaryButton, QbzCheckbox, QbzToggle, ToggleButton
and QbzSelect; the press now reaches the TouchArea on the first click
(Tab/Enter/Space activation unaffected).

cc
2026-06-22 21:34:59 -06:00

68 lines
2.1 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;
// See QbzPrimaryButton: declared after `ta`, hit-tested first;
// focus-on-click:false stops it eating the first press (two-click bug).
focus-on-click: false;
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;
}
}