slint/internal/compiler/widgets/material/tabwidget.slint
2025-03-04 12:37:06 +01:00

163 lines
5.1 KiB
Text

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
import { MaterialPalette, MaterialFontSettings } from "styling.slint";
import { TabBarBase } from "../common/tabwidget-base.slint";
export component TabWidgetImpl inherits Rectangle {
in property <length> tabbar-preferred-height;
in property <length> tabbar-preferred-width;
in property <length> content-min-height;
in property <length> content-min-width;
in property <int> current-index;
in property <int> current-focused;
out property <length> content-x: 0;
out property <length> content-y: root.tabbar-preferred-height;
out property <length> content-height: root.height - root.tabbar-preferred-height;
out property <length> content-width: root.width;
out property <length> tabbar-x: 0;
out property <length> tabbar-y: 0;
out property <length> tabbar-height: root.tabbar-preferred-height;
out property <length> tabbar-width: root.width;
preferred-width: root.content-min-width;
min-width: max(root.content-min-width, root.tabbar-preferred-width);
preferred-height: root.content-min-height + root.tabbar-preferred-height;
min-height: root.content-min-height + root.tabbar-preferred-height;
}
export component TabImpl inherits Rectangle {
// The currently focused tab
in property <int> current-focused;
// The index of this tab
in property <int> tab-index;
// The total number of tabs
in property <int> num-tabs;
in property <string> title <=> text.text;
in property <bool> enabled: true;
// The currently selected tab
in-out property <int> current;
property <bool> has-focus: root.current-focused == root.tab-index;
property <bool> active: root.tab-index == root.current;
min-width: max(48px, layout.min-width);
min-height: max(48px, layout.min-height);
accessible-role: tab;
accessible-enabled: root.enabled;
accessible-label: root.title;
accessible-item-index: root.tab-index;
accessible-item-selectable: true;
accessible-item-selected: root.active;
accessible-action-default => { touch-area.clicked(); }
container := Rectangle {
background: MaterialPalette.alternate-background;
}
state-layer := Rectangle {
opacity: !touch-area.pressed ? !touch-area.has-hover ? 0 : 0.08 : 0.12;
width: 100%;
height: 100%;
border-radius: container.border-radius;
background: MaterialPalette.accent-background;
animate opacity {
duration: 250ms;
easing: ease;
}
}
layout := HorizontalLayout {
padding-left: 16px;
padding-right: 16px;
text := Text {
vertical-alignment: center;
horizontal-alignment: center;
color: !root.active ? MaterialPalette.control-foreground : MaterialPalette.accent-background;
// FIXME after Roboto font can be loaded
//font-family: MaterialFontSettings.title-small.font;
font-size: MaterialFontSettings.title-small.font-size;
font-weight: MaterialFontSettings.title-small.font-weight;
accessible-role: none;
animate color {
duration: 250ms;
easing: ease;
}
}
}
indicator := Rectangle {
opacity: !root.active ? 0 : 1;
width: 100%;
height: 3px;
y: parent.height - self.height;
background: MaterialPalette.accent-background;
animate opacity {
duration: 250ms;
easing: ease;
}
}
touch-area := TouchArea {
enabled <=> root.enabled;
clicked => {
root.current = root.tab-index;
}
}
}
export component TabBarImpl inherits TabBarBase {
// injected properties:
// The currently focused tab
in-out property <int> current-focused: focus-scope.has-focus ? focus-scope.focused-tab : -1;
accessible-role: tab-list;
accessible-delegate-focus: root.current-focused >= 0 ? root.current-focused : root.current;
accessible-item-count: root.num-tabs;
Flickable {
HorizontalLayout {
@children
}
}
focus-scope := FocusScope {
property <int> focused-tab: 0;
x: 0;
// Do not react on clicks
width: 0;
key-pressed(event) => {
if (event.text == "\n") {
root.current = root.current-focused;
return accept;
}
if (event.text == Key.LeftArrow) {
self.focused-tab = Math.max(self.focused-tab - 1, 0);
return accept;
}
if (event.text == Key.RightArrow) {
self.focused-tab = Math.min(self.focused-tab + 1, root.num-tabs - 1);
return accept;
}
return reject;
}
key-released(event) => {
if (event.text == " ") {
root.current = root.current-focused;
return accept;
}
return reject;
}
}
}
export component TabWidget inherits TabWidget { }