// Copyright © SixtyFPS GmbH // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 import { FluentFontSettings, FluentPalette, Icons } from "styling.slint"; import { FocusBorder } from "components.slint"; export component CheckBox { in property text; in property enabled <=> touch-area.enabled; out property has-focus: focus-scope.has-focus; in-out property checked; callback toggled; private property text-color: FluentPalette.foreground; min-height: max(18px, layout.min-height); accessible-checkable: true; accessible-enabled: root.enabled; accessible-label: root.text; accessible-checked <=> root.checked; accessible-role: checkbox; accessible-action-default => { root.checked = !root.checked; root.toggled(); } forward-focus: focus-scope; states [ disabled when !root.enabled : { border.border-color: FluentPalette.control-strong-stroke-disabled; background.background: root.checked ? FluentPalette.accent-disabled : FluentPalette.control-alt-disabled; icon.colorize: FluentPalette.text-accent-foreground-disabled; root.text-color: FluentPalette.text-disabled; } pressed when touch-area.pressed : { border.border-color: FluentPalette.control-strong-stroke-disabled; background.background: root.checked ? FluentPalette.tertiary-accent-background : FluentPalette.control-alt-quartiary; icon.colorize: FluentPalette.text-accent-foreground-secondary; } hover when touch-area.has-hover : { background.background: root.checked ? FluentPalette.secondary-accent-background : FluentPalette.control-alt-tertiary; } checked when root.checked && root.enabled : { background.background: FluentPalette.accent-background; } ] animate text-color { duration: 200ms; } layout := HorizontalLayout { spacing: 12px; background := Rectangle { width: 18px; height: self.width; y: (parent.height - self.height) / 2; background: FluentPalette.control-alt-secondary; border-radius: 2px; animate background, border-color { duration: 150ms; } border := Rectangle { border-color: FluentPalette.control-strong-stroke; border-width: root.checked ? 0 : 1px; border-radius: parent.border-radius; } icon := Image { image-fit: contain; visible: root.checked; source: Icons.check-mark; colorize: FluentPalette.accent-foreground; width: 12px; accessible-role: none; animate colorize { duration: 150ms; } } } if root.text != "" : Text { text: root.text; color: root.text-color; font-size: FluentFontSettings.body.font-size; font-weight: FluentFontSettings.body.font-weight; vertical-alignment: center; horizontal-alignment: left; } } touch-area := TouchArea { clicked => { if (root.enabled) { root.checked = !root.checked; root.toggled(); } } } focus-scope := FocusScope { x: 0; width: 0; // Do not react on clicks enabled <=> root.enabled; key-pressed(event) => { if (event.text == " " || event.text == "\n") { touch-area.clicked(); return accept; } return reject; } } // focus border if root.has-focus && root.enabled : FocusBorder { border-radius: 4px; } }