popup_menu.cpp 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509
  1. /**************************************************************************/
  2. /* popup_menu.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 "popup_menu.h"
  31. #include "popup_menu.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/input/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "scene/gui/graph_element.h"
  37. #include "scene/gui/menu_bar.h"
  38. #include "scene/gui/panel_container.h"
  39. #include "scene/main/timer.h"
  40. #include "scene/resources/style_box_flat.h"
  41. #include "scene/theme/theme_db.h"
  42. HashMap<NativeMenu::SystemMenus, PopupMenu *> PopupMenu::system_menus;
  43. bool PopupMenu::_set_item_accelerator(int p_index, const Ref<InputEventKey> &p_ie) {
  44. NativeMenu *nmenu = NativeMenu::get_singleton();
  45. if (p_ie->get_physical_keycode() == Key::NONE && p_ie->get_keycode() == Key::NONE && p_ie->get_key_label() != Key::NONE) {
  46. nmenu->set_item_accelerator(global_menu, p_index, p_ie->get_key_label_with_modifiers());
  47. return true;
  48. } else if (p_ie->get_keycode() != Key::NONE) {
  49. nmenu->set_item_accelerator(global_menu, p_index, p_ie->get_keycode_with_modifiers());
  50. return true;
  51. } else if (p_ie->get_physical_keycode() != Key::NONE) {
  52. nmenu->set_item_accelerator(global_menu, p_index, DisplayServer::get_singleton()->keyboard_get_keycode_from_physical(p_ie->get_physical_keycode_with_modifiers()));
  53. return true;
  54. }
  55. return false;
  56. }
  57. void PopupMenu::_set_item_checkable_type(int p_index, int p_checkable_type) {
  58. switch (p_checkable_type) {
  59. case Item::CHECKABLE_TYPE_NONE: {
  60. set_item_as_checkable(p_index, false);
  61. } break;
  62. case Item::CHECKABLE_TYPE_CHECK_BOX: {
  63. set_item_as_checkable(p_index, true);
  64. } break;
  65. case Item::CHECKABLE_TYPE_RADIO_BUTTON: {
  66. set_item_as_radio_checkable(p_index, true);
  67. } break;
  68. }
  69. }
  70. int PopupMenu::_get_item_checkable_type(int p_index) const {
  71. ERR_FAIL_INDEX_V(p_index, items.size(), Item::CHECKABLE_TYPE_NONE);
  72. return items[p_index].checkable_type;
  73. }
  74. RID PopupMenu::bind_global_menu() {
  75. #ifdef TOOLS_ENABLED
  76. if (is_part_of_edited_scene()) {
  77. return RID();
  78. }
  79. #endif
  80. if (!NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_POPUP_MENU)) {
  81. return RID();
  82. }
  83. if (global_menu.is_valid()) {
  84. return global_menu; // Already bound;
  85. }
  86. NativeMenu *nmenu = NativeMenu::get_singleton();
  87. if (system_menu_id != NativeMenu::INVALID_MENU_ID && nmenu->has_system_menu(system_menu_id)) {
  88. if (system_menus.has(system_menu_id)) {
  89. WARN_PRINT(vformat("Attempting to bind PopupMenu to the system menu %s, but another menu is already bound to it. This menu: %s, current menu: %s", nmenu->get_system_menu_name(system_menu_id), get_description(), system_menus[system_menu_id]->get_description()));
  90. global_menu = nmenu->create_menu();
  91. } else {
  92. system_menus[system_menu_id] = this;
  93. system_menu = nmenu->get_system_menu(system_menu_id);
  94. global_menu = system_menu;
  95. }
  96. } else {
  97. global_menu = nmenu->create_menu();
  98. }
  99. nmenu->set_interface_direction(global_menu, control->is_layout_rtl());
  100. nmenu->set_popup_open_callback(global_menu, callable_mp(this, &PopupMenu::_about_to_popup));
  101. nmenu->set_popup_close_callback(global_menu, callable_mp(this, &PopupMenu::_about_to_close));
  102. for (int i = 0; i < items.size(); i++) {
  103. Item &item = items.write[i];
  104. if (item.separator) {
  105. nmenu->add_separator(global_menu);
  106. } else {
  107. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), item.shortcut_is_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), i);
  108. if (item.submenu) {
  109. RID submenu_rid = item.submenu->bind_global_menu();
  110. nmenu->set_item_submenu(global_menu, index, submenu_rid);
  111. item.submenu_bound = true;
  112. if (item.submenu->is_connected("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden))) {
  113. item.submenu->disconnect("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden));
  114. }
  115. }
  116. if (item.checkable_type == Item::CHECKABLE_TYPE_CHECK_BOX) {
  117. nmenu->set_item_checkable(global_menu, index, true);
  118. } else if (item.checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON) {
  119. nmenu->set_item_radio_checkable(global_menu, index, true);
  120. }
  121. nmenu->set_item_checked(global_menu, index, item.checked);
  122. nmenu->set_item_disabled(global_menu, index, item.disabled);
  123. nmenu->set_item_max_states(global_menu, index, item.max_states);
  124. nmenu->set_item_icon(global_menu, index, item.icon);
  125. nmenu->set_item_state(global_menu, index, item.state);
  126. nmenu->set_item_indentation_level(global_menu, index, item.indent);
  127. nmenu->set_item_tooltip(global_menu, index, item.tooltip);
  128. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  129. Array events = item.shortcut->get_events();
  130. for (const Variant &v : events) {
  131. Ref<InputEventKey> ie = v;
  132. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  133. break;
  134. }
  135. }
  136. } else if (item.accel != Key::NONE) {
  137. nmenu->set_item_accelerator(global_menu, index, item.accel);
  138. }
  139. }
  140. }
  141. return global_menu;
  142. }
  143. void PopupMenu::unbind_global_menu() {
  144. if (global_menu.is_null()) {
  145. return;
  146. }
  147. if (global_menu == system_menu && system_menus[system_menu_id] == this) {
  148. system_menus.erase(system_menu_id);
  149. }
  150. for (int i = 0; i < items.size(); i++) {
  151. Item &item = items.write[i];
  152. if (item.submenu) {
  153. item.submenu->unbind_global_menu();
  154. item.submenu_bound = false;
  155. }
  156. }
  157. if (system_menu != global_menu) {
  158. NativeMenu::get_singleton()->free_menu(global_menu);
  159. } else {
  160. NativeMenu::get_singleton()->clear(global_menu);
  161. }
  162. system_menu = RID();
  163. global_menu = RID();
  164. }
  165. bool PopupMenu::is_system_menu() const {
  166. return (global_menu == system_menu) && (system_menu_id != NativeMenu::INVALID_MENU_ID);
  167. }
  168. void PopupMenu::set_system_menu(NativeMenu::SystemMenus p_system_menu_id) {
  169. if (is_inside_tree() && system_menu_id != NativeMenu::INVALID_MENU_ID) {
  170. unbind_global_menu();
  171. }
  172. system_menu_id = p_system_menu_id;
  173. if (is_inside_tree() && system_menu_id != NativeMenu::INVALID_MENU_ID) {
  174. bind_global_menu();
  175. }
  176. }
  177. NativeMenu::SystemMenus PopupMenu::get_system_menu() const {
  178. return system_menu_id;
  179. }
  180. String PopupMenu::_get_accel_text(const Item &p_item) const {
  181. if (p_item.shortcut.is_valid()) {
  182. return p_item.shortcut->get_as_text();
  183. } else if (p_item.accel != Key::NONE) {
  184. return keycode_get_string(p_item.accel);
  185. }
  186. return String();
  187. }
  188. Size2 PopupMenu::_get_item_icon_size(int p_idx) const {
  189. const PopupMenu::Item &item = items[p_idx];
  190. Size2 icon_size = item.get_icon_size();
  191. int max_width = 0;
  192. if (theme_cache.icon_max_width > 0) {
  193. max_width = theme_cache.icon_max_width;
  194. }
  195. if (item.icon_max_width > 0 && (max_width == 0 || item.icon_max_width < max_width)) {
  196. max_width = item.icon_max_width;
  197. }
  198. if (max_width > 0 && icon_size.width > max_width) {
  199. icon_size.height = icon_size.height * max_width / icon_size.width;
  200. icon_size.width = max_width;
  201. }
  202. return icon_size;
  203. }
  204. Size2 PopupMenu::_get_contents_minimum_size() const {
  205. Size2 minsize = theme_cache.panel_style->get_minimum_size();
  206. minsize.width += scroll_container->get_v_scroll_bar()->get_size().width;
  207. // Take shadows into account.
  208. minsize.width += panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT);
  209. minsize.height += panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM);
  210. real_t body_max_w = 0.0; // Indentation, text, and submenu arrow.
  211. real_t icon_max_w = 0.0;
  212. real_t accel_max_w = 0.0;
  213. bool has_check_gutter = false;
  214. bool gutter_compact = theme_cache.gutter_compact;
  215. for (int i = 0; i < items.size(); i++) {
  216. _shape_item(i);
  217. icon_max_w = MAX(_get_item_icon_size(i).width, icon_max_w);
  218. if (items[i].checkable_type && !items[i].separator) {
  219. has_check_gutter = true;
  220. if (items[i].icon.is_valid()) {
  221. gutter_compact = false;
  222. }
  223. }
  224. if (items[i].accel != Key::NONE || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) {
  225. real_t accel_w = theme_cache.h_separation * 2 + items[i].accel_text_buf->get_size().x;
  226. accel_max_w = MAX(accel_w, accel_max_w);
  227. }
  228. real_t body_w = items[i].indent * theme_cache.indent + items[i].text_buf->get_size().x;
  229. if (items[i].submenu) {
  230. body_w += theme_cache.submenu->get_width();
  231. }
  232. body_max_w = MAX(body_max_w, body_w);
  233. minsize.height += _get_item_height(i) + theme_cache.v_separation;
  234. }
  235. minsize.width += theme_cache.item_start_padding + body_max_w + accel_max_w + theme_cache.item_end_padding;
  236. const int check_w = MAX(theme_cache.checked->get_width(), theme_cache.radio_checked->get_width());
  237. if (gutter_compact) {
  238. minsize.width += MAX(icon_max_w, check_w) + theme_cache.h_separation;
  239. } else {
  240. if (icon_max_w > 0) {
  241. minsize.width += icon_max_w + theme_cache.h_separation;
  242. }
  243. if (has_check_gutter) {
  244. minsize.width += check_w + theme_cache.h_separation;
  245. }
  246. }
  247. if (is_inside_tree()) {
  248. int height_limit = get_usable_parent_rect().size.height;
  249. if (minsize.height > height_limit) {
  250. minsize.height = height_limit;
  251. }
  252. }
  253. minsize.height = Math::ceil(minsize.height); // Ensures enough height at fractional content scales to prevent the v_scroll_bar from showing.
  254. return minsize;
  255. }
  256. int PopupMenu::_get_item_height(int p_idx) const {
  257. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  258. Size2 icon_size = _get_item_icon_size(p_idx);
  259. int icon_height = icon_size.height;
  260. if (items[p_idx].checkable_type && !items[p_idx].separator) {
  261. icon_height = MAX(icon_height, MAX(theme_cache.checked->get_height(), theme_cache.radio_checked->get_height()));
  262. }
  263. int text_height = items[p_idx].text_buf->get_size().height;
  264. if (text_height == 0 && !items[p_idx].separator) {
  265. text_height = theme_cache.font->get_height(theme_cache.font_size);
  266. }
  267. int separator_height = 0;
  268. if (items[p_idx].separator) {
  269. separator_height = MAX(theme_cache.separator_style->get_minimum_size().height, MAX(theme_cache.labeled_separator_left->get_minimum_size().height, theme_cache.labeled_separator_right->get_minimum_size().height));
  270. }
  271. return MAX(separator_height, MAX(text_height, icon_height));
  272. }
  273. int PopupMenu::_get_items_total_height() const {
  274. // Get total height of all items by taking max of icon height and font height
  275. int items_total_height = 0;
  276. for (int i = 0; i < items.size(); i++) {
  277. items_total_height += _get_item_height(i) + theme_cache.v_separation;
  278. }
  279. return items_total_height;
  280. }
  281. int PopupMenu::_get_mouse_over(const Point2 &p_over) const {
  282. // Transform to scroll_container local coordinates.
  283. const Point2 scaled_pos = p_over / get_content_scale_factor();
  284. const Point2 over_scroll_container =
  285. scroll_container->get_global_transform_with_canvas().xform_inv(scaled_pos);
  286. // Check if point is inside the item control as clipped by scroll_container.
  287. const Rect2 scroll_container_rect = Rect2(Point2(), scroll_container->get_size());
  288. const Rect2 bounding_rect = scroll_container_rect.intersection(control->get_rect());
  289. if (!bounding_rect.has_point(over_scroll_container)) {
  290. return -1;
  291. }
  292. // Perform item hit check in control node local space,
  293. // so we don't need to worry about any of the container theming.
  294. const float over_control_y = control->get_transform().xform_inv(over_scroll_container).y;
  295. float bottom_edge = 0;
  296. for (int i = 0; i < items.size(); i++) {
  297. bottom_edge += theme_cache.v_separation;
  298. bottom_edge += _get_item_height(i);
  299. if (bottom_edge > over_control_y) {
  300. return i;
  301. }
  302. }
  303. return -1;
  304. }
  305. void PopupMenu::_activate_submenu(int p_over, bool p_by_keyboard) {
  306. ERR_FAIL_INDEX_MSG(p_over, items.size(), vformat("Invalid submenu index %d in _activate_submenu.", p_over));
  307. PopupMenu *submenu_popup = items[p_over].submenu;
  308. if (submenu_popup->is_visible()) {
  309. WARN_VERBOSE(vformat("_activate_submenu should not be called on an open submenu - index: %d.", p_over));
  310. return;
  311. }
  312. submenu_popup->this_submenu_index = p_over;
  313. active_submenu_index = p_over;
  314. submenu_popup->get_window()->set_exclusive(false); // Ensure mouse inputs to parent menu are not inhibited by the submenu in exclusive mode.
  315. const float win_scale = get_content_scale_factor();
  316. const Point2 this_pos = get_position();
  317. Rect2 this_rect = Rect2(this_pos, panel->get_size());
  318. submenu_popup->reset_size(); // Shrink the popup size to its contents.
  319. const Size2 submenu_size = submenu_popup->get_size();
  320. // Calculate the submenu's position.
  321. Point2 submenu_pos = Point2(0, 0);
  322. Rect2i screen_rect = is_embedded() ? Rect2i(get_embedder()->get_visible_rect()) : get_parent_rect();
  323. active_submenu_target_line.clear();
  324. panel_offset_start = Point2(panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP)) * win_scale;
  325. const Point2 panel_offset_end = Point2(-panel->get_offset(SIDE_RIGHT), -panel->get_offset(SIDE_BOTTOM)) * win_scale;
  326. const Vector2 scaled_this_size = this_rect.size * win_scale;
  327. const float scaled_theme_v_separation = theme_cache.v_separation * win_scale;
  328. const float scroll_offset = control->get_position().y;
  329. const float scaled_ofs_cache = items[p_over]._ofs_cache * win_scale;
  330. const float scaled_height_cache = items[p_over]._height_cache * win_scale;
  331. if (is_layout_rtl()) {
  332. is_active_submenu_left = true;
  333. submenu_pos += this_pos + Point2(-submenu_size.width + panel_offset_end.x, scaled_ofs_cache + scroll_offset - int(scaled_theme_v_separation * 0.5) + panel_offset_start.y);
  334. if (submenu_pos.x < screen_rect.position.x) {
  335. submenu_pos.x = this_pos.x + this_rect.size.width - panel_offset_start.x;
  336. is_active_submenu_left = false;
  337. }
  338. } else {
  339. is_active_submenu_left = false;
  340. submenu_pos += this_pos + Point2(scaled_this_size.x + panel_offset_start.x, scaled_ofs_cache + scroll_offset - int(scaled_theme_v_separation * 0.5) + panel_offset_start.y);
  341. if (submenu_pos.x + submenu_size.width > screen_rect.position.x + screen_rect.size.width) {
  342. submenu_pos.x = this_pos.x - submenu_size.width + panel_offset_end.x;
  343. is_active_submenu_left = true;
  344. }
  345. }
  346. submenu_popup->set_position(submenu_pos);
  347. submenu_popup->activated_by_keyboard = p_by_keyboard;
  348. // If not triggered by the mouse, start the popup with its first enabled item focused.
  349. if (p_by_keyboard) {
  350. for (int i = 0; i < submenu_popup->get_item_count(); i++) {
  351. if (!submenu_popup->is_item_disabled(i)) {
  352. submenu_popup->set_focused_item(i);
  353. break;
  354. }
  355. }
  356. }
  357. submenu_popup->popup();
  358. // The autohide areas are set on the submenu, but are aligned over the parent menu,
  359. // so we spoof `this_rect` position as the negative relative offset of the parent from the submenu.
  360. this_rect.position = -(submenu_popup->get_position() - this_pos);
  361. const Rect2 safe_area(get_position(), get_size());
  362. Viewport *vp = submenu_popup->get_embedder();
  363. if (vp) {
  364. vp->subwindow_set_popup_safe_rect(submenu_popup, safe_area);
  365. } else {
  366. DisplayServer::get_singleton()->window_set_popup_safe_rect(submenu_popup->get_window_id(), safe_area);
  367. }
  368. // Set the mouse movement target line at the top and bottom points of the submenu vertical side abutting the parent menu.
  369. if (is_active_submenu_left) {
  370. active_submenu_target_line.push_back(Point2(submenu_popup->get_position().x + submenu_popup->get_size().x, submenu_popup->get_position().y));
  371. } else {
  372. active_submenu_target_line.push_back(submenu_popup->get_position());
  373. }
  374. active_submenu_target_line.push_back(Point2(active_submenu_target_line[0].x, active_submenu_target_line[0].y + submenu_popup->get_size().y));
  375. submenu_popup->clear_autohide_areas();
  376. // Add an autohide area above the submenu item unless it's the top item.
  377. // This avoids a narrow strip of area that can trigger the submenu to reload when reentering the parent item from the top.
  378. const int y_to_item_top = scaled_ofs_cache + scroll_offset - int(scaled_theme_v_separation * 0.5) + theme_cache.panel_style->get_margin(SIDE_TOP) * win_scale;
  379. Rect2 top_rect = Rect2(this_rect.position.x, this_rect.position.y, scaled_this_size.width, y_to_item_top);
  380. if (active_submenu_index != 0) {
  381. submenu_popup->add_autohide_area(top_rect);
  382. }
  383. // If there is an area below the submenu item, add an autohide area there unless it's the last item.
  384. if (active_submenu_index != items.size() - 1) {
  385. const int y_to_item_bottom = y_to_item_top + scaled_height_cache + scaled_theme_v_separation;
  386. submenu_popup->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y + y_to_item_bottom, scaled_this_size.x, scaled_this_size.y - y_to_item_bottom));
  387. }
  388. queue_accessibility_update();
  389. control->queue_redraw();
  390. }
  391. void PopupMenu::_parent_focused() {
  392. if (is_embedded()) {
  393. Point2 mouse_pos_adjusted;
  394. Window *window_parent = Object::cast_to<Window>(get_parent()->get_viewport());
  395. while (window_parent) {
  396. if (!window_parent->is_embedded()) {
  397. mouse_pos_adjusted += window_parent->get_position();
  398. break;
  399. }
  400. window_parent = Object::cast_to<Window>(window_parent->get_parent()->get_viewport());
  401. }
  402. Rect2 safe_area = get_embedder()->subwindow_get_popup_safe_rect(this);
  403. Point2 pos = DisplayServer::get_singleton()->mouse_get_position() - mouse_pos_adjusted;
  404. if (safe_area == Rect2i() || !safe_area.has_point(pos)) {
  405. Popup::_parent_focused();
  406. } else {
  407. grab_focus();
  408. }
  409. }
  410. }
  411. void PopupMenu::_submenu_timeout() {
  412. if (mouse_over == submenu_over) {
  413. _activate_submenu(mouse_over);
  414. }
  415. }
  416. void PopupMenu::_input_from_window(const Ref<InputEvent> &p_event) {
  417. if (p_event.is_valid()) {
  418. _input_from_window_internal(p_event);
  419. } else {
  420. WARN_PRINT_ONCE("PopupMenu has received an invalid InputEvent. Consider filtering out invalid events.");
  421. }
  422. Popup::_input_from_window(p_event);
  423. }
  424. void PopupMenu::_input_from_window_internal(const Ref<InputEvent> &p_event) {
  425. if (!items.is_empty()) {
  426. Input *input = Input::get_singleton();
  427. Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
  428. Ref<InputEventJoypadButton> joypadbutton_event = p_event;
  429. bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
  430. if (is_joypad_event && !has_focus_or_active_popup()) {
  431. return;
  432. }
  433. if (p_event->is_action("ui_down", true) && p_event->is_pressed()) {
  434. if (is_joypad_event) {
  435. if (!input->is_action_just_pressed_by_event("ui_down", p_event, true)) {
  436. return;
  437. }
  438. joypad_event_process = true;
  439. set_process_internal(true);
  440. }
  441. int search_from = mouse_over + 1;
  442. if (search_from >= items.size()) {
  443. search_from = 0;
  444. }
  445. bool match_found = false;
  446. for (int i = search_from; i < items.size(); i++) {
  447. if (!items[i].separator && !items[i].disabled) {
  448. prev_mouse_over = mouse_over;
  449. mouse_over = i;
  450. emit_signal(SNAME("id_focused"), items[i].id);
  451. scroll_to_item(i);
  452. queue_accessibility_update();
  453. control->queue_redraw();
  454. set_input_as_handled();
  455. match_found = true;
  456. break;
  457. }
  458. }
  459. if (!match_found) {
  460. // If the last item is not selectable, try re-searching from the start.
  461. for (int i = 0; i < search_from; i++) {
  462. if (!items[i].separator && !items[i].disabled) {
  463. prev_mouse_over = mouse_over;
  464. mouse_over = i;
  465. emit_signal(SNAME("id_focused"), items[i].id);
  466. scroll_to_item(i);
  467. queue_accessibility_update();
  468. control->queue_redraw();
  469. set_input_as_handled();
  470. break;
  471. }
  472. }
  473. }
  474. } else if (p_event->is_action("ui_up", true) && p_event->is_pressed()) {
  475. if (is_joypad_event) {
  476. if (!input->is_action_just_pressed_by_event("ui_up", p_event, true)) {
  477. return;
  478. }
  479. joypad_event_process = true;
  480. set_process_internal(true);
  481. }
  482. int search_from = mouse_over - 1;
  483. if (search_from < 0) {
  484. search_from = items.size() - 1;
  485. }
  486. bool match_found = false;
  487. for (int i = search_from; i >= 0; i--) {
  488. if (!items[i].separator && !items[i].disabled) {
  489. prev_mouse_over = mouse_over;
  490. mouse_over = i;
  491. emit_signal(SNAME("id_focused"), items[i].id);
  492. scroll_to_item(i);
  493. queue_accessibility_update();
  494. control->queue_redraw();
  495. set_input_as_handled();
  496. match_found = true;
  497. break;
  498. }
  499. }
  500. if (!match_found) {
  501. // If the first item is not selectable, try re-searching from the end.
  502. for (int i = items.size() - 1; i >= search_from; i--) {
  503. if (!items[i].separator && !items[i].disabled) {
  504. prev_mouse_over = mouse_over;
  505. mouse_over = i;
  506. emit_signal(SNAME("id_focused"), items[i].id);
  507. scroll_to_item(i);
  508. queue_accessibility_update();
  509. control->queue_redraw();
  510. set_input_as_handled();
  511. break;
  512. }
  513. }
  514. }
  515. } else if (p_event->is_action("ui_left", true) && p_event->is_pressed()) {
  516. Node *n = get_parent();
  517. if (n) {
  518. if (PopupMenu *parent_popup = Object::cast_to<PopupMenu>(n)) {
  519. parent_popup->activated_by_keyboard = true;
  520. hide();
  521. set_input_as_handled();
  522. } else if (Object::cast_to<MenuBar>(n)) {
  523. Object::cast_to<MenuBar>(n)->gui_input(p_event);
  524. set_input_as_handled();
  525. return;
  526. }
  527. }
  528. } else if (p_event->is_action("ui_right", true) && p_event->is_pressed()) {
  529. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && items[mouse_over].submenu && submenu_over != mouse_over) {
  530. _activate_submenu(mouse_over, true);
  531. set_input_as_handled();
  532. } else {
  533. Node *n = get_parent();
  534. if (n && Object::cast_to<MenuBar>(n)) {
  535. Object::cast_to<MenuBar>(n)->gui_input(p_event);
  536. set_input_as_handled();
  537. return;
  538. }
  539. }
  540. } else if (p_event->is_action("ui_accept", true) && p_event->is_pressed()) {
  541. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) {
  542. if (items[mouse_over].submenu && submenu_over != mouse_over) {
  543. _activate_submenu(mouse_over, true);
  544. } else {
  545. activate_item(mouse_over);
  546. }
  547. set_input_as_handled();
  548. }
  549. }
  550. }
  551. // Make the item area exclude shadows and the vertical margins and scrollbar.
  552. Rect2 item_clickable_area = panel->get_global_rect();
  553. if (scroll_container->get_v_scroll_bar()->is_visible_in_tree()) {
  554. int scroll_width = scroll_container->get_v_scroll_bar()->get_size().width;
  555. if (is_layout_rtl()) {
  556. item_clickable_area.position.x += scroll_width;
  557. item_clickable_area.size.width -= scroll_width;
  558. }
  559. item_clickable_area.size.width -= scroll_width;
  560. }
  561. float win_scale = get_content_scale_factor();
  562. item_clickable_area.position.x += theme_cache.panel_style->get_margin(SIDE_LEFT);
  563. item_clickable_area.position.y += theme_cache.panel_style->get_margin(SIDE_TOP);
  564. item_clickable_area.position *= win_scale;
  565. item_clickable_area.size.width -= theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.panel_style->get_margin(SIDE_RIGHT);
  566. item_clickable_area.size.y -= theme_cache.panel_style->get_margin(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_BOTTOM);
  567. item_clickable_area.size *= win_scale;
  568. Ref<InputEventMouseButton> b = p_event;
  569. if (b.is_valid()) {
  570. MouseButton button_idx = b->get_button_index();
  571. // Activate the item on release of either the left mouse button or
  572. // any mouse button held down when the popup was opened.
  573. // This allows for opening the popup and triggering an action in a single mouse click.
  574. if (button_idx == MouseButton::LEFT || initial_button_mask.has_flag(mouse_button_to_mask(button_idx))) {
  575. if (b->is_pressed()) {
  576. during_grabbed_click = false;
  577. is_scrolling = is_layout_rtl() ? b->get_position().x < item_clickable_area.position.x - item_clickable_area.size.width : b->get_position().x > item_clickable_area.size.width + item_clickable_area.position.x;
  578. // Hide it if the shadows have been clicked.
  579. if (get_flag(FLAG_POPUP)) {
  580. Rect2 panel_area = panel->get_global_rect();
  581. panel_area.position *= win_scale;
  582. panel_area.size *= win_scale;
  583. if (!panel_area.has_point(b->get_position())) {
  584. _close_pressed();
  585. return;
  586. }
  587. }
  588. if (!item_clickable_area.has_point(b->get_position())) {
  589. return;
  590. }
  591. int over = _get_mouse_over(b->get_position());
  592. if (over < 0 || items[over].separator || items[over].disabled || (items[over].submenu && items[over].submenu->is_visible())) {
  593. return;
  594. } else {
  595. _mouse_over_update(b->get_position());
  596. }
  597. } else {
  598. if (is_scrolling) {
  599. is_scrolling = false;
  600. return;
  601. }
  602. bool was_during_grabbed_click = during_grabbed_click;
  603. during_grabbed_click = false;
  604. initial_button_mask.clear();
  605. if (!item_clickable_area.has_point(b->get_position())) {
  606. return;
  607. }
  608. // Disable clicks under a time threshold to avoid selection right when opening the popup.
  609. if (was_during_grabbed_click && OS::get_singleton()->get_ticks_msec() - popup_time_msec < 400) {
  610. return;
  611. }
  612. int over = _get_mouse_over(b->get_position());
  613. if (over < 0) {
  614. if (!was_during_grabbed_click) {
  615. hide();
  616. }
  617. return;
  618. }
  619. if (items[over].separator || items[over].disabled) {
  620. return;
  621. }
  622. if (items[over].submenu) {
  623. if (!items[over].submenu->is_visible()) {
  624. _activate_submenu(over);
  625. }
  626. return;
  627. }
  628. activate_item(over);
  629. }
  630. }
  631. }
  632. Ref<InputEventMouseMotion> m = p_event;
  633. if (m.is_valid()) {
  634. if (m->get_velocity().is_zero_approx() || m->get_relative() == Vector2(0, 0)) {
  635. return;
  636. }
  637. activated_by_keyboard = false;
  638. if (this_submenu_index != -1) { // Is a submenu.
  639. PopupMenu *parent_popup = Object::cast_to<PopupMenu>(get_parent());
  640. ERR_FAIL_NULL(parent_popup);
  641. Point2 areas_mouse_pos = get_mouse_position() - parent_popup->panel_offset_start;
  642. for (const Rect2 &E : autohide_areas) {
  643. if (!scroll_container->get_global_rect().has_point(m->get_position()) && E.has_point(areas_mouse_pos)) {
  644. _close_or_suspend();
  645. return;
  646. }
  647. }
  648. }
  649. if (this_submenu_index != -1) { // Is a submenu.
  650. // The mouse left the safe area, but came back again, so cancel the auto-closing.
  651. if (PopupMenu *parent_popup = Object::cast_to<PopupMenu>(get_parent())) {
  652. if (!parent_popup->close_suspended_timer->is_stopped()) {
  653. parent_popup->close_suspended_timer->stop();
  654. }
  655. }
  656. }
  657. if (mouse_over == -1 && !item_clickable_area.has_point(m->get_position())) {
  658. Vector2 m_relative = m->get_relative();
  659. if (is_embedded() && m_relative != Vector2()) { // Only if the mouse has moved relative.
  660. if (PopupMenu *parent_popup = Object::cast_to<PopupMenu>(get_parent())) { // Is a submenu.
  661. parent_popup->last_submenu_mouse_position = m->get_position();
  662. } else {
  663. last_submenu_mouse_position = m->get_position();
  664. }
  665. }
  666. return;
  667. }
  668. _mouse_over_update(m->get_position());
  669. }
  670. Ref<InputEventKey> k = p_event;
  671. if (allow_search && k.is_valid() && k->get_unicode() && k->is_pressed()) {
  672. uint64_t now = OS::get_singleton()->get_ticks_msec();
  673. uint64_t diff = now - search_time_msec;
  674. uint64_t max_interval = uint64_t(GLOBAL_GET_CACHED(uint64_t, "gui/timers/incremental_search_max_interval_msec"));
  675. search_time_msec = now;
  676. if (diff > max_interval) {
  677. search_string = "";
  678. }
  679. if (String::chr(k->get_unicode()) != search_string) {
  680. search_string += String::chr(k->get_unicode());
  681. }
  682. for (int i = mouse_over + 1; i <= items.size(); i++) {
  683. if (i == items.size()) {
  684. if (mouse_over <= 0) {
  685. break;
  686. } else {
  687. i = 0;
  688. }
  689. }
  690. if (i == mouse_over) {
  691. break;
  692. }
  693. if (items[i].text.findn(search_string) == 0) {
  694. prev_mouse_over = mouse_over;
  695. mouse_over = i;
  696. emit_signal(SNAME("id_focused"), items[i].id);
  697. scroll_to_item(i);
  698. queue_accessibility_update();
  699. control->queue_redraw();
  700. set_input_as_handled();
  701. break;
  702. }
  703. }
  704. }
  705. }
  706. bool PopupMenu::_is_mouse_moving_toward_submenu(const Vector2 &p_relative, bool p_is_submenu_left, const Vector2 &p_mouse_position, const Vector<Point2> &p_active_submenu_target_line) const {
  707. Vector2 top_target = (p_active_submenu_target_line[0] - p_mouse_position).rotated(p_is_submenu_left ? -Math::PI * 0.5 : Math::PI * 0.5);
  708. Vector2 bottom_target = (p_active_submenu_target_line[1] - p_mouse_position).rotated(p_is_submenu_left ? Math::PI * 0.5 : -Math::PI * 0.5);
  709. // The top_target vector is perpendicular to the vector between the mouse position and the top point of the submenu target line.
  710. // The dot product is > 0 if the relative vector is +/- 90 degrees of the top_vector.
  711. // The bottom_target vector is perpendicular to the vector between the mouse position and the bottom point of the submenu target line,
  712. // but pointing in the opposite direction of the top_target vector. Thus, the intersection of top and bottom test semicircles is the area
  713. // in which the relative vector is deemed to be moving toward the submenu.
  714. // Without normalization, the comparison is correct by testing only the sign, but would not work against a range of +/- 1.
  715. return bottom_target.dot(p_relative) > 0 && top_target.dot(p_relative) > 0;
  716. }
  717. void PopupMenu::_mouse_over_update(const Point2 &p_over) {
  718. int over_index = _get_mouse_over(p_over);
  719. int id = (over_index < 0 || items[over_index].separator || items[over_index].disabled) ? -1 : (items[over_index].id >= 0 ? items[over_index].id : over_index);
  720. if (id < 0) {
  721. mouse_over = -1;
  722. submenu_over = -1;
  723. submenu_timer->stop();
  724. queue_accessibility_update();
  725. control->queue_redraw();
  726. return;
  727. }
  728. if (items[over_index].submenu) {
  729. if (items[over_index].submenu->is_visible()) {
  730. if (submenu_over == over_index) {
  731. last_submenu_mouse_position = is_embedded() ? p_over : Point2(DisplayServer::get_singleton()->mouse_get_position() - get_position());
  732. }
  733. if (items[over_index].submenu->active_submenu_index != -1) { // Close any secondary popup open on the visible submenu when the mouse is on the parent item.
  734. PopupMenu *secondary_popup = Object::cast_to<PopupMenu>(items[over_index].submenu);
  735. ERR_FAIL_INDEX_MSG(secondary_popup->active_submenu_index, secondary_popup->items.size(), vformat("Invalid active_submenu_index index %d in _mouse_over_update.", secondary_popup->active_submenu_index));
  736. secondary_popup->items[secondary_popup->active_submenu_index].submenu->_close_pressed();
  737. }
  738. } else if (!is_scrolling && submenu_over != over_index && !close_was_suspended && active_submenu_index == -1) {
  739. submenu_timer->start();
  740. submenu_over = over_index;
  741. }
  742. } else {
  743. submenu_timer->stop();
  744. submenu_over = -1;
  745. }
  746. if (over_index != mouse_over) {
  747. mouse_over = over_index;
  748. queue_accessibility_update();
  749. control->queue_redraw();
  750. }
  751. }
  752. void PopupMenu::_draw_items() {
  753. control->set_custom_minimum_size(Size2(0, _get_items_total_height()));
  754. RID ci = control->get_canvas_item();
  755. // Space between the item content and the sides of popup menu.
  756. bool rtl = control->is_layout_rtl();
  757. // In Item::checkable_type enum order (less the non-checkable member), with disabled repeated at the end.
  758. Ref<Texture2D> check[] = { theme_cache.checked, theme_cache.radio_checked, theme_cache.checked_disabled, theme_cache.radio_checked_disabled };
  759. Ref<Texture2D> uncheck[] = { theme_cache.unchecked, theme_cache.radio_unchecked, theme_cache.unchecked_disabled, theme_cache.radio_unchecked_disabled };
  760. Ref<Texture2D> submenu;
  761. if (rtl) {
  762. submenu = theme_cache.submenu_mirrored;
  763. } else {
  764. submenu = theme_cache.submenu;
  765. }
  766. float display_width = control->get_size().width;
  767. // Find the widest icon and whether any items have a checkbox, and store the offsets for each.
  768. real_t icon_max_w = 0.0;
  769. real_t check_max_w = 0.0;
  770. bool has_check_gutter = false;
  771. bool gutter_compact = theme_cache.gutter_compact;
  772. for (int i = 0; i < items.size(); i++) {
  773. if (items[i].separator) {
  774. continue;
  775. }
  776. icon_max_w = MAX(_get_item_icon_size(i).width, icon_max_w);
  777. if (items[i].checkable_type) {
  778. has_check_gutter = true;
  779. if (items[i].icon.is_valid()) {
  780. gutter_compact = false;
  781. }
  782. }
  783. }
  784. if (has_check_gutter) {
  785. for (int i = 0; i < 4; i++) {
  786. check_max_w = MAX(check_max_w, check[i]->get_width());
  787. check_max_w = MAX(check_max_w, uncheck[i]->get_width());
  788. }
  789. }
  790. Point2 ofs;
  791. // Loop through all items and draw each.
  792. for (int i = 0; i < items.size(); i++) {
  793. // For the first item only add half a separation. For all other items, add a whole separation to the offset.
  794. ofs.y += i > 0 ? theme_cache.v_separation : theme_cache.v_separation / 2;
  795. _shape_item(i);
  796. Point2 item_ofs = ofs;
  797. Size2 icon_size = _get_item_icon_size(i);
  798. float h = _get_item_height(i);
  799. if ((active_submenu_index == -1 && i == mouse_over) || i == active_submenu_index) {
  800. theme_cache.hover_style->draw(ci, Rect2(item_ofs + Point2(0, -theme_cache.v_separation / 2), Size2(display_width, h + theme_cache.v_separation)));
  801. }
  802. String text = items[i].xl_text;
  803. // Separator
  804. item_ofs.x += items[i].indent * theme_cache.indent;
  805. if (items[i].separator) {
  806. if (!text.is_empty() || items[i].icon.is_valid()) {
  807. int content_size = items[i].text_buf->get_size().width + theme_cache.h_separation * 2;
  808. if (items[i].icon.is_valid()) {
  809. content_size += icon_size.width + theme_cache.h_separation;
  810. }
  811. int content_center = display_width / 2;
  812. int content_left = content_center - content_size / 2;
  813. int content_right = content_center + content_size / 2;
  814. if (content_left > item_ofs.x) {
  815. int sep_h = theme_cache.labeled_separator_left->get_minimum_size().height;
  816. int sep_ofs = Math::floor((h - sep_h) / 2.0);
  817. theme_cache.labeled_separator_left->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(MAX(0, content_left - item_ofs.x), sep_h)));
  818. }
  819. if (content_right < display_width) {
  820. int sep_h = theme_cache.labeled_separator_right->get_minimum_size().height;
  821. int sep_ofs = Math::floor((h - sep_h) / 2.0);
  822. theme_cache.labeled_separator_right->draw(ci, Rect2(Point2(content_right, item_ofs.y + sep_ofs), Size2(MAX(0, display_width - content_right), sep_h)));
  823. }
  824. } else {
  825. int sep_h = theme_cache.separator_style->get_minimum_size().height;
  826. int sep_ofs = Math::floor((h - sep_h) / 2.0);
  827. theme_cache.separator_style->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(display_width, sep_h)));
  828. }
  829. }
  830. Color icon_color = items[i].icon_modulate;
  831. // For non-separator items, add some padding for the content.
  832. if (!items[i].separator) {
  833. item_ofs.x += theme_cache.item_start_padding;
  834. }
  835. // Checkboxes
  836. if (items[i].checkable_type && !items[i].separator) {
  837. int disabled = int(items[i].disabled) * 2;
  838. Texture2D *icon = (items[i].checked ? check[items[i].checkable_type - 1 + disabled] : uncheck[items[i].checkable_type - 1 + disabled]).ptr();
  839. if (rtl) {
  840. icon->draw(ci, Size2(control->get_size().width - item_ofs.x - icon->get_width(), item_ofs.y) + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
  841. } else {
  842. icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
  843. }
  844. }
  845. int separator_ofs = (display_width - items[i].text_buf->get_size().width) / 2;
  846. // Icon
  847. if (items[i].icon.is_valid()) {
  848. const Point2 icon_offset = Point2(0, Math::floor((h - icon_size.height) / 2.0));
  849. Point2 icon_pos;
  850. if (items[i].separator) {
  851. separator_ofs -= (icon_size.width + theme_cache.h_separation) / 2;
  852. if (rtl) {
  853. icon_pos = Size2(control->get_size().width - item_ofs.x - separator_ofs - icon_size.width, item_ofs.y);
  854. } else {
  855. icon_pos = item_ofs + Size2(separator_ofs, 0);
  856. separator_ofs += icon_size.width + theme_cache.h_separation;
  857. }
  858. } else {
  859. const real_t check_w = (gutter_compact || !has_check_gutter) ? 0 : (check_max_w + theme_cache.h_separation);
  860. if (rtl) {
  861. icon_pos = Size2(control->get_size().width - item_ofs.x - check_w - icon_size.width, item_ofs.y);
  862. } else {
  863. icon_pos = item_ofs + Size2(check_w, 0);
  864. }
  865. }
  866. items[i].icon->draw_rect(ci, Rect2(icon_pos + icon_offset, icon_size), false, icon_color);
  867. }
  868. // Submenu arrow on right hand side.
  869. if (items[i].submenu) {
  870. if (rtl) {
  871. submenu->draw(ci, Point2(theme_cache.item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
  872. } else {
  873. submenu->draw(ci, Point2(display_width - submenu->get_width() - theme_cache.item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
  874. }
  875. }
  876. // Text
  877. if (items[i].separator) {
  878. if (!text.is_empty()) {
  879. Vector2 text_pos = Point2(separator_ofs, item_ofs.y + Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  880. if (theme_cache.font_separator_outline_size > 0 && theme_cache.font_separator_outline_color.a > 0) {
  881. items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_separator_outline_size, theme_cache.font_separator_outline_color);
  882. }
  883. items[i].text_buf->draw(ci, text_pos, theme_cache.font_separator_color);
  884. }
  885. } else {
  886. if (gutter_compact) {
  887. item_ofs.x += MAX(icon_max_w, check_max_w) + theme_cache.h_separation;
  888. } else {
  889. if (icon_max_w > 0) {
  890. item_ofs.x += icon_max_w + theme_cache.h_separation;
  891. }
  892. if (has_check_gutter) {
  893. item_ofs.x += check_max_w + theme_cache.h_separation;
  894. }
  895. }
  896. if (rtl) {
  897. Vector2 text_pos = Size2(control->get_size().width - items[i].text_buf->get_size().width - item_ofs.x, item_ofs.y) + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  898. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  899. items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  900. }
  901. items[i].text_buf->draw(ci, text_pos, items[i].disabled ? theme_cache.font_disabled_color : (((active_submenu_index == -1 && i == mouse_over) || i == active_submenu_index) ? theme_cache.font_hover_color : theme_cache.font_color));
  902. } else {
  903. Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  904. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  905. items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  906. }
  907. items[i].text_buf->draw(ci, text_pos, items[i].disabled ? theme_cache.font_disabled_color : (((active_submenu_index == -1 && i == mouse_over) || i == active_submenu_index) ? theme_cache.font_hover_color : theme_cache.font_color));
  908. }
  909. }
  910. // Accelerator / Shortcut
  911. if (items[i].accel != Key::NONE || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) {
  912. if (rtl) {
  913. item_ofs.x = theme_cache.item_end_padding;
  914. } else {
  915. item_ofs.x = display_width - items[i].accel_text_buf->get_size().x - theme_cache.item_end_padding;
  916. }
  917. Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].accel_text_buf->get_size().y) / 2.0));
  918. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  919. items[i].accel_text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  920. }
  921. items[i].accel_text_buf->draw(ci, text_pos, ((active_submenu_index == -1 && i == mouse_over) || i == active_submenu_index) ? theme_cache.font_hover_color : theme_cache.font_accelerator_color);
  922. }
  923. // Cache the item vertical offset from the first item and the height.
  924. items.write[i]._ofs_cache = ofs.y;
  925. items.write[i]._height_cache = h;
  926. ofs.y += h;
  927. }
  928. }
  929. void PopupMenu::_close_pressed() {
  930. if (this_submenu_index != -1 && active_submenu_index != -1) { // Close secondary submenus on first submenu close.
  931. ERR_FAIL_INDEX_MSG(active_submenu_index, items.size(), vformat("Invalid active_submenu_index index %d in _close_pressed.", active_submenu_index));
  932. items[active_submenu_index].submenu->_close_pressed();
  933. }
  934. Popup::_close_pressed();
  935. }
  936. void PopupMenu::_close_or_suspend() {
  937. if (this_submenu_index != -1) { // Is a submenu.
  938. PopupMenu *parent_popup = Object::cast_to<PopupMenu>(get_parent());
  939. ERR_FAIL_NULL(parent_popup);
  940. Point2 mouse_pos = is_embedded() ? parent_popup->get_mouse_position() : Point2(DisplayServer::get_singleton()->mouse_get_position() - parent_popup->get_position());
  941. if (parent_popup->_get_mouse_over(mouse_pos) == this_submenu_index) {
  942. parent_popup->submenu_mouse_exited_ticks_msec = -1;
  943. parent_popup->mouse_movement_was_tested = false;
  944. parent_popup->close_suspended_timer->stop();
  945. parent_popup->close_was_suspended = false;
  946. parent_popup->submenu_timer->stop();
  947. return;
  948. }
  949. if (parent_popup->submenu_mouse_exited_ticks_msec == -1) { // ticks are reset to -1 when any open submenu is hidden.
  950. parent_popup->submenu_mouse_exited_ticks_msec = OS::get_singleton()->get_ticks_msec();
  951. }
  952. if (!parent_popup->close_was_suspended && parent_popup->close_suspended_timer->is_stopped()) { // Provisionally suspend submenu close until the check below after several msecs.
  953. parent_popup->close_suspended_timer->start();
  954. parent_popup->close_was_suspended = true; // Stays true until the submenu is closed so it can only be suspended once.
  955. parent_popup->submenu_timer->stop();
  956. } else if (!parent_popup->mouse_movement_was_tested && OS::get_singleton()->get_ticks_msec() - parent_popup->submenu_mouse_exited_ticks_msec > 30) {
  957. // Allow the mouse to move away for several msecs before calculating the relative vector to the mouse exit point out of the submenu item.
  958. parent_popup->mouse_movement_was_tested = true;
  959. if (is_embedded()) { // Correct for relative positioning of embedded subwindows.
  960. parent_popup->last_submenu_mouse_position += get_position() - parent_popup->get_position();
  961. }
  962. const Vector2 screen_mouse_position = mouse_pos + parent_popup->get_position();
  963. Vector2 mouse_relative_to_submenu_exit = mouse_pos - parent_popup->last_submenu_mouse_position;
  964. // If the mouse is not moving toward the submenu target line, force the submenu closed.
  965. if (!_is_mouse_moving_toward_submenu(mouse_relative_to_submenu_exit, parent_popup->is_active_submenu_left, screen_mouse_position, parent_popup->active_submenu_target_line)) {
  966. parent_popup->close_suspended_timer->stop();
  967. parent_popup->close_was_suspended = false;
  968. _close_pressed();
  969. }
  970. } else if (parent_popup->mouse_movement_was_tested && OS::get_singleton()->get_ticks_msec() - parent_popup->submenu_mouse_exited_ticks_msec > CLOSE_SUSPENDED_TIMER_DELAY * 1000) {
  971. _close_pressed(); // This is called when the mouse is moved off an open submenu onto an autohide area.
  972. }
  973. } else { // Is the parent popup menu.
  974. _close_pressed();
  975. }
  976. }
  977. void PopupMenu::_shape_item(int p_idx) const {
  978. if (items.write[p_idx].dirty) {
  979. items.write[p_idx].text_buf->clear();
  980. Ref<Font> font = items[p_idx].separator ? theme_cache.font_separator : theme_cache.font;
  981. int font_size = items[p_idx].separator ? theme_cache.font_separator_size : theme_cache.font_size;
  982. if (items[p_idx].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  983. items.write[p_idx].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  984. } else {
  985. items.write[p_idx].text_buf->set_direction((TextServer::Direction)items[p_idx].text_direction);
  986. }
  987. const String &lang = items[p_idx].language.is_empty() ? _get_locale() : items[p_idx].language;
  988. items.write[p_idx].text_buf->add_string(items.write[p_idx].xl_text, font, font_size, lang);
  989. items.write[p_idx].accel_text_buf->clear();
  990. items.write[p_idx].accel_text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  991. items.write[p_idx].accel_text_buf->add_string(_get_accel_text(items.write[p_idx]), font, font_size, lang);
  992. items.write[p_idx].dirty = false;
  993. }
  994. }
  995. void PopupMenu::_menu_changed() {
  996. emit_signal(SNAME("menu_changed"));
  997. }
  998. void PopupMenu::_update_shadow_offsets() const {
  999. if (!DisplayServer::get_singleton()->is_window_transparency_available() && !is_embedded()) {
  1000. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  1001. return;
  1002. }
  1003. Ref<StyleBoxFlat> sb = theme_cache.panel_style;
  1004. if (sb.is_null()) {
  1005. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  1006. return;
  1007. }
  1008. const int shadow_size = sb->get_shadow_size();
  1009. if (shadow_size == 0) {
  1010. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  1011. return;
  1012. }
  1013. // Offset the background panel so it leaves space inside the window for the shadows to be drawn.
  1014. const Point2 shadow_offset = sb->get_shadow_offset();
  1015. if (is_layout_rtl()) {
  1016. panel->set_offset(SIDE_LEFT, MAX(0, shadow_size + shadow_offset.x));
  1017. panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size + shadow_offset.x));
  1018. } else {
  1019. panel->set_offset(SIDE_LEFT, MAX(0, shadow_size - shadow_offset.x));
  1020. panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size - shadow_offset.x));
  1021. }
  1022. panel->set_offset(SIDE_TOP, MAX(0, shadow_size - shadow_offset.y));
  1023. panel->set_offset(SIDE_BOTTOM, MIN(0, -shadow_size - shadow_offset.y));
  1024. }
  1025. Rect2i PopupMenu::_popup_adjust_rect() const {
  1026. Rect2i current = Popup::_popup_adjust_rect();
  1027. if (current == Rect2i()) {
  1028. return current;
  1029. }
  1030. pre_popup_rect = current;
  1031. _update_shadow_offsets();
  1032. if (is_layout_rtl()) {
  1033. current.position -= Vector2(-panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
  1034. } else {
  1035. current.position -= Vector2(panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
  1036. }
  1037. current.size += Vector2(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM)) * get_content_scale_factor();
  1038. return current;
  1039. }
  1040. void PopupMenu::add_child_notify(Node *p_child) {
  1041. Window::add_child_notify(p_child);
  1042. if (global_menu.is_valid()) {
  1043. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  1044. for (int i = 0; i < items.size(); i++) {
  1045. if (items[i].submenu == p_child) {
  1046. RID submenu_rid = pm->bind_global_menu();
  1047. NativeMenu::get_singleton()->set_item_submenu(global_menu, i, submenu_rid);
  1048. items.write[i].submenu_bound = true;
  1049. }
  1050. }
  1051. }
  1052. _menu_changed();
  1053. }
  1054. void PopupMenu::remove_child_notify(Node *p_child) {
  1055. Window::remove_child_notify(p_child);
  1056. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  1057. if (!pm) {
  1058. return;
  1059. }
  1060. if (Object::cast_to<PopupMenu>(p_child) && global_menu.is_valid()) {
  1061. for (int i = 0; i < items.size(); i++) {
  1062. if (items[i].submenu == p_child) {
  1063. NativeMenu::get_singleton()->set_item_submenu(global_menu, i, RID());
  1064. items.write[i].submenu_bound = false;
  1065. }
  1066. }
  1067. pm->unbind_global_menu();
  1068. }
  1069. _menu_changed();
  1070. }
  1071. void PopupMenu::_accessibility_action_click(const Variant &p_data, int p_idx) {
  1072. activate_item(p_idx);
  1073. }
  1074. RID PopupMenu::get_focused_accessibility_element() const {
  1075. if (mouse_over == -1) {
  1076. return get_accessibility_element();
  1077. } else {
  1078. const Item &item = items[mouse_over];
  1079. return item.accessibility_item_element;
  1080. }
  1081. }
  1082. void PopupMenu::_notification(int p_what) {
  1083. switch (p_what) {
  1084. case NOTIFICATION_EXIT_TREE: {
  1085. if (system_menu_id != NativeMenu::INVALID_MENU_ID) {
  1086. unbind_global_menu();
  1087. }
  1088. [[fallthrough]];
  1089. }
  1090. case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
  1091. for (int i = 0; i < items.size(); i++) {
  1092. items.write[i].accessibility_item_element = RID();
  1093. }
  1094. accessibility_scroll_element = RID();
  1095. } break;
  1096. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  1097. RID ae = get_accessibility_element();
  1098. ERR_FAIL_COND(ae.is_null());
  1099. if (has_meta("_menu_name")) {
  1100. DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_meta("_menu_name", get_name()));
  1101. }
  1102. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_MENU);
  1103. DisplayServer::get_singleton()->accessibility_update_set_list_item_count(ae, items.size());
  1104. if (accessibility_scroll_element.is_null()) {
  1105. accessibility_scroll_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
  1106. }
  1107. Transform2D scroll_xform;
  1108. scroll_xform.set_origin(Vector2i(0, -scroll_container->get_v_scroll_bar()->get_value()));
  1109. DisplayServer::get_singleton()->accessibility_update_set_transform(accessibility_scroll_element, scroll_xform);
  1110. DisplayServer::get_singleton()->accessibility_update_set_bounds(accessibility_scroll_element, Rect2(0, 0, get_size().x, scroll_container->get_v_scroll_bar()->get_max()));
  1111. float scroll_width = scroll_container->get_v_scroll_bar()->is_visible_in_tree() ? scroll_container->get_v_scroll_bar()->get_size().width : 0;
  1112. float display_width = control->get_size().width - scroll_width;
  1113. Point2 ofs;
  1114. for (int i = 0; i < items.size(); i++) {
  1115. const Item &item = items.write[i];
  1116. // Avoid discrepancy between min size and drawn items due to rounding.
  1117. ofs.y += i > 0 ? theme_cache.v_separation : theme_cache.v_separation - (theme_cache.v_separation / 2);
  1118. Point2 item_ofs = ofs;
  1119. if (item.accessibility_item_element.is_null()) {
  1120. item.accessibility_item_element = DisplayServer::get_singleton()->accessibility_create_sub_element(accessibility_scroll_element, DisplayServer::AccessibilityRole::ROLE_MENU_ITEM);
  1121. item.accessibility_item_dirty = true;
  1122. }
  1123. item_ofs.x += item.indent * theme_cache.indent;
  1124. float h = _get_item_height(i);
  1125. if (item.accessibility_item_dirty || i == prev_mouse_over || i == mouse_over) {
  1126. switch (item.checkable_type) {
  1127. case Item::CHECKABLE_TYPE_NONE: {
  1128. DisplayServer::get_singleton()->accessibility_update_set_role(item.accessibility_item_element, DisplayServer::AccessibilityRole::ROLE_MENU_ITEM);
  1129. } break;
  1130. case Item::CHECKABLE_TYPE_CHECK_BOX: {
  1131. DisplayServer::get_singleton()->accessibility_update_set_role(item.accessibility_item_element, DisplayServer::AccessibilityRole::ROLE_MENU_ITEM_CHECK_BOX);
  1132. DisplayServer::get_singleton()->accessibility_update_set_checked(item.accessibility_item_element, item.checked);
  1133. } break;
  1134. case Item::CHECKABLE_TYPE_RADIO_BUTTON: {
  1135. DisplayServer::get_singleton()->accessibility_update_set_role(item.accessibility_item_element, DisplayServer::AccessibilityRole::ROLE_MENU_ITEM_RADIO);
  1136. DisplayServer::get_singleton()->accessibility_update_set_checked(item.accessibility_item_element, item.checked);
  1137. } break;
  1138. }
  1139. DisplayServer::get_singleton()->accessibility_update_add_action(item.accessibility_item_element, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &PopupMenu::_accessibility_action_click).bind(i));
  1140. DisplayServer::get_singleton()->accessibility_update_set_list_item_index(item.accessibility_item_element, i);
  1141. DisplayServer::get_singleton()->accessibility_update_set_list_item_level(item.accessibility_item_element, 0);
  1142. DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(item.accessibility_item_element, i == mouse_over);
  1143. DisplayServer::get_singleton()->accessibility_update_set_name(item.accessibility_item_element, item.xl_text);
  1144. DisplayServer::get_singleton()->accessibility_update_set_flag(item.accessibility_item_element, DisplayServer::AccessibilityFlags::FLAG_DISABLED, item.disabled);
  1145. DisplayServer::get_singleton()->accessibility_update_set_tooltip(item.accessibility_item_element, item.tooltip);
  1146. DisplayServer::get_singleton()->accessibility_update_set_bounds(item.accessibility_item_element, Rect2(item_ofs, Size2(display_width, h + theme_cache.v_separation)));
  1147. item.accessibility_item_dirty = false;
  1148. }
  1149. ofs.y += h;
  1150. }
  1151. prev_mouse_over = -1;
  1152. } break;
  1153. case NOTIFICATION_ENTER_TREE: {
  1154. PopupMenu *pm = Object::cast_to<PopupMenu>(get_parent());
  1155. if (pm) {
  1156. // Inherit submenu's popup delay time from parent menu.
  1157. float pm_delay = pm->get_submenu_popup_delay();
  1158. set_submenu_popup_delay(pm_delay);
  1159. }
  1160. if (system_menu_id != NativeMenu::INVALID_MENU_ID) {
  1161. bind_global_menu();
  1162. }
  1163. } break;
  1164. case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  1165. case NOTIFICATION_THEME_CHANGED: {
  1166. panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style);
  1167. if (is_visible()) {
  1168. _update_shadow_offsets();
  1169. }
  1170. #ifdef TOOLS_ENABLED
  1171. update_configuration_warnings();
  1172. #endif
  1173. [[fallthrough]];
  1174. }
  1175. case NOTIFICATION_TRANSLATION_CHANGED: {
  1176. NativeMenu *nmenu = NativeMenu::get_singleton();
  1177. bool is_global = global_menu.is_valid();
  1178. if (is_global) {
  1179. nmenu->set_interface_direction(global_menu, control->is_layout_rtl());
  1180. }
  1181. for (int i = 0; i < items.size(); i++) {
  1182. Item &item = items.write[i];
  1183. item.xl_text = _atr(i, item.text);
  1184. item.dirty = true;
  1185. if (is_global) {
  1186. nmenu->set_item_text(global_menu, i, item.xl_text);
  1187. }
  1188. item.accessibility_item_dirty = true;
  1189. _shape_item(i);
  1190. queue_accessibility_update();
  1191. }
  1192. child_controls_changed();
  1193. _menu_changed();
  1194. control->queue_redraw();
  1195. } break;
  1196. case NOTIFICATION_WM_MOUSE_ENTER: {
  1197. grab_focus();
  1198. } break;
  1199. case NOTIFICATION_WM_MOUSE_EXIT: {
  1200. mouse_over = -1;
  1201. queue_accessibility_update();
  1202. control->queue_redraw();
  1203. } break;
  1204. case NOTIFICATION_WM_SIZE_CHANGED: {
  1205. if (is_visible()) {
  1206. const Vector2i offsets = Vector2i(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM));
  1207. // Check if the size actually changed.
  1208. if (pre_popup_rect.size + offsets != get_size()) {
  1209. // Play safe, and stick with the new size.
  1210. pre_popup_rect = Rect2i();
  1211. }
  1212. }
  1213. } break;
  1214. case NOTIFICATION_POST_POPUP: {
  1215. popup_time_msec = OS::get_singleton()->get_ticks_msec();
  1216. initial_button_mask = Input::get_singleton()->get_mouse_button_mask();
  1217. during_grabbed_click = (bool)initial_button_mask;
  1218. } break;
  1219. case NOTIFICATION_INTERNAL_PROCESS: {
  1220. Input *input = Input::get_singleton();
  1221. if (joypad_event_process && has_focus_or_active_popup()) {
  1222. if (input->is_action_just_released("ui_up") || input->is_action_just_released("ui_down")) {
  1223. gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  1224. set_process_internal(false);
  1225. return;
  1226. }
  1227. gamepad_event_delay_ms -= get_process_delta_time();
  1228. if (gamepad_event_delay_ms <= 0) {
  1229. if (input->is_action_pressed("ui_down")) {
  1230. gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
  1231. int search_from = mouse_over + 1;
  1232. if (search_from >= items.size()) {
  1233. search_from = 0;
  1234. }
  1235. bool match_found = false;
  1236. for (int i = search_from; i < items.size(); i++) {
  1237. if (!items[i].separator && !items[i].disabled) {
  1238. mouse_over = i;
  1239. emit_signal(SNAME("id_focused"), items[i].id);
  1240. scroll_to_item(i);
  1241. control->queue_redraw();
  1242. match_found = true;
  1243. break;
  1244. }
  1245. }
  1246. if (!match_found) {
  1247. // If the last item is not selectable, try re-searching from the start.
  1248. for (int i = 0; i < search_from; i++) {
  1249. if (!items[i].separator && !items[i].disabled) {
  1250. mouse_over = i;
  1251. emit_signal(SNAME("id_focused"), items[i].id);
  1252. scroll_to_item(i);
  1253. control->queue_redraw();
  1254. break;
  1255. }
  1256. }
  1257. }
  1258. }
  1259. if (input->is_action_pressed("ui_up")) {
  1260. gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
  1261. int search_from = mouse_over - 1;
  1262. if (search_from < 0) {
  1263. search_from = items.size() - 1;
  1264. }
  1265. bool match_found = false;
  1266. for (int i = search_from; i >= 0; i--) {
  1267. if (!items[i].separator && !items[i].disabled) {
  1268. mouse_over = i;
  1269. emit_signal(SNAME("id_focused"), items[i].id);
  1270. scroll_to_item(i);
  1271. control->queue_redraw();
  1272. match_found = true;
  1273. break;
  1274. }
  1275. }
  1276. if (!match_found) {
  1277. // If the first item is not selectable, try re-searching from the end.
  1278. for (int i = items.size() - 1; i >= search_from; i--) {
  1279. if (!items[i].separator && !items[i].disabled) {
  1280. mouse_over = i;
  1281. emit_signal(SNAME("id_focused"), items[i].id);
  1282. scroll_to_item(i);
  1283. control->queue_redraw();
  1284. break;
  1285. }
  1286. }
  1287. }
  1288. }
  1289. }
  1290. }
  1291. // Only used when using operating system windows, and only on submenus.
  1292. if (!activated_by_keyboard && !is_embedded() && autohide_areas.size() && this_submenu_index != -1) {
  1293. PopupMenu *parent_popup = Object::cast_to<PopupMenu>(get_parent());
  1294. ERR_FAIL_NULL(parent_popup);
  1295. const float win_scale = get_content_scale_factor();
  1296. Point2 mouse_pos = DisplayServer::get_singleton()->mouse_get_position() - get_position();
  1297. Point2 areas_mouse_pos = mouse_pos - parent_popup->panel_offset_start;
  1298. for (const Rect2 &E : autohide_areas) {
  1299. if (!Rect2(Point2(), control->get_size() * win_scale).has_point(areas_mouse_pos) && E.has_point(areas_mouse_pos)) {
  1300. _close_or_suspend();
  1301. return;
  1302. }
  1303. }
  1304. }
  1305. } break;
  1306. case NOTIFICATION_VISIBILITY_CHANGED: {
  1307. if (!is_visible()) {
  1308. this_submenu_index = -1;
  1309. if (mouse_over >= 0) {
  1310. prev_mouse_over = mouse_over;
  1311. mouse_over = -1;
  1312. queue_accessibility_update();
  1313. control->queue_redraw();
  1314. }
  1315. for (int i = 0; i < items.size(); i++) {
  1316. if (!items[i].submenu) {
  1317. continue;
  1318. }
  1319. items[i].submenu->hide();
  1320. }
  1321. set_process_internal(false);
  1322. // Remove the extra space used by the shadows, so they can be ignored when the popup is hidden.
  1323. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  1324. if (pre_popup_rect != Rect2i()) {
  1325. set_position(pre_popup_rect.position);
  1326. set_size(pre_popup_rect.size);
  1327. pre_popup_rect = Rect2i();
  1328. }
  1329. } else {
  1330. if (!is_embedded()) {
  1331. joypad_event_process = false;
  1332. set_process_internal(true);
  1333. }
  1334. // The popup was made visible directly (without `popup_*()`), so just update the offsets without touching the rect.
  1335. if (pre_popup_rect == Rect2i()) {
  1336. _update_shadow_offsets();
  1337. }
  1338. }
  1339. } break;
  1340. }
  1341. }
  1342. /* Methods to add items with or without icon, checkbox, shortcut.
  1343. * Be sure to keep them in sync when adding new properties in the Item struct.
  1344. */
  1345. #define ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel) \
  1346. item.text = p_label; \
  1347. item.xl_text = atr(p_label); \
  1348. item.id = p_id == -1 ? items.size() : p_id; \
  1349. item.accel = p_accel;
  1350. void PopupMenu::add_item(const String &p_label, int p_id, Key p_accel) {
  1351. Item item;
  1352. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1353. items.push_back(item);
  1354. if (global_menu.is_valid()) {
  1355. NativeMenu *nmenu = NativeMenu::get_singleton();
  1356. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1357. if (item.accel != Key::NONE) {
  1358. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1359. }
  1360. }
  1361. _shape_item(items.size() - 1);
  1362. queue_accessibility_update();
  1363. control->queue_redraw();
  1364. child_controls_changed();
  1365. notify_property_list_changed();
  1366. _menu_changed();
  1367. }
  1368. void PopupMenu::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) {
  1369. Item item;
  1370. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1371. item.icon = p_icon;
  1372. items.push_back(item);
  1373. if (global_menu.is_valid()) {
  1374. NativeMenu *nmenu = NativeMenu::get_singleton();
  1375. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1376. if (item.accel != Key::NONE) {
  1377. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1378. }
  1379. nmenu->set_item_icon(global_menu, index, item.icon);
  1380. }
  1381. _shape_item(items.size() - 1);
  1382. queue_accessibility_update();
  1383. control->queue_redraw();
  1384. child_controls_changed();
  1385. notify_property_list_changed();
  1386. _menu_changed();
  1387. }
  1388. void PopupMenu::add_check_item(const String &p_label, int p_id, Key p_accel) {
  1389. Item item;
  1390. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1391. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1392. items.push_back(item);
  1393. if (global_menu.is_valid()) {
  1394. NativeMenu *nmenu = NativeMenu::get_singleton();
  1395. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1396. if (item.accel != Key::NONE) {
  1397. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1398. }
  1399. nmenu->set_item_checkable(global_menu, index, true);
  1400. }
  1401. _shape_item(items.size() - 1);
  1402. queue_accessibility_update();
  1403. control->queue_redraw();
  1404. child_controls_changed();
  1405. notify_property_list_changed();
  1406. _menu_changed();
  1407. }
  1408. void PopupMenu::add_icon_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) {
  1409. Item item;
  1410. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1411. item.icon = p_icon;
  1412. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1413. items.push_back(item);
  1414. if (global_menu.is_valid()) {
  1415. NativeMenu *nmenu = NativeMenu::get_singleton();
  1416. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1417. if (item.accel != Key::NONE) {
  1418. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1419. }
  1420. nmenu->set_item_icon(global_menu, index, item.icon);
  1421. nmenu->set_item_checkable(global_menu, index, true);
  1422. }
  1423. _shape_item(items.size() - 1);
  1424. queue_accessibility_update();
  1425. control->queue_redraw();
  1426. child_controls_changed();
  1427. notify_property_list_changed();
  1428. _menu_changed();
  1429. }
  1430. void PopupMenu::add_radio_check_item(const String &p_label, int p_id, Key p_accel) {
  1431. Item item;
  1432. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1433. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1434. items.push_back(item);
  1435. if (global_menu.is_valid()) {
  1436. NativeMenu *nmenu = NativeMenu::get_singleton();
  1437. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1438. if (item.accel != Key::NONE) {
  1439. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1440. }
  1441. nmenu->set_item_radio_checkable(global_menu, index, true);
  1442. }
  1443. _shape_item(items.size() - 1);
  1444. queue_accessibility_update();
  1445. control->queue_redraw();
  1446. child_controls_changed();
  1447. notify_property_list_changed();
  1448. _menu_changed();
  1449. }
  1450. void PopupMenu::add_icon_radio_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) {
  1451. Item item;
  1452. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1453. item.icon = p_icon;
  1454. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1455. items.push_back(item);
  1456. if (global_menu.is_valid()) {
  1457. NativeMenu *nmenu = NativeMenu::get_singleton();
  1458. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1459. if (item.accel != Key::NONE) {
  1460. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1461. }
  1462. nmenu->set_item_icon(global_menu, index, item.icon);
  1463. nmenu->set_item_radio_checkable(global_menu, index, true);
  1464. }
  1465. _shape_item(items.size() - 1);
  1466. queue_accessibility_update();
  1467. control->queue_redraw();
  1468. child_controls_changed();
  1469. notify_property_list_changed();
  1470. _menu_changed();
  1471. }
  1472. void PopupMenu::add_multistate_item(const String &p_label, int p_max_states, int p_default_state, int p_id, Key p_accel) {
  1473. Item item;
  1474. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1475. item.max_states = p_max_states;
  1476. item.state = p_default_state;
  1477. items.push_back(item);
  1478. if (global_menu.is_valid()) {
  1479. NativeMenu *nmenu = NativeMenu::get_singleton();
  1480. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1481. if (item.accel != Key::NONE) {
  1482. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1483. }
  1484. nmenu->set_item_max_states(global_menu, index, item.max_states);
  1485. nmenu->set_item_state(global_menu, index, item.state);
  1486. }
  1487. _shape_item(items.size() - 1);
  1488. queue_accessibility_update();
  1489. control->queue_redraw();
  1490. child_controls_changed();
  1491. _menu_changed();
  1492. notify_property_list_changed();
  1493. }
  1494. #define ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo) \
  1495. ERR_FAIL_COND_MSG(p_shortcut.is_null(), "Cannot add item with invalid Shortcut."); \
  1496. _ref_shortcut(p_shortcut); \
  1497. item.text = p_shortcut->get_name(); \
  1498. item.xl_text = atr(item.text); \
  1499. item.id = p_id == -1 ? items.size() : p_id; \
  1500. item.shortcut = p_shortcut; \
  1501. item.shortcut_is_global = p_global; \
  1502. item.allow_echo = p_allow_echo;
  1503. void PopupMenu::add_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global, bool p_allow_echo) {
  1504. Item item;
  1505. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo);
  1506. items.push_back(item);
  1507. if (global_menu.is_valid()) {
  1508. NativeMenu *nmenu = NativeMenu::get_singleton();
  1509. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1510. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1511. Array events = item.shortcut->get_events();
  1512. for (int j = 0; j < events.size(); j++) {
  1513. Ref<InputEventKey> ie = events[j];
  1514. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1515. break;
  1516. }
  1517. }
  1518. }
  1519. }
  1520. _shape_item(items.size() - 1);
  1521. queue_accessibility_update();
  1522. control->queue_redraw();
  1523. child_controls_changed();
  1524. notify_property_list_changed();
  1525. _menu_changed();
  1526. }
  1527. void PopupMenu::add_icon_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global, bool p_allow_echo) {
  1528. Item item;
  1529. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo);
  1530. item.icon = p_icon;
  1531. items.push_back(item);
  1532. if (global_menu.is_valid()) {
  1533. NativeMenu *nmenu = NativeMenu::get_singleton();
  1534. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1535. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1536. Array events = item.shortcut->get_events();
  1537. for (int j = 0; j < events.size(); j++) {
  1538. Ref<InputEventKey> ie = events[j];
  1539. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1540. break;
  1541. }
  1542. }
  1543. }
  1544. nmenu->set_item_icon(global_menu, index, item.icon);
  1545. }
  1546. _shape_item(items.size() - 1);
  1547. queue_accessibility_update();
  1548. control->queue_redraw();
  1549. child_controls_changed();
  1550. notify_property_list_changed();
  1551. _menu_changed();
  1552. }
  1553. void PopupMenu::add_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1554. Item item;
  1555. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false); // Echo for check shortcuts doesn't make sense.
  1556. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1557. items.push_back(item);
  1558. if (global_menu.is_valid()) {
  1559. NativeMenu *nmenu = NativeMenu::get_singleton();
  1560. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1561. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1562. Array events = item.shortcut->get_events();
  1563. for (int j = 0; j < events.size(); j++) {
  1564. Ref<InputEventKey> ie = events[j];
  1565. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1566. break;
  1567. }
  1568. }
  1569. }
  1570. nmenu->set_item_checkable(global_menu, index, true);
  1571. }
  1572. _shape_item(items.size() - 1);
  1573. queue_accessibility_update();
  1574. control->queue_redraw();
  1575. child_controls_changed();
  1576. notify_property_list_changed();
  1577. _menu_changed();
  1578. }
  1579. void PopupMenu::add_icon_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1580. Item item;
  1581. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false);
  1582. item.icon = p_icon;
  1583. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1584. items.push_back(item);
  1585. if (global_menu.is_valid()) {
  1586. NativeMenu *nmenu = NativeMenu::get_singleton();
  1587. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1588. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1589. Array events = item.shortcut->get_events();
  1590. for (int j = 0; j < events.size(); j++) {
  1591. Ref<InputEventKey> ie = events[j];
  1592. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1593. break;
  1594. }
  1595. }
  1596. }
  1597. nmenu->set_item_icon(global_menu, index, item.icon);
  1598. nmenu->set_item_checkable(global_menu, index, true);
  1599. }
  1600. _shape_item(items.size() - 1);
  1601. queue_accessibility_update();
  1602. control->queue_redraw();
  1603. child_controls_changed();
  1604. notify_property_list_changed();
  1605. _menu_changed();
  1606. }
  1607. void PopupMenu::add_radio_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1608. Item item;
  1609. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false);
  1610. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1611. items.push_back(item);
  1612. if (global_menu.is_valid()) {
  1613. NativeMenu *nmenu = NativeMenu::get_singleton();
  1614. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1615. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1616. Array events = item.shortcut->get_events();
  1617. for (int j = 0; j < events.size(); j++) {
  1618. Ref<InputEventKey> ie = events[j];
  1619. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1620. break;
  1621. }
  1622. }
  1623. }
  1624. nmenu->set_item_radio_checkable(global_menu, index, true);
  1625. }
  1626. _shape_item(items.size() - 1);
  1627. queue_accessibility_update();
  1628. control->queue_redraw();
  1629. child_controls_changed();
  1630. notify_property_list_changed();
  1631. _menu_changed();
  1632. }
  1633. void PopupMenu::add_icon_radio_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1634. Item item;
  1635. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false);
  1636. item.icon = p_icon;
  1637. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1638. items.push_back(item);
  1639. if (global_menu.is_valid()) {
  1640. NativeMenu *nmenu = NativeMenu::get_singleton();
  1641. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1642. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1643. Array events = item.shortcut->get_events();
  1644. for (int j = 0; j < events.size(); j++) {
  1645. Ref<InputEventKey> ie = events[j];
  1646. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1647. break;
  1648. }
  1649. }
  1650. }
  1651. nmenu->set_item_icon(global_menu, index, item.icon);
  1652. nmenu->set_item_radio_checkable(global_menu, index, true);
  1653. }
  1654. _shape_item(items.size() - 1);
  1655. queue_accessibility_update();
  1656. control->queue_redraw();
  1657. child_controls_changed();
  1658. notify_property_list_changed();
  1659. _menu_changed();
  1660. }
  1661. void PopupMenu::add_submenu_item(const String &p_label, const String &p_submenu, int p_id) {
  1662. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(p_submenu));
  1663. ERR_FAIL_NULL_MSG(pm, vformat("Child PopupMenu \"%s\" does not exist.", p_submenu));
  1664. add_submenu_node_item(p_label, pm, p_id);
  1665. }
  1666. void PopupMenu::add_submenu_node_item(const String &p_label, PopupMenu *p_submenu, int p_id) {
  1667. ERR_FAIL_NULL(p_submenu);
  1668. if (p_submenu->get_parent() != this) {
  1669. ERR_FAIL_COND_MSG(p_submenu->get_parent() != nullptr, vformat("The submenu \"%s\" already has a different parent.", p_submenu->get_name()));
  1670. add_child(p_submenu);
  1671. }
  1672. Item item;
  1673. item.text = p_label;
  1674. item.xl_text = atr(p_label);
  1675. item.id = p_id == -1 ? items.size() : p_id;
  1676. item.accessibility_item_dirty = true;
  1677. item.submenu = p_submenu;
  1678. item.submenu_name = p_submenu->get_name();
  1679. items.push_back(item);
  1680. if (global_menu.is_valid()) {
  1681. NativeMenu *nmenu = NativeMenu::get_singleton();
  1682. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1683. RID submenu_rid = p_submenu->bind_global_menu();
  1684. nmenu->set_item_submenu(global_menu, index, submenu_rid);
  1685. items.write[index].submenu_bound = true;
  1686. } else if (!p_submenu->is_connected("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden))) {
  1687. p_submenu->connect("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden));
  1688. }
  1689. _shape_item(items.size() - 1);
  1690. queue_accessibility_update();
  1691. control->queue_redraw();
  1692. child_controls_changed();
  1693. notify_property_list_changed();
  1694. _menu_changed();
  1695. }
  1696. #undef ITEM_SETUP_WITH_ACCEL
  1697. #undef ITEM_SETUP_WITH_SHORTCUT
  1698. /* Methods to modify existing items. */
  1699. void PopupMenu::set_item_text(int p_idx, const String &p_text) {
  1700. if (p_idx < 0) {
  1701. p_idx += get_item_count();
  1702. }
  1703. ERR_FAIL_INDEX(p_idx, items.size());
  1704. if (items[p_idx].text == p_text) {
  1705. return;
  1706. }
  1707. items.write[p_idx].text = p_text;
  1708. items.write[p_idx].xl_text = _atr(p_idx, p_text);
  1709. items.write[p_idx].dirty = true;
  1710. items.write[p_idx].accessibility_item_dirty = true;
  1711. if (global_menu.is_valid()) {
  1712. NativeMenu::get_singleton()->set_item_text(global_menu, p_idx, items[p_idx].xl_text);
  1713. }
  1714. _shape_item(p_idx);
  1715. queue_accessibility_update();
  1716. control->queue_redraw();
  1717. child_controls_changed();
  1718. _menu_changed();
  1719. }
  1720. void PopupMenu::set_item_text_direction(int p_idx, Control::TextDirection p_text_direction) {
  1721. if (p_idx < 0) {
  1722. p_idx += get_item_count();
  1723. }
  1724. ERR_FAIL_INDEX(p_idx, items.size());
  1725. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  1726. if (items[p_idx].text_direction != p_text_direction) {
  1727. items.write[p_idx].text_direction = p_text_direction;
  1728. items.write[p_idx].dirty = true;
  1729. items.write[p_idx].accessibility_item_dirty = true;
  1730. _shape_item(p_idx);
  1731. queue_accessibility_update();
  1732. control->queue_redraw();
  1733. }
  1734. }
  1735. void PopupMenu::set_item_language(int p_idx, const String &p_language) {
  1736. if (p_idx < 0) {
  1737. p_idx += get_item_count();
  1738. }
  1739. ERR_FAIL_INDEX(p_idx, items.size());
  1740. if (items[p_idx].language != p_language) {
  1741. items.write[p_idx].language = p_language;
  1742. items.write[p_idx].dirty = true;
  1743. items.write[p_idx].accessibility_item_dirty = true;
  1744. _shape_item(p_idx);
  1745. queue_accessibility_update();
  1746. control->queue_redraw();
  1747. }
  1748. }
  1749. void PopupMenu::set_item_auto_translate_mode(int p_idx, AutoTranslateMode p_mode) {
  1750. if (p_idx < 0) {
  1751. p_idx += get_item_count();
  1752. }
  1753. ERR_FAIL_INDEX(p_idx, items.size());
  1754. if (items[p_idx].auto_translate_mode == p_mode) {
  1755. return;
  1756. }
  1757. items.write[p_idx].auto_translate_mode = p_mode;
  1758. items.write[p_idx].xl_text = _atr(p_idx, items[p_idx].text);
  1759. items.write[p_idx].dirty = true;
  1760. control->queue_redraw();
  1761. }
  1762. void PopupMenu::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
  1763. if (p_idx < 0) {
  1764. p_idx += get_item_count();
  1765. }
  1766. ERR_FAIL_INDEX(p_idx, items.size());
  1767. if (items[p_idx].icon == p_icon) {
  1768. return;
  1769. }
  1770. items.write[p_idx].icon = p_icon;
  1771. if (global_menu.is_valid()) {
  1772. NativeMenu::get_singleton()->set_item_icon(global_menu, p_idx, items[p_idx].icon);
  1773. }
  1774. control->queue_redraw();
  1775. child_controls_changed();
  1776. _menu_changed();
  1777. }
  1778. void PopupMenu::set_item_icon_max_width(int p_idx, int p_width) {
  1779. if (p_idx < 0) {
  1780. p_idx += get_item_count();
  1781. }
  1782. ERR_FAIL_INDEX(p_idx, items.size());
  1783. if (items[p_idx].icon_max_width == p_width) {
  1784. return;
  1785. }
  1786. items.write[p_idx].icon_max_width = p_width;
  1787. control->queue_redraw();
  1788. child_controls_changed();
  1789. _menu_changed();
  1790. }
  1791. void PopupMenu::set_item_icon_modulate(int p_idx, const Color &p_modulate) {
  1792. if (p_idx < 0) {
  1793. p_idx += get_item_count();
  1794. }
  1795. ERR_FAIL_INDEX(p_idx, items.size());
  1796. if (items[p_idx].icon_modulate == p_modulate) {
  1797. return;
  1798. }
  1799. items.write[p_idx].icon_modulate = p_modulate;
  1800. control->queue_redraw();
  1801. }
  1802. void PopupMenu::set_item_checked(int p_idx, bool p_checked) {
  1803. if (p_idx < 0) {
  1804. p_idx += get_item_count();
  1805. }
  1806. ERR_FAIL_INDEX(p_idx, items.size());
  1807. if (items[p_idx].checked == p_checked) {
  1808. return;
  1809. }
  1810. items.write[p_idx].checked = p_checked;
  1811. items.write[p_idx].accessibility_item_dirty = true;
  1812. if (global_menu.is_valid()) {
  1813. NativeMenu::get_singleton()->set_item_checked(global_menu, p_idx, p_checked);
  1814. }
  1815. queue_accessibility_update();
  1816. control->queue_redraw();
  1817. child_controls_changed();
  1818. _menu_changed();
  1819. }
  1820. void PopupMenu::set_item_id(int p_idx, int p_id) {
  1821. if (p_idx < 0) {
  1822. p_idx += get_item_count();
  1823. }
  1824. ERR_FAIL_INDEX(p_idx, items.size());
  1825. if (items[p_idx].id == p_id) {
  1826. return;
  1827. }
  1828. items.write[p_idx].id = p_id;
  1829. if (global_menu.is_valid()) {
  1830. NativeMenu::get_singleton()->set_item_tag(global_menu, p_idx, p_id);
  1831. }
  1832. control->queue_redraw();
  1833. child_controls_changed();
  1834. _menu_changed();
  1835. }
  1836. void PopupMenu::set_item_accelerator(int p_idx, Key p_accel) {
  1837. if (p_idx < 0) {
  1838. p_idx += get_item_count();
  1839. }
  1840. ERR_FAIL_INDEX(p_idx, items.size());
  1841. if (items[p_idx].accel == p_accel) {
  1842. return;
  1843. }
  1844. items.write[p_idx].accel = p_accel;
  1845. items.write[p_idx].dirty = true;
  1846. items.write[p_idx].accessibility_item_dirty = true;
  1847. if (global_menu.is_valid()) {
  1848. NativeMenu::get_singleton()->set_item_accelerator(global_menu, p_idx, p_accel);
  1849. }
  1850. queue_accessibility_update();
  1851. control->queue_redraw();
  1852. child_controls_changed();
  1853. _menu_changed();
  1854. }
  1855. void PopupMenu::set_item_metadata(int p_idx, const Variant &p_meta) {
  1856. if (p_idx < 0) {
  1857. p_idx += get_item_count();
  1858. }
  1859. ERR_FAIL_INDEX(p_idx, items.size());
  1860. if (items[p_idx].metadata == p_meta) {
  1861. return;
  1862. }
  1863. items.write[p_idx].metadata = p_meta;
  1864. child_controls_changed();
  1865. _menu_changed();
  1866. }
  1867. void PopupMenu::set_item_disabled(int p_idx, bool p_disabled) {
  1868. if (p_idx < 0) {
  1869. p_idx += get_item_count();
  1870. }
  1871. ERR_FAIL_INDEX(p_idx, items.size());
  1872. if (items[p_idx].disabled == p_disabled) {
  1873. return;
  1874. }
  1875. items.write[p_idx].disabled = p_disabled;
  1876. items.write[p_idx].accessibility_item_dirty = true;
  1877. if (global_menu.is_valid()) {
  1878. NativeMenu::get_singleton()->set_item_disabled(global_menu, p_idx, p_disabled);
  1879. }
  1880. queue_accessibility_update();
  1881. control->queue_redraw();
  1882. child_controls_changed();
  1883. _menu_changed();
  1884. }
  1885. void PopupMenu::set_item_submenu(int p_idx, const String &p_submenu) {
  1886. if (p_idx < 0) {
  1887. p_idx += get_item_count();
  1888. }
  1889. ERR_FAIL_INDEX(p_idx, items.size());
  1890. if (items[p_idx].submenu_name == p_submenu) {
  1891. return;
  1892. }
  1893. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(p_submenu));
  1894. ERR_FAIL_NULL_MSG(pm, vformat("Child PopupMenu \"%s\" does not exist.", p_submenu));
  1895. set_item_submenu_node(p_idx, pm);
  1896. }
  1897. void PopupMenu::set_item_submenu_node(int p_idx, PopupMenu *p_submenu) {
  1898. ERR_FAIL_NULL(p_submenu);
  1899. if (p_idx < 0) {
  1900. p_idx += get_item_count();
  1901. }
  1902. ERR_FAIL_INDEX(p_idx, items.size());
  1903. if (p_submenu->get_parent() != this) {
  1904. ERR_FAIL_COND_MSG(p_submenu->get_parent() != nullptr, vformat("The submenu \"%s\" already has a different parent.", p_submenu->get_name()));
  1905. add_child(p_submenu);
  1906. }
  1907. if (global_menu.is_valid()) {
  1908. if (items[p_idx].submenu_bound) {
  1909. PopupMenu *pm = items[p_idx].submenu;
  1910. if (pm) {
  1911. NativeMenu::get_singleton()->set_item_submenu(global_menu, p_idx, RID());
  1912. pm->unbind_global_menu();
  1913. }
  1914. items.write[p_idx].submenu_bound = false;
  1915. }
  1916. }
  1917. items.write[p_idx].submenu = p_submenu;
  1918. if (global_menu.is_valid()) {
  1919. PopupMenu *pm = items[p_idx].submenu;
  1920. if (pm) {
  1921. RID submenu_rid = pm->bind_global_menu();
  1922. NativeMenu::get_singleton()->set_item_submenu(global_menu, p_idx, submenu_rid);
  1923. items.write[p_idx].submenu_bound = true;
  1924. }
  1925. } else if (!p_submenu->is_connected("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden))) {
  1926. p_submenu->connect("popup_hide", callable_mp(this, &PopupMenu::_submenu_hidden));
  1927. }
  1928. control->queue_redraw();
  1929. child_controls_changed();
  1930. _menu_changed();
  1931. }
  1932. void PopupMenu::_close_suspended_timeout() {
  1933. if (submenu_over != -1 && is_embedded()) {
  1934. ERR_FAIL_INDEX_MSG(submenu_over, items.size(), vformat("Invalid submenu index %d in _close_suspended_timeout.", submenu_over));
  1935. if (items[submenu_over].submenu->is_visible()) {
  1936. items[submenu_over].submenu->_close_or_suspend();
  1937. }
  1938. }
  1939. if (active_submenu_index != -1 && items[active_submenu_index].submenu->is_visible()) {
  1940. // Closes the submenu if the mouse is moved off the parent item toward the submenu,
  1941. // but comes to a stop before reaching the submenu and the timeout is reached.
  1942. items[active_submenu_index].submenu->_close_pressed();
  1943. }
  1944. }
  1945. void PopupMenu::_submenu_hidden() {
  1946. // Ensure the submenu_timer is not running to avoid any race conditions between opening and closing submenus.
  1947. if (!submenu_timer->is_stopped()) {
  1948. WARN_VERBOSE("The submenu_timer should never be running when the _submenu_hidden signal is emitted.");
  1949. return;
  1950. }
  1951. if (active_submenu_index == -1) {
  1952. WARN_VERBOSE("The active_submenu_index should never be -1 when _submenu_hidden is entered.");
  1953. return;
  1954. }
  1955. active_submenu_index = -1;
  1956. submenu_over = -1;
  1957. submenu_mouse_exited_ticks_msec = -1;
  1958. mouse_movement_was_tested = false;
  1959. close_was_suspended = false;
  1960. queue_accessibility_update();
  1961. control->queue_redraw();
  1962. if (!activated_by_keyboard) {
  1963. Point2 mouse_pos = is_embedded() ? get_mouse_position() : Point2(DisplayServer::get_singleton()->mouse_get_position() - get_position());
  1964. _mouse_over_update(mouse_pos);
  1965. }
  1966. }
  1967. void PopupMenu::toggle_item_checked(int p_idx) {
  1968. ERR_FAIL_INDEX(p_idx, items.size());
  1969. items.write[p_idx].checked = !items[p_idx].checked;
  1970. items.write[p_idx].accessibility_item_dirty = true;
  1971. if (global_menu.is_valid()) {
  1972. NativeMenu::get_singleton()->set_item_checked(global_menu, p_idx, items[p_idx].checked);
  1973. }
  1974. queue_accessibility_update();
  1975. control->queue_redraw();
  1976. child_controls_changed();
  1977. _menu_changed();
  1978. }
  1979. String PopupMenu::get_item_text(int p_idx) const {
  1980. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1981. return items[p_idx].text;
  1982. }
  1983. String PopupMenu::get_item_xl_text(int p_idx) const {
  1984. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1985. return items[p_idx].xl_text;
  1986. }
  1987. Control::TextDirection PopupMenu::get_item_text_direction(int p_idx) const {
  1988. ERR_FAIL_INDEX_V(p_idx, items.size(), Control::TEXT_DIRECTION_INHERITED);
  1989. return items[p_idx].text_direction;
  1990. }
  1991. String PopupMenu::get_item_language(int p_idx) const {
  1992. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1993. return items[p_idx].language;
  1994. }
  1995. Node::AutoTranslateMode PopupMenu::get_item_auto_translate_mode(int p_idx) const {
  1996. ERR_FAIL_INDEX_V(p_idx, items.size(), AUTO_TRANSLATE_MODE_INHERIT);
  1997. return items[p_idx].auto_translate_mode;
  1998. }
  1999. int PopupMenu::get_item_idx_from_text(const String &text) const {
  2000. for (int idx = 0; idx < items.size(); idx++) {
  2001. if (items[idx].text == text) {
  2002. return idx;
  2003. }
  2004. }
  2005. return -1;
  2006. }
  2007. Ref<Texture2D> PopupMenu::get_item_icon(int p_idx) const {
  2008. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture2D>());
  2009. return items[p_idx].icon;
  2010. }
  2011. int PopupMenu::get_item_icon_max_width(int p_idx) const {
  2012. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  2013. return items[p_idx].icon_max_width;
  2014. }
  2015. Color PopupMenu::get_item_icon_modulate(int p_idx) const {
  2016. ERR_FAIL_INDEX_V(p_idx, items.size(), Color());
  2017. return items[p_idx].icon_modulate;
  2018. }
  2019. Key PopupMenu::get_item_accelerator(int p_idx) const {
  2020. ERR_FAIL_INDEX_V(p_idx, items.size(), Key::NONE);
  2021. return items[p_idx].accel;
  2022. }
  2023. Variant PopupMenu::get_item_metadata(int p_idx) const {
  2024. ERR_FAIL_INDEX_V(p_idx, items.size(), Variant());
  2025. return items[p_idx].metadata;
  2026. }
  2027. bool PopupMenu::is_item_disabled(int p_idx) const {
  2028. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  2029. return items[p_idx].disabled;
  2030. }
  2031. bool PopupMenu::is_item_checked(int p_idx) const {
  2032. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  2033. return items[p_idx].checked;
  2034. }
  2035. int PopupMenu::get_item_id(int p_idx) const {
  2036. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  2037. return items[p_idx].id;
  2038. }
  2039. int PopupMenu::get_item_index(int p_id) const {
  2040. for (int i = 0; i < items.size(); i++) {
  2041. if (items[i].id == p_id) {
  2042. return i;
  2043. }
  2044. }
  2045. return -1;
  2046. }
  2047. String PopupMenu::get_item_submenu(int p_idx) const {
  2048. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  2049. return items[p_idx].submenu_name;
  2050. }
  2051. PopupMenu *PopupMenu::get_item_submenu_node(int p_idx) const {
  2052. ERR_FAIL_INDEX_V(p_idx, items.size(), nullptr);
  2053. return items[p_idx].submenu;
  2054. }
  2055. String PopupMenu::get_item_tooltip(int p_idx) const {
  2056. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  2057. return items[p_idx].tooltip;
  2058. }
  2059. Ref<Shortcut> PopupMenu::get_item_shortcut(int p_idx) const {
  2060. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Shortcut>());
  2061. return items[p_idx].shortcut;
  2062. }
  2063. int PopupMenu::get_item_indent(int p_idx) const {
  2064. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  2065. return items[p_idx].indent;
  2066. }
  2067. int PopupMenu::get_item_max_states(int p_idx) const {
  2068. ERR_FAIL_INDEX_V(p_idx, items.size(), -1);
  2069. return items[p_idx].max_states;
  2070. }
  2071. int PopupMenu::get_item_state(int p_idx) const {
  2072. ERR_FAIL_INDEX_V(p_idx, items.size(), -1);
  2073. return items[p_idx].state;
  2074. }
  2075. void PopupMenu::set_item_as_separator(int p_idx, bool p_separator) {
  2076. if (p_idx < 0) {
  2077. p_idx += get_item_count();
  2078. }
  2079. ERR_FAIL_INDEX(p_idx, items.size());
  2080. if (items[p_idx].separator == p_separator) {
  2081. return;
  2082. }
  2083. items.write[p_idx].separator = p_separator;
  2084. items.write[p_idx].accessibility_item_dirty = true;
  2085. queue_accessibility_update();
  2086. control->queue_redraw();
  2087. }
  2088. bool PopupMenu::is_item_separator(int p_idx) const {
  2089. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  2090. return items[p_idx].separator;
  2091. }
  2092. void PopupMenu::set_item_as_checkable(int p_idx, bool p_checkable) {
  2093. if (p_idx < 0) {
  2094. p_idx += get_item_count();
  2095. }
  2096. ERR_FAIL_INDEX(p_idx, items.size());
  2097. int type = (int)(p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE);
  2098. if (type == items[p_idx].checkable_type) {
  2099. return;
  2100. }
  2101. items.write[p_idx].checkable_type = p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE;
  2102. items.write[p_idx].accessibility_item_dirty = true;
  2103. if (global_menu.is_valid()) {
  2104. NativeMenu::get_singleton()->set_item_checkable(global_menu, p_idx, p_checkable);
  2105. }
  2106. queue_accessibility_update();
  2107. control->queue_redraw();
  2108. _menu_changed();
  2109. }
  2110. void PopupMenu::set_item_as_radio_checkable(int p_idx, bool p_radio_checkable) {
  2111. if (p_idx < 0) {
  2112. p_idx += get_item_count();
  2113. }
  2114. ERR_FAIL_INDEX(p_idx, items.size());
  2115. int type = (int)(p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE);
  2116. if (type == items[p_idx].checkable_type) {
  2117. return;
  2118. }
  2119. items.write[p_idx].checkable_type = p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE;
  2120. items.write[p_idx].accessibility_item_dirty = true;
  2121. if (global_menu.is_valid()) {
  2122. NativeMenu::get_singleton()->set_item_radio_checkable(global_menu, p_idx, p_radio_checkable);
  2123. }
  2124. queue_accessibility_update();
  2125. control->queue_redraw();
  2126. _menu_changed();
  2127. }
  2128. void PopupMenu::set_item_tooltip(int p_idx, const String &p_tooltip) {
  2129. if (p_idx < 0) {
  2130. p_idx += get_item_count();
  2131. }
  2132. ERR_FAIL_INDEX(p_idx, items.size());
  2133. if (items[p_idx].tooltip == p_tooltip) {
  2134. return;
  2135. }
  2136. items.write[p_idx].tooltip = p_tooltip;
  2137. items.write[p_idx].accessibility_item_dirty = true;
  2138. if (global_menu.is_valid()) {
  2139. NativeMenu::get_singleton()->set_item_tooltip(global_menu, p_idx, p_tooltip);
  2140. }
  2141. queue_accessibility_update();
  2142. control->queue_redraw();
  2143. _menu_changed();
  2144. }
  2145. void PopupMenu::set_item_shortcut(int p_idx, const Ref<Shortcut> &p_shortcut, bool p_global) {
  2146. if (p_idx < 0) {
  2147. p_idx += get_item_count();
  2148. }
  2149. ERR_FAIL_INDEX(p_idx, items.size());
  2150. if (items[p_idx].shortcut == p_shortcut && items[p_idx].shortcut_is_global == p_global && items[p_idx].shortcut.is_valid() == p_shortcut.is_valid()) {
  2151. return;
  2152. }
  2153. if (items[p_idx].shortcut.is_valid()) {
  2154. _unref_shortcut(items[p_idx].shortcut);
  2155. }
  2156. items.write[p_idx].shortcut = p_shortcut;
  2157. items.write[p_idx].shortcut_is_global = p_global;
  2158. items.write[p_idx].dirty = true;
  2159. if (items[p_idx].shortcut.is_valid()) {
  2160. _ref_shortcut(items[p_idx].shortcut);
  2161. }
  2162. if (global_menu.is_valid()) {
  2163. NativeMenu *nmenu = NativeMenu::get_singleton();
  2164. nmenu->set_item_accelerator(global_menu, p_idx, Key::NONE);
  2165. if (!items[p_idx].shortcut_is_disabled && items[p_idx].shortcut.is_valid() && items[p_idx].shortcut->has_valid_event()) {
  2166. Array events = items[p_idx].shortcut->get_events();
  2167. for (int j = 0; j < events.size(); j++) {
  2168. Ref<InputEventKey> ie = events[j];
  2169. if (ie.is_valid() && _set_item_accelerator(p_idx, ie)) {
  2170. break;
  2171. }
  2172. }
  2173. if (p_global) {
  2174. nmenu->set_item_key_callback(global_menu, p_idx, callable_mp(this, &PopupMenu::activate_item));
  2175. } else {
  2176. nmenu->set_item_key_callback(global_menu, p_idx, Callable());
  2177. }
  2178. }
  2179. }
  2180. control->queue_redraw();
  2181. _menu_changed();
  2182. }
  2183. void PopupMenu::set_item_indent(int p_idx, int p_indent) {
  2184. if (p_idx < 0) {
  2185. p_idx += get_item_count();
  2186. }
  2187. ERR_FAIL_INDEX(p_idx, items.size());
  2188. if (items.write[p_idx].indent == p_indent) {
  2189. return;
  2190. }
  2191. items.write[p_idx].indent = p_indent;
  2192. if (global_menu.is_valid()) {
  2193. NativeMenu::get_singleton()->set_item_indentation_level(global_menu, p_idx, p_indent);
  2194. }
  2195. control->queue_redraw();
  2196. child_controls_changed();
  2197. _menu_changed();
  2198. }
  2199. void PopupMenu::set_item_max_states(int p_idx, int p_max_states) {
  2200. if (p_idx < 0) {
  2201. p_idx += get_item_count();
  2202. }
  2203. ERR_FAIL_INDEX(p_idx, items.size());
  2204. if (items[p_idx].max_states == p_max_states) {
  2205. return;
  2206. }
  2207. items.write[p_idx].max_states = p_max_states;
  2208. if (global_menu.is_valid()) {
  2209. NativeMenu::get_singleton()->set_item_max_states(global_menu, p_idx, p_max_states);
  2210. }
  2211. control->queue_redraw();
  2212. _menu_changed();
  2213. }
  2214. void PopupMenu::set_item_multistate(int p_idx, int p_state) {
  2215. if (p_idx < 0) {
  2216. p_idx += get_item_count();
  2217. }
  2218. ERR_FAIL_INDEX(p_idx, items.size());
  2219. if (items[p_idx].state == p_state) {
  2220. return;
  2221. }
  2222. items.write[p_idx].state = p_state;
  2223. items.write[p_idx].accessibility_item_dirty = true;
  2224. if (global_menu.is_valid()) {
  2225. NativeMenu::get_singleton()->set_item_state(global_menu, p_idx, p_state);
  2226. }
  2227. queue_accessibility_update();
  2228. control->queue_redraw();
  2229. _menu_changed();
  2230. }
  2231. void PopupMenu::set_item_shortcut_disabled(int p_idx, bool p_disabled) {
  2232. if (p_idx < 0) {
  2233. p_idx += get_item_count();
  2234. }
  2235. ERR_FAIL_INDEX(p_idx, items.size());
  2236. if (items[p_idx].shortcut_is_disabled == p_disabled) {
  2237. return;
  2238. }
  2239. items.write[p_idx].shortcut_is_disabled = p_disabled;
  2240. if (global_menu.is_valid()) {
  2241. NativeMenu *nmenu = NativeMenu::get_singleton();
  2242. nmenu->set_item_accelerator(global_menu, p_idx, Key::NONE);
  2243. if (!items[p_idx].shortcut_is_disabled && items[p_idx].shortcut.is_valid() && items[p_idx].shortcut->has_valid_event()) {
  2244. Array events = items[p_idx].shortcut->get_events();
  2245. for (int j = 0; j < events.size(); j++) {
  2246. Ref<InputEventKey> ie = events[j];
  2247. if (ie.is_valid() && _set_item_accelerator(p_idx, ie)) {
  2248. break;
  2249. }
  2250. }
  2251. }
  2252. }
  2253. control->queue_redraw();
  2254. _menu_changed();
  2255. }
  2256. void PopupMenu::toggle_item_multistate(int p_idx) {
  2257. ERR_FAIL_INDEX(p_idx, items.size());
  2258. if (0 >= items[p_idx].max_states) {
  2259. return;
  2260. }
  2261. ++items.write[p_idx].state;
  2262. if (items.write[p_idx].max_states <= items[p_idx].state) {
  2263. items.write[p_idx].state = 0;
  2264. }
  2265. items.write[p_idx].accessibility_item_dirty = true;
  2266. if (global_menu.is_valid()) {
  2267. NativeMenu::get_singleton()->set_item_state(global_menu, p_idx, items[p_idx].state);
  2268. }
  2269. queue_accessibility_update();
  2270. control->queue_redraw();
  2271. _menu_changed();
  2272. }
  2273. bool PopupMenu::is_item_checkable(int p_idx) const {
  2274. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  2275. return items[p_idx].checkable_type;
  2276. }
  2277. bool PopupMenu::is_item_radio_checkable(int p_idx) const {
  2278. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  2279. return items[p_idx].checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  2280. }
  2281. bool PopupMenu::is_item_shortcut_global(int p_idx) const {
  2282. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  2283. return items[p_idx].shortcut_is_global;
  2284. }
  2285. bool PopupMenu::is_item_shortcut_disabled(int p_idx) const {
  2286. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  2287. return items[p_idx].shortcut_is_disabled;
  2288. }
  2289. void PopupMenu::set_focused_item(int p_idx) {
  2290. if (p_idx != -1) {
  2291. ERR_FAIL_INDEX(p_idx, items.size());
  2292. }
  2293. if (mouse_over == p_idx) {
  2294. return;
  2295. }
  2296. prev_mouse_over = mouse_over;
  2297. mouse_over = p_idx;
  2298. if (mouse_over != -1) {
  2299. scroll_to_item(mouse_over);
  2300. }
  2301. queue_accessibility_update();
  2302. control->queue_redraw();
  2303. }
  2304. int PopupMenu::get_focused_item() const {
  2305. return mouse_over;
  2306. }
  2307. void PopupMenu::set_item_count(int p_count) {
  2308. ERR_FAIL_COND(p_count < 0);
  2309. int prev_size = items.size();
  2310. if (prev_size == p_count) {
  2311. return;
  2312. }
  2313. NativeMenu *nmenu = NativeMenu::get_singleton();
  2314. bool is_global = global_menu.is_valid();
  2315. if (is_global && prev_size > p_count) {
  2316. for (int i = prev_size - 1; i >= p_count; i--) {
  2317. nmenu->remove_item(global_menu, i);
  2318. if (items[i].accessibility_item_element.is_valid()) {
  2319. DisplayServer::get_singleton()->accessibility_free_element(items.write[i].accessibility_item_element);
  2320. items.write[i].accessibility_item_element = RID();
  2321. }
  2322. }
  2323. }
  2324. items.resize(p_count);
  2325. if (prev_size < p_count) {
  2326. for (int i = prev_size; i < p_count; i++) {
  2327. items.write[i].id = i;
  2328. if (is_global) {
  2329. nmenu->add_item(global_menu, String(), callable_mp(this, &PopupMenu::activate_item), Callable(), i);
  2330. }
  2331. }
  2332. }
  2333. control->queue_redraw();
  2334. child_controls_changed();
  2335. notify_property_list_changed();
  2336. _menu_changed();
  2337. }
  2338. int PopupMenu::get_item_count() const {
  2339. return items.size();
  2340. }
  2341. void PopupMenu::scroll_to_item(int p_idx) {
  2342. ERR_FAIL_INDEX(p_idx, items.size());
  2343. // Calculate the position of the item relative to the visible area.
  2344. int item_y = items[p_idx]._ofs_cache;
  2345. int visible_height = scroll_container->get_size().height;
  2346. int relative_y = item_y - scroll_container->get_v_scroll();
  2347. // If item is not fully visible, adjust scroll.
  2348. if (relative_y < 0) {
  2349. scroll_container->set_v_scroll(item_y);
  2350. } else if (relative_y + items[p_idx]._height_cache > visible_height) {
  2351. scroll_container->set_v_scroll(item_y + items[p_idx]._height_cache - visible_height);
  2352. }
  2353. }
  2354. void PopupMenu::set_prefer_native_menu(bool p_enabled) {
  2355. if (prefer_native != p_enabled) {
  2356. prefer_native = p_enabled;
  2357. if (prefer_native) {
  2358. bind_global_menu();
  2359. } else {
  2360. unbind_global_menu();
  2361. }
  2362. }
  2363. }
  2364. bool PopupMenu::is_prefer_native_menu() const {
  2365. return prefer_native;
  2366. }
  2367. bool PopupMenu::is_native_menu() const {
  2368. #ifdef TOOLS_ENABLED
  2369. if (is_part_of_edited_scene()) {
  2370. return false;
  2371. }
  2372. #endif
  2373. return global_menu.is_valid();
  2374. }
  2375. bool PopupMenu::activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only) {
  2376. ERR_FAIL_COND_V(p_event.is_null(), false);
  2377. Key code = Key::NONE;
  2378. Ref<InputEventKey> k = p_event;
  2379. if (k.is_valid()) {
  2380. code = k->get_keycode();
  2381. if (code == Key::NONE) {
  2382. code = (Key)k->get_unicode();
  2383. }
  2384. if (k->is_ctrl_pressed()) {
  2385. code |= KeyModifierMask::CTRL;
  2386. }
  2387. if (k->is_alt_pressed()) {
  2388. code |= KeyModifierMask::ALT;
  2389. }
  2390. if (k->is_meta_pressed()) {
  2391. code |= KeyModifierMask::META;
  2392. }
  2393. if (k->is_shift_pressed()) {
  2394. code |= KeyModifierMask::SHIFT;
  2395. }
  2396. }
  2397. for (int i = 0; i < items.size(); i++) {
  2398. if (is_item_disabled(i) || items[i].shortcut_is_disabled || (!items[i].allow_echo && p_event->is_echo())) {
  2399. continue;
  2400. }
  2401. if (items[i].shortcut.is_valid() && items[i].shortcut->matches_event(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) {
  2402. activate_item(i);
  2403. return true;
  2404. }
  2405. if (code != Key::NONE && items[i].accel == code) {
  2406. activate_item(i);
  2407. return true;
  2408. }
  2409. if (items[i].submenu) {
  2410. if (items[i].submenu->activate_item_by_event(p_event, p_for_global_only)) {
  2411. return true;
  2412. }
  2413. }
  2414. }
  2415. return false;
  2416. }
  2417. void PopupMenu::_about_to_popup() {
  2418. ERR_MAIN_THREAD_GUARD;
  2419. emit_signal(SNAME("about_to_popup"));
  2420. }
  2421. void PopupMenu::_about_to_close() {
  2422. ERR_MAIN_THREAD_GUARD;
  2423. emit_signal(SNAME("popup_hide"));
  2424. }
  2425. void PopupMenu::activate_item(int p_idx) {
  2426. ERR_FAIL_INDEX(p_idx, items.size());
  2427. ERR_FAIL_COND(items[p_idx].separator);
  2428. int id = items[p_idx].id >= 0 ? items[p_idx].id : p_idx;
  2429. //hide all parent PopupMenus
  2430. Node *next = get_parent();
  2431. PopupMenu *pop = Object::cast_to<PopupMenu>(next);
  2432. while (pop) {
  2433. // We close all parents that are chained together,
  2434. // with hide_on_item_selection enabled
  2435. if (items[p_idx].checkable_type) {
  2436. if (!hide_on_checkable_item_selection || !pop->is_hide_on_checkable_item_selection()) {
  2437. break;
  2438. }
  2439. } else if (0 < items[p_idx].max_states) {
  2440. if (!hide_on_multistate_item_selection || !pop->is_hide_on_multistate_item_selection()) {
  2441. break;
  2442. }
  2443. } else if (!hide_on_item_selection || !pop->is_hide_on_item_selection()) {
  2444. break;
  2445. }
  2446. pop->hide();
  2447. next = next->get_parent();
  2448. pop = Object::cast_to<PopupMenu>(next);
  2449. }
  2450. // Hides popup by default; unless otherwise specified
  2451. // by using set_hide_on_item_selection and set_hide_on_checkable_item_selection
  2452. bool need_hide = true;
  2453. if (items[p_idx].checkable_type) {
  2454. if (!hide_on_checkable_item_selection) {
  2455. need_hide = false;
  2456. }
  2457. } else if (0 < items[p_idx].max_states) {
  2458. if (!hide_on_multistate_item_selection) {
  2459. need_hide = false;
  2460. }
  2461. } else if (!hide_on_item_selection) {
  2462. need_hide = false;
  2463. }
  2464. if (need_hide) {
  2465. hide();
  2466. }
  2467. emit_signal(SceneStringName(id_pressed), id);
  2468. emit_signal(SNAME("index_pressed"), p_idx);
  2469. }
  2470. void PopupMenu::remove_item(int p_idx) {
  2471. ERR_FAIL_INDEX(p_idx, items.size());
  2472. if (items[p_idx].accessibility_item_element.is_valid()) {
  2473. DisplayServer::get_singleton()->accessibility_free_element(items.write[p_idx].accessibility_item_element);
  2474. items.write[p_idx].accessibility_item_element = RID();
  2475. }
  2476. if (items[p_idx].shortcut.is_valid()) {
  2477. _unref_shortcut(items[p_idx].shortcut);
  2478. }
  2479. items.remove_at(p_idx);
  2480. if (global_menu.is_valid()) {
  2481. NativeMenu::get_singleton()->remove_item(global_menu, p_idx);
  2482. }
  2483. control->queue_redraw();
  2484. child_controls_changed();
  2485. _menu_changed();
  2486. }
  2487. void PopupMenu::add_separator(const String &p_text, int p_id) {
  2488. Item sep;
  2489. sep.separator = true;
  2490. sep.id = p_id;
  2491. sep.accessibility_item_dirty = true;
  2492. if (!p_text.is_empty()) {
  2493. sep.text = p_text;
  2494. sep.xl_text = atr(p_text);
  2495. }
  2496. items.push_back(sep);
  2497. if (global_menu.is_valid()) {
  2498. NativeMenu::get_singleton()->add_separator(global_menu);
  2499. }
  2500. control->queue_redraw();
  2501. _menu_changed();
  2502. }
  2503. void PopupMenu::clear(bool p_free_submenus) {
  2504. for (Item &I : items) {
  2505. if (I.accessibility_item_element.is_valid()) {
  2506. DisplayServer::get_singleton()->accessibility_free_element(I.accessibility_item_element);
  2507. I.accessibility_item_element = RID();
  2508. }
  2509. if (I.shortcut.is_valid()) {
  2510. _unref_shortcut(I.shortcut);
  2511. }
  2512. if (p_free_submenus && I.submenu) {
  2513. remove_child(I.submenu);
  2514. I.submenu->queue_free();
  2515. }
  2516. }
  2517. if (global_menu.is_valid()) {
  2518. NativeMenu *nmenu = NativeMenu::get_singleton();
  2519. for (int i = items.size() - 1; i >= 0; i--) {
  2520. Item &item = items.write[i];
  2521. if (item.submenu) {
  2522. item.submenu->unbind_global_menu();
  2523. item.submenu_bound = false;
  2524. }
  2525. nmenu->remove_item(global_menu, i);
  2526. }
  2527. }
  2528. items.clear();
  2529. prev_mouse_over = -1;
  2530. mouse_over = -1;
  2531. queue_accessibility_update();
  2532. control->queue_redraw();
  2533. child_controls_changed();
  2534. notify_property_list_changed();
  2535. _menu_changed();
  2536. }
  2537. void PopupMenu::_ref_shortcut(Ref<Shortcut> p_sc) {
  2538. if (!shortcut_refcount.has(p_sc)) {
  2539. shortcut_refcount[p_sc] = 1;
  2540. p_sc->connect_changed(callable_mp(this, &PopupMenu::_shortcut_changed));
  2541. } else {
  2542. shortcut_refcount[p_sc] += 1;
  2543. }
  2544. }
  2545. void PopupMenu::_unref_shortcut(Ref<Shortcut> p_sc) {
  2546. ERR_FAIL_COND(!shortcut_refcount.has(p_sc));
  2547. shortcut_refcount[p_sc]--;
  2548. if (shortcut_refcount[p_sc] == 0) {
  2549. p_sc->disconnect_changed(callable_mp(this, &PopupMenu::_shortcut_changed));
  2550. shortcut_refcount.erase(p_sc);
  2551. }
  2552. }
  2553. void PopupMenu::_shortcut_changed() {
  2554. for (int i = 0; i < items.size(); i++) {
  2555. Item &item = items.write[i];
  2556. item.dirty = true;
  2557. if (global_menu.is_valid() && !item.separator) {
  2558. NativeMenu *nmenu = NativeMenu::get_singleton();
  2559. nmenu->set_item_accelerator(global_menu, i, Key::NONE);
  2560. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  2561. Array events = item.shortcut->get_events();
  2562. for (const Variant &v : events) {
  2563. Ref<InputEventKey> ie = v;
  2564. if (ie.is_valid() && _set_item_accelerator(i, ie)) {
  2565. break;
  2566. }
  2567. }
  2568. if (item.shortcut_is_global) {
  2569. nmenu->set_item_key_callback(global_menu, i, callable_mp(this, &PopupMenu::activate_item));
  2570. } else {
  2571. nmenu->set_item_key_callback(global_menu, i, Callable());
  2572. }
  2573. }
  2574. }
  2575. }
  2576. control->queue_redraw();
  2577. }
  2578. // Hide on item selection determines whether or not the popup will close after item selection
  2579. void PopupMenu::set_hide_on_item_selection(bool p_enabled) {
  2580. hide_on_item_selection = p_enabled;
  2581. }
  2582. bool PopupMenu::is_hide_on_item_selection() const {
  2583. return hide_on_item_selection;
  2584. }
  2585. void PopupMenu::set_hide_on_checkable_item_selection(bool p_enabled) {
  2586. hide_on_checkable_item_selection = p_enabled;
  2587. }
  2588. bool PopupMenu::is_hide_on_checkable_item_selection() const {
  2589. return hide_on_checkable_item_selection;
  2590. }
  2591. void PopupMenu::set_hide_on_multistate_item_selection(bool p_enabled) {
  2592. hide_on_multistate_item_selection = p_enabled;
  2593. }
  2594. bool PopupMenu::is_hide_on_multistate_item_selection() const {
  2595. return hide_on_multistate_item_selection;
  2596. }
  2597. void PopupMenu::set_submenu_popup_delay(float p_time) {
  2598. if (p_time <= 0) {
  2599. p_time = 0.01;
  2600. }
  2601. submenu_timer_popup_delay = p_time;
  2602. submenu_timer->set_wait_time(p_time);
  2603. }
  2604. float PopupMenu::get_submenu_popup_delay() const {
  2605. return submenu_timer->get_wait_time();
  2606. }
  2607. void PopupMenu::set_allow_search(bool p_allow) {
  2608. allow_search = p_allow;
  2609. }
  2610. bool PopupMenu::get_allow_search() const {
  2611. return allow_search;
  2612. }
  2613. String PopupMenu::get_tooltip(const Point2 &p_pos) const {
  2614. int over = _get_mouse_over(p_pos);
  2615. if (over < 0 || over >= items.size()) {
  2616. return "";
  2617. }
  2618. return items[over].tooltip;
  2619. }
  2620. #ifdef TOOLS_ENABLED
  2621. PackedStringArray PopupMenu::get_configuration_warnings() const {
  2622. PackedStringArray warnings = Popup::get_configuration_warnings();
  2623. if (!DisplayServer::get_singleton()->is_window_transparency_available() && !GLOBAL_GET_CACHED(bool, "display/window/subwindows/embed_subwindows")) {
  2624. Ref<StyleBoxFlat> sb = theme_cache.panel_style;
  2625. if (sb.is_valid() && (sb->get_shadow_size() > 0 || sb->get_corner_radius(CORNER_TOP_LEFT) > 0 || sb->get_corner_radius(CORNER_TOP_RIGHT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_LEFT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_RIGHT) > 0)) {
  2626. warnings.push_back(RTR("The current theme style has shadows and/or rounded corners for popups, but those won't display correctly if \"display/window/per_pixel_transparency/allowed\" isn't enabled in the Project Settings, nor if it isn't supported."));
  2627. }
  2628. }
  2629. return warnings;
  2630. }
  2631. #endif
  2632. void PopupMenu::add_autohide_area(const Rect2 &p_area) {
  2633. autohide_areas.push_back(p_area);
  2634. }
  2635. void PopupMenu::clear_autohide_areas() {
  2636. autohide_areas.clear();
  2637. }
  2638. bool PopupMenu::_set(const StringName &p_name, const Variant &p_value) {
  2639. if (property_helper.property_set_value(p_name, p_value)) {
  2640. return true;
  2641. }
  2642. #ifndef DISABLE_DEPRECATED
  2643. // Compatibility.
  2644. if (p_name == "items") {
  2645. Array arr = p_value;
  2646. ERR_FAIL_COND_V(arr.size() % 10, false);
  2647. clear();
  2648. for (int i = 0; i < arr.size(); i += 10) {
  2649. String text = arr[i + 0];
  2650. Ref<Texture2D> icon = arr[i + 1];
  2651. // For compatibility, use false/true for no/checkbox and integers for other values
  2652. bool checkable = arr[i + 2];
  2653. bool radio_checkable = (int)arr[i + 2] == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  2654. bool checked = arr[i + 3];
  2655. bool disabled = arr[i + 4];
  2656. int id = arr[i + 5];
  2657. int accel = arr[i + 6];
  2658. Variant meta = arr[i + 7];
  2659. String subm = arr[i + 8];
  2660. bool sep = arr[i + 9];
  2661. int idx = get_item_count();
  2662. add_item(text, id);
  2663. set_item_icon(idx, icon);
  2664. if (checkable) {
  2665. if (radio_checkable) {
  2666. set_item_as_radio_checkable(idx, true);
  2667. } else {
  2668. set_item_as_checkable(idx, true);
  2669. }
  2670. }
  2671. set_item_checked(idx, checked);
  2672. set_item_disabled(idx, disabled);
  2673. set_item_id(idx, id);
  2674. set_item_metadata(idx, meta);
  2675. set_item_as_separator(idx, sep);
  2676. set_item_accelerator(idx, (Key)accel);
  2677. set_item_submenu(idx, subm);
  2678. }
  2679. }
  2680. #endif
  2681. return false;
  2682. }
  2683. void PopupMenu::_bind_methods() {
  2684. ClassDB::bind_method(D_METHOD("activate_item_by_event", "event", "for_global_only"), &PopupMenu::activate_item_by_event, DEFVAL(false));
  2685. ClassDB::bind_method(D_METHOD("set_prefer_native_menu", "enabled"), &PopupMenu::set_prefer_native_menu);
  2686. ClassDB::bind_method(D_METHOD("is_prefer_native_menu"), &PopupMenu::is_prefer_native_menu);
  2687. ClassDB::bind_method(D_METHOD("is_native_menu"), &PopupMenu::is_native_menu);
  2688. ClassDB::bind_method(D_METHOD("add_item", "label", "id", "accel"), &PopupMenu::add_item, DEFVAL(-1), DEFVAL(0));
  2689. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_item, DEFVAL(-1), DEFVAL(0));
  2690. ClassDB::bind_method(D_METHOD("add_check_item", "label", "id", "accel"), &PopupMenu::add_check_item, DEFVAL(-1), DEFVAL(0));
  2691. ClassDB::bind_method(D_METHOD("add_icon_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_check_item, DEFVAL(-1), DEFVAL(0));
  2692. ClassDB::bind_method(D_METHOD("add_radio_check_item", "label", "id", "accel"), &PopupMenu::add_radio_check_item, DEFVAL(-1), DEFVAL(0));
  2693. ClassDB::bind_method(D_METHOD("add_icon_radio_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_radio_check_item, DEFVAL(-1), DEFVAL(0));
  2694. ClassDB::bind_method(D_METHOD("add_multistate_item", "label", "max_states", "default_state", "id", "accel"), &PopupMenu::add_multistate_item, DEFVAL(0), DEFVAL(-1), DEFVAL(0));
  2695. ClassDB::bind_method(D_METHOD("add_shortcut", "shortcut", "id", "global", "allow_echo"), &PopupMenu::add_shortcut, DEFVAL(-1), DEFVAL(false), DEFVAL(false));
  2696. ClassDB::bind_method(D_METHOD("add_icon_shortcut", "texture", "shortcut", "id", "global", "allow_echo"), &PopupMenu::add_icon_shortcut, DEFVAL(-1), DEFVAL(false), DEFVAL(false));
  2697. ClassDB::bind_method(D_METHOD("add_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2698. ClassDB::bind_method(D_METHOD("add_icon_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2699. ClassDB::bind_method(D_METHOD("add_radio_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_radio_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2700. ClassDB::bind_method(D_METHOD("add_icon_radio_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_radio_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2701. ClassDB::bind_method(D_METHOD("add_submenu_item", "label", "submenu", "id"), &PopupMenu::add_submenu_item, DEFVAL(-1));
  2702. ClassDB::bind_method(D_METHOD("add_submenu_node_item", "label", "submenu", "id"), &PopupMenu::add_submenu_node_item, DEFVAL(-1));
  2703. ClassDB::bind_method(D_METHOD("set_item_text", "index", "text"), &PopupMenu::set_item_text);
  2704. ClassDB::bind_method(D_METHOD("set_item_text_direction", "index", "direction"), &PopupMenu::set_item_text_direction);
  2705. ClassDB::bind_method(D_METHOD("set_item_language", "index", "language"), &PopupMenu::set_item_language);
  2706. ClassDB::bind_method(D_METHOD("set_item_auto_translate_mode", "index", "mode"), &PopupMenu::set_item_auto_translate_mode);
  2707. ClassDB::bind_method(D_METHOD("set_item_icon", "index", "icon"), &PopupMenu::set_item_icon);
  2708. ClassDB::bind_method(D_METHOD("set_item_icon_max_width", "index", "width"), &PopupMenu::set_item_icon_max_width);
  2709. ClassDB::bind_method(D_METHOD("set_item_icon_modulate", "index", "modulate"), &PopupMenu::set_item_icon_modulate);
  2710. ClassDB::bind_method(D_METHOD("set_item_checked", "index", "checked"), &PopupMenu::set_item_checked);
  2711. ClassDB::bind_method(D_METHOD("set_item_id", "index", "id"), &PopupMenu::set_item_id);
  2712. ClassDB::bind_method(D_METHOD("set_item_accelerator", "index", "accel"), &PopupMenu::set_item_accelerator);
  2713. ClassDB::bind_method(D_METHOD("set_item_metadata", "index", "metadata"), &PopupMenu::set_item_metadata);
  2714. ClassDB::bind_method(D_METHOD("set_item_disabled", "index", "disabled"), &PopupMenu::set_item_disabled);
  2715. ClassDB::bind_method(D_METHOD("set_item_submenu", "index", "submenu"), &PopupMenu::set_item_submenu);
  2716. ClassDB::bind_method(D_METHOD("set_item_submenu_node", "index", "submenu"), &PopupMenu::set_item_submenu_node);
  2717. ClassDB::bind_method(D_METHOD("set_item_as_separator", "index", "enable"), &PopupMenu::set_item_as_separator);
  2718. ClassDB::bind_method(D_METHOD("set_item_as_checkable", "index", "enable"), &PopupMenu::set_item_as_checkable);
  2719. ClassDB::bind_method(D_METHOD("set_item_as_radio_checkable", "index", "enable"), &PopupMenu::set_item_as_radio_checkable);
  2720. ClassDB::bind_method(D_METHOD("set_item_tooltip", "index", "tooltip"), &PopupMenu::set_item_tooltip);
  2721. ClassDB::bind_method(D_METHOD("set_item_shortcut", "index", "shortcut", "global"), &PopupMenu::set_item_shortcut, DEFVAL(false));
  2722. ClassDB::bind_method(D_METHOD("set_item_indent", "index", "indent"), &PopupMenu::set_item_indent);
  2723. ClassDB::bind_method(D_METHOD("set_item_multistate", "index", "state"), &PopupMenu::set_item_multistate);
  2724. ClassDB::bind_method(D_METHOD("set_item_multistate_max", "index", "max_states"), &PopupMenu::set_item_max_states);
  2725. ClassDB::bind_method(D_METHOD("set_item_shortcut_disabled", "index", "disabled"), &PopupMenu::set_item_shortcut_disabled);
  2726. ClassDB::bind_method(D_METHOD("toggle_item_checked", "index"), &PopupMenu::toggle_item_checked);
  2727. ClassDB::bind_method(D_METHOD("toggle_item_multistate", "index"), &PopupMenu::toggle_item_multistate);
  2728. ClassDB::bind_method(D_METHOD("get_item_text", "index"), &PopupMenu::get_item_text);
  2729. ClassDB::bind_method(D_METHOD("get_item_text_direction", "index"), &PopupMenu::get_item_text_direction);
  2730. ClassDB::bind_method(D_METHOD("get_item_language", "index"), &PopupMenu::get_item_language);
  2731. ClassDB::bind_method(D_METHOD("get_item_auto_translate_mode", "index"), &PopupMenu::get_item_auto_translate_mode);
  2732. ClassDB::bind_method(D_METHOD("get_item_icon", "index"), &PopupMenu::get_item_icon);
  2733. ClassDB::bind_method(D_METHOD("get_item_icon_max_width", "index"), &PopupMenu::get_item_icon_max_width);
  2734. ClassDB::bind_method(D_METHOD("get_item_icon_modulate", "index"), &PopupMenu::get_item_icon_modulate);
  2735. ClassDB::bind_method(D_METHOD("is_item_checked", "index"), &PopupMenu::is_item_checked);
  2736. ClassDB::bind_method(D_METHOD("get_item_id", "index"), &PopupMenu::get_item_id);
  2737. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &PopupMenu::get_item_index);
  2738. ClassDB::bind_method(D_METHOD("get_item_accelerator", "index"), &PopupMenu::get_item_accelerator);
  2739. ClassDB::bind_method(D_METHOD("get_item_metadata", "index"), &PopupMenu::get_item_metadata);
  2740. ClassDB::bind_method(D_METHOD("is_item_disabled", "index"), &PopupMenu::is_item_disabled);
  2741. ClassDB::bind_method(D_METHOD("get_item_submenu", "index"), &PopupMenu::get_item_submenu);
  2742. ClassDB::bind_method(D_METHOD("get_item_submenu_node", "index"), &PopupMenu::get_item_submenu_node);
  2743. ClassDB::bind_method(D_METHOD("is_item_separator", "index"), &PopupMenu::is_item_separator);
  2744. ClassDB::bind_method(D_METHOD("is_item_checkable", "index"), &PopupMenu::is_item_checkable);
  2745. ClassDB::bind_method(D_METHOD("is_item_radio_checkable", "index"), &PopupMenu::is_item_radio_checkable);
  2746. ClassDB::bind_method(D_METHOD("is_item_shortcut_disabled", "index"), &PopupMenu::is_item_shortcut_disabled);
  2747. ClassDB::bind_method(D_METHOD("get_item_tooltip", "index"), &PopupMenu::get_item_tooltip);
  2748. ClassDB::bind_method(D_METHOD("get_item_shortcut", "index"), &PopupMenu::get_item_shortcut);
  2749. ClassDB::bind_method(D_METHOD("get_item_indent", "index"), &PopupMenu::get_item_indent);
  2750. ClassDB::bind_method(D_METHOD("get_item_multistate_max", "index"), &PopupMenu::get_item_max_states);
  2751. ClassDB::bind_method(D_METHOD("get_item_multistate", "index"), &PopupMenu::get_item_state);
  2752. ClassDB::bind_method(D_METHOD("set_focused_item", "index"), &PopupMenu::set_focused_item);
  2753. ClassDB::bind_method(D_METHOD("get_focused_item"), &PopupMenu::get_focused_item);
  2754. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &PopupMenu::set_item_count);
  2755. ClassDB::bind_method(D_METHOD("get_item_count"), &PopupMenu::get_item_count);
  2756. ClassDB::bind_method(D_METHOD("scroll_to_item", "index"), &PopupMenu::scroll_to_item);
  2757. ClassDB::bind_method(D_METHOD("remove_item", "index"), &PopupMenu::remove_item);
  2758. ClassDB::bind_method(D_METHOD("add_separator", "label", "id"), &PopupMenu::add_separator, DEFVAL(String()), DEFVAL(-1));
  2759. ClassDB::bind_method(D_METHOD("clear", "free_submenus"), &PopupMenu::clear, DEFVAL(false));
  2760. ClassDB::bind_method(D_METHOD("set_hide_on_item_selection", "enable"), &PopupMenu::set_hide_on_item_selection);
  2761. ClassDB::bind_method(D_METHOD("is_hide_on_item_selection"), &PopupMenu::is_hide_on_item_selection);
  2762. ClassDB::bind_method(D_METHOD("set_hide_on_checkable_item_selection", "enable"), &PopupMenu::set_hide_on_checkable_item_selection);
  2763. ClassDB::bind_method(D_METHOD("is_hide_on_checkable_item_selection"), &PopupMenu::is_hide_on_checkable_item_selection);
  2764. ClassDB::bind_method(D_METHOD("set_hide_on_state_item_selection", "enable"), &PopupMenu::set_hide_on_multistate_item_selection);
  2765. ClassDB::bind_method(D_METHOD("is_hide_on_state_item_selection"), &PopupMenu::is_hide_on_multistate_item_selection);
  2766. ClassDB::bind_method(D_METHOD("set_submenu_popup_delay", "seconds"), &PopupMenu::set_submenu_popup_delay);
  2767. ClassDB::bind_method(D_METHOD("get_submenu_popup_delay"), &PopupMenu::get_submenu_popup_delay);
  2768. ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &PopupMenu::set_allow_search);
  2769. ClassDB::bind_method(D_METHOD("get_allow_search"), &PopupMenu::get_allow_search);
  2770. ClassDB::bind_method(D_METHOD("is_system_menu"), &PopupMenu::is_system_menu);
  2771. ClassDB::bind_method(D_METHOD("set_system_menu", "system_menu_id"), &PopupMenu::set_system_menu);
  2772. ClassDB::bind_method(D_METHOD("get_system_menu"), &PopupMenu::get_system_menu);
  2773. ClassDB::bind_method(D_METHOD("set_shrink_height", "shrink"), &PopupMenu::set_shrink_height);
  2774. ClassDB::bind_method(D_METHOD("get_shrink_height"), &PopupMenu::get_shrink_height);
  2775. ClassDB::bind_method(D_METHOD("set_shrink_width", "shrink"), &PopupMenu::set_shrink_width);
  2776. ClassDB::bind_method(D_METHOD("get_shrink_width"), &PopupMenu::get_shrink_width);
  2777. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_item_selection"), "set_hide_on_item_selection", "is_hide_on_item_selection");
  2778. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_checkable_item_selection"), "set_hide_on_checkable_item_selection", "is_hide_on_checkable_item_selection");
  2779. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_state_item_selection"), "set_hide_on_state_item_selection", "is_hide_on_state_item_selection");
  2780. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "submenu_popup_delay", PROPERTY_HINT_NONE, "suffix:s"), "set_submenu_popup_delay", "get_submenu_popup_delay");
  2781. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
  2782. ADD_PROPERTY(PropertyInfo(Variant::INT, "system_menu_id", PROPERTY_HINT_ENUM, "None:0,Application Menu:2,Window Menu:3,Help Menu:4,Dock:5"), "set_system_menu", "get_system_menu");
  2783. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "prefer_native_menu"), "set_prefer_native_menu", "is_prefer_native_menu");
  2784. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shrink_height"), "set_shrink_height", "get_shrink_height");
  2785. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shrink_width"), "set_shrink_width", "get_shrink_width");
  2786. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "item_");
  2787. ADD_SIGNAL(MethodInfo("id_pressed", PropertyInfo(Variant::INT, "id")));
  2788. ADD_SIGNAL(MethodInfo("id_focused", PropertyInfo(Variant::INT, "id")));
  2789. ADD_SIGNAL(MethodInfo("index_pressed", PropertyInfo(Variant::INT, "index")));
  2790. ADD_SIGNAL(MethodInfo("menu_changed"));
  2791. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, panel_style, "panel");
  2792. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, hover_style, "hover");
  2793. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, separator_style, "separator");
  2794. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, labeled_separator_left);
  2795. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, labeled_separator_right);
  2796. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, v_separation);
  2797. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, h_separation);
  2798. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, indent);
  2799. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, item_start_padding);
  2800. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, item_end_padding);
  2801. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, icon_max_width);
  2802. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, gutter_compact);
  2803. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, checked);
  2804. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, checked_disabled);
  2805. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, unchecked);
  2806. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, unchecked_disabled);
  2807. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_checked);
  2808. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_checked_disabled);
  2809. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_unchecked);
  2810. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_unchecked_disabled);
  2811. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, submenu);
  2812. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, submenu_mirrored);
  2813. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, PopupMenu, font);
  2814. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, PopupMenu, font_size);
  2815. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, PopupMenu, font_separator);
  2816. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, PopupMenu, font_separator_size);
  2817. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_color);
  2818. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_hover_color);
  2819. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_disabled_color);
  2820. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_accelerator_color);
  2821. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, PopupMenu, font_outline_size, "outline_size");
  2822. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_outline_color);
  2823. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_separator_color);
  2824. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, PopupMenu, font_separator_outline_size, "separator_outline_size");
  2825. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_separator_outline_color);
  2826. Item defaults(true);
  2827. base_property_helper.set_prefix("item_");
  2828. base_property_helper.set_array_length_getter(&PopupMenu::get_item_count);
  2829. base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text, &PopupMenu::set_item_text, &PopupMenu::get_item_text);
  2830. base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &PopupMenu::set_item_icon, &PopupMenu::get_item_icon);
  2831. base_property_helper.register_property(PropertyInfo(Variant::INT, "checkable", PROPERTY_HINT_ENUM, "No,As checkbox,As radio button"), defaults.checkable_type, &PopupMenu::_set_item_checkable_type, &PopupMenu::_get_item_checkable_type);
  2832. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "checked"), defaults.checked, &PopupMenu::set_item_checked, &PopupMenu::is_item_checked);
  2833. base_property_helper.register_property(PropertyInfo(Variant::INT, "id", PROPERTY_HINT_RANGE, "0,10,1,or_greater", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL), defaults.id, &PopupMenu::set_item_id, &PopupMenu::get_item_id);
  2834. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &PopupMenu::set_item_disabled, &PopupMenu::is_item_disabled);
  2835. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "separator"), defaults.separator, &PopupMenu::set_item_as_separator, &PopupMenu::is_item_separator);
  2836. PropertyListHelper::register_base_helper(&base_property_helper);
  2837. }
  2838. void PopupMenu::_native_popup(const Rect2i &p_rect) {
  2839. Point2i popup_pos = p_rect.position;
  2840. if (is_embedded()) {
  2841. popup_pos = get_embedder()->get_screen_transform().xform(popup_pos); // Note: for embedded windows "screen transform" is transform relative to embedder not the actual screen.
  2842. DisplayServer::WindowID wid = get_window_id();
  2843. if (wid == DisplayServer::INVALID_WINDOW_ID) {
  2844. wid = DisplayServer::MAIN_WINDOW_ID;
  2845. }
  2846. popup_pos += DisplayServer::get_singleton()->window_get_position(wid);
  2847. }
  2848. float win_scale = get_parent_visible_window()->get_content_scale_factor();
  2849. NativeMenu::get_singleton()->set_minimum_width(global_menu, p_rect.size.x * win_scale);
  2850. NativeMenu::get_singleton()->popup(global_menu, popup_pos);
  2851. }
  2852. String PopupMenu::_atr(int p_idx, const String &p_text) const {
  2853. ERR_FAIL_INDEX_V(p_idx, items.size(), atr(p_text));
  2854. switch (items[p_idx].auto_translate_mode) {
  2855. case AUTO_TRANSLATE_MODE_INHERIT: {
  2856. return atr(p_text);
  2857. } break;
  2858. case AUTO_TRANSLATE_MODE_ALWAYS: {
  2859. return tr(p_text);
  2860. } break;
  2861. case AUTO_TRANSLATE_MODE_DISABLED: {
  2862. return p_text;
  2863. } break;
  2864. }
  2865. ERR_FAIL_V_MSG(atr(p_text), "Unexpected auto translate mode: " + itos(items[p_idx].auto_translate_mode));
  2866. }
  2867. void PopupMenu::_popup_base(const Rect2i &p_bounds) {
  2868. bool native = global_menu.is_valid();
  2869. #ifdef TOOLS_ENABLED
  2870. if (is_part_of_edited_scene()) {
  2871. native = false;
  2872. }
  2873. #endif
  2874. if (native) {
  2875. _native_popup(p_bounds != Rect2i() ? p_bounds : Rect2i(get_position(), Size2i()));
  2876. } else {
  2877. if (is_inside_tree()) {
  2878. set_flag(FLAG_POPUP, true);
  2879. set_flag(FLAG_NO_FOCUS, !is_embedded());
  2880. }
  2881. moved = Vector2();
  2882. popup_time_msec = OS::get_singleton()->get_ticks_msec();
  2883. Popup::_popup_base(p_bounds);
  2884. }
  2885. }
  2886. void PopupMenu::set_shrink_height(bool p_shrink) {
  2887. shrink_height = p_shrink;
  2888. }
  2889. bool PopupMenu::get_shrink_height() const {
  2890. return shrink_height;
  2891. }
  2892. void PopupMenu::set_shrink_width(bool p_shrink) {
  2893. shrink_width = p_shrink;
  2894. }
  2895. bool PopupMenu::get_shrink_width() const {
  2896. return shrink_width;
  2897. }
  2898. void PopupMenu::_pre_popup() {
  2899. real_t popup_scale = 1.0;
  2900. bool scale_with_parent = true;
  2901. // Disable content scaling to avoid too tiny or too big menus when using GraphEdit zoom, applied only if menu is a child of GraphElement.
  2902. Node *p = get_parent();
  2903. while (p) {
  2904. GraphElement *ge = Object::cast_to<GraphElement>(p);
  2905. if (ge) {
  2906. scale_with_parent = ge->is_scaling_menus();
  2907. break;
  2908. }
  2909. p = p->get_parent();
  2910. }
  2911. Viewport *vp = get_parent_viewport();
  2912. if (scale_with_parent && vp) {
  2913. Size2 scale = is_embedded() ? vp->get_popup_base_transform().get_scale() : vp->get_popup_base_transform_native().get_scale();
  2914. CanvasItem *c = Object::cast_to<CanvasItem>(get_parent());
  2915. if (c) {
  2916. scale *= c->get_global_transform_with_canvas().get_scale();
  2917. }
  2918. popup_scale = MIN(scale.x, scale.y);
  2919. }
  2920. set_content_scale_factor(popup_scale);
  2921. if (is_wrapping_controls()) {
  2922. Size2 minsize = get_contents_minimum_size() * popup_scale;
  2923. Size2 maxsize = get_max_size();
  2924. if (maxsize.height > 0) {
  2925. minsize.height = MIN(minsize.height, maxsize.height);
  2926. }
  2927. if (maxsize.width > 0) {
  2928. minsize.width = MIN(minsize.width, maxsize.width);
  2929. }
  2930. minsize.height = Math::ceil(minsize.height); // Ensures enough height at fractional content scales to prevent the v_scroll_bar from showing.
  2931. set_min_size(minsize); // `height` is truncated here by the cast to Size2i for Window.min_size.
  2932. Size2i sz = get_size(); // Shrinkwraps to min size.
  2933. set_size(Vector2i(shrink_width ? 0 : sz.x, shrink_height ? 0 : sz.y));
  2934. }
  2935. }
  2936. void PopupMenu::set_visible(bool p_visible) {
  2937. bool native = global_menu.is_valid();
  2938. #ifdef TOOLS_ENABLED
  2939. if (is_part_of_edited_scene()) {
  2940. native = false;
  2941. }
  2942. #endif
  2943. if (native) {
  2944. if (p_visible) {
  2945. _native_popup(Rect2i(get_position(), get_size()));
  2946. }
  2947. } else {
  2948. if (p_visible && is_inside_tree()) {
  2949. set_flag(FLAG_POPUP, true);
  2950. set_flag(FLAG_NO_FOCUS, !is_embedded());
  2951. }
  2952. Popup::set_visible(p_visible);
  2953. }
  2954. }
  2955. PopupMenu::PopupMenu() {
  2956. set_flag(FLAG_TRANSPARENT, true);
  2957. // The panel used to draw the panel style.
  2958. panel = memnew(PanelContainer);
  2959. panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2960. add_child(panel, false, INTERNAL_MODE_FRONT);
  2961. // Scroll Container
  2962. scroll_container = memnew(ScrollContainer);
  2963. scroll_container->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2964. panel->add_child(scroll_container, false, INTERNAL_MODE_FRONT);
  2965. // The control which will display the items
  2966. control = memnew(Control);
  2967. control->set_clip_contents(false);
  2968. control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2969. control->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2970. control->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2971. scroll_container->add_child(control, false, INTERNAL_MODE_FRONT);
  2972. control->connect(SceneStringName(draw), callable_mp(this, &PopupMenu::_draw_items));
  2973. submenu_timer = memnew(Timer);
  2974. submenu_timer->set_wait_time(submenu_timer_popup_delay); // Default is 0.2.
  2975. submenu_timer->set_one_shot(true);
  2976. submenu_timer->connect("timeout", callable_mp(this, &PopupMenu::_submenu_timeout));
  2977. add_child(submenu_timer, false, INTERNAL_MODE_FRONT);
  2978. close_suspended_timer = memnew(Timer);
  2979. close_suspended_timer->set_wait_time(CLOSE_SUSPENDED_TIMER_DELAY);
  2980. close_suspended_timer->set_one_shot(true);
  2981. close_suspended_timer->connect("timeout", callable_mp(this, &PopupMenu::_close_suspended_timeout));
  2982. add_child(close_suspended_timer, false, INTERNAL_MODE_FRONT);
  2983. property_helper.setup_for_instance(base_property_helper, this);
  2984. #ifdef TOOLS_ENABLED
  2985. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp((Node *)this, &Node::update_configuration_warnings));
  2986. #endif
  2987. }
  2988. PopupMenu::~PopupMenu() {
  2989. unbind_global_menu();
  2990. }