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

114 lines
3.9 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 } from "styling.slint";
import { FocusBorder } from "components.slint";
export component Button {
callback clicked;
in property <string> text <=> i-text.text;
in property <image> icon;
in property <bool> primary;
in property <bool> enabled <=> i-touch-area.enabled;
in property <bool> checkable;
out property <bool> has-focus: i-focus-scope.has-focus;
out property <bool> pressed: self.enabled && i-touch-area.pressed;
in-out property <bool> checked;
min-width: max(32px, i-layout.min-width);
min-height: max(32px, i-layout.min-height);
horizontal-stretch: 0;
vertical-stretch: 0;
accessible-label <=> i-text.text;
accessible-role: button;
i-background := Rectangle {
border-radius: 4px;
background: root.primary ? Palette.accent-default : Palette.control-default;
i-border := Rectangle {
border-radius: parent.border-radius;
border-width: 1px;
border-color: root.primary ? Palette.accent-control-border : Palette.control-border;
}
i-layout := HorizontalLayout {
padding-left: 12px;
padding-right: 12px;
padding-top: 5px;
padding-bottom: 5px;
spacing: 4px;
alignment: center;
if (root.icon.width > 0 && root.icon.height > 0) : Image {
y: (parent.height - self.height) / 2;
source <=> root.icon;
width: 20px;
}
i-text := Text {
color: root.primary || root.checked ? Palette.text-on-accent-primary : Palette.text-primary;
font-size: Typography.body.font-size;
font-weight: Typography.body.font-weight;
horizontal-alignment: center;
vertical-alignment: center;
animate color { duration: 150ms; }
}
}
animate background, border-color { duration: 150ms; }
}
i-touch-area := TouchArea {
clicked => {
if (root.checkable) {
root.checked = !root.checked;
}
root.clicked();
}
}
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: i-background.border-radius;
}
states [
disabled when !root.enabled : {
i-background.background: root.primary || root.checked ? Palette.accent-disabled : Palette.control-disabled;
i-border.border-color: root.primary || root.checked ? transparent : Palette.control-stroke;
i-text.color: root.primary || root.checked ? Palette.text-on-accent-disabled : Palette.text-disabled;
}
pressed when root.pressed : {
i-background.background: root.primary || root.checked ? Palette.accent-tertiary : Palette.control-tertiary;
i-border.border-color: Palette.control-stroke;
i-text.color: root.primary || root.checked ? Palette.text-on-accent-secondary : Palette.text-secondary;
}
hover when i-touch-area.has-hover : {
i-background.background: root.primary || root.checked ? Palette.accent-secondary : Palette.control-secondary;
}
checked when root.checked : {
i-background.background: Palette.accent-default;
i-border.border-color: Palette.accent-control-border;
i-text.color: Palette.text-on-accent-primary;
}
]
}