// Copyright © SixtyFPS GmbH // 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 background: checked && root.enabled ? Palette.accent : Palette.surface-alt; private property text-color: Palette.foreground; callback toggled; in property enabled: true; in property text; out property has-focus: i-focus-scope.has-focus; in-out property checked: true; 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; border-radius: 8px; i-rail := Rectangle { border-radius: parent.border-radius; background: root.background; border-width: 0.5px; border-color: Palette.border; clip: true; Rectangle { border-width: 1px; border-color: @radial-gradient(circle, #00000026, #00000000); border-radius: parent.border-radius; } animate background, border-color { duration: 150ms; easing: linear; } } 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; background: Palette.surface-thumb; drop-shadow-blur: 0.5px; drop-shadow-offset-y: 0.25px; drop-shadow-color: #0000001F; animate background { 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 : { root.background: root.checked ? Palette.accent-secondary : Palette.surface-secondary; } selected when root.checked : { i-rail.background: Palette.accent; } ] }