slint/internal/compiler/widgets/material-base/widget-tabwidget.slint
2023-02-10 16:49:25 +01:00

137 lines
4.4 KiB
Text

// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
import { md } from "md.slint";
export component TabWidgetImpl {
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;
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;
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 {
in property<string> title <=> label.text;
in property<bool> enabled: true;
property<bool> has-focus: root.current-focused == root.tab-index;
property<bool> active: root.tab-index == root.current;
in-out property<int> current; // The currently selected tab
in property<int> current-focused; // The currently focused tab
in property<int> tab-index; // The index of this tab
in property<int> num-tabs; // The total number of tabs
height: 48px;
accessible-role: tab;
accessible-label: root.title;
container := Rectangle {
background: md.sys.color.surface;
}
state-layer := Rectangle {
opacity: !touch.pressed ? !touch.has-hover ? 0 : 0.08 : 0.12;
width: 100%;
height: 100%;
border-radius: container.border-radius;
background: md.sys.color.primary;
animate opacity { duration: 250ms; easing: ease; }
}
layout := HorizontalLayout {
padding-left: 16px;
padding-right: 16px;
label := Text {
vertical-alignment: center;
color: !root.active ? md.sys.color.on-surface : md.sys.color.primary;
// FIXME after Roboto font can be loaded
//font-family: md.sys.typescale.title-small.font;
font-size: md.sys.typescale.title-small.size;
font-weight: md.sys.typescale.title-small.weight;
animate color { duration: 250ms; easing: ease; }
}
}
indicator := Rectangle {
opacity: !root.active ? 0 : 1;
width: 100%;
height: 3px;
y: parent.height - self.height;
background: md.sys.color.primary;
animate opacity { duration: 250ms; easing: ease; }
}
touch := TouchArea {
enabled <=> root.enabled;
clicked => {
root.current = root.tab-index;
}
}
}
export component TabBarImpl {
// injected properties:
in-out property<int> current; // The currently selected tab
in-out property<int> current-focused: fs.has-focus ? fs.focused-tab : -1; // The currently focused tab
in-out property<int> num-tabs; // The total number of tabs
HorizontalLayout {
alignment: start;
@children
}
accessible-role: tab;
accessible-delegate-focus: root.current-focused >= 0 ? root.current-focused : root.current;
fs := FocusScope {
x:0;
width: 0px; // Do not react on clicks
property<int> focused-tab: 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 {}