tab_bar.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  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. #include "tab_bar.h"
  31. #include "scene/gui/box_container.h"
  32. #include "scene/gui/label.h"
  33. #include "scene/gui/texture_rect.h"
  34. #include "scene/main/viewport.h"
  35. #include "scene/theme/theme_db.h"
  36. Size2 TabBar::get_minimum_size() const {
  37. Size2 ms;
  38. if (tabs.is_empty()) {
  39. return ms;
  40. }
  41. 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);
  42. int max_tab_width = 0;
  43. for (int i = 0; i < tabs.size(); i++) {
  44. if (tabs[i].hidden) {
  45. continue;
  46. }
  47. int ofs = ms.width;
  48. Ref<StyleBox> style;
  49. if (tabs[i].disabled) {
  50. style = theme_cache.tab_disabled_style;
  51. } else if (current == i) {
  52. style = theme_cache.tab_selected_style;
  53. } else if (hover == i) {
  54. style = theme_cache.tab_hovered_style;
  55. } else {
  56. style = theme_cache.tab_unselected_style;
  57. }
  58. ms.width += style->get_minimum_size().width;
  59. if (tabs[i].icon.is_valid()) {
  60. const Size2 icon_size = _get_tab_icon_size(i);
  61. ms.height = MAX(ms.height, icon_size.height + y_margin);
  62. ms.width += icon_size.width + theme_cache.h_separation;
  63. }
  64. if (!tabs[i].text.is_empty()) {
  65. ms.width += tabs[i].size_text + theme_cache.h_separation;
  66. }
  67. ms.height = MAX(ms.height, tabs[i].text_buf->get_size().y + y_margin);
  68. bool close_visible = cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current);
  69. if (tabs[i].right_button.is_valid()) {
  70. Ref<Texture2D> rb = tabs[i].right_button;
  71. if (close_visible) {
  72. ms.width += theme_cache.button_hl_style->get_minimum_size().width + rb->get_width();
  73. } else {
  74. ms.width += theme_cache.button_hl_style->get_margin(SIDE_LEFT) + rb->get_width() + theme_cache.h_separation;
  75. }
  76. ms.height = MAX(ms.height, rb->get_height() + y_margin);
  77. }
  78. if (close_visible) {
  79. ms.width += theme_cache.button_hl_style->get_margin(SIDE_LEFT) + theme_cache.close_icon->get_width() + theme_cache.h_separation;
  80. ms.height = MAX(ms.height, theme_cache.close_icon->get_height() + y_margin);
  81. }
  82. if (ms.width - ofs > style->get_minimum_size().width) {
  83. ms.width -= theme_cache.h_separation;
  84. }
  85. if (i < tabs.size() - 1) {
  86. ms.width += theme_cache.tab_separation;
  87. }
  88. if (ms.width - ofs > max_tab_width) {
  89. max_tab_width = ms.width - ofs;
  90. }
  91. }
  92. if (clip_tabs) {
  93. ms.width = max_tab_width + (get_tab_count() > 1 ? theme_cache.decrement_icon->get_width() + theme_cache.increment_icon->get_width() : 0);
  94. }
  95. return ms;
  96. }
  97. void TabBar::gui_input(const Ref<InputEvent> &p_event) {
  98. ERR_FAIL_COND(p_event.is_null());
  99. Ref<InputEventMouseMotion> mm = p_event;
  100. if (mm.is_valid()) {
  101. Point2 pos = mm->get_position();
  102. if (buttons_visible) {
  103. if (is_layout_rtl()) {
  104. if (pos.x < theme_cache.decrement_icon->get_width()) {
  105. if (highlight_arrow != 1) {
  106. highlight_arrow = 1;
  107. queue_redraw();
  108. }
  109. } else if (pos.x < theme_cache.increment_icon->get_width() + theme_cache.decrement_icon->get_width()) {
  110. if (highlight_arrow != 0) {
  111. highlight_arrow = 0;
  112. queue_redraw();
  113. }
  114. } else if (highlight_arrow != -1) {
  115. highlight_arrow = -1;
  116. queue_redraw();
  117. }
  118. } else {
  119. int limit_minus_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  120. if (pos.x > limit_minus_buttons + theme_cache.decrement_icon->get_width()) {
  121. if (highlight_arrow != 1) {
  122. highlight_arrow = 1;
  123. queue_redraw();
  124. }
  125. } else if (pos.x > limit_minus_buttons) {
  126. if (highlight_arrow != 0) {
  127. highlight_arrow = 0;
  128. queue_redraw();
  129. }
  130. } else if (highlight_arrow != -1) {
  131. highlight_arrow = -1;
  132. queue_redraw();
  133. }
  134. }
  135. }
  136. if (get_viewport()->gui_is_dragging() && can_drop_data(pos, get_viewport()->gui_get_drag_data())) {
  137. dragging_valid_tab = true;
  138. queue_redraw();
  139. }
  140. if (!tabs.is_empty()) {
  141. _update_hover();
  142. }
  143. return;
  144. }
  145. Ref<InputEventMouseButton> mb = p_event;
  146. if (mb.is_valid()) {
  147. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP && !mb->is_command_or_control_pressed()) {
  148. if (scrolling_enabled && buttons_visible) {
  149. if (offset > 0) {
  150. offset--;
  151. _update_cache();
  152. queue_redraw();
  153. }
  154. }
  155. }
  156. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN && !mb->is_command_or_control_pressed()) {
  157. if (scrolling_enabled && buttons_visible) {
  158. if (missing_right && offset < tabs.size()) {
  159. offset++;
  160. _update_cache();
  161. queue_redraw();
  162. }
  163. }
  164. }
  165. if (rb_pressing && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  166. if (rb_hover != -1) {
  167. emit_signal(SNAME("tab_button_pressed"), rb_hover);
  168. }
  169. rb_pressing = false;
  170. queue_redraw();
  171. }
  172. if (cb_pressing && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  173. if (cb_hover != -1) {
  174. emit_signal(SNAME("tab_close_pressed"), cb_hover);
  175. }
  176. cb_pressing = false;
  177. queue_redraw();
  178. }
  179. if (close_with_middle_mouse && mb->is_pressed() && mb->get_button_index() == MouseButton::MIDDLE) {
  180. if (hover != -1) {
  181. emit_signal(SNAME("tab_close_pressed"), hover);
  182. }
  183. }
  184. if (mb->is_pressed() && (mb->get_button_index() == MouseButton::LEFT || (select_with_rmb && mb->get_button_index() == MouseButton::RIGHT))) {
  185. Point2 pos = mb->get_position();
  186. if (buttons_visible) {
  187. if (is_layout_rtl()) {
  188. if (pos.x < theme_cache.decrement_icon->get_width()) {
  189. if (missing_right) {
  190. offset++;
  191. _update_cache();
  192. queue_redraw();
  193. }
  194. return;
  195. } else if (pos.x < theme_cache.increment_icon->get_width() + theme_cache.decrement_icon->get_width()) {
  196. if (offset > 0) {
  197. offset--;
  198. _update_cache();
  199. queue_redraw();
  200. }
  201. return;
  202. }
  203. } else {
  204. int limit = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  205. if (pos.x > limit + theme_cache.decrement_icon->get_width()) {
  206. if (missing_right) {
  207. offset++;
  208. _update_cache();
  209. queue_redraw();
  210. }
  211. return;
  212. } else if (pos.x > limit) {
  213. if (offset > 0) {
  214. offset--;
  215. _update_cache();
  216. queue_redraw();
  217. }
  218. return;
  219. }
  220. }
  221. }
  222. if (tabs.is_empty()) {
  223. // Return early if there are no actual tabs to handle input for.
  224. return;
  225. }
  226. int found = -1;
  227. for (int i = offset; i <= max_drawn_tab; i++) {
  228. if (tabs[i].hidden) {
  229. continue;
  230. }
  231. if (tabs[i].rb_rect.has_point(pos)) {
  232. rb_pressing = true;
  233. _update_hover();
  234. queue_redraw();
  235. return;
  236. }
  237. if (tabs[i].cb_rect.has_point(pos) && (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current))) {
  238. cb_pressing = true;
  239. _update_hover();
  240. queue_redraw();
  241. return;
  242. }
  243. if (pos.x >= get_tab_rect(i).position.x && pos.x < get_tab_rect(i).position.x + tabs[i].size_cache) {
  244. if (!tabs[i].disabled) {
  245. found = i;
  246. }
  247. break;
  248. }
  249. }
  250. if (found != -1) {
  251. if (deselect_enabled && get_current_tab() == found) {
  252. set_current_tab(-1);
  253. } else {
  254. set_current_tab(found);
  255. }
  256. if (mb->get_button_index() == MouseButton::RIGHT) {
  257. // Right mouse button clicked.
  258. emit_signal(SNAME("tab_rmb_clicked"), found);
  259. }
  260. emit_signal(SNAME("tab_clicked"), found);
  261. }
  262. }
  263. }
  264. if (p_event->is_pressed()) {
  265. Input *input = Input::get_singleton();
  266. Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
  267. Ref<InputEventJoypadButton> joypadbutton_event = p_event;
  268. bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
  269. if (p_event->is_action("ui_right", true)) {
  270. if (is_joypad_event) {
  271. if (!input->is_action_just_pressed("ui_right", true)) {
  272. return;
  273. }
  274. set_process_internal(true);
  275. }
  276. if (is_layout_rtl() ? select_previous_available() : select_next_available()) {
  277. accept_event();
  278. }
  279. } else if (p_event->is_action("ui_left", true)) {
  280. if (is_joypad_event) {
  281. if (!input->is_action_just_pressed("ui_left", true)) {
  282. return;
  283. }
  284. set_process_internal(true);
  285. }
  286. if (is_layout_rtl() ? select_next_available() : select_previous_available()) {
  287. accept_event();
  288. }
  289. }
  290. }
  291. }
  292. String TabBar::get_tooltip(const Point2 &p_pos) const {
  293. int tab_idx = get_tab_idx_at_point(p_pos);
  294. if (tab_idx < 0) {
  295. return Control::get_tooltip(p_pos);
  296. }
  297. if (tabs[tab_idx].tooltip.is_empty() && tabs[tab_idx].truncated) {
  298. return tabs[tab_idx].text;
  299. }
  300. return tabs[tab_idx].tooltip;
  301. }
  302. void TabBar::_shape(int p_tab) {
  303. tabs.write[p_tab].text_buf->clear();
  304. tabs.write[p_tab].text_buf->set_width(-1);
  305. if (tabs[p_tab].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  306. tabs.write[p_tab].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  307. } else {
  308. tabs.write[p_tab].text_buf->set_direction((TextServer::Direction)tabs[p_tab].text_direction);
  309. }
  310. tabs.write[p_tab].text_buf->add_string(atr(tabs[p_tab].text), theme_cache.font, theme_cache.font_size, tabs[p_tab].language);
  311. }
  312. RID TabBar::get_tab_accessibility_element(int p_tab) const {
  313. RID ae = get_accessibility_element();
  314. ERR_FAIL_COND_V(ae.is_null(), RID());
  315. const Tab &item = tabs[p_tab];
  316. if (item.accessibility_item_element.is_null()) {
  317. item.accessibility_item_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_TAB);
  318. item.accessibility_item_dirty = true;
  319. }
  320. return item.accessibility_item_element;
  321. }
  322. RID TabBar::get_focused_accessibility_element() const {
  323. if (current == -1) {
  324. return get_accessibility_element();
  325. } else {
  326. const Tab &item = tabs[current];
  327. return item.accessibility_item_element;
  328. }
  329. }
  330. void TabBar::_accessibility_action_scroll_into_view(const Variant &p_data, int p_index) {
  331. ensure_tab_visible(p_index);
  332. }
  333. void TabBar::_accessibility_action_focus(const Variant &p_data, int p_index) {
  334. set_current_tab(p_index);
  335. }
  336. void TabBar::_notification(int p_what) {
  337. switch (p_what) {
  338. case NOTIFICATION_ENTER_TREE: {
  339. if (scroll_to_selected) {
  340. ensure_tab_visible(current);
  341. }
  342. // Set initialized even if no tabs were set.
  343. initialized = true;
  344. } break;
  345. case NOTIFICATION_INTERNAL_PROCESS: {
  346. Input *input = Input::get_singleton();
  347. if (input->is_action_just_released("ui_left") || input->is_action_just_released("ui_right")) {
  348. gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  349. set_process_internal(false);
  350. return;
  351. }
  352. gamepad_event_delay_ms -= get_process_delta_time();
  353. if (gamepad_event_delay_ms <= 0) {
  354. gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
  355. if (input->is_action_pressed("ui_right")) {
  356. is_layout_rtl() ? select_previous_available() : select_next_available();
  357. }
  358. if (input->is_action_pressed("ui_left")) {
  359. is_layout_rtl() ? select_next_available() : select_previous_available();
  360. }
  361. }
  362. } break;
  363. case NOTIFICATION_EXIT_TREE:
  364. case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
  365. for (int i = 0; i < tabs.size(); i++) {
  366. tabs.write[i].accessibility_item_element = RID();
  367. }
  368. } break;
  369. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  370. RID ae = get_accessibility_element();
  371. ERR_FAIL_COND(ae.is_null());
  372. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_TAB_BAR);
  373. DisplayServer::get_singleton()->accessibility_update_set_list_item_count(ae, tabs.size());
  374. for (int i = 0; i < tabs.size(); i++) {
  375. const Tab &item = tabs[i];
  376. if (item.accessibility_item_element.is_null()) {
  377. item.accessibility_item_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_TAB);
  378. item.accessibility_item_dirty = true;
  379. }
  380. if (item.accessibility_item_dirty) {
  381. 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));
  382. DisplayServer::get_singleton()->accessibility_update_add_action(item.accessibility_item_element, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &TabBar::_accessibility_action_focus).bind(i));
  383. DisplayServer::get_singleton()->accessibility_update_set_list_item_index(item.accessibility_item_element, i);
  384. DisplayServer::get_singleton()->accessibility_update_set_name(item.accessibility_item_element, atr(item.text));
  385. DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(item.accessibility_item_element, i == current);
  386. DisplayServer::get_singleton()->accessibility_update_set_flag(item.accessibility_item_element, DisplayServer::AccessibilityFlags::FLAG_DISABLED, item.disabled);
  387. DisplayServer::get_singleton()->accessibility_update_set_flag(item.accessibility_item_element, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, item.hidden);
  388. DisplayServer::get_singleton()->accessibility_update_set_tooltip(item.accessibility_item_element, item.tooltip);
  389. DisplayServer::get_singleton()->accessibility_update_set_bounds(item.accessibility_item_element, Rect2(Point2(item.ofs_cache, 0), Size2(item.size_cache, get_size().height)));
  390. item.accessibility_item_dirty = false;
  391. }
  392. }
  393. } break;
  394. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  395. queue_redraw();
  396. } break;
  397. case NOTIFICATION_THEME_CHANGED:
  398. case NOTIFICATION_TRANSLATION_CHANGED: {
  399. for (int i = 0; i < tabs.size(); ++i) {
  400. _shape(i);
  401. }
  402. queue_accessibility_update();
  403. queue_redraw();
  404. update_minimum_size();
  405. [[fallthrough]];
  406. }
  407. case NOTIFICATION_RESIZED: {
  408. int ofs_old = offset;
  409. int max_old = max_drawn_tab;
  410. _update_cache();
  411. _ensure_no_over_offset();
  412. if (scroll_to_selected && (offset != ofs_old || max_drawn_tab != max_old)) {
  413. ensure_tab_visible(current);
  414. }
  415. } break;
  416. case NOTIFICATION_DRAG_END: {
  417. if (dragging_valid_tab) {
  418. dragging_valid_tab = false;
  419. queue_redraw();
  420. }
  421. } break;
  422. case NOTIFICATION_DRAW: {
  423. bool rtl = is_layout_rtl();
  424. Vector2 size = get_size();
  425. if (tabs.is_empty()) {
  426. // Draw the drop indicator where the first tab would be if there are no tabs.
  427. if (dragging_valid_tab) {
  428. int x = rtl ? size.x : 0;
  429. 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);
  430. }
  431. return;
  432. }
  433. int limit_minus_buttons = size.width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  434. // Draw unselected tabs in the back.
  435. for (int i = offset; i <= max_drawn_tab; i++) {
  436. if (tabs[i].hidden) {
  437. continue;
  438. }
  439. if (i != current) {
  440. Ref<StyleBox> sb;
  441. Color col;
  442. if (tabs[i].disabled) {
  443. sb = theme_cache.tab_disabled_style;
  444. col = theme_cache.font_disabled_color;
  445. } else if (i == hover) {
  446. sb = theme_cache.tab_hovered_style;
  447. col = theme_cache.font_hovered_color;
  448. } else {
  449. sb = theme_cache.tab_unselected_style;
  450. col = theme_cache.font_unselected_color;
  451. }
  452. _draw_tab(sb, col, i, rtl ? (size.width - tabs[i].ofs_cache - tabs[i].size_cache) : tabs[i].ofs_cache, false);
  453. }
  454. }
  455. // Draw selected tab in the front, but only if it's visible.
  456. if (current >= offset && current <= max_drawn_tab && !tabs[current].hidden) {
  457. Ref<StyleBox> sb = tabs[current].disabled ? theme_cache.tab_disabled_style : theme_cache.tab_selected_style;
  458. _draw_tab(sb, theme_cache.font_selected_color, current, rtl ? (size.width - tabs[current].ofs_cache - tabs[current].size_cache) : tabs[current].ofs_cache, has_focus());
  459. }
  460. if (buttons_visible) {
  461. int vofs = (size.height - theme_cache.increment_icon->get_size().height) / 2;
  462. if (rtl) {
  463. if (missing_right) {
  464. draw_texture(highlight_arrow == 1 ? theme_cache.decrement_hl_icon : theme_cache.decrement_icon, Point2(0, vofs));
  465. } else {
  466. draw_texture(theme_cache.decrement_icon, Point2(0, vofs), Color(1, 1, 1, 0.5));
  467. }
  468. if (offset > 0) {
  469. draw_texture(highlight_arrow == 0 ? theme_cache.increment_hl_icon : theme_cache.increment_icon, Point2(theme_cache.increment_icon->get_size().width, vofs));
  470. } else {
  471. draw_texture(theme_cache.increment_icon, Point2(theme_cache.increment_icon->get_size().width, vofs), Color(1, 1, 1, 0.5));
  472. }
  473. } else {
  474. if (offset > 0) {
  475. draw_texture(highlight_arrow == 0 ? theme_cache.decrement_hl_icon : theme_cache.decrement_icon, Point2(limit_minus_buttons, vofs));
  476. } else {
  477. draw_texture(theme_cache.decrement_icon, Point2(limit_minus_buttons, vofs), Color(1, 1, 1, 0.5));
  478. }
  479. if (missing_right) {
  480. 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));
  481. } else {
  482. draw_texture(theme_cache.increment_icon, Point2(limit_minus_buttons + theme_cache.decrement_icon->get_size().width, vofs), Color(1, 1, 1, 0.5));
  483. }
  484. }
  485. }
  486. if (dragging_valid_tab) {
  487. _draw_tab_drop(get_canvas_item());
  488. }
  489. } break;
  490. }
  491. }
  492. void TabBar::_draw_tab_drop(RID p_canvas_item) {
  493. Vector2 size = get_size();
  494. int x;
  495. bool rtl = is_layout_rtl();
  496. int closest_tab = get_closest_tab_idx_to_point(get_local_mouse_position());
  497. if (closest_tab != -1) {
  498. Rect2 tab_rect = get_tab_rect(closest_tab);
  499. x = tab_rect.position.x;
  500. // Only add the tab_separation if closest tab is not on the edge.
  501. bool not_leftmost_tab = -1 != (rtl ? get_next_available(closest_tab) : get_previous_available(closest_tab));
  502. bool not_rightmost_tab = -1 != (rtl ? get_previous_available(closest_tab) : get_next_available(closest_tab));
  503. // Calculate midpoint between tabs.
  504. if (get_local_mouse_position().x > tab_rect.get_center().x) {
  505. x += tab_rect.size.x;
  506. if (not_rightmost_tab) {
  507. x += Math::ceil(0.5f * theme_cache.tab_separation);
  508. }
  509. } else if (not_leftmost_tab) {
  510. x -= Math::floor(0.5f * theme_cache.tab_separation);
  511. }
  512. } else {
  513. if (rtl ^ (get_local_mouse_position().x < get_tab_rect(0).position.x)) {
  514. x = get_tab_rect(0).position.x;
  515. if (rtl) {
  516. x += get_tab_rect(0).size.width;
  517. }
  518. } else {
  519. Rect2 tab_rect = get_tab_rect(get_tab_count() - 1);
  520. x = tab_rect.position.x;
  521. if (!rtl) {
  522. x += tab_rect.size.width;
  523. }
  524. }
  525. }
  526. 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);
  527. }
  528. void TabBar::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x, bool p_focus) {
  529. RID ci = get_canvas_item();
  530. bool rtl = is_layout_rtl();
  531. Rect2 sb_rect = Rect2(p_x, 0, tabs[p_index].size_cache, get_size().height);
  532. if (tab_style_v_flip) {
  533. draw_set_transform(Point2(0.0, p_tab_style->get_draw_rect(sb_rect).size.y), 0.0, Size2(1.0, -1.0));
  534. }
  535. p_tab_style->draw(ci, sb_rect);
  536. if (tab_style_v_flip) {
  537. draw_set_transform(Point2(), 0.0, Size2(1.0, 1.0));
  538. }
  539. if (p_focus) {
  540. Ref<StyleBox> focus_style = theme_cache.tab_focus_style;
  541. focus_style->draw(ci, sb_rect);
  542. }
  543. p_x += rtl ? tabs[p_index].size_cache - p_tab_style->get_margin(SIDE_LEFT) : p_tab_style->get_margin(SIDE_LEFT);
  544. Size2i sb_ms = p_tab_style->get_minimum_size();
  545. // Draw the icon.
  546. Ref<Texture2D> icon = tabs[p_index].icon;
  547. if (icon.is_valid()) {
  548. const Size2 icon_size = _get_tab_icon_size(p_index);
  549. 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);
  550. icon->draw_rect(ci, Rect2(icon_pos, icon_size));
  551. p_x = rtl ? p_x - icon_size.width - theme_cache.h_separation : p_x + icon_size.width + theme_cache.h_separation;
  552. }
  553. // Draw the text.
  554. if (!tabs[p_index].text.is_empty()) {
  555. Point2i text_pos = Point2i(rtl ? p_x - tabs[p_index].size_text : p_x,
  556. p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - tabs[p_index].text_buf->get_size().y) / 2);
  557. if (theme_cache.outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  558. tabs[p_index].text_buf->draw_outline(ci, text_pos, theme_cache.outline_size, theme_cache.font_outline_color);
  559. }
  560. tabs[p_index].text_buf->draw(ci, text_pos, p_font_color);
  561. 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;
  562. }
  563. // Draw and calculate rect of the right button.
  564. if (tabs[p_index].right_button.is_valid()) {
  565. Ref<StyleBox> style = theme_cache.button_hl_style;
  566. Ref<Texture2D> rb = tabs[p_index].right_button;
  567. Rect2 rb_rect;
  568. rb_rect.size = style->get_minimum_size() + rb->get_size();
  569. rb_rect.position.x = rtl ? p_x - rb_rect.size.width : p_x;
  570. rb_rect.position.y = p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (rb_rect.size.y)) / 2;
  571. tabs.write[p_index].rb_rect = rb_rect;
  572. if (rb_hover == p_index) {
  573. if (rb_pressing) {
  574. theme_cache.button_pressed_style->draw(ci, rb_rect);
  575. } else {
  576. style->draw(ci, rb_rect);
  577. }
  578. }
  579. rb->draw(ci, Point2i(rb_rect.position.x + style->get_margin(SIDE_LEFT), rb_rect.position.y + style->get_margin(SIDE_TOP)));
  580. p_x = rtl ? rb_rect.position.x : rb_rect.position.x + rb_rect.size.width;
  581. } else {
  582. tabs.write[p_index].rb_rect = Rect2();
  583. }
  584. // Draw and calculate rect of the close button.
  585. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_index == current)) {
  586. Ref<StyleBox> style = theme_cache.button_hl_style;
  587. Ref<Texture2D> cb = theme_cache.close_icon;
  588. Rect2 cb_rect;
  589. cb_rect.size = style->get_minimum_size() + cb->get_size();
  590. cb_rect.position.x = rtl ? p_x - cb_rect.size.width : p_x;
  591. cb_rect.position.y = p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (cb_rect.size.y)) / 2;
  592. tabs.write[p_index].cb_rect = cb_rect;
  593. if (!tabs[p_index].disabled && cb_hover == p_index) {
  594. if (cb_pressing) {
  595. theme_cache.button_pressed_style->draw(ci, cb_rect);
  596. } else {
  597. style->draw(ci, cb_rect);
  598. }
  599. }
  600. cb->draw(ci, Point2i(cb_rect.position.x + style->get_margin(SIDE_LEFT), cb_rect.position.y + style->get_margin(SIDE_TOP)));
  601. } else {
  602. tabs.write[p_index].cb_rect = Rect2();
  603. }
  604. }
  605. void TabBar::set_tab_count(int p_count) {
  606. if (p_count == tabs.size()) {
  607. return;
  608. }
  609. ERR_FAIL_COND(p_count < 0);
  610. if (tabs.size() > p_count) {
  611. for (int i = p_count; i < tabs.size(); i++) {
  612. if (tabs[i].accessibility_item_element.is_valid()) {
  613. DisplayServer::get_singleton()->accessibility_free_element(tabs.write[i].accessibility_item_element);
  614. tabs.write[i].accessibility_item_element = RID();
  615. }
  616. }
  617. }
  618. tabs.resize(p_count);
  619. if (p_count == 0) {
  620. offset = 0;
  621. max_drawn_tab = 0;
  622. current = -1;
  623. previous = -1;
  624. } else {
  625. offset = MIN(offset, p_count - 1);
  626. max_drawn_tab = MIN(max_drawn_tab, p_count - 1);
  627. current = MIN(current, p_count - 1);
  628. // Fix range if unable to deselect.
  629. if (current == -1 && !_can_deselect()) {
  630. current = 0;
  631. }
  632. _update_cache();
  633. _ensure_no_over_offset();
  634. if (scroll_to_selected) {
  635. ensure_tab_visible(current);
  636. }
  637. }
  638. if (!initialized) {
  639. initialized = true;
  640. if (queued_current != CURRENT_TAB_UNINITIALIZED && queued_current != current) {
  641. set_current_tab(queued_current);
  642. }
  643. }
  644. queue_accessibility_update();
  645. queue_redraw();
  646. update_minimum_size();
  647. notify_property_list_changed();
  648. }
  649. int TabBar::get_tab_count() const {
  650. return tabs.size();
  651. }
  652. void TabBar::set_current_tab(int p_current) {
  653. if (p_current == -1) {
  654. // An index of -1 is only valid if deselecting is enabled or there are no valid tabs.
  655. ERR_FAIL_COND_MSG(!_can_deselect(), "Cannot deselect tabs, deselection is not enabled.");
  656. } else {
  657. if (!initialized && p_current >= get_tab_count()) {
  658. queued_current = p_current;
  659. return;
  660. }
  661. ERR_FAIL_INDEX(p_current, get_tab_count());
  662. }
  663. previous = current;
  664. current = p_current;
  665. if (current == previous) {
  666. emit_signal(SNAME("tab_selected"), current);
  667. return;
  668. }
  669. emit_signal(SNAME("tab_selected"), current);
  670. _update_cache();
  671. if (scroll_to_selected) {
  672. ensure_tab_visible(current);
  673. }
  674. queue_accessibility_update();
  675. queue_redraw();
  676. emit_signal(SNAME("tab_changed"), p_current);
  677. }
  678. int TabBar::get_current_tab() const {
  679. return current;
  680. }
  681. int TabBar::get_previous_tab() const {
  682. return previous;
  683. }
  684. int TabBar::get_hovered_tab() const {
  685. return hover;
  686. }
  687. int TabBar::get_previous_available(int p_idx) const {
  688. ERR_FAIL_COND_V(p_idx < -1 || p_idx > get_tab_count(), -1);
  689. const int idx = p_idx == -1 ? get_current_tab() : p_idx;
  690. const int offset_end = idx + 1;
  691. for (int i = 1; i < offset_end; i++) {
  692. int target_tab = idx - i;
  693. if (target_tab < 0) {
  694. target_tab += get_tab_count();
  695. }
  696. if (!is_tab_disabled(target_tab) && !is_tab_hidden(target_tab)) {
  697. return target_tab;
  698. }
  699. }
  700. return -1;
  701. }
  702. int TabBar::get_next_available(int p_idx) const {
  703. ERR_FAIL_COND_V(p_idx < -1 || p_idx > get_tab_count(), -1);
  704. const int idx = p_idx == -1 ? get_current_tab() : p_idx;
  705. const int offset_end = get_tab_count() - idx;
  706. for (int i = 1; i < offset_end; i++) {
  707. int target_tab = (idx + i) % get_tab_count();
  708. if (!is_tab_disabled(target_tab) && !is_tab_hidden(target_tab)) {
  709. return target_tab;
  710. }
  711. }
  712. return -1;
  713. }
  714. bool TabBar::select_previous_available() {
  715. const int previous_available = get_previous_available();
  716. if (previous_available != -1) {
  717. set_current_tab(previous_available);
  718. }
  719. return previous_available != -1;
  720. }
  721. bool TabBar::select_next_available() {
  722. const int next_available = get_next_available();
  723. if (next_available != -1) {
  724. set_current_tab(next_available);
  725. }
  726. return next_available != -1;
  727. }
  728. void TabBar::set_tab_offset(int p_offset) {
  729. ERR_FAIL_INDEX(p_offset, tabs.size());
  730. offset = p_offset;
  731. _update_cache();
  732. queue_accessibility_update();
  733. queue_redraw();
  734. }
  735. int TabBar::get_tab_offset() const {
  736. return offset;
  737. }
  738. bool TabBar::get_offset_buttons_visible() const {
  739. return buttons_visible;
  740. }
  741. void TabBar::set_tab_title(int p_tab, const String &p_title) {
  742. ERR_FAIL_INDEX(p_tab, tabs.size());
  743. if (tabs[p_tab].text == p_title) {
  744. return;
  745. }
  746. tabs.write[p_tab].text = p_title;
  747. _shape(p_tab);
  748. _update_cache();
  749. _ensure_no_over_offset();
  750. if (scroll_to_selected) {
  751. ensure_tab_visible(current);
  752. }
  753. queue_accessibility_update();
  754. queue_redraw();
  755. update_minimum_size();
  756. }
  757. String TabBar::get_tab_title(int p_tab) const {
  758. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  759. return tabs[p_tab].text;
  760. }
  761. void TabBar::set_tab_tooltip(int p_tab, const String &p_tooltip) {
  762. ERR_FAIL_INDEX(p_tab, tabs.size());
  763. tabs.write[p_tab].tooltip = p_tooltip;
  764. queue_accessibility_update();
  765. }
  766. String TabBar::get_tab_tooltip(int p_tab) const {
  767. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  768. return tabs[p_tab].tooltip;
  769. }
  770. void TabBar::set_tab_text_direction(int p_tab, Control::TextDirection p_text_direction) {
  771. ERR_FAIL_INDEX(p_tab, tabs.size());
  772. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  773. if (tabs[p_tab].text_direction != p_text_direction) {
  774. tabs.write[p_tab].text_direction = p_text_direction;
  775. _shape(p_tab);
  776. queue_accessibility_update();
  777. queue_redraw();
  778. }
  779. }
  780. Control::TextDirection TabBar::get_tab_text_direction(int p_tab) const {
  781. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Control::TEXT_DIRECTION_INHERITED);
  782. return tabs[p_tab].text_direction;
  783. }
  784. void TabBar::set_tab_language(int p_tab, const String &p_language) {
  785. ERR_FAIL_INDEX(p_tab, tabs.size());
  786. if (tabs[p_tab].language != p_language) {
  787. tabs.write[p_tab].language = p_language;
  788. _shape(p_tab);
  789. _update_cache();
  790. _ensure_no_over_offset();
  791. if (scroll_to_selected) {
  792. ensure_tab_visible(current);
  793. }
  794. queue_accessibility_update();
  795. queue_redraw();
  796. update_minimum_size();
  797. }
  798. }
  799. String TabBar::get_tab_language(int p_tab) const {
  800. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  801. return tabs[p_tab].language;
  802. }
  803. void TabBar::set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  804. ERR_FAIL_INDEX(p_tab, tabs.size());
  805. if (tabs[p_tab].icon == p_icon) {
  806. return;
  807. }
  808. tabs.write[p_tab].icon = p_icon;
  809. _update_cache();
  810. _ensure_no_over_offset();
  811. if (scroll_to_selected) {
  812. ensure_tab_visible(current);
  813. }
  814. queue_redraw();
  815. update_minimum_size();
  816. }
  817. Ref<Texture2D> TabBar::get_tab_icon(int p_tab) const {
  818. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
  819. return tabs[p_tab].icon;
  820. }
  821. void TabBar::set_tab_icon_max_width(int p_tab, int p_width) {
  822. ERR_FAIL_INDEX(p_tab, tabs.size());
  823. if (tabs[p_tab].icon_max_width == p_width) {
  824. return;
  825. }
  826. tabs.write[p_tab].icon_max_width = p_width;
  827. _update_cache();
  828. _ensure_no_over_offset();
  829. if (scroll_to_selected) {
  830. ensure_tab_visible(current);
  831. }
  832. queue_redraw();
  833. update_minimum_size();
  834. }
  835. int TabBar::get_tab_icon_max_width(int p_tab) const {
  836. ERR_FAIL_INDEX_V(p_tab, tabs.size(), 0);
  837. return tabs[p_tab].icon_max_width;
  838. }
  839. void TabBar::set_tab_disabled(int p_tab, bool p_disabled) {
  840. ERR_FAIL_INDEX(p_tab, tabs.size());
  841. if (tabs[p_tab].disabled == p_disabled) {
  842. return;
  843. }
  844. tabs.write[p_tab].disabled = p_disabled;
  845. _update_cache();
  846. _ensure_no_over_offset();
  847. if (scroll_to_selected) {
  848. ensure_tab_visible(current);
  849. }
  850. queue_accessibility_update();
  851. queue_redraw();
  852. update_minimum_size();
  853. }
  854. bool TabBar::is_tab_disabled(int p_tab) const {
  855. ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
  856. return tabs[p_tab].disabled;
  857. }
  858. void TabBar::set_tab_hidden(int p_tab, bool p_hidden) {
  859. ERR_FAIL_INDEX(p_tab, tabs.size());
  860. if (tabs[p_tab].hidden == p_hidden) {
  861. return;
  862. }
  863. tabs.write[p_tab].hidden = p_hidden;
  864. _update_cache();
  865. _ensure_no_over_offset();
  866. if (scroll_to_selected) {
  867. ensure_tab_visible(current);
  868. }
  869. queue_accessibility_update();
  870. queue_redraw();
  871. update_minimum_size();
  872. }
  873. bool TabBar::is_tab_hidden(int p_tab) const {
  874. ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
  875. return tabs[p_tab].hidden;
  876. }
  877. void TabBar::set_tab_metadata(int p_tab, const Variant &p_metadata) {
  878. ERR_FAIL_INDEX(p_tab, tabs.size());
  879. tabs.write[p_tab].metadata = p_metadata;
  880. }
  881. Variant TabBar::get_tab_metadata(int p_tab) const {
  882. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Variant());
  883. return tabs[p_tab].metadata;
  884. }
  885. void TabBar::set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  886. ERR_FAIL_INDEX(p_tab, tabs.size());
  887. if (tabs[p_tab].right_button == p_icon) {
  888. return;
  889. }
  890. tabs.write[p_tab].right_button = p_icon;
  891. _update_cache();
  892. _ensure_no_over_offset();
  893. if (scroll_to_selected) {
  894. ensure_tab_visible(current);
  895. }
  896. queue_redraw();
  897. update_minimum_size();
  898. }
  899. Ref<Texture2D> TabBar::get_tab_button_icon(int p_tab) const {
  900. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
  901. return tabs[p_tab].right_button;
  902. }
  903. void TabBar::_update_hover() {
  904. if (!is_inside_tree()) {
  905. return;
  906. }
  907. ERR_FAIL_COND(tabs.is_empty());
  908. const Point2 &pos = get_local_mouse_position();
  909. // Test hovering to display right or close button.
  910. int hover_now = -1;
  911. int hover_buttons = -1;
  912. for (int i = offset; i <= max_drawn_tab; i++) {
  913. if (tabs[i].hidden) {
  914. continue;
  915. }
  916. Rect2 rect = get_tab_rect(i);
  917. if (rect.has_point(pos)) {
  918. hover_now = i;
  919. }
  920. if (tabs[i].rb_rect.has_point(pos)) {
  921. rb_hover = i;
  922. cb_hover = -1;
  923. hover_buttons = i;
  924. } else if (!tabs[i].disabled && tabs[i].cb_rect.has_point(pos)) {
  925. cb_hover = i;
  926. rb_hover = -1;
  927. hover_buttons = i;
  928. }
  929. if (hover_buttons != -1) {
  930. queue_redraw();
  931. break;
  932. }
  933. }
  934. if (hover != hover_now) {
  935. hover = hover_now;
  936. if (hover != -1) {
  937. emit_signal(SNAME("tab_hovered"), hover);
  938. }
  939. _update_cache();
  940. queue_redraw();
  941. }
  942. if (hover_buttons == -1) { // No hover.
  943. int rb_hover_old = rb_hover;
  944. int cb_hover_old = cb_hover;
  945. rb_hover = hover_buttons;
  946. cb_hover = hover_buttons;
  947. if (rb_hover != rb_hover_old || cb_hover != cb_hover_old) {
  948. queue_redraw();
  949. }
  950. }
  951. }
  952. void TabBar::_update_cache(bool p_update_hover) {
  953. if (tabs.is_empty()) {
  954. buttons_visible = false;
  955. return;
  956. }
  957. int limit = get_size().width;
  958. int limit_minus_buttons = limit - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  959. int w = 0;
  960. max_drawn_tab = tabs.size() - 1;
  961. for (int i = 0; i < tabs.size(); i++) {
  962. tabs.write[i].text_buf->set_width(-1);
  963. tabs.write[i].size_text = Math::ceil(tabs[i].text_buf->get_size().x);
  964. tabs.write[i].size_cache = get_tab_width(i);
  965. tabs.write[i].accessibility_item_dirty = true;
  966. tabs.write[i].truncated = max_width > 0 && tabs[i].size_cache > max_width;
  967. if (tabs[i].truncated) {
  968. int size_textless = tabs[i].size_cache - tabs[i].size_text;
  969. int mw = MAX(size_textless, max_width);
  970. tabs.write[i].size_text = MAX(mw - size_textless, 1);
  971. tabs.write[i].text_buf->set_width(tabs[i].size_text);
  972. tabs.write[i].size_cache = size_textless + tabs[i].size_text;
  973. }
  974. if (i < offset || i > max_drawn_tab) {
  975. tabs.write[i].ofs_cache = 0;
  976. continue;
  977. }
  978. tabs.write[i].ofs_cache = w;
  979. if (tabs[i].hidden) {
  980. continue;
  981. }
  982. w += tabs[i].size_cache;
  983. // Check if all tabs would fit inside the area.
  984. if (clip_tabs && i > offset && (w > limit || (offset > 0 && w > limit_minus_buttons))) {
  985. tabs.write[i].ofs_cache = 0;
  986. w -= tabs[i].size_cache;
  987. w -= theme_cache.tab_separation;
  988. max_drawn_tab = i - 1;
  989. while (w > limit_minus_buttons && max_drawn_tab > offset) {
  990. tabs.write[max_drawn_tab].ofs_cache = 0;
  991. if (!tabs[max_drawn_tab].hidden) {
  992. w -= tabs[max_drawn_tab].size_cache;
  993. w -= theme_cache.tab_separation;
  994. }
  995. max_drawn_tab--;
  996. }
  997. } else if (i < tabs.size() - 1) {
  998. // Only add the tab separation if this isn't the last tab drawn.
  999. w += theme_cache.tab_separation;
  1000. }
  1001. }
  1002. missing_right = max_drawn_tab < tabs.size() - 1;
  1003. buttons_visible = offset > 0 || missing_right;
  1004. if (tab_alignment == ALIGNMENT_LEFT) {
  1005. if (p_update_hover) {
  1006. _update_hover();
  1007. }
  1008. return;
  1009. }
  1010. if (tab_alignment == ALIGNMENT_CENTER) {
  1011. w = ((buttons_visible ? limit_minus_buttons : limit) - w) / 2;
  1012. } else if (tab_alignment == ALIGNMENT_RIGHT) {
  1013. w = (buttons_visible ? limit_minus_buttons : limit) - w;
  1014. }
  1015. for (int i = offset; i <= max_drawn_tab; i++) {
  1016. if (!tabs[i].hidden) {
  1017. tabs.write[i].ofs_cache = w;
  1018. w += tabs[i].size_cache;
  1019. w += theme_cache.tab_separation;
  1020. }
  1021. }
  1022. if (p_update_hover) {
  1023. _update_hover();
  1024. }
  1025. }
  1026. void TabBar::_on_mouse_exited() {
  1027. rb_hover = -1;
  1028. cb_hover = -1;
  1029. hover = -1;
  1030. highlight_arrow = -1;
  1031. dragging_valid_tab = false;
  1032. _update_cache(false);
  1033. queue_redraw();
  1034. }
  1035. void TabBar::add_tab(const String &p_str, const Ref<Texture2D> &p_icon) {
  1036. Tab t;
  1037. t.text = p_str;
  1038. t.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1039. t.icon = p_icon;
  1040. tabs.push_back(t);
  1041. _shape(tabs.size() - 1);
  1042. _update_cache();
  1043. if (scroll_to_selected) {
  1044. ensure_tab_visible(current);
  1045. }
  1046. queue_accessibility_update();
  1047. queue_redraw();
  1048. update_minimum_size();
  1049. if (!deselect_enabled && tabs.size() == 1) {
  1050. if (is_inside_tree()) {
  1051. set_current_tab(0);
  1052. } else {
  1053. current = 0;
  1054. previous = -1;
  1055. }
  1056. }
  1057. }
  1058. void TabBar::clear_tabs() {
  1059. if (tabs.is_empty()) {
  1060. return;
  1061. }
  1062. for (int i = 0; i < tabs.size(); i++) {
  1063. if (tabs[i].accessibility_item_element.is_valid()) {
  1064. DisplayServer::get_singleton()->accessibility_free_element(tabs.write[i].accessibility_item_element);
  1065. tabs.write[i].accessibility_item_element = RID();
  1066. }
  1067. }
  1068. tabs.clear();
  1069. offset = 0;
  1070. max_drawn_tab = 0;
  1071. current = -1;
  1072. previous = -1;
  1073. queue_accessibility_update();
  1074. queue_redraw();
  1075. update_minimum_size();
  1076. notify_property_list_changed();
  1077. }
  1078. void TabBar::remove_tab(int p_idx) {
  1079. ERR_FAIL_INDEX(p_idx, tabs.size());
  1080. if (tabs[p_idx].accessibility_item_element.is_valid()) {
  1081. DisplayServer::get_singleton()->accessibility_free_element(tabs.write[p_idx].accessibility_item_element);
  1082. tabs.write[p_idx].accessibility_item_element = RID();
  1083. }
  1084. tabs.remove_at(p_idx);
  1085. bool is_tab_changing = current == p_idx;
  1086. if (current >= p_idx && current > 0) {
  1087. current--;
  1088. }
  1089. if (previous >= p_idx && previous > 0) {
  1090. previous--;
  1091. }
  1092. if (tabs.is_empty()) {
  1093. offset = 0;
  1094. max_drawn_tab = 0;
  1095. current = -1;
  1096. previous = -1;
  1097. } else {
  1098. if (current != -1) {
  1099. // Try to change to a valid tab if possible (without firing the `tab_selected` signal).
  1100. for (int i = current; i < tabs.size(); i++) {
  1101. if (!is_tab_disabled(i) && !is_tab_hidden(i)) {
  1102. current = i;
  1103. break;
  1104. }
  1105. }
  1106. // If nothing, try backwards.
  1107. if (is_tab_disabled(current) || is_tab_hidden(current)) {
  1108. for (int i = current - 1; i >= 0; i--) {
  1109. if (!is_tab_disabled(i) && !is_tab_hidden(i)) {
  1110. current = i;
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. // If still no valid tab, deselect.
  1116. if (is_tab_disabled(current) || is_tab_hidden(current)) {
  1117. current = -1;
  1118. }
  1119. }
  1120. offset = MIN(offset, tabs.size() - 1);
  1121. max_drawn_tab = MIN(max_drawn_tab, tabs.size() - 1);
  1122. _update_cache();
  1123. _ensure_no_over_offset();
  1124. if (scroll_to_selected) {
  1125. ensure_tab_visible(current);
  1126. }
  1127. }
  1128. queue_accessibility_update();
  1129. queue_redraw();
  1130. update_minimum_size();
  1131. notify_property_list_changed();
  1132. if (is_tab_changing && is_inside_tree()) {
  1133. emit_signal(SNAME("tab_changed"), current);
  1134. }
  1135. }
  1136. Variant TabBar::get_drag_data(const Point2 &p_point) {
  1137. Variant drag_data = Control::get_drag_data(p_point);
  1138. if (drag_data != Variant()) {
  1139. return drag_data;
  1140. }
  1141. if (drag_to_rearrange_enabled) {
  1142. return _handle_get_drag_data("tab_bar_tab", p_point);
  1143. }
  1144. return Variant();
  1145. }
  1146. bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  1147. bool drop_override = Control::can_drop_data(p_point, p_data);
  1148. if (drop_override) {
  1149. return drop_override;
  1150. }
  1151. if (drag_to_rearrange_enabled) {
  1152. return _handle_can_drop_data("tab_bar_tab", p_point, p_data);
  1153. }
  1154. return false;
  1155. }
  1156. void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) {
  1157. Control::drop_data(p_point, p_data);
  1158. if (drag_to_rearrange_enabled) {
  1159. _handle_drop_data("tab_bar_tab", p_point, p_data, callable_mp(this, &TabBar::move_tab), callable_mp(this, &TabBar::_move_tab_from));
  1160. }
  1161. }
  1162. Variant TabBar::_handle_get_drag_data(const String &p_type, const Point2 &p_point) {
  1163. int tab_over = (p_point == Vector2(Math::INF, Math::INF)) ? current : get_tab_idx_at_point(p_point);
  1164. if (tab_over < 0) {
  1165. return Variant();
  1166. }
  1167. HBoxContainer *drag_preview = memnew(HBoxContainer);
  1168. if (tabs[tab_over].icon.is_valid()) {
  1169. const Size2 icon_size = _get_tab_icon_size(tab_over);
  1170. TextureRect *tf = memnew(TextureRect);
  1171. tf->set_texture(tabs[tab_over].icon);
  1172. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  1173. tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1174. tf->set_custom_minimum_size(icon_size);
  1175. drag_preview->add_child(tf);
  1176. }
  1177. Label *label = memnew(Label(get_tab_title(tab_over)));
  1178. label->set_auto_translate_mode(get_auto_translate_mode()); // Reflect how the title is displayed.
  1179. drag_preview->add_child(label);
  1180. set_drag_preview(drag_preview);
  1181. Dictionary drag_data;
  1182. drag_data["type"] = p_type;
  1183. drag_data["tab_index"] = tab_over;
  1184. drag_data["from_path"] = get_path();
  1185. return drag_data;
  1186. }
  1187. bool TabBar::_handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const {
  1188. Dictionary d = p_data;
  1189. if (!d.has("type")) {
  1190. return false;
  1191. }
  1192. if (String(d["type"]) == p_type) {
  1193. NodePath from_path = d["from_path"];
  1194. NodePath to_path = get_path();
  1195. if (from_path == to_path) {
  1196. return true;
  1197. } else if (get_tabs_rearrange_group() != -1) {
  1198. // Drag and drop between other TabBars.
  1199. Node *from_node = get_node(from_path);
  1200. TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
  1201. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  1202. return true;
  1203. }
  1204. }
  1205. }
  1206. return false;
  1207. }
  1208. 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) {
  1209. Dictionary d = p_data;
  1210. if (!d.has("type")) {
  1211. return;
  1212. }
  1213. if (String(d["type"]) == p_type) {
  1214. int tab_from_id = d["tab_index"];
  1215. int hover_now = (p_point == Vector2(Math::INF, Math::INF)) ? current : get_closest_tab_idx_to_point(p_point);
  1216. NodePath from_path = d["from_path"];
  1217. NodePath to_path = get_path();
  1218. if (from_path == to_path) {
  1219. if (tab_from_id == hover_now) {
  1220. return;
  1221. }
  1222. // Drop the new tab to the left or right depending on where the target tab is being hovered.
  1223. if (hover_now != -1) {
  1224. Rect2 tab_rect = get_tab_rect(hover_now);
  1225. if (is_layout_rtl() ^ (p_point.x <= tab_rect.position.x + tab_rect.size.width / 2)) {
  1226. if (hover_now > tab_from_id) {
  1227. hover_now -= 1;
  1228. }
  1229. } else if (tab_from_id > hover_now) {
  1230. hover_now += 1;
  1231. }
  1232. } else {
  1233. int x = tabs.is_empty() ? 0 : get_tab_rect(0).position.x;
  1234. hover_now = is_layout_rtl() ^ (p_point.x < x) ? 0 : get_tab_count() - 1;
  1235. }
  1236. p_move_tab_callback.call(tab_from_id, hover_now);
  1237. if (!is_tab_disabled(hover_now)) {
  1238. emit_signal(SNAME("active_tab_rearranged"), hover_now);
  1239. set_current_tab(hover_now);
  1240. }
  1241. } else if (get_tabs_rearrange_group() != -1) {
  1242. // Drag and drop between Tabs.
  1243. Node *from_node = get_node(from_path);
  1244. TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
  1245. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  1246. if (tab_from_id >= from_tabs->get_tab_count()) {
  1247. return;
  1248. }
  1249. // Drop the new tab to the left or right depending on where the target tab is being hovered.
  1250. if (hover_now != -1) {
  1251. Rect2 tab_rect = get_tab_rect(hover_now);
  1252. if (is_layout_rtl() ^ (p_point.x > tab_rect.position.x + tab_rect.size.width / 2)) {
  1253. hover_now += 1;
  1254. }
  1255. } else {
  1256. hover_now = tabs.is_empty() || (is_layout_rtl() ^ (p_point.x < get_tab_rect(0).position.x)) ? 0 : get_tab_count();
  1257. }
  1258. p_move_tab_from_other_callback.call(from_tabs, tab_from_id, hover_now);
  1259. }
  1260. }
  1261. }
  1262. }
  1263. void TabBar::_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index) {
  1264. Tab moving_tab = p_from_tabbar->tabs[p_from_index];
  1265. moving_tab.accessibility_item_element = RID();
  1266. moving_tab.accessibility_item_dirty = true;
  1267. p_from_tabbar->remove_tab(p_from_index);
  1268. tabs.insert(p_to_index, moving_tab);
  1269. if (tabs.size() > 1) {
  1270. if (current >= p_to_index) {
  1271. current++;
  1272. }
  1273. if (previous >= p_to_index) {
  1274. previous++;
  1275. }
  1276. }
  1277. if (!is_tab_disabled(p_to_index)) {
  1278. set_current_tab(p_to_index);
  1279. } else {
  1280. _update_cache();
  1281. queue_redraw();
  1282. }
  1283. queue_accessibility_update();
  1284. update_minimum_size();
  1285. }
  1286. int TabBar::get_tab_idx_at_point(const Point2 &p_point) const {
  1287. if (tabs.is_empty()) {
  1288. return -1;
  1289. }
  1290. int hover_now = -1;
  1291. for (int i = offset; i <= max_drawn_tab; i++) {
  1292. if (!tabs[i].hidden) {
  1293. Rect2 rect = get_tab_rect(i);
  1294. if (rect.has_point(p_point)) {
  1295. hover_now = i;
  1296. }
  1297. }
  1298. }
  1299. return hover_now;
  1300. }
  1301. int TabBar::get_closest_tab_idx_to_point(const Point2 &p_point) const {
  1302. if (tabs.is_empty()) {
  1303. return -1;
  1304. }
  1305. int closest_tab = get_tab_idx_at_point(p_point);
  1306. float closest_distance = FLT_MAX;
  1307. // Search along the x-axis since the TabBar is horizontal.
  1308. if (closest_tab == -1) {
  1309. for (int i = offset; i <= max_drawn_tab; i++) {
  1310. if (!tabs[i].hidden) {
  1311. float center = get_tab_rect(i).get_center().x;
  1312. float distance = Math::abs(center - p_point.x);
  1313. if (distance < closest_distance) {
  1314. closest_distance = distance;
  1315. closest_tab = i;
  1316. }
  1317. }
  1318. }
  1319. }
  1320. return closest_tab;
  1321. }
  1322. void TabBar::set_tab_alignment(AlignmentMode p_alignment) {
  1323. ERR_FAIL_INDEX(p_alignment, ALIGNMENT_MAX);
  1324. if (tab_alignment == p_alignment) {
  1325. return;
  1326. }
  1327. tab_alignment = p_alignment;
  1328. _update_cache();
  1329. queue_redraw();
  1330. }
  1331. TabBar::AlignmentMode TabBar::get_tab_alignment() const {
  1332. return tab_alignment;
  1333. }
  1334. void TabBar::set_clip_tabs(bool p_clip_tabs) {
  1335. if (clip_tabs == p_clip_tabs) {
  1336. return;
  1337. }
  1338. clip_tabs = p_clip_tabs;
  1339. if (!clip_tabs) {
  1340. offset = 0;
  1341. max_drawn_tab = 0;
  1342. }
  1343. _update_cache();
  1344. if (scroll_to_selected) {
  1345. ensure_tab_visible(current);
  1346. }
  1347. queue_redraw();
  1348. update_minimum_size();
  1349. }
  1350. bool TabBar::get_clip_tabs() const {
  1351. return clip_tabs;
  1352. }
  1353. void TabBar::set_tab_style_v_flip(bool p_tab_style_v_flip) {
  1354. tab_style_v_flip = p_tab_style_v_flip;
  1355. }
  1356. void TabBar::move_tab(int p_from, int p_to) {
  1357. if (p_from == p_to) {
  1358. return;
  1359. }
  1360. ERR_FAIL_INDEX(p_from, tabs.size());
  1361. ERR_FAIL_INDEX(p_to, tabs.size());
  1362. Tab tab_from = tabs[p_from];
  1363. tab_from.accessibility_item_dirty = true;
  1364. tabs.remove_at(p_from);
  1365. tabs.insert(p_to, tab_from);
  1366. if (current == p_from) {
  1367. current = p_to;
  1368. } else if (current > p_from && current <= p_to) {
  1369. current--;
  1370. } else if (current < p_from && current >= p_to) {
  1371. current++;
  1372. }
  1373. if (previous == p_from) {
  1374. previous = p_to;
  1375. } else if (previous > p_from && previous <= p_to) {
  1376. previous--;
  1377. } else if (previous < p_from && previous >= p_to) {
  1378. previous++;
  1379. }
  1380. _update_cache();
  1381. _ensure_no_over_offset();
  1382. if (scroll_to_selected) {
  1383. ensure_tab_visible(current);
  1384. }
  1385. queue_accessibility_update();
  1386. queue_redraw();
  1387. notify_property_list_changed();
  1388. }
  1389. int TabBar::get_tab_width(int p_idx) const {
  1390. ERR_FAIL_INDEX_V(p_idx, tabs.size(), 0);
  1391. Ref<StyleBox> style;
  1392. if (tabs[p_idx].disabled) {
  1393. style = theme_cache.tab_disabled_style;
  1394. } else if (current == p_idx) {
  1395. style = theme_cache.tab_selected_style;
  1396. // Always pick the widest style between hovered and unselected, to avoid an infinite loop when switching tabs with the mouse.
  1397. } else if (theme_cache.tab_hovered_style->get_minimum_size().width > theme_cache.tab_unselected_style->get_minimum_size().width) {
  1398. style = theme_cache.tab_hovered_style;
  1399. } else {
  1400. style = theme_cache.tab_unselected_style;
  1401. }
  1402. int x = style->get_minimum_size().width;
  1403. if (tabs[p_idx].icon.is_valid()) {
  1404. const Size2 icon_size = _get_tab_icon_size(p_idx);
  1405. x += icon_size.width + theme_cache.h_separation;
  1406. }
  1407. if (!tabs[p_idx].text.is_empty()) {
  1408. x += tabs[p_idx].size_text + theme_cache.h_separation;
  1409. }
  1410. bool close_visible = cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx == current);
  1411. if (tabs[p_idx].right_button.is_valid()) {
  1412. Ref<StyleBox> btn_style = theme_cache.button_hl_style;
  1413. Ref<Texture2D> rb = tabs[p_idx].right_button;
  1414. if (close_visible) {
  1415. x += btn_style->get_minimum_size().width + rb->get_width();
  1416. } else {
  1417. x += btn_style->get_margin(SIDE_LEFT) + rb->get_width() + theme_cache.h_separation;
  1418. }
  1419. }
  1420. if (close_visible) {
  1421. Ref<StyleBox> btn_style = theme_cache.button_hl_style;
  1422. Ref<Texture2D> cb = theme_cache.close_icon;
  1423. x += btn_style->get_margin(SIDE_LEFT) + cb->get_width() + theme_cache.h_separation;
  1424. }
  1425. if (x > style->get_minimum_size().width) {
  1426. x -= theme_cache.h_separation;
  1427. }
  1428. return x;
  1429. }
  1430. Size2 TabBar::_get_tab_icon_size(int p_index) const {
  1431. ERR_FAIL_INDEX_V(p_index, tabs.size(), Size2());
  1432. const TabBar::Tab &tab = tabs[p_index];
  1433. Size2 icon_size = tab.icon->get_size();
  1434. int icon_max_width = 0;
  1435. if (theme_cache.icon_max_width > 0) {
  1436. icon_max_width = theme_cache.icon_max_width;
  1437. }
  1438. if (tab.icon_max_width > 0 && (icon_max_width == 0 || tab.icon_max_width < icon_max_width)) {
  1439. icon_max_width = tab.icon_max_width;
  1440. }
  1441. if (icon_max_width > 0 && icon_size.width > icon_max_width) {
  1442. icon_size.height = icon_size.height * icon_max_width / icon_size.width;
  1443. icon_size.width = icon_max_width;
  1444. }
  1445. return icon_size;
  1446. }
  1447. void TabBar::_ensure_no_over_offset() {
  1448. if (!is_inside_tree() || !buttons_visible) {
  1449. return;
  1450. }
  1451. int limit_minus_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  1452. int prev_offset = offset;
  1453. int total_w = tabs[max_drawn_tab].ofs_cache + tabs[max_drawn_tab].size_cache - tabs[offset].ofs_cache;
  1454. for (int i = offset; i > 0; i--) {
  1455. if (tabs[i - 1].hidden) {
  1456. continue;
  1457. }
  1458. total_w += tabs[i - 1].size_cache;
  1459. if (total_w < limit_minus_buttons) {
  1460. offset--;
  1461. } else {
  1462. break;
  1463. }
  1464. }
  1465. if (prev_offset != offset) {
  1466. _update_cache();
  1467. queue_redraw();
  1468. }
  1469. }
  1470. bool TabBar::_can_deselect() const {
  1471. if (deselect_enabled) {
  1472. return true;
  1473. }
  1474. // All tabs must be disabled or hidden.
  1475. for (const Tab &tab : tabs) {
  1476. if (!tab.disabled && !tab.hidden) {
  1477. return false;
  1478. }
  1479. }
  1480. return true;
  1481. }
  1482. void TabBar::ensure_tab_visible(int p_idx) {
  1483. if (p_idx == -1 || !is_inside_tree() || !buttons_visible) {
  1484. return;
  1485. }
  1486. ERR_FAIL_INDEX(p_idx, tabs.size());
  1487. if (tabs[p_idx].hidden || (p_idx >= offset && p_idx <= max_drawn_tab)) {
  1488. return;
  1489. }
  1490. if (p_idx < offset) {
  1491. offset = p_idx;
  1492. _update_cache();
  1493. queue_redraw();
  1494. return;
  1495. }
  1496. int limit_minus_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  1497. int total_w = tabs[max_drawn_tab].ofs_cache - tabs[offset].ofs_cache;
  1498. for (int i = max_drawn_tab; i <= p_idx; i++) {
  1499. if (tabs[i].hidden) {
  1500. continue;
  1501. }
  1502. total_w += tabs[i].size_cache;
  1503. }
  1504. int prev_offset = offset;
  1505. for (int i = offset; i < p_idx; i++) {
  1506. if (tabs[i].hidden) {
  1507. continue;
  1508. }
  1509. if (total_w > limit_minus_buttons) {
  1510. total_w -= tabs[i].size_cache;
  1511. offset++;
  1512. } else {
  1513. break;
  1514. }
  1515. }
  1516. if (prev_offset != offset) {
  1517. _update_cache();
  1518. queue_redraw();
  1519. }
  1520. }
  1521. Rect2 TabBar::get_tab_rect(int p_tab) const {
  1522. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Rect2());
  1523. if (is_layout_rtl()) {
  1524. return Rect2(get_size().width - tabs[p_tab].ofs_cache - tabs[p_tab].size_cache, 0, tabs[p_tab].size_cache, get_size().height);
  1525. } else {
  1526. return Rect2(tabs[p_tab].ofs_cache, 0, tabs[p_tab].size_cache, get_size().height);
  1527. }
  1528. }
  1529. void TabBar::set_close_with_middle_mouse(bool p_scroll_close) {
  1530. close_with_middle_mouse = p_scroll_close;
  1531. }
  1532. bool TabBar::get_close_with_middle_mouse() const {
  1533. return close_with_middle_mouse;
  1534. }
  1535. void TabBar::set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy) {
  1536. ERR_FAIL_INDEX(p_policy, CLOSE_BUTTON_MAX);
  1537. if (cb_displaypolicy == p_policy) {
  1538. return;
  1539. }
  1540. cb_displaypolicy = p_policy;
  1541. _update_cache();
  1542. _ensure_no_over_offset();
  1543. if (scroll_to_selected) {
  1544. ensure_tab_visible(current);
  1545. }
  1546. queue_redraw();
  1547. update_minimum_size();
  1548. }
  1549. TabBar::CloseButtonDisplayPolicy TabBar::get_tab_close_display_policy() const {
  1550. return cb_displaypolicy;
  1551. }
  1552. void TabBar::set_max_tab_width(int p_width) {
  1553. ERR_FAIL_COND(p_width < 0);
  1554. if (max_width == p_width) {
  1555. return;
  1556. }
  1557. max_width = p_width;
  1558. _update_cache();
  1559. _ensure_no_over_offset();
  1560. if (scroll_to_selected) {
  1561. ensure_tab_visible(current);
  1562. }
  1563. queue_redraw();
  1564. update_minimum_size();
  1565. }
  1566. int TabBar::get_max_tab_width() const {
  1567. return max_width;
  1568. }
  1569. void TabBar::set_scrolling_enabled(bool p_enabled) {
  1570. scrolling_enabled = p_enabled;
  1571. }
  1572. bool TabBar::get_scrolling_enabled() const {
  1573. return scrolling_enabled;
  1574. }
  1575. void TabBar::set_drag_to_rearrange_enabled(bool p_enabled) {
  1576. drag_to_rearrange_enabled = p_enabled;
  1577. }
  1578. bool TabBar::get_drag_to_rearrange_enabled() const {
  1579. return drag_to_rearrange_enabled;
  1580. }
  1581. void TabBar::set_tabs_rearrange_group(int p_group_id) {
  1582. tabs_rearrange_group = p_group_id;
  1583. }
  1584. int TabBar::get_tabs_rearrange_group() const {
  1585. return tabs_rearrange_group;
  1586. }
  1587. void TabBar::set_scroll_to_selected(bool p_enabled) {
  1588. scroll_to_selected = p_enabled;
  1589. if (p_enabled) {
  1590. ensure_tab_visible(current);
  1591. }
  1592. }
  1593. bool TabBar::get_scroll_to_selected() const {
  1594. return scroll_to_selected;
  1595. }
  1596. void TabBar::set_select_with_rmb(bool p_enabled) {
  1597. select_with_rmb = p_enabled;
  1598. }
  1599. bool TabBar::get_select_with_rmb() const {
  1600. return select_with_rmb;
  1601. }
  1602. void TabBar::set_deselect_enabled(bool p_enabled) {
  1603. if (deselect_enabled == p_enabled) {
  1604. return;
  1605. }
  1606. deselect_enabled = p_enabled;
  1607. if (!deselect_enabled && current == -1 && !tabs.is_empty()) {
  1608. select_next_available();
  1609. }
  1610. }
  1611. bool TabBar::get_deselect_enabled() const {
  1612. return deselect_enabled;
  1613. }
  1614. void TabBar::_bind_methods() {
  1615. ClassDB::bind_method(D_METHOD("set_tab_count", "count"), &TabBar::set_tab_count);
  1616. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabBar::get_tab_count);
  1617. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabBar::set_current_tab);
  1618. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabBar::get_current_tab);
  1619. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabBar::get_previous_tab);
  1620. ClassDB::bind_method(D_METHOD("select_previous_available"), &TabBar::select_previous_available);
  1621. ClassDB::bind_method(D_METHOD("select_next_available"), &TabBar::select_next_available);
  1622. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabBar::set_tab_title);
  1623. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabBar::get_tab_title);
  1624. ClassDB::bind_method(D_METHOD("set_tab_tooltip", "tab_idx", "tooltip"), &TabBar::set_tab_tooltip);
  1625. ClassDB::bind_method(D_METHOD("get_tab_tooltip", "tab_idx"), &TabBar::get_tab_tooltip);
  1626. ClassDB::bind_method(D_METHOD("set_tab_text_direction", "tab_idx", "direction"), &TabBar::set_tab_text_direction);
  1627. ClassDB::bind_method(D_METHOD("get_tab_text_direction", "tab_idx"), &TabBar::get_tab_text_direction);
  1628. ClassDB::bind_method(D_METHOD("set_tab_language", "tab_idx", "language"), &TabBar::set_tab_language);
  1629. ClassDB::bind_method(D_METHOD("get_tab_language", "tab_idx"), &TabBar::get_tab_language);
  1630. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabBar::set_tab_icon);
  1631. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabBar::get_tab_icon);
  1632. ClassDB::bind_method(D_METHOD("set_tab_icon_max_width", "tab_idx", "width"), &TabBar::set_tab_icon_max_width);
  1633. ClassDB::bind_method(D_METHOD("get_tab_icon_max_width", "tab_idx"), &TabBar::get_tab_icon_max_width);
  1634. ClassDB::bind_method(D_METHOD("set_tab_button_icon", "tab_idx", "icon"), &TabBar::set_tab_button_icon);
  1635. ClassDB::bind_method(D_METHOD("get_tab_button_icon", "tab_idx"), &TabBar::get_tab_button_icon);
  1636. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabBar::set_tab_disabled);
  1637. ClassDB::bind_method(D_METHOD("is_tab_disabled", "tab_idx"), &TabBar::is_tab_disabled);
  1638. ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabBar::set_tab_hidden);
  1639. ClassDB::bind_method(D_METHOD("is_tab_hidden", "tab_idx"), &TabBar::is_tab_hidden);
  1640. ClassDB::bind_method(D_METHOD("set_tab_metadata", "tab_idx", "metadata"), &TabBar::set_tab_metadata);
  1641. ClassDB::bind_method(D_METHOD("get_tab_metadata", "tab_idx"), &TabBar::get_tab_metadata);
  1642. ClassDB::bind_method(D_METHOD("remove_tab", "tab_idx"), &TabBar::remove_tab);
  1643. ClassDB::bind_method(D_METHOD("add_tab", "title", "icon"), &TabBar::add_tab, DEFVAL(""), DEFVAL(Ref<Texture2D>()));
  1644. ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabBar::get_tab_idx_at_point);
  1645. ClassDB::bind_method(D_METHOD("set_tab_alignment", "alignment"), &TabBar::set_tab_alignment);
  1646. ClassDB::bind_method(D_METHOD("get_tab_alignment"), &TabBar::get_tab_alignment);
  1647. ClassDB::bind_method(D_METHOD("set_clip_tabs", "clip_tabs"), &TabBar::set_clip_tabs);
  1648. ClassDB::bind_method(D_METHOD("get_clip_tabs"), &TabBar::get_clip_tabs);
  1649. ClassDB::bind_method(D_METHOD("get_tab_offset"), &TabBar::get_tab_offset);
  1650. ClassDB::bind_method(D_METHOD("get_offset_buttons_visible"), &TabBar::get_offset_buttons_visible);
  1651. ClassDB::bind_method(D_METHOD("ensure_tab_visible", "idx"), &TabBar::ensure_tab_visible);
  1652. ClassDB::bind_method(D_METHOD("get_tab_rect", "tab_idx"), &TabBar::get_tab_rect);
  1653. ClassDB::bind_method(D_METHOD("move_tab", "from", "to"), &TabBar::move_tab);
  1654. ClassDB::bind_method(D_METHOD("set_close_with_middle_mouse", "enabled"), &TabBar::set_close_with_middle_mouse);
  1655. ClassDB::bind_method(D_METHOD("get_close_with_middle_mouse"), &TabBar::get_close_with_middle_mouse);
  1656. ClassDB::bind_method(D_METHOD("set_tab_close_display_policy", "policy"), &TabBar::set_tab_close_display_policy);
  1657. ClassDB::bind_method(D_METHOD("get_tab_close_display_policy"), &TabBar::get_tab_close_display_policy);
  1658. ClassDB::bind_method(D_METHOD("set_max_tab_width", "width"), &TabBar::set_max_tab_width);
  1659. ClassDB::bind_method(D_METHOD("get_max_tab_width"), &TabBar::get_max_tab_width);
  1660. ClassDB::bind_method(D_METHOD("set_scrolling_enabled", "enabled"), &TabBar::set_scrolling_enabled);
  1661. ClassDB::bind_method(D_METHOD("get_scrolling_enabled"), &TabBar::get_scrolling_enabled);
  1662. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabBar::set_drag_to_rearrange_enabled);
  1663. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabBar::get_drag_to_rearrange_enabled);
  1664. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabBar::set_tabs_rearrange_group);
  1665. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabBar::get_tabs_rearrange_group);
  1666. ClassDB::bind_method(D_METHOD("set_scroll_to_selected", "enabled"), &TabBar::set_scroll_to_selected);
  1667. ClassDB::bind_method(D_METHOD("get_scroll_to_selected"), &TabBar::get_scroll_to_selected);
  1668. ClassDB::bind_method(D_METHOD("set_select_with_rmb", "enabled"), &TabBar::set_select_with_rmb);
  1669. ClassDB::bind_method(D_METHOD("get_select_with_rmb"), &TabBar::get_select_with_rmb);
  1670. ClassDB::bind_method(D_METHOD("set_deselect_enabled", "enabled"), &TabBar::set_deselect_enabled);
  1671. ClassDB::bind_method(D_METHOD("get_deselect_enabled"), &TabBar::get_deselect_enabled);
  1672. ClassDB::bind_method(D_METHOD("clear_tabs"), &TabBar::clear_tabs);
  1673. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  1674. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  1675. ADD_SIGNAL(MethodInfo("tab_clicked", PropertyInfo(Variant::INT, "tab")));
  1676. ADD_SIGNAL(MethodInfo("tab_rmb_clicked", PropertyInfo(Variant::INT, "tab")));
  1677. ADD_SIGNAL(MethodInfo("tab_close_pressed", PropertyInfo(Variant::INT, "tab")));
  1678. ADD_SIGNAL(MethodInfo("tab_button_pressed", PropertyInfo(Variant::INT, "tab")));
  1679. ADD_SIGNAL(MethodInfo("tab_hovered", PropertyInfo(Variant::INT, "tab")));
  1680. ADD_SIGNAL(MethodInfo("active_tab_rearranged", PropertyInfo(Variant::INT, "idx_to")));
  1681. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1"), "set_current_tab", "get_current_tab");
  1682. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_alignment", "get_tab_alignment");
  1683. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_tabs"), "set_clip_tabs", "get_clip_tabs");
  1684. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "close_with_middle_mouse"), "set_close_with_middle_mouse", "get_close_with_middle_mouse");
  1685. 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");
  1686. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_tab_width", PROPERTY_HINT_RANGE, "0,99999,1,suffix:px"), "set_max_tab_width", "get_max_tab_width");
  1687. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrolling_enabled"), "set_scrolling_enabled", "get_scrolling_enabled");
  1688. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  1689. ADD_PROPERTY(PropertyInfo(Variant::INT, "tabs_rearrange_group"), "set_tabs_rearrange_group", "get_tabs_rearrange_group");
  1690. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_to_selected"), "set_scroll_to_selected", "get_scroll_to_selected");
  1691. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_with_rmb"), "set_select_with_rmb", "get_select_with_rmb");
  1692. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_enabled"), "set_deselect_enabled", "get_deselect_enabled");
  1693. ADD_ARRAY_COUNT("Tabs", "tab_count", "set_tab_count", "get_tab_count", "tab_");
  1694. BIND_ENUM_CONSTANT(ALIGNMENT_LEFT);
  1695. BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
  1696. BIND_ENUM_CONSTANT(ALIGNMENT_RIGHT);
  1697. BIND_ENUM_CONSTANT(ALIGNMENT_MAX);
  1698. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_NEVER);
  1699. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ACTIVE_ONLY);
  1700. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ALWAYS);
  1701. BIND_ENUM_CONSTANT(CLOSE_BUTTON_MAX);
  1702. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, h_separation);
  1703. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, tab_separation);
  1704. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, icon_max_width);
  1705. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_unselected_style, "tab_unselected");
  1706. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_hovered_style, "tab_hovered");
  1707. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_selected_style, "tab_selected");
  1708. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_disabled_style, "tab_disabled");
  1709. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_focus_style, "tab_focus");
  1710. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, increment_icon, "increment");
  1711. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, increment_hl_icon, "increment_highlight");
  1712. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, decrement_icon, "decrement");
  1713. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, decrement_hl_icon, "decrement_highlight");
  1714. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, drop_mark_icon, "drop_mark");
  1715. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, drop_mark_color);
  1716. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_selected_color);
  1717. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_hovered_color);
  1718. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_unselected_color);
  1719. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_disabled_color);
  1720. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_outline_color);
  1721. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, TabBar, font);
  1722. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, TabBar, font_size);
  1723. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, outline_size);
  1724. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, close_icon, "close");
  1725. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, button_pressed_style, "button_pressed");
  1726. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, button_hl_style, "button_highlight");
  1727. Tab defaults(true);
  1728. base_property_helper.set_prefix("tab_");
  1729. base_property_helper.set_array_length_getter(&TabBar::get_tab_count);
  1730. base_property_helper.register_property(PropertyInfo(Variant::STRING, "title"), defaults.text, &TabBar::set_tab_title, &TabBar::get_tab_title);
  1731. base_property_helper.register_property(PropertyInfo(Variant::STRING, "tooltip"), defaults.tooltip, &TabBar::set_tab_tooltip, &TabBar::get_tab_tooltip);
  1732. base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &TabBar::set_tab_icon, &TabBar::get_tab_icon);
  1733. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &TabBar::set_tab_disabled, &TabBar::is_tab_disabled);
  1734. PropertyListHelper::register_base_helper(&base_property_helper);
  1735. }
  1736. TabBar::TabBar() {
  1737. set_focus_mode(FOCUS_ACCESSIBILITY);
  1738. set_size(Size2(get_size().width, get_minimum_size().height));
  1739. set_focus_mode(FOCUS_ALL);
  1740. connect(SceneStringName(mouse_exited), callable_mp(this, &TabBar::_on_mouse_exited));
  1741. property_helper.setup_for_instance(base_property_helper, this);
  1742. }