// Copyright © SixtyFPS GmbH // 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 Switch { private property text-color: Palette.text-secondary; callback toggled; in property enabled: true; in property text; in-out property checked: true; out property has-focus: i-focus-scope.has-focus; min-width: 40px; min-height: 20px; 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: 40px; height: 20px; i-rail := Rectangle { border-radius: 10px; border-width: root.checked ? 0 : 1px; border-color: Palette.control-strong-stroke; background: Palette.control-alt-secondary; } i-thumb := Rectangle { x: root.checked ? parent.width - self.width - 4px : 4px; y: (parent.height - self.height) / 2; width: 12px; height: self.width; border-radius: self.height / 2; background: Palette.text-secondary; border-color: Palette.circle-border; 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 : { i-rail.background: root.checked ? Palette.accent-disabled : transparent; i-rail.border-color: Palette.control-strong-stroke-disabled; i-thumb.background: Palette.text-disabled; root.text-color: Palette.text-disabled; i-thumb.background: root.checked ? Palette.text-on-accent-disabled : Palette.text-secondary; } pressed when i-touch-area.pressed : { i-rail.background: root.checked ? Palette.accent-tertiary : Palette.control-alt-quartiary; i-thumb.width: 17px; i-thumb.height: 14px; i-thumb.border-width: root.checked ? 1px : 0; i-thumb.background: root.checked ? Palette.text-on-accent-primary : Palette.text-secondary; } hover when i-touch-area.has-hover : { i-rail.background: root.checked ? Palette.accent-secondary : Palette.control-alt-tertiary; i-thumb.width: 14px; i-thumb.border-width: root.checked ? 1px : 0; i-thumb.background: root.checked ? Palette.text-on-accent-primary : Palette.text-secondary; } selected when root.checked : { i-rail.background: Palette.accent-default; i-thumb.border-width: 1px; i-thumb.border-color: Palette.circle-border; i-thumb.background: Palette.text-on-accent-primary; } ] }