Tunal/crates/tunal/ui/components/tab.slint

128 lines
3.6 KiB
Text

import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Icon, Icons } from "icon.slint";
import { IconButton } from "icon-button.slint";
import { StateLayer } from "state-layer.slint";
// One tab: icon, title and close button. The active indicator belongs to `TabBar` so it can
// slide between tabs.
export component Tab inherits StateLayer {
in property <string> title;
// Glyph from `Icons`; empty for none.
in property <string> icon;
in property <bool> active;
in property <bool> closable: true;
callback close();
property <color> content: !root.enabled ? Theme.on-surface.with-alpha(Theme.disabled-content)
: root.active ? Theme.primary
: Theme.on-surface-variant;
height: 40px;
border-radius: Theme.corner-sm;
squeeze: Motion.squeeze-large;
ring-offset: -Theme.focus-ring-width;
accessible-role: tab;
accessible-label: root.title;
accessible-enabled: root.enabled;
accessible-item-selectable: true;
accessible-item-selected: root.active;
accessible-action-default => {
self.activate();
}
HorizontalLayout {
padding-left: Theme.space-3;
padding-right: root.closable ? Theme.space-1 : Theme.space-3;
spacing: Theme.space-2;
if root.icon != "": VerticalLayout {
alignment: center;
Icon {
glyph: root.icon;
size: 18px;
color: root.content;
}
}
Text {
horizontal-stretch: 1;
text: root.title;
color: root.content;
font-size: Theme.label-large.size;
font-weight: Theme.label-large.weight;
vertical-alignment: center;
overflow: elide;
animate color {
duration: Motion.time(Motion.short4);
easing: Motion.standard;
}
}
if root.closable: VerticalLayout {
alignment: center;
IconButton {
icon: Icons.close;
size: 28px;
icon-size: 18px;
tooltip: "Close tab";
enabled: root.enabled;
clicked => {
root.close();
}
}
}
}
}
export struct TabItem {
title: string,
icon: string,
}
// A row of equal-width tabs with one indicator that slides to the active tab.
export component TabBar {
in property <[TabItem]> tabs;
in-out property <int> current;
in property <length> max-tab-width: 240px;
callback selected(index: int);
callback close(index: int);
property <length> tab-width: tabs.length == 0 ? 0 : min(max-tab-width, root.width / tabs.length);
property <length> indicator-height: 3px;
height: 40px + indicator-height;
accessible-role: tab-list;
for tab[index] in root.tabs: Tab {
x: index * root.tab-width;
y: 0;
width: root.tab-width;
title: tab.title;
icon: tab.icon;
active: index == root.current;
clicked => {
root.current = index;
root.selected(index);
}
close => {
root.close(index);
}
}
if root.tabs.length > 0: Rectangle {
x: root.current * root.tab-width + Theme.space-2;
y: root.height - self.height;
width: root.tab-width - 2 * Theme.space-2;
height: root.indicator-height;
border-radius: Theme.corner-full(self.height);
background: Theme.primary;
animate x, width {
duration: Motion.time(Motion.medium2);
easing: Motion.emphasized;
}
}
}