mirror of
https://github.com/slint-ui/slint.git
synced 2025-09-28 12:54:45 +00:00
146 lines
No EOL
4.2 KiB
Text
146 lines
No EOL
4.2 KiB
Text
// Copyright © SixtyFPS GmbH <info@slint.dev>
|
|
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
|
|
|
|
import { Typography, Palette, Icons } from "styling.slint";
|
|
import { FocusBorder } from "components.slint";
|
|
|
|
export component CheckBox {
|
|
private property <brush> background: checked && root.enabled ? Palette.accent : Palette.surface;
|
|
private property <brush> icon-color: Palette.on-surface;
|
|
|
|
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(16px, i-layout.min-height);
|
|
|
|
accessible-checkable: true;
|
|
accessible-label: root.text;
|
|
accessible-checked <=> root.checked;
|
|
accessible-role: checkbox;
|
|
|
|
FocusBorder {
|
|
x: (parent.width - self.width) / 2;
|
|
y: (parent.height - self.height) / 2;
|
|
width: parent.width + 6px;
|
|
height: parent.height + 6px;
|
|
border-radius: 8px;
|
|
has-focus: root.has-focus;
|
|
}
|
|
|
|
i-layout := HorizontalLayout {
|
|
padding-left: 8px;
|
|
spacing: 6px;
|
|
|
|
if (!root.checked) : Rectangle {
|
|
y: (parent.height - self.height) / 2;
|
|
width: 14px;
|
|
height: self.width;
|
|
border-radius: 4px;
|
|
background: root.background;
|
|
clip: true;
|
|
|
|
Rectangle {
|
|
background: @radial-gradient(circle, #00000000 0%, #00000000 50%, #0000001A 100%);
|
|
}
|
|
|
|
Rectangle {
|
|
width: 100%;
|
|
height: 100%;
|
|
border-radius: parent.border-radius;
|
|
border-width: 0.5px;
|
|
border-color: Palette.border;
|
|
}
|
|
}
|
|
|
|
if (root.checked) : Rectangle {
|
|
drop-shadow-blur: 3px;
|
|
drop-shadow-color: #00000066;
|
|
drop-shadow-offset-y: 0.5px;
|
|
y: (parent.height - self.height) / 2;
|
|
width: 14px;
|
|
height: self.width;
|
|
border-radius: 4px;
|
|
background: root.background;
|
|
|
|
Rectangle {
|
|
drop-shadow-blur: 2px;
|
|
drop-shadow-color: #00000026;
|
|
drop-shadow-offset-y: 1px;
|
|
border-radius: parent.border-radius;
|
|
background: root.background;
|
|
}
|
|
|
|
Rectangle {
|
|
drop-shadow-blur: 1px;
|
|
drop-shadow-color: #00000026;
|
|
drop-shadow-offset-y: 0.5px;
|
|
border-radius: parent.border-radius;
|
|
background: root.background;
|
|
}
|
|
|
|
Rectangle {
|
|
border-radius: parent.border-radius;
|
|
background: Palette.dimmer;
|
|
opacity: 0.17;
|
|
}
|
|
|
|
i-icon := Image {
|
|
image-fit: contain;
|
|
visible: root.checked;
|
|
source: Icons.check-mark;
|
|
colorize: root.icon-color;
|
|
width: 12px;
|
|
|
|
animate colorize { duration: 150ms; }
|
|
}
|
|
}
|
|
|
|
if (root.text != "") : Text {
|
|
text: root.text;
|
|
color: Palette.foreground;
|
|
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;
|
|
}
|
|
}
|
|
|
|
states [
|
|
disabled when !root.enabled : {
|
|
opacity: 0.5;
|
|
icon-color: Palette.foreground;
|
|
}
|
|
pressed when i-touch-area.pressed : {
|
|
root.background: root.checked ? Palette.accent-secondary : Palette.surface-secondary;
|
|
}
|
|
]
|
|
|
|
animate background { duration: 150ms; }
|
|
} |