1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4import { FluentPalette, FluentFontSettings } from "styling.slint";
5import { FocusBorder } from "components.slint";
6import { TabBarBase } from "../common/tabwidget-base.slint";
7
8export component TabWidgetImpl inherits Rectangle {
9 in property <length> tabbar-preferred-height;
10 in property <length> tabbar-preferred-width;
11 in property <length> content-min-height;
12 in property <length> content-min-width;
13 in property <int> current-index;
14 in property <int> current-focused;
15 out property <length> content-x: 0;
16 out property <length> content-y: root.tabbar-preferred-height;
17 out property <length> content-height: root.height - root.tabbar-preferred-height;
18 out property <length> content-width: root.width;
19 out property <length> tabbar-x: 0;
20 out property <length> tabbar-y: 0;
21 out property <length> tabbar-height: root.tabbar-preferred-height;
22 out property <length> tabbar-width: root.width;
23
24 preferred-width: root.content-min-width;
25 min-width: max(root.content-min-width, root.tabbar-preferred-width);
26 preferred-height: root.content-min-height + root.tabbar-preferred-height;
27 min-height: root.content-min-height + root.tabbar-preferred-height;
28}
29
30export component TabImpl inherits Rectangle {
31 // The currently focused tab
32 in property <int> current-focused;
33 // The index of this tab
34 in property <int> tab-index;
35 // The total number of tabs
36 in property <int> num-tabs;
37 in property <string> title <=> i-text.text;
38 in property <bool> enabled: true;
39 out property <bool> has-focus: root.current-focused == root.tab-index;
40 // The currently selected tab
41 in-out property <int> current;
42
43 private property <bool> hide-right-border: root.tab-index == root.num-tabs - 1 || tab-index + 1 == current;
44 private property <bool> is-current: root.tab-index == root.current;
45
46 min-width: max(32px, i-text.min-width);
47 min-height: max(32px, i-text.min-height);
48 horizontal-stretch: 0;
49 vertical-stretch: 0;
50 accessible-role: tab;
51 accessible-enabled: root.enabled;
52 accessible-label: root.title;
53 accessible-item-index: root.tab-index;
54 accessible-item-selectable: true;
55 accessible-item-selected: root.is-current;
56 accessible-action-default => { i-touch-area.clicked(); }
57
58 Rectangle {
59 clip: true;
60 width: 100%;
61 height: 100%;
62
63 i-background := Rectangle {
64 y: 1px;
65 width: 100%;
66 height: parent.height + self.border-radius;
67 border-radius: 7px;
68 border-width: root.is-current ? 1px : 0px;
69 border-color: FluentPalette.card-stroke;
70 background: i-touch-area.pressed || root.is-current ? FluentPalette.layer-on-mica-base-alt : i-touch-area.has-hover ? FluentPalette.layer-on-mica-base-alt-secondary : transparent;
71 }
72 }
73
74 i-touch-area := TouchArea {
75 enabled <=> root.enabled;
76
77 clicked => {
78 root.current = root.tab-index;
79 }
80 }
81
82 i-layout := HorizontalLayout {
83 padding-left: 12px;
84 padding-right: 12px;
85
86 i-text := Text {
87 vertical-alignment: center;
88 horizontal-alignment: left;
89 font-size: root.is-current ? FluentFontSettings.body-strong.font-size : FluentFontSettings.body.font-size;
90 font-weight: root.is-current ? FluentFontSettings.body-strong.font-weight : FluentFontSettings.body.font-weight;
91 color: root.is-current ? FluentPalette.control-foreground : FluentPalette.text-secondary;
92 accessible-role: none;
93 }
94 }
95
96 if (!root.is-current && !root.hide-right-border && !i-touch-area.has-hover && !i-touch-area.pressed): Rectangle {
97 x: (parent.width - self.width);
98 width: 2px;
99 height: 16px;
100 background: FluentPalette.divider;
101 }
102
103 if (!root.is-current && !i-touch-area.has-hover && !i-touch-area.pressed): Rectangle {
104 y: (parent.height - self.height);
105 width: 100%;
106 height: 1px;
107 background: FluentPalette.divider;
108 }
109
110 // focus border
111 if (root.has-focus && root.enabled): FocusBorder {
112 border-radius: i-background.border-radius;
113 }
114}
115
116export component TabBarImpl inherits TabBarBase {
117 // injected properties:
118 // The currently focused tab
119 in-out property <int> current-focused: i-focus-scope.has-focus ? i-focus-scope.focused-tab : -1;
120
121 Flickable {
122 HorizontalLayout {
123 @children
124 }
125 }
126
127 accessible-role: tab-list;
128 accessible-delegate-focus: root.current-focused >= 0 ? root.current-focused : root.current;
129 accessible-item-count: root.num-tabs;
130
131 i-focus-scope := FocusScope {
132 property <int> focused-tab: 0;
133
134 x: 0;
135 // Do not react on clicks
136 width: 0px;
137
138 key-pressed(event) => {
139 if (event.text == "\n") {
140 root.current = root.current-focused;
141 return accept;
142 }
143 if (event.text == Key.LeftArrow) {
144 self.focused-tab = Math.max(self.focused-tab - 1, 0);
145 return accept;
146 }
147 if (event.text == Key.RightArrow) {
148 self.focused-tab = Math.min(self.focused-tab + 1, root.num-tabs - 1);
149 return accept;
150 }
151 return reject;
152 }
153
154 key-released(event) => {
155 if (event.text == " ") {
156 root.current = root.current-focused;
157 return accept;
158 }
159 return reject;
160 }
161 }
162}
163
164export component TabWidget inherits TabWidget { }
165