tab_bar.cpp 64 KB

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