slint/internal/compiler/widgets/fluent-base/checkbox.slint
2023-06-20 14:54:10 +02:00

114 lines
No EOL
3.6 KiB
Text

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.0 OR LicenseRef-Slint-commercial
import { Typography, Palette, Icons } from "styling.slint";
import { FocusBorder } from "components.slint";
export component CheckBox {
private property <color> text-color: Palette.text-secondary;
callback toggled;
in property <string> text;
in property <bool> enabled <=> i-touch-area.enabled;
out property <bool> has-focus: i-focus-scope.has-focus;
in-out property <bool> checked;
min-height: max(32px, i-layout.min-height);
accessible-checkable: true;
accessible-label: root.text;
accessible-checked <=> root.checked;
accessible-role: checkbox;
i-layout := HorizontalLayout {
padding-left: 8px;
padding-right: root.text == "" ? 8px : 12px;
spacing: 12px;
i-background := Rectangle {
width: 18px;
height: self.width;
y: (parent.height - self.height) / 2;
background: Palette.control-alt-secondary;
border-radius: 2px;
i-border := Rectangle {
border-color: Palette.control-strong-stroke;
border-width: root.checked ? 0 : 1px;
border-radius: parent.border-radius;
}
i-icon := Image {
image-fit: contain;
visible: root.checked;
source: Icons.check-mark;
colorize: Palette.text-on-accent-primary;
width: 12px;
animate colorize { duration: 150ms; }
}
animate background, border-color { duration: 150ms; }
}
if (root.text != "") : Text {
text: root.text;
color: root.text-color;
font-size: Typography.body.font-size;
font-weight: Typography.body.font-weight;
vertical-alignment: center;
horizontal-alignment: left;
}
}
i-touch-area := TouchArea {
clicked => {
if (root.enabled) {
root.checked = !root.checked;
root.toggled();
}
}
}
i-focus-scope := FocusScope {
x: 0;
width: 0; // Do not react on clicks
enabled <=> root.enabled;
key-pressed(event) => {
if (event.text == " " || event.text == "\n") {
i-touch-area.clicked();
return accept;
}
return reject;
}
}
// focus border
if (root.has-focus && root.enabled) : FocusBorder {
border-radius: 4px;
}
states [
disabled when !root.enabled : {
i-border.border-color: Palette.control-strong-stroke-disabled;
i-background.background: root.checked ? Palette.accent-disabled : Palette.control-alt-disabled;
i-icon.colorize: Palette.text-on-accent-disabled;
root.text-color: Palette.text-disabled;
}
pressed when i-touch-area.pressed : {
i-border.border-color: Palette.control-strong-stroke-disabled;
i-background.background: root.checked ? Palette.accent-tertiary : Palette.control-alt-quartiary;
i-icon.colorize: Palette.text-on-accent-secondary;
}
hover when i-touch-area.has-hover : {
i-background.background: root.checked ? Palette.accent-secondary : Palette.control-alt-tertiary;
}
checked when root.checked && root.enabled : {
i-background.background: Palette.accent-default;
}
]
animate text-color { duration: 200ms; }
}