slint/internal/compiler/widgets/fluent-base/spinbox.slint
2023-06-19 17:04:56 +02:00

168 lines
No EOL
4.7 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 { Palette, Typography, Icons } from "styling.slint";
component SpinBoxButton {
callback clicked <=> i-touch-area.clicked;
in property <image> icon <=> i-icon.source;
min-width: 28px;
horizontal-stretch: 0;
i-background := Rectangle {
border-radius: 3px;
i-icon := Image {
image-fit: contain;
colorize: Palette.text-secondary;
width: 12px;
animate colorize { duration: 150ms; }
}
}
i-touch-area := TouchArea {}
states [
pressed when i-touch-area.pressed : {
i-background.background: Palette.subtle;
}
]
}
export component SpinBox {
in property <int> minimum;
in property <int> maximum: 100;
in property <bool> enabled <=> i-text-input.enabled;
out property <bool> has-focus <=> i-text-input.has-focus;
in-out property <int> value;
min-width: 128px;
min-height: 30px;
vertical-stretch: 0;
horizontal-stretch: 1;
accessible-role: spinbox;
accessible-value: root.value;
accessible-value-minimum: root.minimum;
accessible-value-maximum: root.maximum;
accessible-value-step: (root.maximum - root.minimum) / 100;
i-background := Rectangle {
border-radius: 4px;
background: Palette.control-default;
border-width: 1px;
border-color: Palette.text-control-border;
i-layout := HorizontalLayout {
padding-left: 12px;
padding-right: 2px;
padding-top: 4px;
padding-bottom: 4px;
spacing: 4px;
Rectangle {
clip: true;
i-text-input := TextInput {
vertical-alignment: center;
horizontal-alignment: left;
color: Palette.text-primary;
font-size: Typography.body.font-size;
font-weight: Typography.body.font-weight;
selection-background-color: Palette.accent-selected-text;
selection-foreground-color: self.color;
horizontal-stretch: 1;
text: root.value;
accepted => {
if (self.text == "") {
root.update-value(root.minimum);
}
}
edited => {
if (self.text == "") {
root.update-value(root.minimum);
return;
}
if (self.text.is-float()) {
root.update-value(root.value + 1);
return;
}
self.text = root.value;
}
}
i-touch-area := TouchArea {
clicked => {
i-text-input.focus();
}
}
}
SpinBoxButton {
visible: root.enabled;
icon: Icons.chevron-up;
clicked => {
root.increment();
}
}
SpinBoxButton {
visible: root.enabled;
icon: Icons.chevron-down;
clicked => {
root.decrement();
}
}
}
i-focus-border := Rectangle {
x: parent.border-radius;
y: parent.height - self.height;
width: parent.width - 2 * parent.border-radius;
height: 2px;
}
}
function update-value(value: int) {
if (value < root.minimum || value > root.maximum) {
return;
}
root.value = value;
}
function increment() {
root.update-value(root.value + 1);
root.update-text();
}
function decrement() {
root.update-value(root.value - 1);
root.update-text();
}
function update-text() {
i-text-input.text = root.value;
}
states [
disabled when !root.enabled : {
i-background.background: Palette.control-disabled;
i-background.border-color: Palette.control-stroke;
i-text-input.color: Palette.text-disabled;
}
focused when root.has-focus : {
i-background.background: Palette.control-input-active;
i-background.border-color: Palette.control-stroke;
i-focus-border.background: Palette.accent-default;
}
]
}