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 { CupertinoPalette, CupertinoFontSettings } 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(20px, i-text.min-height);
48 horizontal-stretch: 1;
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 if (root.is-current || i-touch-area.pressed): Rectangle {
59 width: 100%;
60 height: 100%;
61 background: i-touch-area.pressed ? CupertinoPalette.secondary-control-background : CupertinoPalette.control-background;
62 border-radius: 5px;
63 drop-shadow-blur: 0.25px;
64 drop-shadow-color: #0000001f;
65 drop-shadow-offset-y: 1px;
66 border-width: 1px;
67 border-color: CupertinoPalette.decent-border;
68
69 animate background {
70 duration: 75ms;
71 easing: linear;
72 }
73 }
74
75 if (!root.is-current && root.tab-index < root.num-tabs - 1): Rectangle {
76 x: root.width - self.width;
77 y: (parent.height - self.height) / 2;
78 width: 1px;
79 height: root.height - 6px;
80 background: CupertinoPalette.inner-border;
81 }
82
83 i-touch-area := TouchArea {
84 enabled <=> root.enabled;
85
86 clicked => {
87 root.current = root.tab-index;
88 }
89 }
90
91 i-layout := HorizontalLayout {
92 padding-left: 7px;
93 padding-right: 7px;
94
95 i-text := Text {
96 vertical-alignment: center;
97 horizontal-alignment: center;
98 font-size: CupertinoFontSettings.body-strong.font-size;
99 font-weight: CupertinoFontSettings.body-strong.font-weight;
100 color: CupertinoPalette.foreground;
101 accessible-role: none;
102 }
103 }
104}
105
106export component TabBarImpl inherits TabBarBase {
107 // injected properties:
108 // The currently focused tab
109 in-out property <int> current-focused: i-focus-scope.has-focus ? i-focus-scope.focused-tab : -1;
110
111 accessible-role: tab-list;
112 accessible-delegate-focus: root.current-focused >= 0 ? root.current-focused : root.current;
113 accessible-item-count: root.num-tabs;
114
115 Rectangle {
116 border-radius: 7px;
117 border-color: CupertinoPalette.bar-border;
118 background: CupertinoPalette.bar-background;
119 border-width: 1px;
120 width: 100%;
121 height: 100%;
122
123 Rectangle {
124 x: (parent.width - self.width) / 2;
125 y: parent.border-width;
126 width: parent.width - 2 * parent.border-radius;
127 height: 1px;
128 background: CupertinoPalette.inner-shadow;
129 }
130 }
131
132 Flickable {
133 HorizontalLayout {
134 min-height: 22px;
135 padding: 1px;
136
137 @children
138 }
139 }
140
141 i-focus-scope := FocusScope {
142 property <int> focused-tab: 0;
143
144 x: 0;
145 // Do not react on clicks
146 width: 0px;
147
148 key-pressed(event) => {
149 if (event.text == "\n") {
150 root.current = root.current-focused;
151 return accept;
152 }
153 if (event.text == Key.LeftArrow) {
154 self.focused-tab = Math.max(self.focused-tab - 1, 0);
155 return accept;
156 }
157 if (event.text == Key.RightArrow) {
158 self.focused-tab = Math.min(self.focused-tab + 1, root.num-tabs - 1);
159 return accept;
160 }
161 return reject;
162 }
163
164 key-released(event) => {
165 if (event.text == " ") {
166 root.current = root.current-focused;
167 return accept;
168 }
169 return reject;
170 }
171 }
172}
173
174export component TabWidget inherits TabWidget { }
175