tab_bar.cpp 68 KB

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