1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4import { FluentFontSettings, FluentPalette } from "styling.slint";
5import { FocusBorder } from "components.slint";
6
7export component Switch {
8 in property <bool> enabled: true;
9 in property <string> text;
10 in-out property <bool> checked;
11 out property <bool> has-focus: focus-scope.has-focus;
12
13 callback toggled;
14
15 private property <color> text-color: FluentPalette.foreground;
16
17 function toggle-checked() {
18 if(!root.enabled) {
19 return;
20 }
21
22 root.checked = !root.checked;
23 root.toggled();
24 }
25
26 min-width: 40px;
27 min-height: max(20px, layout.min-height);
28 vertical-stretch: 0;
29 horizontal-stretch: 0;
30 accessible-enabled: root.enabled;
31 accessible-label: root.text;
32 accessible-checkable: true;
33 accessible-checked <=> root.checked;
34 accessible-role: switch;
35 accessible-action-default => {
36 root.checked = !root.checked;
37 root.toggled();
38 }
39 forward-focus: focus-scope;
40
41 states [
42 disabled when !root.enabled : {
43 rail.background: root.checked ? FluentPalette.accent-disabled : transparent;
44 rail.border-color: FluentPalette.control-strong-stroke-disabled;
45 thumb.background: FluentPalette.text-disabled;
46 root.text-color: FluentPalette.text-disabled;
47 thumb.background: root.checked ? FluentPalette.text-accent-foreground-disabled : FluentPalette.text-secondary;
48 }
49 pressed when touch-area.pressed : {
50 rail.background: root.checked ? FluentPalette.tertiary-accent-background : FluentPalette.control-alt-quartiary;
51 thumb.width: 17px;
52 thumb.height: 14px;
53 thumb.border-width: root.checked ? 1px : 0;
54 thumb.background: root.checked ? FluentPalette.accent-foreground : FluentPalette.text-secondary;
55 }
56 hover when touch-area.has-hover : {
57 rail.background: root.checked ? FluentPalette.secondary-accent-background : FluentPalette.control-alt-tertiary;
58 thumb.width: 14px;
59 thumb.border-width: root.checked ? 1px : 0;
60 thumb.background: root.checked ? FluentPalette.accent-foreground : FluentPalette.text-secondary;
61 }
62 selected when root.checked : {
63 rail.background: FluentPalette.accent-background;
64 thumb.border-width: 1px;
65 thumb.border-color: FluentPalette.circle-border;
66 thumb.background: FluentPalette.accent-foreground;
67 }
68 ]
69
70 layout := HorizontalLayout {
71 spacing: 12px;
72
73 VerticalLayout {
74 alignment: center;
75
76 Rectangle {
77 width: 40px;
78 height: 20px;
79
80 rail := Rectangle {
81 border-radius: 10px;
82 border-width: root.checked ? 0 : 1px;
83 border-color: FluentPalette.control-strong-stroke;
84 background: FluentPalette.control-alt-secondary;
85 }
86
87 thumb := Rectangle {
88 x: root.checked ? parent.width - self.width - 4px : 4px;
89 y: (parent.height - self.height) / 2;
90 width: 12px;
91 height: self.width;
92 border-radius: self.height / 2;
93 background: FluentPalette.text-secondary;
94 border-color: FluentPalette.circle-border;
95
96 animate background, width { duration: 150ms; easing: linear; }
97 }
98
99 // focus border
100 if root.has-focus && root.enabled : FocusBorder {
101 border-radius: rail.border-radius;
102 }
103 }
104 }
105
106 if (root.text != "") : Text {
107 text: root.text;
108 color: root.text-color;
109 font-size: FluentFontSettings.body.font-size;
110 font-weight: FluentFontSettings.body.font-weight;
111 vertical-alignment: center;
112 horizontal-alignment: left;
113 }
114 }
115
116 touch-area := TouchArea {
117 enabled <=> root.enabled;
118
119 clicked => {
120 root.toggle-checked();
121 }
122 }
123
124 focus-scope := FocusScope {
125 x:0;
126 width: 0px; // Do not react on clicks
127 enabled <=> root.enabled;
128
129 key-pressed(event) => {
130 if (event.text == " " || event.text == "\n") {
131 root.toggle-checked();
132 return accept;
133 }
134 return reject;
135 }
136 }
137}
138