Files
qbz/crates/qbz-slint/ui/primitives/QbzCheckbox.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

64 lines
2.0 KiB
Plaintext

// Small square checkbox primitive matching the QBZ dark theme.
import { Theme } from "../foundation/semantic-colors.slint";
import { QbzIcon } from "QbzIcon.slint";
export component QbzCheckbox inherits Rectangle {
in-out property <bool> checked: false;
callback toggled();
width: 18px;
height: 18px;
border-radius: 4px;
background: root.checked ? Theme.accent : transparent;
border-width: root.checked ? 0px : 1.5px;
border-color: Theme.text-muted;
QbzIcon {
source: @image-url("../assets/icons/check.svg");
visible: root.checked;
width: 12px;
height: 12px;
x: Math.round((parent.width - self.width) / 2 / 1px) * 1px;
y: Math.round((parent.height - self.height) / 2 / 1px) * 1px;
tint: Theme.accent-text;
}
TouchArea {
mouse-cursor: pointer;
clicked => {
root.checked = !root.checked;
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 {
// See QbzPrimaryButton: declared after `ta`, so Slint hit-tests it
// first; focus-on-click:false stops it eating the first press.
focus-on-click: false;
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;
}
}