slint/internal/compiler/widgets/cupertino-base/switch.slint
Florian Blasius 80407d0375
Added cupertino style for std-widgets (#3301)
* Started with cupertino style
2023-08-21 11:04:18 +02:00

135 lines
No EOL
3.9 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 } from "styling.slint";
import { FocusBorder } from "components.slint";
export component Switch {
private property <color> text-color: Palette.foreground;
callback toggled;
in property<bool> enabled: true;
in property <string> text;
in-out property <bool> checked: true;
out property <bool> has-focus: i-focus-scope.has-focus;
min-width: 26px;
min-height: 15px;
vertical-stretch: 0;
horizontal-stretch: 0;
accessible-label: root.text;
accessible-checkable: true;
accessible-checked <=> root.checked;
accessible-role: checkbox;
forward-focus: i-focus-scope;
i-layout := HorizontalLayout {
spacing: 12px;
VerticalLayout {
alignment: center;
Rectangle {
width: 26px;
height: 15px;
i-rail := Rectangle {
border-radius: 7px;
background: Palette.bar-background;
Rectangle {
border-color: Palette.fake-inner-shadow;
border-radius: parent.border-radius;
border-width: 1px;
}
}
i-thumb := Rectangle {
x: root.checked ? parent.width - self.width - 1px : 2px;
y: (parent.height - self.height) / 2;
height: parent.height - 2px;
width: self.height;
border-radius: self.height / 2;
i-thumb-border := Rectangle {
x: (parent.width - self.width) / 2;
y: (parent.height - self.height) / 2;
width: parent.width + 2px;
height: self.width;
border-radius: parent.border-radius + 1px;
border-width: 1px;
border-color: Palette.fake-shadow;
}
i-thumb-background := Rectangle {
background: Palette.thumb-background;
border-radius: parent.border-radius;
}
animate background, width { duration: 150ms; easing: linear; }
}
animate background, border-color { duration: 150ms; easing: linear; }
// focus border
if (root.has-focus && root.enabled) : FocusBorder {
border-radius: i-rail.border-radius;
}
}
}
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 {
enabled <=> root.enabled;
clicked => {
root.toggle-checked();
}
}
i-focus-scope := FocusScope {
x:0;
width: 0px; // Do not react on clicks
enabled <=> root.enabled;
key-pressed(event) => {
if (event.text == " " || event.text == "\n") {
root.toggle-checked();
return accept;
}
return reject;
}
}
function toggle-checked() {
if(!root.enabled) {
return;
}
root.checked = !root.checked;
root.toggled();
}
states [
disabled when !root.enabled : {
root.opacity: 0.5;
}
pressed when i-touch-area.pressed : {
}
hover when i-touch-area.has-hover : {
}
selected when root.checked : {
i-rail.background: Palette.primary;
}
]
}