Debian Codegraph @

scene/gui/tab_bar.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* tab_bar.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "tab_bar.h"
32
33 #include "scene/gui/box_container.h"
34 #include "scene/gui/label.h"
35 #include "scene/gui/texture_rect.h"
36 #include "scene/main/timer.h"
37 #include "scene/main/viewport.h"
38 #include "scene/theme/theme_db.h"
39
40 static inline Color _select_color(const Color &p_override_color, const Color &p_default_color) {
41 return p_override_color.a > 0 ? p_override_color : p_default_color;
42 }
43
44 Size2 TabBar::get_minimum_size() const {
45 Size2 ms;
46
47 if (tabs.is_empty()) {
48 return ms;
49 }
50
51 int y_margin = MAX(MAX(MAX(theme_cache.tab_unselected_style->get_minimum_size().height, theme_cache.tab_hovered_style->get_minimum_size().height), theme_cache.tab_selected_style->get_minimum_size().height), theme_cache.tab_disabled_style->get_minimum_size().height);
52 int max_tab_width = 0;
53
54 for (int i = 0; i < tabs.size(); i++) {
55 if (tabs[i].hidden) {
56 continue;
57 }
58
59 int ofs = ms.width;
60
61 Ref<StyleBox> style;
62 if (tabs[i].disabled) {
63 style = theme_cache.tab_disabled_style;
64 } else if (current == i) {
65 style = theme_cache.tab_selected_style;
66 } else if (hover == i) {
67 style = theme_cache.tab_hovered_style;
68 } else {
69 style = theme_cache.tab_unselected_style;
70 }
71 ms.width += style->get_minimum_size().width;
72
73 if (tabs[i].icon.is_valid()) {
74 const Size2 icon_size = _get_tab_icon_size(i);
75 ms.height = MAX(ms.height, icon_size.height + y_margin);
76 ms.width += icon_size.width + theme_cache.h_separation;
77 }
78
79 if (!tabs[i].text.is_empty()) {
80 ms.width += tabs[i].size_text + theme_cache.h_separation;
81 }
82 ms.height = MAX(ms.height, tabs[i].text_buf->get_size().y + y_margin);
83
84 bool close_visible = cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current);
85
86 if (tabs[i].right_button.is_valid()) {
87 Ref<Texture2D> rb = tabs[i].right_button;
88
89 if (close_visible) {
90 ms.width += theme_cache.button_hl_style->get_minimum_size().width + rb->get_width();
91 } else {
92 ms.width += theme_cache.button_hl_style->get_margin(SIDE_LEFT) + rb->get_width() + theme_cache.h_separation;
93 }
94
95 ms.height = MAX(ms.height, rb->get_height() + y_margin);
96 }
97
98 if (close_visible) {
99 ms.width += theme_cache.button_hl_style->get_margin(SIDE_LEFT) + theme_cache.close_icon->get_width() + theme_cache.h_separation;
100
101 ms.height = MAX(ms.height, theme_cache.close_icon->get_height() + y_margin);
102 }
103
104 if (ms.width - ofs > style->get_minimum_size().width) {
105 ms.width -= theme_cache.h_separation;
106 }
107
108 if (i < tabs.size() - 1) {
109 ms.width += theme_cache.tab_separation;
110 }
111
112 if (ms.width - ofs > max_tab_width) {
113 max_tab_width = ms.width - ofs;
114 }
115 }
116
117 if (clip_tabs) {
118 ms.width = max_tab_width + (get_tab_count() > 1 ? theme_cache.decrement_icon->get_width() + theme_cache.increment_icon->get_width() : 0);
119 }
120
121 return ms;
122 }
123
124 void TabBar::gui_input(const Ref<InputEvent> &p_event) {
125 ERR_FAIL_COND(p_event.is_null());
126
127 Ref<InputEventMouseMotion> mm = p_event;
128
129 if (mm.is_valid()) {
130 Point2 pos = mm->get_position();
131
132 if (buttons_visible) {
133 if (is_layout_rtl()) {
134 if (pos.x < theme_cache.decrement_icon->get_width()) {
135 if (highlight_arrow != 1) {
136 highlight_arrow = 1;
137 queue_redraw();
138 }
139 } else if (pos.x < theme_cache.increment_icon->get_width() + theme_cache.decrement_icon->get_width()) {
140 if (highlight_arrow != 0) {
141 highlight_arrow = 0;
142 queue_redraw();
143 }
144 } else if (highlight_arrow != -1) {
145 highlight_arrow = -1;
146 queue_redraw();
147 }
148 } else {
149 int limit_minus_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
150 if (pos.x > limit_minus_buttons + theme_cache.decrement_icon->get_width()) {
151 if (highlight_arrow != 1) {
152 highlight_arrow = 1;
153 queue_redraw();
154 }
155 } else if (pos.x > limit_minus_buttons) {
156 if (highlight_arrow != 0) {
157 highlight_arrow = 0;
158 queue_redraw();
159 }
160 } else if (highlight_arrow != -1) {
161 highlight_arrow = -1;
162 queue_redraw();
163 }
164 }
165 }
166
167 if (get_viewport()->gui_is_dragging() && can_drop_data(pos, get_viewport()->gui_get_drag_data())) {
168 dragging_valid_tab = true;
169 queue_redraw();
170 }
171
172 if (!tabs.is_empty()) {
173 _update_hover();
174 }
175
176 return;
177 }
178
179 Ref<InputEventMouseButton> mb = p_event;
180
181 if (mb.is_valid()) {
182 if (mb->is_pressed() && (mb->get_button_index() == MouseButton::WHEEL_UP || (is_layout_rtl() ? mb->get_button_index() == MouseButton::WHEEL_RIGHT : mb->get_button_index() == MouseButton::WHEEL_LEFT)) && !mb->is_command_or_control_pressed()) {
183 if (scrolling_enabled && buttons_visible) {
184 if (offset > 0) {
185 offset--;
186 _update_cache();
187 queue_redraw();
188 }
189 }
190 }
191
192 if (mb->is_pressed() && (mb->get_button_index() == MouseButton::WHEEL_DOWN || mb->get_button_index() == (is_layout_rtl() ? MouseButton::WHEEL_LEFT : MouseButton::WHEEL_RIGHT)) && !mb->is_command_or_control_pressed()) {
193 if (scrolling_enabled && buttons_visible) {
194 if (missing_right && offset < tabs.size()) {
195 offset++;
196 _update_cache();
197 queue_redraw();
198 }
199 }
200 }
201
202 if (rb_pressing && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
203 if (rb_hover != -1) {
204 emit_signal(SNAME("tab_button_pressed"), rb_hover);
205 }
206
207 rb_pressing = false;
208 queue_redraw();
209 }
210
211 if (cb_pressing && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
212 if (cb_hover != -1) {
213 emit_signal(SNAME("tab_close_pressed"), cb_hover);
214 }
215
216 cb_pressing = false;
217 queue_redraw();
218 }
219
220 if (close_with_middle_mouse && mb->is_pressed() && mb->get_button_index() == MouseButton::MIDDLE) {
221 if (hover != -1) {
222 emit_signal(SNAME("tab_close_pressed"), hover);
223 }
224 }
225
226 if (mb->is_pressed() != switch_on_release) {
227 Point2 pos = mb->get_position();
228 bool selecting = mb->get_button_index() == MouseButton::LEFT || (select_with_rmb && mb->get_button_index() == MouseButton::RIGHT);
229
230 if (buttons_visible && selecting) {
231 if (is_layout_rtl()) {
232 if (pos.x < theme_cache.decrement_icon->get_width()) {
233 if (missing_right) {
234 offset++;
235 _update_cache();
236 queue_redraw();
237 }
238 return;
239 } else if (pos.x < theme_cache.increment_icon->get_width() + theme_cache.decrement_icon->get_width()) {
240 if (offset > 0) {
241 offset--;
242 _update_cache();
243 queue_redraw();
244 }
245 return;
246 }
247 } else {
248 int limit = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
249 if (pos.x > limit + theme_cache.decrement_icon->get_width()) {
250 if (missing_right) {
251 offset++;
252 _update_cache();
253 queue_redraw();
254 }
255 return;
256 } else if (pos.x > limit) {
257 if (offset > 0) {
258 offset--;
259 _update_cache();
260 queue_redraw();
261 }
262 return;
263 }
264 }
265 }
266
267 if (tabs.is_empty()) {
268 // Return early if there are no actual tabs to handle input for.
269 return;
270 }
271
272 int found = get_tab_idx_at_point(pos);
273 if (found != -1) {
274 // Clicking right button icon.
275 if (tabs[found].rb_rect.has_point(pos)) {
276 if (selecting) {
277 rb_pressing = true;
278 _update_hover();
279 queue_redraw();
280 }
281 return;
282 }
283
284 // Clicking close button.
285 if (tabs[found].cb_rect.has_point(pos) && (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && found == current))) {
286 if (selecting) {
287 cb_pressing = true;
288 _update_hover();
289 queue_redraw();
290 }
291 return;
292 }
293
294 // Selecting a tab.
295 if (selecting && !tabs[found].disabled) {
296 if (deselect_enabled && get_current_tab() == found) {
297 set_current_tab(-1);
298 } else {
299 set_current_tab(found);
300 }
301
302 emit_signal(SNAME("tab_clicked"), found);
303 }
304
305 // Right mouse button clicked on a tab.
306 if (mb->get_button_index() == MouseButton::RIGHT) {
307 emit_signal(SNAME("tab_rmb_clicked"), found);
308 }
309 }
310 }
311 }
312
313 if (p_event->is_pressed()) {
314 Input *input = Input::get_singleton();
315 Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
316 Ref<InputEventJoypadButton> joypadbutton_event = p_event;
317 bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
318 if (p_event->is_action("ui_right", true)) {
319 if (is_joypad_event) {
320 if (!input->is_action_just_pressed_by_event("ui_right", p_event, true)) {
321 return;
322 }
323 set_process_internal(true);
324 }
325 if (is_layout_rtl() ? select_previous_available() : select_next_available()) {
326 accept_event();
327 }
328 } else if (p_event->is_action("ui_left", true)) {
329 if (is_joypad_event) {
330 if (!input->is_action_just_pressed_by_event("ui_left", p_event, true)) {
331 return;
332 }
333 set_process_internal(true);
334 }
335 if (is_layout_rtl() ? select_next_available() : select_previous_available()) {
336 accept_event();
337 }
338 }
339 }
340 }
341
342 String TabBar::get_tooltip(const Point2 &p_pos) const {
343 int tab_idx = get_tab_idx_at_point(p_pos);
344 if (tab_idx < 0) {
345 return Control::get_tooltip(p_pos);
346 }
347
348 if (tabs[tab_idx].tooltip.is_empty() && tabs[tab_idx].truncated) {
349 return tabs[tab_idx].text;
350 }
351
352 return tabs[tab_idx].tooltip;
353 }
354
355 void TabBar::_shape(int p_tab) {
356 tabs.write[p_tab].text_buf->clear();
357 tabs.write[p_tab].text_buf->set_width(-1);
358 if (tabs[p_tab].text_direction == Control::TEXT_DIRECTION_INHERITED) {
359 tabs.write[p_tab].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
360 } else {
361 tabs.write[p_tab].text_buf->set_direction((TextServer::Direction)tabs[p_tab].text_direction);
362 }
363
364 const String &lang = tabs[p_tab].language.is_empty() ? _get_locale() : tabs[p_tab].language;
365 tabs.write[p_tab].text_buf->add_string(atr(tabs[p_tab].text), theme_cache.font, theme_cache.font_size, lang);
366 }
367
368 RID TabBar::get_tab_accessibility_element(int p_tab) const {
369 RID ae = get_accessibility_element();
370 ERR_FAIL_COND_V(ae.is_null(), RID());
371
372 const Tab &item = tabs[p_tab];
373 if (item.accessibility_item_element.is_null()) {
374 item.accessibility_item_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_TAB);
375 item.accessibility_item_dirty = true;
376 }
377 return item.accessibility_item_element;
378 }
379
380 RID TabBar::get_focused_accessibility_element() const {
381 if (current == -1) {
382 return get_accessibility_element();
383 } else {
384 const Tab &item = tabs[current];
385 return item.accessibility_item_element;
386 }
387 }
388
389 void TabBar::_accessibility_action_scroll_into_view(const Variant &p_data, int p_index) {
390 ensure_tab_visible(p_index);
391 }
392
393 void TabBar::_accessibility_action_focus(const Variant &p_data, int p_index) {
394 set_current_tab(p_index);
395 }
396
397 void TabBar::_notification(int p_what) {
398 switch (p_what) {
399 case NOTIFICATION_ENTER_TREE: {
400 if (scroll_to_selected) {
401 ensure_tab_visible(current);
402 }
403 // Set initialized even if no tabs were set.
404 initialized = true;
405 } break;
406
407 case NOTIFICATION_INTERNAL_PROCESS: {
408 Input *input = Input::get_singleton();
409
410 if (input->is_action_just_released("ui_left") || input->is_action_just_released("ui_right")) {
411 gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
412 set_process_internal(false);
413 return;
414 }
415
416 gamepad_event_delay_ms -= get_process_delta_time();
417 if (gamepad_event_delay_ms <= 0) {
418 gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
419 if (input->is_action_pressed("ui_right")) {
420 is_layout_rtl() ? select_previous_available() : select_next_available();
421 }
422
423 if (input->is_action_pressed("ui_left")) {
424 is_layout_rtl() ? select_next_available() : select_previous_available();
425 }
426 }
427 } break;
428
429 case NOTIFICATION_EXIT_TREE:
430 case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
431 for (int i = 0; i < tabs.size(); i++) {
432 tabs.write[i].accessibility_item_element = RID();
433 }
434 } break;
435
436 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
437 RID ae = get_accessibility_element();
438 ERR_FAIL_COND(ae.is_null());
439
440 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_TAB_BAR);
441 DisplayServer::get_singleton()->accessibility_update_set_list_item_count(ae, tabs.size());
442
443 for (int i = 0; i < tabs.size(); i++) {
444 const Tab &item = tabs[i];
445
446 if (item.accessibility_item_element.is_null()) {
447 item.accessibility_item_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_TAB);
448 item.accessibility_item_dirty = true;
449 }
450
451 if (item.accessibility_item_dirty) {
452 DisplayServer::get_singleton()->accessibility_update_add_action(item.accessibility_item_element, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &TabBar::_accessibility_action_scroll_into_view).bind(i));
453 DisplayServer::get_singleton()->accessibility_update_add_action(item.accessibility_item_element, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &TabBar::_accessibility_action_focus).bind(i));
454
455 DisplayServer::get_singleton()->accessibility_update_set_list_item_index(item.accessibility_item_element, i);
456 DisplayServer::get_singleton()->accessibility_update_set_name(item.accessibility_item_element, atr(item.text));
457 DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(item.accessibility_item_element, i == current);
458 DisplayServer::get_singleton()->accessibility_update_set_flag(item.accessibility_item_element, DisplayServer::AccessibilityFlags::FLAG_DISABLED, item.disabled);
459 DisplayServer::get_singleton()->accessibility_update_set_flag(item.accessibility_item_element, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, item.hidden);
460 DisplayServer::get_singleton()->accessibility_update_set_tooltip(item.accessibility_item_element, item.tooltip);
461
462 DisplayServer::get_singleton()->accessibility_update_set_bounds(item.accessibility_item_element, Rect2(Point2(item.ofs_cache, 0), Size2(item.size_cache, get_size().height)));
463
464 item.accessibility_item_dirty = false;
465 }
466 }
467 } break;
468
469 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
470 queue_redraw();
471 } break;
472
473 case NOTIFICATION_THEME_CHANGED:
474 case NOTIFICATION_TRANSLATION_CHANGED: {
475 for (int i = 0; i < tabs.size(); ++i) {
476 _shape(i);
477 }
478
479 queue_accessibility_update();
480 queue_redraw();
481 update_minimum_size();
482
483 [[fallthrough]];
484 }
485 case NOTIFICATION_RESIZED: {
486 int ofs_old = offset;
487 int max_old = max_drawn_tab;
488
489 _update_cache();
490 _ensure_no_over_offset();
491
492 if (scroll_to_selected && (offset != ofs_old || max_drawn_tab != max_old)) {
493 ensure_tab_visible(current);
494 }
495 } break;
496
497 case NOTIFICATION_DRAG_END: {
498 if (dragging_valid_tab) {
499 dragging_valid_tab = false;
500 queue_redraw();
501 }
502 [[fallthrough]];
503 }
504
505 case NOTIFICATION_MOUSE_EXIT: {
506 if (!hover_switch_delay->is_stopped()) {
507 hover_switch_delay->stop();
508 }
509 } break;
510
511 case NOTIFICATION_DRAW: {
512 bool rtl = is_layout_rtl();
513 Vector2 size = get_size();
514
515 if (tabs.is_empty()) {
516 // Draw the drop indicator where the first tab would be if there are no tabs.
517 if (dragging_valid_tab) {
518 int x = rtl ? size.x : 0;
519 theme_cache.drop_mark_icon->draw(get_canvas_item(), Point2(x - (theme_cache.drop_mark_icon->get_width() / 2), (size.height - theme_cache.drop_mark_icon->get_height()) / 2), theme_cache.drop_mark_color);
520 }
521
522 return;
523 }
524
525 int limit_minus_buttons = size.width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
526
527 // Draw unselected tabs in the back.
528 for (int i = offset; i <= max_drawn_tab; i++) {
529 if (tabs[i].hidden) {
530 continue;
531 }
532
533 if (i != current) {
534 Ref<StyleBox> sb;
535 Color fnt_col;
536 Color icn_col;
537
538 if (tabs[i].disabled) {
539 sb = theme_cache.tab_disabled_style;
540 fnt_col = _select_color(tabs[i].font_color_overrides[DrawMode::DRAW_DISABLED], theme_cache.font_disabled_color);
541 icn_col = theme_cache.icon_disabled_color;
542 } else if (i == hover) {
543 sb = theme_cache.tab_hovered_style;
544 fnt_col = _select_color(tabs[i].font_color_overrides[DrawMode::DRAW_HOVER], theme_cache.font_hovered_color);
545 icn_col = theme_cache.icon_hovered_color;
546 } else {
547 sb = theme_cache.tab_unselected_style;
548 fnt_col = _select_color(tabs[i].font_color_overrides[DrawMode::DRAW_NORMAL], theme_cache.font_unselected_color);
549 icn_col = theme_cache.icon_unselected_color;
550 }
551
552 _draw_tab(sb, fnt_col, icn_col, i, rtl ? (size.width - tabs[i].ofs_cache - tabs[i].size_cache) : tabs[i].ofs_cache, false);
553 }
554 }
555
556 // Draw selected tab in the front, but only if it's visible.
557 if (current >= offset && current <= max_drawn_tab && !tabs[current].hidden) {
558 Ref<StyleBox> sb = tabs[current].disabled ? theme_cache.tab_disabled_style : theme_cache.tab_selected_style;
559 Color col = _select_color(tabs[current].font_color_overrides[DrawMode::DRAW_PRESSED], theme_cache.font_selected_color);
560
561 _draw_tab(sb, col, theme_cache.icon_selected_color, current, rtl ? (size.width - tabs[current].ofs_cache - tabs[current].size_cache) : tabs[current].ofs_cache, has_focus(true));
562 }
563
564 if (buttons_visible) {
565 int vofs = (size.height - theme_cache.increment_icon->get_size().height) / 2;
566
567 if (rtl) {
568 if (missing_right) {
569 draw_texture(highlight_arrow == 1 ? theme_cache.decrement_hl_icon : theme_cache.decrement_icon, Point2(0, vofs));
570 } else {
571 draw_texture(theme_cache.decrement_icon, Point2(0, vofs), Color(1, 1, 1, 0.5));
572 }
573
574 if (offset > 0) {
575 draw_texture(highlight_arrow == 0 ? theme_cache.increment_hl_icon : theme_cache.increment_icon, Point2(theme_cache.increment_icon->get_size().width, vofs));
576 } else {
577 draw_texture(theme_cache.increment_icon, Point2(theme_cache.increment_icon->get_size().width, vofs), Color(1, 1, 1, 0.5));
578 }
579 } else {
580 if (offset > 0) {
581 draw_texture(highlight_arrow == 0 ? theme_cache.decrement_hl_icon : theme_cache.decrement_icon, Point2(limit_minus_buttons, vofs));
582 } else {
583 draw_texture(theme_cache.decrement_icon, Point2(limit_minus_buttons, vofs), Color(1, 1, 1, 0.5));
584 }
585
586 if (missing_right) {
587 draw_texture(highlight_arrow == 1 ? theme_cache.increment_hl_icon : theme_cache.increment_icon, Point2(limit_minus_buttons + theme_cache.decrement_icon->get_size().width, vofs));
588 } else {
589 draw_texture(theme_cache.increment_icon, Point2(limit_minus_buttons + theme_cache.decrement_icon->get_size().width, vofs), Color(1, 1, 1, 0.5));
590 }
591 }
592 }
593
594 if (dragging_valid_tab) {
595 _draw_tab_drop(get_canvas_item());
596 }
597 } break;
598 }
599 }
600
601 void TabBar::_draw_tab_drop(RID p_canvas_item) {
602 Vector2 size = get_size();
603 int x;
604 bool rtl = is_layout_rtl();
605
606 int closest_tab = get_closest_tab_idx_to_point(get_local_mouse_position());
607 if (closest_tab != -1) {
608 Rect2 tab_rect = get_tab_rect(closest_tab);
609 x = tab_rect.position.x;
610
611 // Only add the tab_separation if closest tab is not on the edge.
612 bool not_leftmost_tab = -1 != (rtl ? get_next_available(closest_tab) : get_previous_available(closest_tab));
613 bool not_rightmost_tab = -1 != (rtl ? get_previous_available(closest_tab) : get_next_available(closest_tab));
614
615 // Calculate midpoint between tabs.
616 if (get_local_mouse_position().x > tab_rect.get_center().x) {
617 x += tab_rect.size.x;
618 if (not_rightmost_tab) {
619 x += Math::ceil(0.5f * theme_cache.tab_separation);
620 }
621 } else if (not_leftmost_tab) {
622 x -= Math::floor(0.5f * theme_cache.tab_separation);
623 }
624 } else {
625 if (rtl ^ (get_local_mouse_position().x < get_tab_rect(0).position.x)) {
626 x = get_tab_rect(0).position.x;
627 if (rtl) {
628 x += get_tab_rect(0).size.width;
629 }
630 } else {
631 Rect2 tab_rect = get_tab_rect(get_tab_count() - 1);
632
633 x = tab_rect.position.x;
634 if (!rtl) {
635 x += tab_rect.size.width;
636 }
637 }
638 }
639
640 theme_cache.drop_mark_icon->draw(p_canvas_item, Point2(x - theme_cache.drop_mark_icon->get_width() / 2, (size.height - theme_cache.drop_mark_icon->get_height()) / 2), theme_cache.drop_mark_color);
641 }
642
643 void TabBar::_draw_tab(Ref<StyleBox> &p_tab_style, const Color &p_font_color, const Color &p_icon_color, int p_index, float p_x, bool p_focus) {
644 RID ci = get_canvas_item();
645 bool rtl = is_layout_rtl();
646
647 Rect2 sb_rect = Rect2(p_x, 0, tabs[p_index].size_cache, get_size().height);
648 if (tab_style_v_flip) {
649 draw_set_transform(Point2(0.0, p_tab_style->get_draw_rect(sb_rect).size.y), 0.0, Size2(1.0, -1.0));
650 }
651 p_tab_style->draw(ci, sb_rect);
652 if (tab_style_v_flip) {
653 draw_set_transform(Point2(), 0.0, Size2(1.0, 1.0));
654 }
655 if (p_focus) {
656 Ref<StyleBox> focus_style = theme_cache.tab_focus_style;
657 focus_style->draw(ci, sb_rect);
658 }
659
660 p_x += rtl ? tabs[p_index].size_cache - p_tab_style->get_margin(SIDE_LEFT) : p_tab_style->get_margin(SIDE_LEFT);
661
662 Size2i sb_ms = p_tab_style->get_minimum_size();
663
664 // Draw the icon.
665 Ref<Texture2D> icon = tabs[p_index].icon;
666 if (icon.is_valid()) {
667 const Size2 icon_size = _get_tab_icon_size(p_index);
668 const Point2 icon_pos = Point2i(rtl ? p_x - icon_size.width : p_x, p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - icon_size.height) / 2);
669 icon->draw_rect(ci, Rect2(icon_pos, icon_size), false, p_icon_color);
670
671 p_x = rtl ? p_x - icon_size.width - theme_cache.h_separation : p_x + icon_size.width + theme_cache.h_separation;
672 }
673
674 // Draw the text.
675 if (!tabs[p_index].text.is_empty()) {
676 Point2i text_pos = Point2i(rtl ? p_x - tabs[p_index].size_text : p_x,
677 p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - tabs[p_index].text_buf->get_size().y) / 2);
678
679 if (theme_cache.outline_size > 0 && theme_cache.font_outline_color.a > 0) {
680 tabs[p_index].text_buf->draw_outline(ci, text_pos, theme_cache.outline_size, theme_cache.font_outline_color);
681 }
682 tabs[p_index].text_buf->draw(ci, text_pos, p_font_color);
683
684 p_x = rtl ? p_x - tabs[p_index].size_text - theme_cache.h_separation : p_x + tabs[p_index].size_text + theme_cache.h_separation;
685 }
686
687 // Draw and calculate rect of the right button.
688 if (tabs[p_index].right_button.is_valid()) {
689 Ref<StyleBox> style = theme_cache.button_hl_style;
690 Ref<Texture2D> rb = tabs[p_index].right_button;
691
692 Rect2 rb_rect;
693 rb_rect.size = style->get_minimum_size() + rb->get_size();
694 rb_rect.position.x = rtl ? p_x - rb_rect.size.width : p_x;
695 rb_rect.position.y = p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (rb_rect.size.y)) / 2;
696
697 tabs.write[p_index].rb_rect = rb_rect;
698
699 if (rb_hover == p_index) {
700 if (rb_pressing) {
701 theme_cache.button_pressed_style->draw(ci, rb_rect);
702 } else {
703 style->draw(ci, rb_rect);
704 }
705 }
706
707 rb->draw(ci, Point2i(rb_rect.position.x + style->get_margin(SIDE_LEFT), rb_rect.position.y + style->get_margin(SIDE_TOP)));
708
709 p_x = rtl ? rb_rect.position.x : rb_rect.position.x + rb_rect.size.width;
710 } else {
711 tabs.write[p_index].rb_rect = Rect2();
712 }
713
714 // Draw and calculate rect of the close button.
715 if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_index == current)) {
716 Ref<StyleBox> style = theme_cache.button_hl_style;
717 Ref<Texture2D> cb = theme_cache.close_icon;
718
719 Rect2 cb_rect;
720 cb_rect.size = style->get_minimum_size() + cb->get_size();
721 cb_rect.position.x = rtl ? p_x - cb_rect.size.width : p_x;
722 cb_rect.position.y = p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (cb_rect.size.y)) / 2;
723
724 tabs.write[p_index].cb_rect = cb_rect;
725
726 if (!tabs[p_index].disabled && cb_hover == p_index) {
727 if (cb_pressing) {
728 theme_cache.button_pressed_style->draw(ci, cb_rect);
729 } else {
730 style->draw(ci, cb_rect);
731 }
732 }
733
734 cb->draw(ci, Point2i(cb_rect.position.x + style->get_margin(SIDE_LEFT), cb_rect.position.y + style->get_margin(SIDE_TOP)));
735 } else {
736 tabs.write[p_index].cb_rect = Rect2();
737 }
738 }
739
740 void TabBar::set_tab_count(int p_count) {
741 if (p_count == tabs.size()) {
742 return;
743 }
744
745 ERR_FAIL_COND(p_count < 0);
746
747 if (tabs.size() > p_count) {
748 for (int i = p_count; i < tabs.size(); i++) {
749 if (tabs[i].accessibility_item_element.is_valid()) {
750 DisplayServer::get_singleton()->accessibility_free_element(tabs.write[i].accessibility_item_element);
751 tabs.write[i].accessibility_item_element = RID();
752 }
753 }
754 }
755 tabs.resize(p_count);
756
757 if (p_count == 0) {
758 offset = 0;
759 max_drawn_tab = 0;
760 current = -1;
761 previous = -1;
762 } else {
763 offset = MIN(offset, p_count - 1);
764 max_drawn_tab = MIN(max_drawn_tab, p_count - 1);
765 current = MIN(current, p_count - 1);
766 // Fix range if unable to deselect.
767 if (current == -1 && !_can_deselect()) {
768 current = 0;
769 }
770
771 _update_cache();
772 _ensure_no_over_offset();
773 if (scroll_to_selected) {
774 ensure_tab_visible(current);
775 }
776 }
777
778 if (!initialized) {
779 initialized = true;
780 if (queued_current != CURRENT_TAB_UNINITIALIZED && queued_current != current) {
781 set_current_tab(queued_current);
782 }
783 }
784
785 queue_accessibility_update();
786 queue_redraw();
787 update_minimum_size();
788 notify_property_list_changed();
789 }
790
791 int TabBar::get_tab_count() const {
792 return tabs.size();
793 }
794
795 void TabBar::set_current_tab(int p_current) {
796 if (p_current == -1) {
797 // An index of -1 is only valid if deselecting is enabled or there are no valid tabs.
798 ERR_FAIL_COND_MSG(!_can_deselect(), "Cannot deselect tabs, deselection is not enabled.");
799 } else {
800 if (!initialized && p_current >= get_tab_count()) {
801 queued_current = p_current;
802 return;
803 }
804 ERR_FAIL_INDEX(p_current, get_tab_count());
805 }
806
807 previous = current;
808 current = p_current;
809
810 if (current == previous) {
811 emit_signal(SNAME("tab_selected"), current);
812 return;
813 }
814
815 emit_signal(SNAME("tab_selected"), current);
816
817 _update_cache();
818 if (scroll_to_selected) {
819 ensure_tab_visible(current);
820 }
821 queue_accessibility_update();
822 queue_redraw();
823
824 emit_signal(SNAME("tab_changed"), p_current);
825 }
826
827 int TabBar::get_current_tab() const {
828 return current;
829 }
830
831 int TabBar::get_previous_tab() const {
832 return previous;
833 }
834
835 int TabBar::get_hovered_tab() const {
836 return hover;
837 }
838
839 int TabBar::get_previous_available(int p_idx) const {
840 ERR_FAIL_COND_V(p_idx < -1 || p_idx > get_tab_count(), -1);
841 const int idx = p_idx == -1 ? get_current_tab() : p_idx;
842 const int offset_end = idx + 1;
843 for (int i = 1; i < offset_end; i++) {
844 int target_tab = idx - i;
845 if (target_tab < 0) {
846 target_tab += get_tab_count();
847 }
848 if (!is_tab_disabled(target_tab) && !is_tab_hidden(target_tab)) {
849 return target_tab;
850 }
851 }
852 return -1;
853 }
854
855 int TabBar::get_next_available(int p_idx) const {
856 ERR_FAIL_COND_V(p_idx < -1 || p_idx > get_tab_count(), -1);
857 const int idx = p_idx == -1 ? get_current_tab() : p_idx;
858 const int offset_end = get_tab_count() - idx;
859 for (int i = 1; i < offset_end; i++) {
860 int target_tab = (idx + i) % get_tab_count();
861 if (!is_tab_disabled(target_tab) && !is_tab_hidden(target_tab)) {
862 return target_tab;
863 }
864 }
865 return -1;
866 }
867
868 bool TabBar::select_previous_available() {
869 const int previous_available = get_previous_available();
870 if (previous_available != -1) {
871 set_current_tab(previous_available);
872 }
873 return previous_available != -1;
874 }
875
876 bool TabBar::select_next_available() {
877 const int next_available = get_next_available();
878 if (next_available != -1) {
879 set_current_tab(next_available);
880 }
881 return next_available != -1;
882 }
883
884 void TabBar::set_tab_offset(int p_offset) {
885 ERR_FAIL_INDEX(p_offset, tabs.size());
886 offset = p_offset;
887 _update_cache();
888 queue_accessibility_update();
889 queue_redraw();
890 }
891
892 int TabBar::get_tab_offset() const {
893 return offset;
894 }
895
896 bool TabBar::get_offset_buttons_visible() const {
897 return buttons_visible;
898 }
899
900 void TabBar::set_tab_title(int p_tab, const String &p_title) {
901 ERR_FAIL_INDEX(p_tab, tabs.size());
902
903 if (tabs[p_tab].text == p_title) {
904 return;
905 }
906
907 tabs.write[p_tab].text = p_title;
908
909 _shape(p_tab);
910 _update_cache();
911 _ensure_no_over_offset();
912 if (scroll_to_selected) {
913 ensure_tab_visible(current);
914 }
915 queue_accessibility_update();
916 queue_redraw();
917 update_minimum_size();
918 }
919
920 String TabBar::get_tab_title(int p_tab) const {
921 ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
922 return tabs[p_tab].text;
923 }
924
925 void TabBar::set_tab_tooltip(int p_tab, const String &p_tooltip) {
926 ERR_FAIL_INDEX(p_tab, tabs.size());
927 tabs.write[p_tab].tooltip = p_tooltip;
928 queue_accessibility_update();
929 }
930
931 String TabBar::get_tab_tooltip(int p_tab) const {
932 ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
933 return tabs[p_tab].tooltip;
934 }
935
936 void TabBar::set_tab_text_direction(int p_tab, Control::TextDirection p_text_direction) {
937 ERR_FAIL_INDEX(p_tab, tabs.size());
938 ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
939
940 if (tabs[p_tab].text_direction != p_text_direction) {
941 tabs.write[p_tab].text_direction = p_text_direction;
942
943 _shape(p_tab);
944 queue_accessibility_update();
945 queue_redraw();
946 }
947 }
948
949 Control::TextDirection TabBar::get_tab_text_direction(int p_tab) const {
950 ERR_FAIL_INDEX_V(p_tab, tabs.size(), Control::TEXT_DIRECTION_INHERITED);
951 return tabs[p_tab].text_direction;
952 }
953
954 void TabBar::set_tab_language(int p_tab, const String &p_language) {
955 ERR_FAIL_INDEX(p_tab, tabs.size());
956
957 if (tabs[p_tab].language != p_language) {
958 tabs.write[p_tab].language = p_language;
959
960 _shape(p_tab);
961 _update_cache();
962 _ensure_no_over_offset();
963 if (scroll_to_selected) {
964 ensure_tab_visible(current);
965 }
966 queue_accessibility_update();
967 queue_redraw();
968 update_minimum_size();
969 }
970 }
971
972 String TabBar::get_tab_language(int p_tab) const {
973 ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
974 return tabs[p_tab].language;
975 }
976
977 void TabBar::set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon) {
978 ERR_FAIL_INDEX(p_tab, tabs.size());
979
980 if (tabs[p_tab].icon == p_icon) {
981 return;
982 }
983
984 tabs.write[p_tab].icon = p_icon;
985
986 _update_cache();
987 _ensure_no_over_offset();
988 if (scroll_to_selected) {
989 ensure_tab_visible(current);
990 }
991 queue_redraw();
992 update_minimum_size();
993 }
994
995 Ref<Texture2D> TabBar::get_tab_icon(int p_tab) const {
996 ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
997 return tabs[p_tab].icon;
998 }
999
1000 void TabBar::set_tab_icon_max_width(int p_tab, int p_width) {
1001 ERR_FAIL_INDEX(p_tab, tabs.size());
1002
1003 if (tabs[p_tab].icon_max_width == p_width) {
1004 return;
1005 }
1006
1007 tabs.write[p_tab].icon_max_width = p_width;
1008
1009 _update_cache();
1010 _ensure_no_over_offset();
1011 if (scroll_to_selected) {
1012 ensure_tab_visible(current);
1013 }
1014 queue_redraw();
1015 update_minimum_size();
1016 }
1017
1018 int TabBar::get_tab_icon_max_width(int p_tab) const {
1019 ERR_FAIL_INDEX_V(p_tab, tabs.size(), 0);
1020 return tabs[p_tab].icon_max_width;
1021 }
1022
1023 void TabBar::set_font_color_override_all(int p_tab, const Color &p_color) {
1024 ERR_FAIL_INDEX(p_tab, tabs.size());
1025
1026 Tab &tab = tabs.write[p_tab];
1027 for (int i = 0; i < DrawMode::DRAW_MAX; i++) {
1028 tab.font_color_overrides[i] = p_color;
1029 }
1030
1031 queue_redraw();
1032 }
1033
1034 void TabBar::set_font_color_override(int p_tab, DrawMode p_draw_mode, const Color &p_color) {
1035 ERR_FAIL_INDEX(p_tab, tabs.size());
1036 ERR_FAIL_INDEX(p_draw_mode, DrawMode::DRAW_MAX);
1037
1038 if (tabs[p_tab].font_color_overrides[p_draw_mode] == p_color) {
1039 return;
1040 }
1041
1042 tabs.write[p_tab].font_color_overrides[p_draw_mode] = p_color;
1043
1044 queue_redraw();
1045 }
1046
1047 Color TabBar::get_font_color_override(int p_tab, DrawMode p_draw_mode) const {
1048 ERR_FAIL_INDEX_V(p_tab, tabs.size(), Color());
1049 ERR_FAIL_INDEX_V(p_draw_mode, DrawMode::DRAW_MAX, Color());
1050
1051 return tabs[p_tab].font_color_overrides[p_draw_mode];
1052 }
1053
1054 void TabBar::set_tab_disabled(int p_tab, bool p_disabled) {
1055 ERR_FAIL_INDEX(p_tab, tabs.size());
1056
1057 if (tabs[p_tab].disabled == p_disabled) {
1058 return;
1059 }
1060
1061 tabs.write[p_tab].disabled = p_disabled;
1062
1063 _update_cache();
1064 _ensure_no_over_offset();
1065 if (scroll_to_selected) {
1066 ensure_tab_visible(current);
1067 }
1068 queue_accessibility_update();
1069 queue_redraw();
1070 update_minimum_size();
1071 }
1072
1073 bool TabBar::is_tab_disabled(int p_tab) const {
1074 ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
1075 return tabs[p_tab].disabled;
1076 }
1077
1078 void TabBar::set_tab_hidden(int p_tab, bool p_hidden) {
1079 ERR_FAIL_INDEX(p_tab, tabs.size());
1080
1081 if (tabs[p_tab].hidden == p_hidden) {
1082 return;
1083 }
1084
1085 tabs.write[p_tab].hidden = p_hidden;
1086
1087 _update_cache();
1088 _ensure_no_over_offset();
1089 if (scroll_to_selected) {
1090 ensure_tab_visible(current);
1091 }
1092 queue_accessibility_update();
1093 queue_redraw();
1094 update_minimum_size();
1095 }
1096
1097 bool TabBar::is_tab_hidden(int p_tab) const {
1098 ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
1099 return tabs[p_tab].hidden;
1100 }
1101
1102 void TabBar::set_tab_metadata(int p_tab, const Variant &p_metadata) {
1103 ERR_FAIL_INDEX(p_tab, tabs.size());
1104 tabs.write[p_tab].metadata = p_metadata;
1105 }
1106
1107 Variant TabBar::get_tab_metadata(int p_tab) const {
1108 ERR_FAIL_INDEX_V(p_tab, tabs.size(), Variant());
1109 return tabs[p_tab].metadata;
1110 }
1111
1112 void TabBar::set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon) {
1113 ERR_FAIL_INDEX(p_tab, tabs.size());
1114
1115 if (tabs[p_tab].right_button == p_icon) {
1116 return;
1117 }
1118
1119 tabs.write[p_tab].right_button = p_icon;
1120
1121 _update_cache();
1122 _ensure_no_over_offset();
1123 if (scroll_to_selected) {
1124 ensure_tab_visible(current);
1125 }
1126 queue_redraw();
1127 update_minimum_size();
1128 }
1129
1130 Ref<Texture2D> TabBar::get_tab_button_icon(int p_tab) const {
1131 ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
1132 return tabs[p_tab].right_button;
1133 }
1134
1135 void TabBar::_update_hover() {
1136 if (!is_inside_tree()) {
1137 return;
1138 }
1139
1140 ERR_FAIL_COND(tabs.is_empty());
1141
1142 const Point2 &pos = get_local_mouse_position();
1143 // Test hovering to display right or close button.
1144 int hover_now = -1;
1145 int hover_buttons = -1;
1146 for (int i = offset; i <= max_drawn_tab; i++) {
1147 if (tabs[i].hidden) {
1148 continue;
1149 }
1150
1151 Rect2 rect = get_tab_rect(i);
1152 if (rect.has_point(pos)) {
1153 hover_now = i;
1154 }
1155
1156 if (tabs[i].rb_rect.has_point(pos)) {
1157 rb_hover = i;
1158 cb_hover = -1;
1159 hover_buttons = i;
1160 } else if (!tabs[i].disabled && tabs[i].cb_rect.has_point(pos)) {
1161 cb_hover = i;
1162 rb_hover = -1;
1163 hover_buttons = i;
1164 }
1165
1166 if (hover_buttons != -1) {
1167 queue_redraw();
1168 break;
1169 }
1170 }
1171
1172 if (hover != hover_now) {
1173 hover = hover_now;
1174
1175 if (hover != -1) {
1176 emit_signal(SNAME("tab_hovered"), hover);
1177 }
1178
1179 _update_cache();
1180 queue_redraw();
1181 }
1182
1183 if (hover_buttons == -1) { // No hover.
1184 int rb_hover_old = rb_hover;
1185 int cb_hover_old = cb_hover;
1186
1187 rb_hover = hover_buttons;
1188 cb_hover = hover_buttons;
1189
1190 if (rb_hover != rb_hover_old || cb_hover != cb_hover_old) {
1191 queue_redraw();
1192 }
1193 }
1194 }
1195
1196 void TabBar::_update_cache(bool p_update_hover) {
1197 if (tabs.is_empty()) {
1198 buttons_visible = false;
1199 return;
1200 }
1201
1202 int limit = get_size().width;
1203 int limit_minus_buttons = limit - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
1204
1205 int w = 0;
1206
1207 max_drawn_tab = tabs.size() - 1;
1208
1209 for (int i = 0; i < tabs.size(); i++) {
1210 tabs.write[i].text_buf->set_width(-1);
1211 tabs.write[i].size_text = Math::ceil(tabs[i].text_buf->get_size().x);
1212 tabs.write[i].size_cache = get_tab_width(i);
1213 tabs.write[i].accessibility_item_dirty = true;
1214
1215 tabs.write[i].truncated = max_width > 0 && tabs[i].size_cache > max_width;
1216 if (tabs[i].truncated) {
1217 int size_textless = tabs[i].size_cache - tabs[i].size_text;
1218 int mw = MAX(size_textless, max_width);
1219
1220 tabs.write[i].size_text = MAX(mw - size_textless, 1);
1221 tabs.write[i].text_buf->set_width(tabs[i].size_text);
1222 tabs.write[i].size_cache = size_textless + tabs[i].size_text;
1223 }
1224
1225 if (i < offset || i > max_drawn_tab) {
1226 tabs.write[i].ofs_cache = 0;
1227 continue;
1228 }
1229
1230 tabs.write[i].ofs_cache = w;
1231
1232 if (tabs[i].hidden) {
1233 continue;
1234 }
1235
1236 w += tabs[i].size_cache;
1237
1238 // Check if all tabs would fit inside the area.
1239 if (clip_tabs && i > offset && (w > limit || (offset > 0 && w > limit_minus_buttons))) {
1240 tabs.write[i].ofs_cache = 0;
1241
1242 w -= tabs[i].size_cache;
1243 w -= theme_cache.tab_separation;
1244
1245 max_drawn_tab = i - 1;
1246
1247 while (w > limit_minus_buttons && max_drawn_tab > offset) {
1248 tabs.write[max_drawn_tab].ofs_cache = 0;
1249
1250 if (!tabs[max_drawn_tab].hidden) {
1251 w -= tabs[max_drawn_tab].size_cache;
1252 w -= theme_cache.tab_separation;
1253 }
1254
1255 max_drawn_tab--;
1256 }
1257 } else if (i < tabs.size() - 1) {
1258 // Only add the tab separation if this isn't the last tab drawn.
1259 w += theme_cache.tab_separation;
1260 }
1261 }
1262
1263 missing_right = max_drawn_tab < tabs.size() - 1;
1264 buttons_visible = offset > 0 || missing_right;
1265
1266 if (tab_alignment == ALIGNMENT_LEFT) {
1267 if (p_update_hover) {
1268 _update_hover();
1269 }
1270 return;
1271 }
1272
1273 if (tab_alignment == ALIGNMENT_CENTER) {
1274 w = ((buttons_visible ? limit_minus_buttons : limit) - w) / 2;
1275 } else if (tab_alignment == ALIGNMENT_RIGHT) {
1276 w = (buttons_visible ? limit_minus_buttons : limit) - w;
1277 }
1278
1279 for (int i = offset; i <= max_drawn_tab; i++) {
1280 if (!tabs[i].hidden) {
1281 tabs.write[i].ofs_cache = w;
1282
1283 w += tabs[i].size_cache;
1284 w += theme_cache.tab_separation;
1285 }
1286 }
1287
1288 if (p_update_hover) {
1289 _update_hover();
1290 }
1291 }
1292
1293 void TabBar::_hover_switch_timeout() {
1294 set_current_tab(hover);
1295 }
1296
1297 void TabBar::_on_mouse_exited() {
1298 rb_hover = -1;
1299 cb_hover = -1;
1300 hover = -1;
1301 highlight_arrow = -1;
1302 dragging_valid_tab = false;
1303
1304 _update_cache(false);
1305 queue_redraw();
1306 }
1307
1308 void TabBar::add_tab(const String &p_str, const Ref<Texture2D> &p_icon) {
1309 Tab t;
1310 t.text = p_str;
1311 t.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
1312 t.icon = p_icon;
1313 tabs.push_back(t);
1314
1315 _shape(tabs.size() - 1);
1316 _update_cache();
1317 if (scroll_to_selected) {
1318 ensure_tab_visible(current);
1319 }
1320 queue_accessibility_update();
1321 queue_redraw();
1322 update_minimum_size();
1323
1324 if (!deselect_enabled && tabs.size() == 1) {
1325 if (is_inside_tree()) {
1326 set_current_tab(0);
1327 } else {
1328 current = 0;
1329 previous = -1;
1330 }
1331 }
1332 }
1333
1334 void TabBar::clear_tabs() {
1335 if (tabs.is_empty()) {
1336 return;
1337 }
1338
1339 for (int i = 0; i < tabs.size(); i++) {
1340 if (tabs[i].accessibility_item_element.is_valid()) {
1341 DisplayServer::get_singleton()->accessibility_free_element(tabs.write[i].accessibility_item_element);
1342 tabs.write[i].accessibility_item_element = RID();
1343 }
1344 }
1345 tabs.clear();
1346 offset = 0;
1347 max_drawn_tab = 0;
1348 current = -1;
1349 previous = -1;
1350
1351 queue_accessibility_update();
1352 queue_redraw();
1353 update_minimum_size();
1354 notify_property_list_changed();
1355 }
1356
1357 void TabBar::remove_tab(int p_idx) {
1358 ERR_FAIL_INDEX(p_idx, tabs.size());
1359
1360 if (tabs[p_idx].accessibility_item_element.is_valid()) {
1361 DisplayServer::get_singleton()->accessibility_free_element(tabs.write[p_idx].accessibility_item_element);
1362 tabs.write[p_idx].accessibility_item_element = RID();
1363 }
1364 tabs.remove_at(p_idx);
1365
1366 bool is_tab_changing = current == p_idx;
1367
1368 if (current >= p_idx && current > 0) {
1369 current--;
1370 }
1371 if (previous >= p_idx && previous > 0) {
1372 previous--;
1373 }
1374
1375 if (tabs.is_empty()) {
1376 offset = 0;
1377 max_drawn_tab = 0;
1378 current = -1;
1379 previous = -1;
1380 } else {
1381 if (current != -1) {
1382 // Try to change to a valid tab if possible (without firing the `tab_selected` signal).
1383 for (int i = current; i < tabs.size(); i++) {
1384 if (!is_tab_disabled(i) && !is_tab_hidden(i)) {
1385 current = i;
1386 break;
1387 }
1388 }
1389 // If nothing, try backwards.
1390 if (is_tab_disabled(current) || is_tab_hidden(current)) {
1391 for (int i = current - 1; i >= 0; i--) {
1392 if (!is_tab_disabled(i) && !is_tab_hidden(i)) {
1393 current = i;
1394 break;
1395 }
1396 }
1397 }
1398 // If still no valid tab, deselect.
1399 if (is_tab_disabled(current) || is_tab_hidden(current)) {
1400 current = -1;
1401 }
1402 }
1403 offset = MIN(offset, tabs.size() - 1);
1404 max_drawn_tab = MIN(max_drawn_tab, tabs.size() - 1);
1405
1406 _update_cache();
1407 _ensure_no_over_offset();
1408 if (scroll_to_selected) {
1409 ensure_tab_visible(current);
1410 }
1411 }
1412
1413 queue_accessibility_update();
1414 queue_redraw();
1415 update_minimum_size();
1416 notify_property_list_changed();
1417
1418 if (is_tab_changing && is_inside_tree()) {
1419 emit_signal(SNAME("tab_changed"), current);
1420 }
1421 }
1422
1423 Variant TabBar::get_drag_data(const Point2 &p_point) {
1424 Variant drag_data = Control::get_drag_data(p_point);
1425 if (drag_data != Variant()) {
1426 return drag_data;
1427 }
1428
1429 if (drag_to_rearrange_enabled) {
1430 return _handle_get_drag_data("tab_bar_tab", p_point);
1431 }
1432 return Variant();
1433 }
1434
1435 bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
1436 if (switch_on_drag_hover) {
1437 _handle_switch_on_hover(p_data);
1438 }
1439
1440 bool drop_override = Control::can_drop_data(p_point, p_data);
1441 if (drop_override) {
1442 return drop_override;
1443 }
1444
1445 if (drag_to_rearrange_enabled) {
1446 return _handle_can_drop_data("tab_bar_tab", p_point, p_data);
1447 }
1448 return false;
1449 }
1450
1451 void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) {
1452 Control::drop_data(p_point, p_data);
1453
1454 if (drag_to_rearrange_enabled) {
1455 _handle_drop_data("tab_bar_tab", p_point, p_data, callable_mp(this, &TabBar::move_tab), callable_mp(this, &TabBar::_move_tab_from));
1456 }
1457 }
1458
1459 Variant TabBar::_handle_get_drag_data(const String &p_type, const Point2 &p_point) {
1460 int tab_over = (p_point == Vector2(Math::INF, Math::INF)) ? current : get_tab_idx_at_point(p_point);
1461 if (tab_over < 0) {
1462 return Variant();
1463 }
1464
1465 HBoxContainer *drag_preview = memnew(HBoxContainer);
1466
1467 if (tabs[tab_over].icon.is_valid()) {
1468 const Size2 icon_size = _get_tab_icon_size(tab_over);
1469
1470 TextureRect *tf = memnew(TextureRect);
1471 tf->set_texture(tabs[tab_over].icon);
1472 tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
1473 tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
1474 tf->set_custom_minimum_size(icon_size);
1475
1476 drag_preview->add_child(tf);
1477 }
1478
1479 Label *label = memnew(Label(get_tab_title(tab_over)));
1480 label->set_auto_translate_mode(get_auto_translate_mode()); // Reflect how the title is displayed.
1481 drag_preview->add_child(label);
1482
1483 set_drag_preview(drag_preview);
1484
1485 Dictionary drag_data;
1486 drag_data["type"] = "tab";
1487 drag_data["tab_type"] = p_type;
1488 drag_data["tab_index"] = tab_over;
1489 drag_data["from_path"] = get_path();
1490
1491 return drag_data;
1492 }
1493
1494 bool TabBar::_handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const {
1495 Dictionary d = p_data;
1496 if (d.get("type", "").operator String() != "tab") {
1497 return false;
1498 }
1499
1500 const String tab_type = d.get("tab_type", "");
1501 if (tab_type == p_type) {
1502 NodePath from_path = d["from_path"];
1503 NodePath to_path = get_path();
1504 if (from_path == to_path) {
1505 return true;
1506 } else if (get_tabs_rearrange_group() != -1) {
1507 // Drag and drop between other TabBars.
1508 Node *from_node = get_node(from_path);
1509 TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
1510 if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
1511 return true;
1512 }
1513 }
1514 }
1515 return false;
1516 }
1517
1518 void TabBar::_handle_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data, const Callable &p_move_tab_callback, const Callable &p_move_tab_from_other_callback) {
1519 Dictionary d = p_data;
1520 if (d.get("type", "").operator String() != "tab") {
1521 return;
1522 }
1523
1524 const String tab_type = d.get("tab_type", "");
1525 if (tab_type == p_type) {
1526 int tab_from_id = d["tab_index"];
1527 int hover_now = (p_point == Vector2(Math::INF, Math::INF)) ? current : get_closest_tab_idx_to_point(p_point);
1528 NodePath from_path = d["from_path"];
1529 NodePath to_path = get_path();
1530
1531 if (from_path == to_path) {
1532 if (tab_from_id == hover_now) {
1533 return;
1534 }
1535
1536 // Drop the new tab to the left or right depending on where the target tab is being hovered.
1537 if (hover_now != -1) {
1538 Rect2 tab_rect = get_tab_rect(hover_now);
1539 if (is_layout_rtl() ^ (p_point.x <= tab_rect.position.x + tab_rect.size.width / 2)) {
1540 if (hover_now > tab_from_id) {
1541 hover_now -= 1;
1542 }
1543 } else if (tab_from_id > hover_now) {
1544 hover_now += 1;
1545 }
1546 } else {
1547 int x = tabs.is_empty() ? 0 : get_tab_rect(0).position.x;
1548 hover_now = is_layout_rtl() ^ (p_point.x < x) ? 0 : get_tab_count() - 1;
1549 }
1550
1551 p_move_tab_callback.call(tab_from_id, hover_now);
1552 if (!is_tab_disabled(hover_now)) {
1553 emit_signal(SNAME("active_tab_rearranged"), hover_now);
1554 set_current_tab(hover_now);
1555 }
1556 } else if (get_tabs_rearrange_group() != -1) {
1557 // Drag and drop between Tabs.
1558
1559 Node *from_node = get_node(from_path);
1560 TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
1561
1562 if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
1563 if (tab_from_id >= from_tabs->get_tab_count()) {
1564 return;
1565 }
1566
1567 // Drop the new tab to the left or right depending on where the target tab is being hovered.
1568 if (hover_now != -1) {
1569 Rect2 tab_rect = get_tab_rect(hover_now);
1570 if (is_layout_rtl() ^ (p_point.x > tab_rect.position.x + tab_rect.size.width / 2)) {
1571 hover_now += 1;
1572 }
1573 } else {
1574 hover_now = tabs.is_empty() || (is_layout_rtl() ^ (p_point.x < get_tab_rect(0).position.x)) ? 0 : get_tab_count();
1575 }
1576
1577 p_move_tab_from_other_callback.call(from_tabs, tab_from_id, hover_now);
1578 }
1579 }
1580 }
1581 }
1582
1583 void TabBar::_handle_switch_on_hover(const Variant &p_data) const {
1584 Dictionary d = p_data;
1585 if (d.get("type", "").operator String() == "tab") {
1586 // Dragging a tab shouldn't switch on hover.
1587 return;
1588 }
1589
1590 if (hover > -1 && hover != current) {
1591 if (hover_switch_delay->is_stopped()) {
1592 const_cast<TabBar *>(this)->hover_switch_delay->start(theme_cache.hover_switch_wait_msec * 0.001);
1593 }
1594 } else if (!hover_switch_delay->is_stopped()) {
1595 hover_switch_delay->stop();
1596 }
1597 }
1598
1599 void TabBar::_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index) {
1600 Tab moving_tab = p_from_tabbar->tabs[p_from_index];
1601 moving_tab.accessibility_item_element = RID();
1602 moving_tab.accessibility_item_dirty = true;
1603 p_from_tabbar->remove_tab(p_from_index);
1604 tabs.insert(p_to_index, moving_tab);
1605
1606 if (tabs.size() > 1) {
1607 if (current >= p_to_index) {
1608 current++;
1609 }
1610 if (previous >= p_to_index) {
1611 previous++;
1612 }
1613 }
1614
1615 if (!is_tab_disabled(p_to_index)) {
1616 set_current_tab(p_to_index);
1617 } else {
1618 _update_cache();
1619 queue_redraw();
1620 }
1621
1622 queue_accessibility_update();
1623 update_minimum_size();
1624 }
1625
1626 int TabBar::get_tab_idx_at_point(const Point2 &p_point) const {
1627 if (tabs.is_empty()) {
1628 return -1;
1629 }
1630
1631 int hover_now = -1;
1632
1633 for (int i = offset; i <= max_drawn_tab; i++) {
1634 if (!tabs[i].hidden) {
1635 Rect2 rect = get_tab_rect(i);
1636 if (rect.has_point(p_point)) {
1637 hover_now = i;
1638 }
1639 }
1640 }
1641
1642 return hover_now;
1643 }
1644
1645 int TabBar::get_closest_tab_idx_to_point(const Point2 &p_point) const {
1646 if (tabs.is_empty()) {
1647 return -1;
1648 }
1649
1650 int closest_tab = get_tab_idx_at_point(p_point);
1651 float closest_distance = FLT_MAX;
1652
1653 // Search along the x-axis since the TabBar is horizontal.
1654 if (closest_tab == -1) {
1655 for (int i = offset; i <= max_drawn_tab; i++) {
1656 if (!tabs[i].hidden) {
1657 float center = get_tab_rect(i).get_center().x;
1658 float distance = Math::abs(center - p_point.x);
1659 if (distance < closest_distance) {
1660 closest_distance = distance;
1661 closest_tab = i;
1662 }
1663 }
1664 }
1665 }
1666
1667 return closest_tab;
1668 }
1669
1670 void TabBar::set_tab_alignment(AlignmentMode p_alignment) {
1671 ERR_FAIL_INDEX(p_alignment, ALIGNMENT_MAX);
1672
1673 if (tab_alignment == p_alignment) {
1674 return;
1675 }
1676
1677 tab_alignment = p_alignment;
1678
1679 _update_cache();
1680 queue_redraw();
1681 }
1682
1683 TabBar::AlignmentMode TabBar::get_tab_alignment() const {
1684 return tab_alignment;
1685 }
1686
1687 void TabBar::set_clip_tabs(bool p_clip_tabs) {
1688 if (clip_tabs == p_clip_tabs) {
1689 return;
1690 }
1691 clip_tabs = p_clip_tabs;
1692
1693 if (!clip_tabs) {
1694 offset = 0;
1695 max_drawn_tab = 0;
1696 }
1697
1698 _update_cache();
1699 if (scroll_to_selected) {
1700 ensure_tab_visible(current);
1701 }
1702 queue_redraw();
1703 update_minimum_size();
1704 }
1705
1706 bool TabBar::get_clip_tabs() const {
1707 return clip_tabs;
1708 }
1709
1710 void TabBar::set_tab_style_v_flip(bool p_tab_style_v_flip) {
1711 tab_style_v_flip = p_tab_style_v_flip;
1712 }
1713
1714 void TabBar::move_tab(int p_from, int p_to) {
1715 if (p_from == p_to) {
1716 return;
1717 }
1718
1719 ERR_FAIL_INDEX(p_from, tabs.size());
1720 ERR_FAIL_INDEX(p_to, tabs.size());
1721
1722 Tab tab_from = tabs[p_from];
1723 tab_from.accessibility_item_dirty = true;
1724
1725 tabs.remove_at(p_from);
1726 tabs.insert(p_to, tab_from);
1727
1728 if (current == p_from) {
1729 current = p_to;
1730 } else if (current > p_from && current <= p_to) {
1731 current--;
1732 } else if (current < p_from && current >= p_to) {
1733 current++;
1734 }
1735
1736 if (previous == p_from) {
1737 previous = p_to;
1738 } else if (previous > p_from && previous <= p_to) {
1739 previous--;
1740 } else if (previous < p_from && previous >= p_to) {
1741 previous++;
1742 }
1743
1744 _update_cache();
1745 _ensure_no_over_offset();
1746 if (scroll_to_selected) {
1747 ensure_tab_visible(current);
1748 }
1749 queue_accessibility_update();
1750 queue_redraw();
1751 notify_property_list_changed();
1752 }
1753
1754 int TabBar::get_tab_width(int p_idx) const {
1755 ERR_FAIL_INDEX_V(p_idx, tabs.size(), 0);
1756
1757 Ref<StyleBox> style;
1758
1759 if (tabs[p_idx].disabled) {
1760 style = theme_cache.tab_disabled_style;
1761 } else if (current == p_idx) {
1762 style = theme_cache.tab_selected_style;
1763 // Always pick the widest style between hovered and unselected, to avoid an infinite loop when switching tabs with the mouse.
1764 } else if (theme_cache.tab_hovered_style->get_minimum_size().width > theme_cache.tab_unselected_style->get_minimum_size().width) {
1765 style = theme_cache.tab_hovered_style;
1766 } else {
1767 style = theme_cache.tab_unselected_style;
1768 }
1769 int x = style->get_minimum_size().width;
1770
1771 if (tabs[p_idx].icon.is_valid()) {
1772 const Size2 icon_size = _get_tab_icon_size(p_idx);
1773 x += icon_size.width + theme_cache.h_separation;
1774 }
1775
1776 if (!tabs[p_idx].text.is_empty()) {
1777 x += tabs[p_idx].size_text + theme_cache.h_separation;
1778 }
1779
1780 bool close_visible = cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx == current);
1781
1782 if (tabs[p_idx].right_button.is_valid()) {
1783 Ref<StyleBox> btn_style = theme_cache.button_hl_style;
1784 Ref<Texture2D> rb = tabs[p_idx].right_button;
1785
1786 if (close_visible) {
1787 x += btn_style->get_minimum_size().width + rb->get_width();
1788 } else {
1789 x += btn_style->get_margin(SIDE_LEFT) + rb->get_width() + theme_cache.h_separation;
1790 }
1791 }
1792
1793 if (close_visible) {
1794 Ref<StyleBox> btn_style = theme_cache.button_hl_style;
1795 Ref<Texture2D> cb = theme_cache.close_icon;
1796 x += btn_style->get_margin(SIDE_LEFT) + cb->get_width() + theme_cache.h_separation;
1797 }
1798
1799 if (x > style->get_minimum_size().width) {
1800 x -= theme_cache.h_separation;
1801 }
1802
1803 return x;
1804 }
1805
1806 Size2 TabBar::_get_tab_icon_size(int p_index) const {
1807 ERR_FAIL_INDEX_V(p_index, tabs.size(), Size2());
1808 const TabBar::Tab &tab = tabs[p_index];
1809 Size2 icon_size = tab.icon->get_size();
1810
1811 int icon_max_width = 0;
1812 if (theme_cache.icon_max_width > 0) {
1813 icon_max_width = theme_cache.icon_max_width;
1814 }
1815 if (tab.icon_max_width > 0 && (icon_max_width == 0 || tab.icon_max_width < icon_max_width)) {
1816 icon_max_width = tab.icon_max_width;
1817 }
1818
1819 if (icon_max_width > 0 && icon_size.width > icon_max_width) {
1820 icon_size.height = icon_size.height * icon_max_width / icon_size.width;
1821 icon_size.width = icon_max_width;
1822 }
1823
1824 return icon_size;
1825 }
1826
1827 void TabBar::_ensure_no_over_offset() {
1828 if (!is_inside_tree() || !buttons_visible) {
1829 return;
1830 }
1831
1832 int limit_with_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
1833 int limit_with_no_button = get_size().width;
1834 int offset_with_buttons = offset;
1835 int offset_with_no_button = offset;
1836
1837 int total_w = tabs[max_drawn_tab].ofs_cache + tabs[max_drawn_tab].size_cache - tabs[offset].ofs_cache;
1838 for (int i = offset - 1; i >= 0; i--) {
1839 if (!tabs[i].hidden) {
1840 total_w += tabs[i].size_cache;
1841 }
1842
1843 if (total_w < limit_with_buttons) {
1844 offset_with_buttons--;
1845 offset_with_no_button--;
1846 } else if (total_w < limit_with_no_button) {
1847 offset_with_no_button--;
1848 } else {
1849 break;
1850 }
1851 }
1852
1853 int new_offset = (offset_with_no_button == 0) ? 0 : offset_with_buttons;
1854
1855 if (new_offset != offset) {
1856 offset = new_offset;
1857 _update_cache();
1858 queue_redraw();
1859 }
1860 }
1861
1862 bool TabBar::_can_deselect() const {
1863 if (deselect_enabled) {
1864 return true;
1865 }
1866 // All tabs must be disabled or hidden.
1867 for (const Tab &tab : tabs) {
1868 if (!tab.disabled && !tab.hidden) {
1869 return false;
1870 }
1871 }
1872 return true;
1873 }
1874
1875 void TabBar::ensure_tab_visible(int p_idx) {
1876 if (p_idx == -1 || !is_inside_tree() || !buttons_visible) {
1877 return;
1878 }
1879 ERR_FAIL_INDEX(p_idx, tabs.size());
1880
1881 if (tabs[p_idx].hidden || (p_idx >= offset && p_idx <= max_drawn_tab)) {
1882 return;
1883 }
1884
1885 if (p_idx < offset) {
1886 offset = p_idx;
1887 _update_cache();
1888 queue_redraw();
1889
1890 return;
1891 }
1892
1893 int limit_minus_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
1894
1895 int total_w = tabs[max_drawn_tab].ofs_cache - tabs[offset].ofs_cache;
1896 for (int i = max_drawn_tab; i <= p_idx; i++) {
1897 if (tabs[i].hidden) {
1898 continue;
1899 }
1900
1901 total_w += tabs[i].size_cache;
1902 }
1903
1904 int prev_offset = offset;
1905
1906 for (int i = offset; i < p_idx; i++) {
1907 if (tabs[i].hidden) {
1908 continue;
1909 }
1910
1911 if (total_w > limit_minus_buttons) {
1912 total_w -= tabs[i].size_cache;
1913 offset++;
1914 } else {
1915 break;
1916 }
1917 }
1918
1919 if (prev_offset != offset) {
1920 _update_cache();
1921 queue_redraw();
1922 }
1923 }
1924
1925 Rect2 TabBar::get_tab_rect(int p_tab) const {
1926 ERR_FAIL_INDEX_V(p_tab, tabs.size(), Rect2());
1927
1928 if (is_layout_rtl()) {
1929 return Rect2(get_size().width - tabs[p_tab].ofs_cache - tabs[p_tab].size_cache, 0, tabs[p_tab].size_cache, get_size().height);
1930 } else {
1931 return Rect2(tabs[p_tab].ofs_cache, 0, tabs[p_tab].size_cache, get_size().height);
1932 }
1933 }
1934
1935 void TabBar::set_close_with_middle_mouse(bool p_scroll_close) {
1936 close_with_middle_mouse = p_scroll_close;
1937 }
1938
1939 bool TabBar::get_close_with_middle_mouse() const {
1940 return close_with_middle_mouse;
1941 }
1942
1943 void TabBar::set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy) {
1944 ERR_FAIL_INDEX(p_policy, CLOSE_BUTTON_MAX);
1945
1946 if (cb_displaypolicy == p_policy) {
1947 return;
1948 }
1949
1950 cb_displaypolicy = p_policy;
1951
1952 _update_cache();
1953 _ensure_no_over_offset();
1954 if (scroll_to_selected) {
1955 ensure_tab_visible(current);
1956 }
1957 queue_redraw();
1958 update_minimum_size();
1959 }
1960
1961 TabBar::CloseButtonDisplayPolicy TabBar::get_tab_close_display_policy() const {
1962 return cb_displaypolicy;
1963 }
1964
1965 void TabBar::set_max_tab_width(int p_width) {
1966 ERR_FAIL_COND(p_width < 0);
1967
1968 if (max_width == p_width) {
1969 return;
1970 }
1971
1972 max_width = p_width;
1973
1974 _update_cache();
1975 _ensure_no_over_offset();
1976 if (scroll_to_selected) {
1977 ensure_tab_visible(current);
1978 }
1979 queue_redraw();
1980 update_minimum_size();
1981 }
1982
1983 int TabBar::get_max_tab_width() const {
1984 return max_width;
1985 }
1986
1987 void TabBar::set_scrolling_enabled(bool p_enabled) {
1988 scrolling_enabled = p_enabled;
1989 }
1990
1991 bool TabBar::get_scrolling_enabled() const {
1992 return scrolling_enabled;
1993 }
1994
1995 void TabBar::set_drag_to_rearrange_enabled(bool p_enabled) {
1996 drag_to_rearrange_enabled = p_enabled;
1997 }
1998
1999 bool TabBar::get_drag_to_rearrange_enabled() const {
2000 return drag_to_rearrange_enabled;
2001 }
2002
2003 void TabBar::set_tabs_rearrange_group(int p_group_id) {
2004 tabs_rearrange_group = p_group_id;
2005 }
2006
2007 int TabBar::get_tabs_rearrange_group() const {
2008 return tabs_rearrange_group;
2009 }
2010
2011 void TabBar::set_scroll_to_selected(bool p_enabled) {
2012 scroll_to_selected = p_enabled;
2013 if (p_enabled) {
2014 ensure_tab_visible(current);
2015 }
2016 }
2017
2018 bool TabBar::get_scroll_to_selected() const {
2019 return scroll_to_selected;
2020 }
2021
2022 void TabBar::set_switch_on_drag_hover(bool p_enabled) {
2023 switch_on_drag_hover = p_enabled;
2024 }
2025
2026 bool TabBar::get_switch_on_drag_hover() const {
2027 return switch_on_drag_hover;
2028 }
2029
2030 void TabBar::set_select_with_rmb(bool p_enabled) {
2031 select_with_rmb = p_enabled;
2032 }
2033
2034 bool TabBar::get_select_with_rmb() const {
2035 return select_with_rmb;
2036 }
2037
2038 void TabBar::set_deselect_enabled(bool p_enabled) {
2039 if (deselect_enabled == p_enabled) {
2040 return;
2041 }
2042 deselect_enabled = p_enabled;
2043 if (!deselect_enabled && current == -1 && !tabs.is_empty()) {
2044 select_next_available();
2045 }
2046 }
2047
2048 bool TabBar::get_deselect_enabled() const {
2049 return deselect_enabled;
2050 }
2051
2052 void TabBar::_bind_methods() {
2053 ClassDB::bind_method(D_METHOD("set_tab_count", "count"), &TabBar::set_tab_count);
2054 ClassDB::bind_method(D_METHOD("get_tab_count"), &TabBar::get_tab_count);
2055 ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabBar::set_current_tab);
2056 ClassDB::bind_method(D_METHOD("get_current_tab"), &TabBar::get_current_tab);
2057 ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabBar::get_previous_tab);
2058 ClassDB::bind_method(D_METHOD("select_previous_available"), &TabBar::select_previous_available);
2059 ClassDB::bind_method(D_METHOD("select_next_available"), &TabBar::select_next_available);
2060 ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabBar::set_tab_title);
2061 ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabBar::get_tab_title);
2062 ClassDB::bind_method(D_METHOD("set_tab_tooltip", "tab_idx", "tooltip"), &TabBar::set_tab_tooltip);
2063 ClassDB::bind_method(D_METHOD("get_tab_tooltip", "tab_idx"), &TabBar::get_tab_tooltip);
2064 ClassDB::bind_method(D_METHOD("set_tab_text_direction", "tab_idx", "direction"), &TabBar::set_tab_text_direction);
2065 ClassDB::bind_method(D_METHOD("get_tab_text_direction", "tab_idx"), &TabBar::get_tab_text_direction);
2066 ClassDB::bind_method(D_METHOD("set_tab_language", "tab_idx", "language"), &TabBar::set_tab_language);
2067 ClassDB::bind_method(D_METHOD("get_tab_language", "tab_idx"), &TabBar::get_tab_language);
2068 ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabBar::set_tab_icon);
2069 ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabBar::get_tab_icon);
2070 ClassDB::bind_method(D_METHOD("set_tab_icon_max_width", "tab_idx", "width"), &TabBar::set_tab_icon_max_width);
2071 ClassDB::bind_method(D_METHOD("get_tab_icon_max_width", "tab_idx"), &TabBar::get_tab_icon_max_width);
2072 ClassDB::bind_method(D_METHOD("set_tab_button_icon", "tab_idx", "icon"), &TabBar::set_tab_button_icon);
2073 ClassDB::bind_method(D_METHOD("get_tab_button_icon", "tab_idx"), &TabBar::get_tab_button_icon);
2074 ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabBar::set_tab_disabled);
2075 ClassDB::bind_method(D_METHOD("is_tab_disabled", "tab_idx"), &TabBar::is_tab_disabled);
2076 ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabBar::set_tab_hidden);
2077 ClassDB::bind_method(D_METHOD("is_tab_hidden", "tab_idx"), &TabBar::is_tab_hidden);
2078 ClassDB::bind_method(D_METHOD("set_tab_metadata", "tab_idx", "metadata"), &TabBar::set_tab_metadata);
2079 ClassDB::bind_method(D_METHOD("get_tab_metadata", "tab_idx"), &TabBar::get_tab_metadata);
2080 ClassDB::bind_method(D_METHOD("remove_tab", "tab_idx"), &TabBar::remove_tab);
2081 ClassDB::bind_method(D_METHOD("add_tab", "title", "icon"), &TabBar::add_tab, DEFVAL(""), DEFVAL(Ref<Texture2D>()));
2082 ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabBar::get_tab_idx_at_point);
2083 ClassDB::bind_method(D_METHOD("set_tab_alignment", "alignment"), &TabBar::set_tab_alignment);
2084 ClassDB::bind_method(D_METHOD("get_tab_alignment"), &TabBar::get_tab_alignment);
2085 ClassDB::bind_method(D_METHOD("set_clip_tabs", "clip_tabs"), &TabBar::set_clip_tabs);
2086 ClassDB::bind_method(D_METHOD("get_clip_tabs"), &TabBar::get_clip_tabs);
2087 ClassDB::bind_method(D_METHOD("get_tab_offset"), &TabBar::get_tab_offset);
2088 ClassDB::bind_method(D_METHOD("get_offset_buttons_visible"), &TabBar::get_offset_buttons_visible);
2089 ClassDB::bind_method(D_METHOD("ensure_tab_visible", "idx"), &TabBar::ensure_tab_visible);
2090 ClassDB::bind_method(D_METHOD("get_tab_rect", "tab_idx"), &TabBar::get_tab_rect);
2091 ClassDB::bind_method(D_METHOD("move_tab", "from", "to"), &TabBar::move_tab);
2092 ClassDB::bind_method(D_METHOD("set_close_with_middle_mouse", "enabled"), &TabBar::set_close_with_middle_mouse);
2093 ClassDB::bind_method(D_METHOD("get_close_with_middle_mouse"), &TabBar::get_close_with_middle_mouse);
2094 ClassDB::bind_method(D_METHOD("set_tab_close_display_policy", "policy"), &TabBar::set_tab_close_display_policy);
2095 ClassDB::bind_method(D_METHOD("get_tab_close_display_policy"), &TabBar::get_tab_close_display_policy);
2096 ClassDB::bind_method(D_METHOD("set_max_tab_width", "width"), &TabBar::set_max_tab_width);
2097 ClassDB::bind_method(D_METHOD("get_max_tab_width"), &TabBar::get_max_tab_width);
2098 ClassDB::bind_method(D_METHOD("set_scrolling_enabled", "enabled"), &TabBar::set_scrolling_enabled);
2099 ClassDB::bind_method(D_METHOD("get_scrolling_enabled"), &TabBar::get_scrolling_enabled);
2100 ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabBar::set_drag_to_rearrange_enabled);
2101 ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabBar::get_drag_to_rearrange_enabled);
2102 ClassDB::bind_method(D_METHOD("set_switch_on_drag_hover", "enabled"), &TabBar::set_switch_on_drag_hover);
2103 ClassDB::bind_method(D_METHOD("get_switch_on_drag_hover"), &TabBar::get_switch_on_drag_hover);
2104 ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabBar::set_tabs_rearrange_group);
2105 ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabBar::get_tabs_rearrange_group);
2106 ClassDB::bind_method(D_METHOD("set_scroll_to_selected", "enabled"), &TabBar::set_scroll_to_selected);
2107 ClassDB::bind_method(D_METHOD("get_scroll_to_selected"), &TabBar::get_scroll_to_selected);
2108 ClassDB::bind_method(D_METHOD("set_select_with_rmb", "enabled"), &TabBar::set_select_with_rmb);
2109 ClassDB::bind_method(D_METHOD("get_select_with_rmb"), &TabBar::get_select_with_rmb);
2110 ClassDB::bind_method(D_METHOD("set_deselect_enabled", "enabled"), &TabBar::set_deselect_enabled);
2111 ClassDB::bind_method(D_METHOD("get_deselect_enabled"), &TabBar::get_deselect_enabled);
2112 ClassDB::bind_method(D_METHOD("clear_tabs"), &TabBar::clear_tabs);
2113
2114 ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
2115 ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
2116 ADD_SIGNAL(MethodInfo("tab_clicked", PropertyInfo(Variant::INT, "tab")));
2117 ADD_SIGNAL(MethodInfo("tab_rmb_clicked", PropertyInfo(Variant::INT, "tab")));
2118 ADD_SIGNAL(MethodInfo("tab_close_pressed", PropertyInfo(Variant::INT, "tab")));
2119 ADD_SIGNAL(MethodInfo("tab_button_pressed", PropertyInfo(Variant::INT, "tab")));
2120 ADD_SIGNAL(MethodInfo("tab_hovered", PropertyInfo(Variant::INT, "tab")));
2121 ADD_SIGNAL(MethodInfo("active_tab_rearranged", PropertyInfo(Variant::INT, "idx_to")));
2122
2123 ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1"), "set_current_tab", "get_current_tab");
2124 ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_alignment", "get_tab_alignment");
2125 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_tabs"), "set_clip_tabs", "get_clip_tabs");
2126 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "close_with_middle_mouse"), "set_close_with_middle_mouse", "get_close_with_middle_mouse");
2127 ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_close_display_policy", PROPERTY_HINT_ENUM, "Show Never,Show Active Only,Show Always"), "set_tab_close_display_policy", "get_tab_close_display_policy");
2128 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_tab_width", PROPERTY_HINT_RANGE, "0,99999,1,suffix:px"), "set_max_tab_width", "get_max_tab_width");
2129 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrolling_enabled"), "set_scrolling_enabled", "get_scrolling_enabled");
2130 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
2131 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_drag_hover"), "set_switch_on_drag_hover", "get_switch_on_drag_hover");
2132 ADD_PROPERTY(PropertyInfo(Variant::INT, "tabs_rearrange_group"), "set_tabs_rearrange_group", "get_tabs_rearrange_group");
2133 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_to_selected"), "set_scroll_to_selected", "get_scroll_to_selected");
2134 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_with_rmb"), "set_select_with_rmb", "get_select_with_rmb");
2135 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_enabled"), "set_deselect_enabled", "get_deselect_enabled");
2136
2137 ADD_ARRAY_COUNT("Tabs", "tab_count", "set_tab_count", "get_tab_count", "tab_");
2138
2139 BIND_ENUM_CONSTANT(ALIGNMENT_LEFT);
2140 BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
2141 BIND_ENUM_CONSTANT(ALIGNMENT_RIGHT);
2142 BIND_ENUM_CONSTANT(ALIGNMENT_MAX);
2143
2144 BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_NEVER);
2145 BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ACTIVE_ONLY);
2146 BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ALWAYS);
2147 BIND_ENUM_CONSTANT(CLOSE_BUTTON_MAX);
2148
2149 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, h_separation);
2150 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, tab_separation);
2151 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, icon_max_width);
2152 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, hover_switch_wait_msec);
2153
2154 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_unselected_style, "tab_unselected");
2155 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_hovered_style, "tab_hovered");
2156 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_selected_style, "tab_selected");
2157 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_disabled_style, "tab_disabled");
2158 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_focus_style, "tab_focus");
2159
2160 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, increment_icon, "increment");
2161 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, increment_hl_icon, "increment_highlight");
2162 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, decrement_icon, "decrement");
2163 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, decrement_hl_icon, "decrement_highlight");
2164 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, drop_mark_icon, "drop_mark");
2165 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, drop_mark_color);
2166
2167 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_selected_color);
2168 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_hovered_color);
2169 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_unselected_color);
2170 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_disabled_color);
2171 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_outline_color);
2172
2173 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, icon_selected_color);
2174 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, icon_hovered_color);
2175 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, icon_unselected_color);
2176 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, icon_disabled_color);
2177
2178 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, TabBar, font);
2179 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, TabBar, font_size);
2180 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, outline_size);
2181
2182 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, close_icon, "close");
2183 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, button_pressed_style, "button_pressed");
2184 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, button_hl_style, "button_highlight");
2185
2186 Tab defaults(true);
2187
2188 base_property_helper.set_prefix("tab_");
2189 base_property_helper.set_array_length_getter(&TabBar::get_tab_count);
2190 base_property_helper.register_property(PropertyInfo(Variant::STRING, "title"), defaults.text, &TabBar::set_tab_title, &TabBar::get_tab_title);
2191 base_property_helper.register_property(PropertyInfo(Variant::STRING, "tooltip"), defaults.tooltip, &TabBar::set_tab_tooltip, &TabBar::get_tab_tooltip);
2192 base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &TabBar::set_tab_icon, &TabBar::get_tab_icon);
2193 base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &TabBar::set_tab_disabled, &TabBar::is_tab_disabled);
2194 PropertyListHelper::register_base_helper(&base_property_helper);
2195 }
2196
2197 TabBar::TabBar() {
2198 set_focus_mode(FOCUS_ACCESSIBILITY);
2199 set_size(Size2(get_size().width, get_minimum_size().height));
2200 set_focus_mode(FOCUS_ALL);
2201 connect(SceneStringName(mouse_exited), callable_mp(this, &TabBar::_on_mouse_exited));
2202
2203 hover_switch_delay = memnew(Timer);
2204 hover_switch_delay->connect("timeout", callable_mp(this, &TabBar::_hover_switch_timeout));
2205 hover_switch_delay->set_one_shot(true);
2206 add_child(hover_switch_delay, false, INTERNAL_MODE_FRONT);
2207
2208 property_helper.setup_for_instance(base_property_helper, this);
2209 }