window.cpp 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150
  1. /**************************************************************************/
  2. /* window.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 "window.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/shortcut.h"
  33. #include "core/string/translation_server.h"
  34. #include "scene/gui/control.h"
  35. #include "scene/theme/theme_db.h"
  36. #include "scene/theme/theme_owner.h"
  37. // Editor integration.
  38. int Window::root_layout_direction = 0;
  39. void Window::set_root_layout_direction(int p_root_dir) {
  40. root_layout_direction = p_root_dir;
  41. }
  42. // Dynamic properties.
  43. bool Window::_set(const StringName &p_name, const Variant &p_value) {
  44. ERR_MAIN_THREAD_GUARD_V(false);
  45. String name = p_name;
  46. if (!name.begins_with("theme_override")) {
  47. return false;
  48. }
  49. if (p_value.get_type() == Variant::NIL || (p_value.get_type() == Variant::OBJECT && (Object *)p_value == nullptr)) {
  50. if (name.begins_with("theme_override_icons/")) {
  51. String dname = name.get_slicec('/', 1);
  52. if (theme_icon_override.has(dname)) {
  53. theme_icon_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  54. }
  55. theme_icon_override.erase(dname);
  56. _notify_theme_override_changed();
  57. } else if (name.begins_with("theme_override_styles/")) {
  58. String dname = name.get_slicec('/', 1);
  59. if (theme_style_override.has(dname)) {
  60. theme_style_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  61. }
  62. theme_style_override.erase(dname);
  63. _notify_theme_override_changed();
  64. } else if (name.begins_with("theme_override_fonts/")) {
  65. String dname = name.get_slicec('/', 1);
  66. if (theme_font_override.has(dname)) {
  67. theme_font_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  68. }
  69. theme_font_override.erase(dname);
  70. _notify_theme_override_changed();
  71. } else if (name.begins_with("theme_override_font_sizes/")) {
  72. String dname = name.get_slicec('/', 1);
  73. theme_font_size_override.erase(dname);
  74. _notify_theme_override_changed();
  75. } else if (name.begins_with("theme_override_colors/")) {
  76. String dname = name.get_slicec('/', 1);
  77. theme_color_override.erase(dname);
  78. _notify_theme_override_changed();
  79. } else if (name.begins_with("theme_override_constants/")) {
  80. String dname = name.get_slicec('/', 1);
  81. theme_constant_override.erase(dname);
  82. _notify_theme_override_changed();
  83. } else {
  84. return false;
  85. }
  86. } else {
  87. if (name.begins_with("theme_override_icons/")) {
  88. String dname = name.get_slicec('/', 1);
  89. add_theme_icon_override(dname, p_value);
  90. } else if (name.begins_with("theme_override_styles/")) {
  91. String dname = name.get_slicec('/', 1);
  92. add_theme_style_override(dname, p_value);
  93. } else if (name.begins_with("theme_override_fonts/")) {
  94. String dname = name.get_slicec('/', 1);
  95. add_theme_font_override(dname, p_value);
  96. } else if (name.begins_with("theme_override_font_sizes/")) {
  97. String dname = name.get_slicec('/', 1);
  98. add_theme_font_size_override(dname, p_value);
  99. } else if (name.begins_with("theme_override_colors/")) {
  100. String dname = name.get_slicec('/', 1);
  101. add_theme_color_override(dname, p_value);
  102. } else if (name.begins_with("theme_override_constants/")) {
  103. String dname = name.get_slicec('/', 1);
  104. add_theme_constant_override(dname, p_value);
  105. } else {
  106. return false;
  107. }
  108. }
  109. return true;
  110. }
  111. bool Window::_get(const StringName &p_name, Variant &r_ret) const {
  112. ERR_READ_THREAD_GUARD_V(false);
  113. String sname = p_name;
  114. if (!sname.begins_with("theme_override")) {
  115. return false;
  116. }
  117. if (sname.begins_with("theme_override_icons/")) {
  118. String name = sname.get_slicec('/', 1);
  119. r_ret = theme_icon_override.has(name) ? Variant(theme_icon_override[name]) : Variant();
  120. } else if (sname.begins_with("theme_override_styles/")) {
  121. String name = sname.get_slicec('/', 1);
  122. r_ret = theme_style_override.has(name) ? Variant(theme_style_override[name]) : Variant();
  123. } else if (sname.begins_with("theme_override_fonts/")) {
  124. String name = sname.get_slicec('/', 1);
  125. r_ret = theme_font_override.has(name) ? Variant(theme_font_override[name]) : Variant();
  126. } else if (sname.begins_with("theme_override_font_sizes/")) {
  127. String name = sname.get_slicec('/', 1);
  128. r_ret = theme_font_size_override.has(name) ? Variant(theme_font_size_override[name]) : Variant();
  129. } else if (sname.begins_with("theme_override_colors/")) {
  130. String name = sname.get_slicec('/', 1);
  131. r_ret = theme_color_override.has(name) ? Variant(theme_color_override[name]) : Variant();
  132. } else if (sname.begins_with("theme_override_constants/")) {
  133. String name = sname.get_slicec('/', 1);
  134. r_ret = theme_constant_override.has(name) ? Variant(theme_constant_override[name]) : Variant();
  135. } else {
  136. return false;
  137. }
  138. return true;
  139. }
  140. void Window::_get_property_list(List<PropertyInfo> *p_list) const {
  141. ERR_READ_THREAD_GUARD;
  142. Ref<Theme> default_theme = ThemeDB::get_singleton()->get_default_theme();
  143. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Theme Overrides", "theme_override_"), PROPERTY_HINT_NONE, "theme_override_", PROPERTY_USAGE_GROUP));
  144. {
  145. List<StringName> names;
  146. default_theme->get_color_list(get_class_name(), &names);
  147. for (const StringName &E : names) {
  148. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  149. if (theme_color_override.has(E)) {
  150. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  151. }
  152. p_list->push_back(PropertyInfo(Variant::COLOR, PNAME("theme_override_colors") + String("/") + E, PROPERTY_HINT_NONE, "", usage));
  153. }
  154. }
  155. {
  156. List<StringName> names;
  157. default_theme->get_constant_list(get_class_name(), &names);
  158. for (const StringName &E : names) {
  159. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  160. if (theme_constant_override.has(E)) {
  161. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  162. }
  163. p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_constants") + String("/") + E, PROPERTY_HINT_RANGE, "-16384,16384", usage));
  164. }
  165. }
  166. {
  167. List<StringName> names;
  168. default_theme->get_font_list(get_class_name(), &names);
  169. for (const StringName &E : names) {
  170. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  171. if (theme_font_override.has(E)) {
  172. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  173. }
  174. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_fonts") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "Font", usage));
  175. }
  176. }
  177. {
  178. List<StringName> names;
  179. default_theme->get_font_size_list(get_class_name(), &names);
  180. for (const StringName &E : names) {
  181. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  182. if (theme_font_size_override.has(E)) {
  183. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  184. }
  185. p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_font_sizes") + String("/") + E, PROPERTY_HINT_RANGE, "1,256,1,or_greater,suffix:px", usage));
  186. }
  187. }
  188. {
  189. List<StringName> names;
  190. default_theme->get_icon_list(get_class_name(), &names);
  191. for (const StringName &E : names) {
  192. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  193. if (theme_icon_override.has(E)) {
  194. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  195. }
  196. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_icons") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", usage));
  197. }
  198. }
  199. {
  200. List<StringName> names;
  201. default_theme->get_stylebox_list(get_class_name(), &names);
  202. for (const StringName &E : names) {
  203. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  204. if (theme_style_override.has(E)) {
  205. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  206. }
  207. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_styles") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", usage));
  208. }
  209. }
  210. }
  211. void Window::_validate_property(PropertyInfo &p_property) const {
  212. if (p_property.name == "position" && initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
  213. p_property.usage = PROPERTY_USAGE_NONE;
  214. }
  215. if (p_property.name == "current_screen" && initial_position != WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN) {
  216. p_property.usage = PROPERTY_USAGE_NONE;
  217. }
  218. if (p_property.name == "theme_type_variation") {
  219. List<StringName> names;
  220. // Only the default theme and the project theme are used for the list of options.
  221. // This is an imposed limitation to simplify the logic needed to leverage those options.
  222. ThemeDB::get_singleton()->get_default_theme()->get_type_variation_list(get_class_name(), &names);
  223. if (ThemeDB::get_singleton()->get_project_theme().is_valid()) {
  224. ThemeDB::get_singleton()->get_project_theme()->get_type_variation_list(get_class_name(), &names);
  225. }
  226. names.sort_custom<StringName::AlphCompare>();
  227. Vector<StringName> unique_names;
  228. String hint_string;
  229. for (const StringName &E : names) {
  230. // Skip duplicate values.
  231. if (unique_names.has(E)) {
  232. continue;
  233. }
  234. hint_string += String(E) + ",";
  235. unique_names.append(E);
  236. }
  237. p_property.hint_string = hint_string;
  238. }
  239. }
  240. //
  241. Window *Window::get_from_id(DisplayServer::WindowID p_window_id) {
  242. if (p_window_id == DisplayServer::INVALID_WINDOW_ID) {
  243. return nullptr;
  244. }
  245. return Object::cast_to<Window>(ObjectDB::get_instance(DisplayServer::get_singleton()->window_get_attached_instance_id(p_window_id)));
  246. }
  247. void Window::set_title(const String &p_title) {
  248. ERR_MAIN_THREAD_GUARD;
  249. title = p_title;
  250. tr_title = atr(p_title);
  251. #ifdef DEBUG_ENABLED
  252. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  253. // Append a suffix to the window title to denote that the project is running
  254. // from a debug build (including the editor). Since this results in lower performance,
  255. // this should be clearly presented to the user.
  256. tr_title = vformat("%s (DEBUG)", tr_title);
  257. }
  258. #endif
  259. if (embedder) {
  260. embedder->_sub_window_update(this);
  261. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  262. DisplayServer::get_singleton()->window_set_title(tr_title, window_id);
  263. if (keep_title_visible) {
  264. Size2i title_size = DisplayServer::get_singleton()->window_get_title_size(tr_title, window_id);
  265. Size2i size_limit = get_clamped_minimum_size();
  266. if (title_size.x > size_limit.x || title_size.y > size_limit.y) {
  267. _update_window_size();
  268. }
  269. }
  270. }
  271. }
  272. String Window::get_title() const {
  273. ERR_READ_THREAD_GUARD_V(String());
  274. return title;
  275. }
  276. String Window::get_translated_title() const {
  277. ERR_READ_THREAD_GUARD_V(String());
  278. return tr_title;
  279. }
  280. void Window::_settings_changed() {
  281. if (visible && initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE && is_in_edited_scene_root()) {
  282. Size2 screen_size = Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
  283. position = (screen_size - size) / 2;
  284. if (embedder) {
  285. embedder->_sub_window_update(this);
  286. }
  287. }
  288. }
  289. void Window::set_initial_position(Window::WindowInitialPosition p_initial_position) {
  290. ERR_MAIN_THREAD_GUARD;
  291. initial_position = p_initial_position;
  292. _settings_changed();
  293. notify_property_list_changed();
  294. }
  295. Window::WindowInitialPosition Window::get_initial_position() const {
  296. ERR_READ_THREAD_GUARD_V(WINDOW_INITIAL_POSITION_ABSOLUTE);
  297. return initial_position;
  298. }
  299. void Window::set_current_screen(int p_screen) {
  300. ERR_MAIN_THREAD_GUARD;
  301. current_screen = p_screen;
  302. if (window_id == DisplayServer::INVALID_WINDOW_ID) {
  303. return;
  304. }
  305. DisplayServer::get_singleton()->window_set_current_screen(p_screen, window_id);
  306. }
  307. int Window::get_current_screen() const {
  308. ERR_READ_THREAD_GUARD_V(0);
  309. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  310. current_screen = DisplayServer::get_singleton()->window_get_current_screen(window_id);
  311. }
  312. return current_screen;
  313. }
  314. void Window::set_position(const Point2i &p_position) {
  315. ERR_MAIN_THREAD_GUARD;
  316. position = p_position;
  317. if (embedder) {
  318. embedder->_sub_window_update(this);
  319. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  320. DisplayServer::get_singleton()->window_set_position(p_position, window_id);
  321. }
  322. }
  323. Point2i Window::get_position() const {
  324. ERR_READ_THREAD_GUARD_V(Point2i());
  325. return position;
  326. }
  327. void Window::move_to_center() {
  328. ERR_MAIN_THREAD_GUARD;
  329. ERR_FAIL_COND(!is_inside_tree());
  330. Rect2 parent_rect;
  331. if (is_embedded()) {
  332. parent_rect = get_embedder()->get_visible_rect();
  333. } else {
  334. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(get_window_id());
  335. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  336. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  337. }
  338. if (parent_rect != Rect2()) {
  339. set_position(parent_rect.position + (parent_rect.size - get_size()) / 2);
  340. }
  341. }
  342. void Window::set_size(const Size2i &p_size) {
  343. ERR_MAIN_THREAD_GUARD;
  344. #if defined(ANDROID_ENABLED)
  345. if (!get_parent()) {
  346. // Can't set root window size on Android.
  347. return;
  348. }
  349. #endif
  350. size = p_size;
  351. _update_window_size();
  352. _settings_changed();
  353. }
  354. Size2i Window::get_size() const {
  355. ERR_READ_THREAD_GUARD_V(Size2i());
  356. return size;
  357. }
  358. void Window::reset_size() {
  359. ERR_MAIN_THREAD_GUARD;
  360. set_size(Size2i());
  361. }
  362. Point2i Window::get_position_with_decorations() const {
  363. ERR_READ_THREAD_GUARD_V(Point2i());
  364. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  365. return DisplayServer::get_singleton()->window_get_position_with_decorations(window_id);
  366. }
  367. if (visible && is_embedded() && !get_flag(Window::FLAG_BORDERLESS)) {
  368. Size2 border_offset;
  369. if (theme_cache.embedded_border.is_valid()) {
  370. border_offset = theme_cache.embedded_border->get_offset();
  371. }
  372. if (theme_cache.embedded_unfocused_border.is_valid()) {
  373. border_offset = border_offset.max(theme_cache.embedded_unfocused_border->get_offset());
  374. }
  375. return position - border_offset;
  376. }
  377. return position;
  378. }
  379. Size2i Window::get_size_with_decorations() const {
  380. ERR_READ_THREAD_GUARD_V(Size2i());
  381. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  382. return DisplayServer::get_singleton()->window_get_size_with_decorations(window_id);
  383. }
  384. if (visible && is_embedded() && !get_flag(Window::FLAG_BORDERLESS)) {
  385. Size2 border_size;
  386. if (theme_cache.embedded_border.is_valid()) {
  387. border_size = theme_cache.embedded_border->get_minimum_size();
  388. }
  389. if (theme_cache.embedded_unfocused_border.is_valid()) {
  390. border_size = border_size.max(theme_cache.embedded_unfocused_border->get_minimum_size());
  391. }
  392. return size + border_size;
  393. }
  394. return size;
  395. }
  396. Size2i Window::_clamp_limit_size(const Size2i &p_limit_size) {
  397. // Force window limits to respect size limitations of rendering server.
  398. Size2i max_window_size = RS::get_singleton()->get_maximum_viewport_size();
  399. if (max_window_size != Size2i()) {
  400. return p_limit_size.clamp(Vector2i(), max_window_size);
  401. } else {
  402. return p_limit_size.maxi(0);
  403. }
  404. }
  405. void Window::_validate_limit_size() {
  406. // When max_size is invalid, max_size_used falls back to respect size limitations of rendering server.
  407. bool max_size_valid = (max_size.x > 0 || max_size.y > 0) && max_size.x >= min_size.x && max_size.y >= min_size.y;
  408. max_size_used = max_size_valid ? max_size : RS::get_singleton()->get_maximum_viewport_size();
  409. }
  410. void Window::set_max_size(const Size2i &p_max_size) {
  411. ERR_MAIN_THREAD_GUARD;
  412. #if defined(ANDROID_ENABLED)
  413. if (!get_parent()) {
  414. // Can't set root window size on Android.
  415. return;
  416. }
  417. #endif
  418. Size2i max_size_clamped = _clamp_limit_size(p_max_size);
  419. if (max_size == max_size_clamped) {
  420. return;
  421. }
  422. max_size = max_size_clamped;
  423. _validate_limit_size();
  424. _update_window_size();
  425. }
  426. Size2i Window::get_max_size() const {
  427. ERR_READ_THREAD_GUARD_V(Size2i());
  428. return max_size;
  429. }
  430. void Window::set_min_size(const Size2i &p_min_size) {
  431. ERR_MAIN_THREAD_GUARD;
  432. #if defined(ANDROID_ENABLED)
  433. if (!get_parent()) {
  434. // Can't set root window size on Android.
  435. return;
  436. }
  437. #endif
  438. Size2i min_size_clamped = _clamp_limit_size(p_min_size);
  439. if (min_size == min_size_clamped) {
  440. return;
  441. }
  442. min_size = min_size_clamped;
  443. _validate_limit_size();
  444. _update_window_size();
  445. }
  446. Size2i Window::get_min_size() const {
  447. ERR_READ_THREAD_GUARD_V(Size2i());
  448. return min_size;
  449. }
  450. void Window::set_mode(Mode p_mode) {
  451. ERR_MAIN_THREAD_GUARD;
  452. mode = p_mode;
  453. if (embedder) {
  454. embedder->_sub_window_update(this);
  455. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  456. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WindowMode(p_mode), window_id);
  457. }
  458. }
  459. Window::Mode Window::get_mode() const {
  460. ERR_READ_THREAD_GUARD_V(MODE_WINDOWED);
  461. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  462. mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
  463. }
  464. return mode;
  465. }
  466. void Window::set_flag(Flags p_flag, bool p_enabled) {
  467. ERR_MAIN_THREAD_GUARD;
  468. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  469. flags[p_flag] = p_enabled;
  470. if (p_flag == FLAG_TRANSPARENT) {
  471. set_transparent_background(p_enabled);
  472. }
  473. if (embedder) {
  474. embedder->_sub_window_update(this);
  475. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  476. if (!is_in_edited_scene_root()) {
  477. DisplayServer::get_singleton()->window_set_flag(DisplayServer::WindowFlags(p_flag), p_enabled, window_id);
  478. }
  479. }
  480. }
  481. bool Window::get_flag(Flags p_flag) const {
  482. ERR_READ_THREAD_GUARD_V(false);
  483. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  484. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  485. if (!is_in_edited_scene_root()) {
  486. flags[p_flag] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(p_flag), window_id);
  487. }
  488. }
  489. return flags[p_flag];
  490. }
  491. bool Window::is_maximize_allowed() const {
  492. ERR_READ_THREAD_GUARD_V(false);
  493. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  494. return DisplayServer::get_singleton()->window_is_maximize_allowed(window_id);
  495. }
  496. return true;
  497. }
  498. void Window::request_attention() {
  499. ERR_MAIN_THREAD_GUARD;
  500. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  501. DisplayServer::get_singleton()->window_request_attention(window_id);
  502. }
  503. }
  504. #ifndef DISABLE_DEPRECATED
  505. void Window::move_to_foreground() {
  506. WARN_DEPRECATED_MSG(R"*(The "move_to_foreground()" method is deprecated, use "grab_focus()" instead.)*");
  507. grab_focus();
  508. }
  509. #endif // DISABLE_DEPRECATED
  510. bool Window::can_draw() const {
  511. ERR_READ_THREAD_GUARD_V(false);
  512. if (!is_inside_tree()) {
  513. return false;
  514. }
  515. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  516. return DisplayServer::get_singleton()->window_can_draw(window_id);
  517. }
  518. return visible;
  519. }
  520. void Window::set_ime_active(bool p_active) {
  521. ERR_MAIN_THREAD_GUARD;
  522. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  523. DisplayServer::get_singleton()->window_set_ime_active(p_active, window_id);
  524. }
  525. }
  526. void Window::set_ime_position(const Point2i &p_pos) {
  527. ERR_MAIN_THREAD_GUARD;
  528. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  529. DisplayServer::get_singleton()->window_set_ime_position(p_pos, window_id);
  530. }
  531. }
  532. bool Window::is_embedded() const {
  533. ERR_READ_THREAD_GUARD_V(false);
  534. return get_embedder() != nullptr;
  535. }
  536. bool Window::is_in_edited_scene_root() const {
  537. ERR_READ_THREAD_GUARD_V(false);
  538. #ifdef TOOLS_ENABLED
  539. return is_part_of_edited_scene();
  540. #else
  541. return false;
  542. #endif
  543. }
  544. void Window::_make_window() {
  545. ERR_FAIL_COND(window_id != DisplayServer::INVALID_WINDOW_ID);
  546. if (transient && transient_to_focused) {
  547. _make_transient();
  548. }
  549. uint32_t f = 0;
  550. for (int i = 0; i < FLAG_MAX; i++) {
  551. if (flags[i]) {
  552. f |= (1 << i);
  553. }
  554. }
  555. DisplayServer::VSyncMode vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
  556. Rect2i window_rect;
  557. if (initial_position == WINDOW_INITIAL_POSITION_ABSOLUTE) {
  558. window_rect = Rect2i(position, size);
  559. } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN) {
  560. window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_PRIMARY) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_PRIMARY) - size) / 2, size);
  561. } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN) {
  562. window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_OF_MAIN_WINDOW) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_OF_MAIN_WINDOW) - size) / 2, size);
  563. } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN) {
  564. window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(current_screen) + (DisplayServer::get_singleton()->screen_get_size(current_screen) - size) / 2, size);
  565. } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS) {
  566. window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_WITH_MOUSE_FOCUS) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_WITH_MOUSE_FOCUS) - size) / 2, size);
  567. } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS) {
  568. window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_WITH_KEYBOARD_FOCUS) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_WITH_KEYBOARD_FOCUS) - size) / 2, size);
  569. }
  570. window_id = DisplayServer::get_singleton()->create_sub_window(DisplayServer::WindowMode(mode), vsync_mode, f, window_rect, is_in_edited_scene_root() ? false : exclusive, transient_parent ? transient_parent->window_id : DisplayServer::INVALID_WINDOW_ID);
  571. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  572. DisplayServer::get_singleton()->window_set_max_size(Size2i(), window_id);
  573. DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
  574. DisplayServer::get_singleton()->window_set_mouse_passthrough(mpath, window_id);
  575. DisplayServer::get_singleton()->window_set_title(tr_title, window_id);
  576. DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
  577. _update_window_size();
  578. if (transient_parent) {
  579. for (const Window *E : transient_children) {
  580. if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
  581. DisplayServer::get_singleton()->window_set_transient(E->window_id, transient_parent->window_id);
  582. }
  583. }
  584. }
  585. _update_window_callbacks();
  586. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  587. DisplayServer::get_singleton()->show_window(window_id);
  588. }
  589. void Window::_update_from_window() {
  590. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  591. mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
  592. for (int i = 0; i < FLAG_MAX; i++) {
  593. flags[i] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(i), window_id);
  594. }
  595. }
  596. void Window::_clear_window() {
  597. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  598. bool had_focus = has_focus();
  599. if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
  600. DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
  601. }
  602. for (const Window *E : transient_children) {
  603. if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
  604. DisplayServer::get_singleton()->window_set_transient(E->window_id, DisplayServer::INVALID_WINDOW_ID);
  605. }
  606. }
  607. _update_from_window();
  608. DisplayServer::get_singleton()->delete_sub_window(window_id);
  609. window_id = DisplayServer::INVALID_WINDOW_ID;
  610. // If closing window was focused and has a parent, return focus.
  611. if (had_focus && transient_parent) {
  612. transient_parent->grab_focus();
  613. }
  614. _update_viewport_size();
  615. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  616. if (transient && transient_to_focused) {
  617. _clear_transient();
  618. }
  619. }
  620. void Window::_rect_changed_callback(const Rect2i &p_callback) {
  621. //we must always accept this as the truth
  622. if (size == p_callback.size && position == p_callback.position) {
  623. return;
  624. }
  625. position = p_callback.position;
  626. if (size != p_callback.size) {
  627. size = p_callback.size;
  628. _update_viewport_size();
  629. }
  630. }
  631. void Window::_propagate_window_notification(Node *p_node, int p_notification) {
  632. p_node->notification(p_notification);
  633. for (int i = 0; i < p_node->get_child_count(); i++) {
  634. Node *child = p_node->get_child(i);
  635. Window *window = Object::cast_to<Window>(child);
  636. if (window) {
  637. continue;
  638. }
  639. _propagate_window_notification(child, p_notification);
  640. }
  641. }
  642. void Window::_event_callback(DisplayServer::WindowEvent p_event) {
  643. switch (p_event) {
  644. case DisplayServer::WINDOW_EVENT_MOUSE_ENTER: {
  645. if (!is_inside_tree()) {
  646. return;
  647. }
  648. Window *root = get_tree()->get_root();
  649. if (mouse_in_window && root->gui.windowmanager_window_over == this) {
  650. return;
  651. }
  652. if (root->gui.windowmanager_window_over) {
  653. #ifdef DEV_ENABLED
  654. WARN_PRINT_ONCE("Entering a window while a window is hovered should never happen in DisplayServer.");
  655. #endif // DEV_ENABLED
  656. root->gui.windowmanager_window_over->_event_callback(DisplayServer::WINDOW_EVENT_MOUSE_EXIT);
  657. }
  658. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
  659. root->gui.windowmanager_window_over = this;
  660. mouse_in_window = true;
  661. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CURSOR_SHAPE)) {
  662. DisplayServer::get_singleton()->cursor_set_shape(DisplayServer::CURSOR_ARROW); //restore cursor shape
  663. }
  664. } break;
  665. case DisplayServer::WINDOW_EVENT_MOUSE_EXIT: {
  666. if (!is_inside_tree()) {
  667. return;
  668. }
  669. // Ensure keeping the order of input events and window events when input events are buffered or accumulated.
  670. Input::get_singleton()->flush_buffered_events();
  671. Window *root = get_tree()->get_root();
  672. if (!root->gui.windowmanager_window_over) {
  673. #ifdef DEV_ENABLED
  674. WARN_PRINT_ONCE("Exiting a window while no window is hovered should never happen in DisplayServer.");
  675. #endif // DEV_ENABLED
  676. return;
  677. }
  678. mouse_in_window = false;
  679. root->gui.windowmanager_window_over->_mouse_leave_viewport();
  680. root->gui.windowmanager_window_over = nullptr;
  681. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
  682. } break;
  683. case DisplayServer::WINDOW_EVENT_FOCUS_IN: {
  684. focused = true;
  685. _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_IN);
  686. emit_signal(SceneStringName(focus_entered));
  687. } break;
  688. case DisplayServer::WINDOW_EVENT_FOCUS_OUT: {
  689. focused = false;
  690. _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_OUT);
  691. emit_signal(SceneStringName(focus_exited));
  692. } break;
  693. case DisplayServer::WINDOW_EVENT_CLOSE_REQUEST: {
  694. if (exclusive_child != nullptr) {
  695. break; //has an exclusive child, can't get events until child is closed
  696. }
  697. _propagate_window_notification(this, NOTIFICATION_WM_CLOSE_REQUEST);
  698. emit_signal(SNAME("close_requested"));
  699. } break;
  700. case DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST: {
  701. _propagate_window_notification(this, NOTIFICATION_WM_GO_BACK_REQUEST);
  702. emit_signal(SNAME("go_back_requested"));
  703. } break;
  704. case DisplayServer::WINDOW_EVENT_DPI_CHANGE: {
  705. _update_viewport_size();
  706. _propagate_window_notification(this, NOTIFICATION_WM_DPI_CHANGE);
  707. emit_signal(SNAME("dpi_changed"));
  708. } break;
  709. case DisplayServer::WINDOW_EVENT_TITLEBAR_CHANGE: {
  710. emit_signal(SNAME("titlebar_changed"));
  711. } break;
  712. }
  713. }
  714. void Window::update_mouse_cursor_state() {
  715. ERR_MAIN_THREAD_GUARD;
  716. // Update states based on mouse cursor position.
  717. // This includes updated mouse_enter or mouse_exit signals or the current mouse cursor shape.
  718. // These details are set in Viewport::_gui_input_event. To instantly
  719. // see the changes in the viewport, we need to trigger a mouse motion event.
  720. // This function should be called whenever scene tree changes affect the mouse cursor.
  721. Ref<InputEventMouseMotion> mm;
  722. Vector2 pos = get_mouse_position();
  723. Transform2D xform = get_global_canvas_transform().affine_inverse();
  724. mm.instantiate();
  725. mm->set_position(pos);
  726. mm->set_global_position(xform.xform(pos));
  727. mm->set_device(InputEvent::DEVICE_ID_INTERNAL);
  728. push_input(mm, true);
  729. }
  730. void Window::show() {
  731. ERR_MAIN_THREAD_GUARD;
  732. set_visible(true);
  733. }
  734. void Window::hide() {
  735. ERR_MAIN_THREAD_GUARD;
  736. set_visible(false);
  737. }
  738. void Window::set_visible(bool p_visible) {
  739. ERR_MAIN_THREAD_GUARD;
  740. if (visible == p_visible) {
  741. return;
  742. }
  743. if (!is_inside_tree()) {
  744. visible = p_visible;
  745. return;
  746. }
  747. ERR_FAIL_NULL_MSG(get_parent(), "Can't change visibility of main window.");
  748. visible = p_visible;
  749. // Stop any queued resizing, as the window will be resized right now.
  750. updating_child_controls = false;
  751. Viewport *embedder_vp = get_embedder();
  752. if (!embedder_vp) {
  753. if (!p_visible && window_id != DisplayServer::INVALID_WINDOW_ID) {
  754. _clear_window();
  755. }
  756. if (p_visible && window_id == DisplayServer::INVALID_WINDOW_ID) {
  757. _make_window();
  758. }
  759. } else {
  760. if (visible) {
  761. embedder = embedder_vp;
  762. if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
  763. if (is_in_edited_scene_root()) {
  764. Size2 screen_size = Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
  765. position = (screen_size - size) / 2;
  766. } else {
  767. position = (embedder->get_visible_rect().size - size) / 2;
  768. }
  769. }
  770. embedder->_sub_window_register(this);
  771. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
  772. } else {
  773. embedder->_sub_window_remove(this);
  774. embedder = nullptr;
  775. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  776. }
  777. _update_window_size();
  778. }
  779. if (!visible) {
  780. focused = false;
  781. }
  782. notification(NOTIFICATION_VISIBILITY_CHANGED);
  783. emit_signal(SceneStringName(visibility_changed));
  784. RS::get_singleton()->viewport_set_active(get_viewport_rid(), visible);
  785. //update transient exclusive
  786. if (transient_parent) {
  787. _set_transient_exclusive_child(true);
  788. }
  789. }
  790. void Window::_clear_transient() {
  791. if (transient_parent) {
  792. if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
  793. DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
  794. }
  795. transient_parent->transient_children.erase(this);
  796. if (transient_parent->exclusive_child == this) {
  797. transient_parent->exclusive_child = nullptr;
  798. }
  799. transient_parent = nullptr;
  800. }
  801. }
  802. void Window::_make_transient() {
  803. if (!get_parent()) {
  804. //main window, can't be transient
  805. return;
  806. }
  807. //find transient parent
  808. Window *window = nullptr;
  809. if (!is_embedded() && transient_to_focused) {
  810. DisplayServer::WindowID focused_window_id = DisplayServer::get_singleton()->get_focused_window();
  811. if (focused_window_id != DisplayServer::INVALID_WINDOW_ID) {
  812. window = Window::get_from_id(focused_window_id);
  813. }
  814. }
  815. if (!window) {
  816. Viewport *vp = get_parent()->get_viewport();
  817. while (vp) {
  818. window = Object::cast_to<Window>(vp);
  819. if (window) {
  820. break;
  821. }
  822. if (!vp->get_parent()) {
  823. break;
  824. }
  825. vp = vp->get_parent()->get_viewport();
  826. }
  827. }
  828. if (window) {
  829. transient_parent = window;
  830. window->transient_children.insert(this);
  831. _set_transient_exclusive_child();
  832. }
  833. //see if we can make transient
  834. if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
  835. DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
  836. }
  837. }
  838. void Window::_set_transient_exclusive_child(bool p_clear_invalid) {
  839. if (exclusive && visible && is_inside_tree()) {
  840. if (!is_in_edited_scene_root()) {
  841. // Transient parent has another exclusive child.
  842. if (transient_parent->exclusive_child && transient_parent->exclusive_child != this) {
  843. ERR_PRINT(vformat("Attempting to make child window exclusive, but the parent window already has another exclusive child. This window: %s, parent window: %s, current exclusive child window: %s", get_description(), transient_parent->get_description(), transient_parent->exclusive_child->get_description()));
  844. }
  845. transient_parent->exclusive_child = this;
  846. }
  847. } else if (p_clear_invalid) {
  848. if (transient_parent->exclusive_child == this) {
  849. transient_parent->exclusive_child = nullptr;
  850. }
  851. }
  852. }
  853. void Window::set_transient(bool p_transient) {
  854. ERR_MAIN_THREAD_GUARD;
  855. if (transient == p_transient) {
  856. return;
  857. }
  858. transient = p_transient;
  859. if (!is_inside_tree()) {
  860. return;
  861. }
  862. if (transient) {
  863. if (!transient_to_focused) {
  864. _make_transient();
  865. }
  866. } else {
  867. _clear_transient();
  868. }
  869. }
  870. bool Window::is_transient() const {
  871. return transient;
  872. }
  873. void Window::set_transient_to_focused(bool p_transient_to_focused) {
  874. ERR_MAIN_THREAD_GUARD;
  875. if (transient_to_focused == p_transient_to_focused) {
  876. return;
  877. }
  878. transient_to_focused = p_transient_to_focused;
  879. }
  880. bool Window::is_transient_to_focused() const {
  881. ERR_READ_THREAD_GUARD_V(false);
  882. return transient_to_focused;
  883. }
  884. void Window::set_exclusive(bool p_exclusive) {
  885. ERR_MAIN_THREAD_GUARD;
  886. if (exclusive == p_exclusive) {
  887. return;
  888. }
  889. exclusive = p_exclusive;
  890. if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID) {
  891. if (is_in_edited_scene_root()) {
  892. DisplayServer::get_singleton()->window_set_exclusive(window_id, false);
  893. } else {
  894. DisplayServer::get_singleton()->window_set_exclusive(window_id, exclusive);
  895. }
  896. }
  897. if (transient_parent) {
  898. _set_transient_exclusive_child(true);
  899. }
  900. }
  901. bool Window::is_exclusive() const {
  902. ERR_READ_THREAD_GUARD_V(false);
  903. return exclusive;
  904. }
  905. bool Window::is_visible() const {
  906. ERR_READ_THREAD_GUARD_V(false);
  907. return visible;
  908. }
  909. Size2i Window::_clamp_window_size(const Size2i &p_size) {
  910. Size2i window_size_clamped = p_size;
  911. Size2 minsize = get_clamped_minimum_size();
  912. window_size_clamped = window_size_clamped.max(minsize);
  913. if (max_size_used != Size2i()) {
  914. window_size_clamped = window_size_clamped.min(max_size_used);
  915. }
  916. return window_size_clamped;
  917. }
  918. void Window::_update_window_size() {
  919. Size2i size_limit = get_clamped_minimum_size();
  920. if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID && keep_title_visible) {
  921. Size2i title_size = DisplayServer::get_singleton()->window_get_title_size(tr_title, window_id);
  922. size_limit = size_limit.max(title_size);
  923. }
  924. size = size.max(size_limit);
  925. bool reset_min_first = false;
  926. if (max_size_used != Size2i()) {
  927. // Force window size to respect size limitations of max_size_used.
  928. size = size.min(max_size_used);
  929. if (size_limit.x > max_size_used.x) {
  930. size_limit.x = max_size_used.x;
  931. reset_min_first = true;
  932. }
  933. if (size_limit.y > max_size_used.y) {
  934. size_limit.y = max_size_used.y;
  935. reset_min_first = true;
  936. }
  937. }
  938. if (embedder) {
  939. size = size.maxi(1);
  940. embedder->_sub_window_update(this);
  941. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  942. if (reset_min_first && wrap_controls) {
  943. // Avoid an error if setting max_size to a value between min_size and the previous size_limit.
  944. DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
  945. }
  946. DisplayServer::get_singleton()->window_set_max_size(max_size_used, window_id);
  947. DisplayServer::get_singleton()->window_set_min_size(size_limit, window_id);
  948. DisplayServer::get_singleton()->window_set_size(size, window_id);
  949. }
  950. //update the viewport
  951. _update_viewport_size();
  952. }
  953. void Window::_update_viewport_size() {
  954. //update the viewport part
  955. Size2i final_size;
  956. Size2i final_size_override;
  957. Rect2i attach_to_screen_rect(Point2i(), size);
  958. double font_oversampling = 1.0;
  959. window_transform = Transform2D();
  960. if (content_scale_stretch == Window::CONTENT_SCALE_STRETCH_INTEGER) {
  961. // We always want to make sure that the content scale factor is a whole
  962. // number, else there will be pixel wobble no matter what.
  963. content_scale_factor = Math::floor(content_scale_factor);
  964. // A content scale factor of zero is pretty useless.
  965. if (content_scale_factor < 1) {
  966. content_scale_factor = 1;
  967. }
  968. }
  969. if (content_scale_mode == CONTENT_SCALE_MODE_DISABLED || content_scale_size.x == 0 || content_scale_size.y == 0) {
  970. font_oversampling = content_scale_factor;
  971. final_size = size;
  972. final_size_override = Size2(size) / content_scale_factor;
  973. } else {
  974. //actual screen video mode
  975. Size2 video_mode = size;
  976. Size2 desired_res = content_scale_size;
  977. Size2 viewport_size;
  978. Size2 screen_size;
  979. float viewport_aspect = desired_res.aspect();
  980. float video_mode_aspect = video_mode.aspect();
  981. if (content_scale_aspect == CONTENT_SCALE_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
  982. //same aspect or ignore aspect
  983. viewport_size = desired_res;
  984. screen_size = video_mode;
  985. } else if (viewport_aspect < video_mode_aspect) {
  986. // screen ratio is smaller vertically
  987. if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_HEIGHT || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
  988. //will stretch horizontally
  989. viewport_size.x = desired_res.y * video_mode_aspect;
  990. viewport_size.y = desired_res.y;
  991. screen_size = video_mode;
  992. } else {
  993. //will need black bars
  994. viewport_size = desired_res;
  995. screen_size.x = video_mode.y * viewport_aspect;
  996. screen_size.y = video_mode.y;
  997. }
  998. } else {
  999. //screen ratio is smaller horizontally
  1000. if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_WIDTH || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
  1001. //will stretch horizontally
  1002. viewport_size.x = desired_res.x;
  1003. viewport_size.y = desired_res.x / video_mode_aspect;
  1004. screen_size = video_mode;
  1005. } else {
  1006. //will need black bars
  1007. viewport_size = desired_res;
  1008. screen_size.x = video_mode.x;
  1009. screen_size.y = video_mode.x / viewport_aspect;
  1010. }
  1011. }
  1012. screen_size = screen_size.floor();
  1013. viewport_size = viewport_size.floor();
  1014. if (content_scale_stretch == Window::CONTENT_SCALE_STRETCH_INTEGER) {
  1015. Size2i screen_scale = (screen_size / viewport_size).floor();
  1016. int scale_factor = MIN(screen_scale.x, screen_scale.y);
  1017. if (scale_factor < 1) {
  1018. scale_factor = 1;
  1019. }
  1020. screen_size = viewport_size * scale_factor;
  1021. }
  1022. Size2 margin;
  1023. Size2 offset;
  1024. if (screen_size.x < video_mode.x) {
  1025. margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
  1026. offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
  1027. }
  1028. if (screen_size.y < video_mode.y) {
  1029. margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
  1030. offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
  1031. }
  1032. switch (content_scale_mode) {
  1033. case CONTENT_SCALE_MODE_DISABLED: {
  1034. // Already handled above
  1035. //_update_font_oversampling(1.0);
  1036. } break;
  1037. case CONTENT_SCALE_MODE_CANVAS_ITEMS: {
  1038. final_size = screen_size;
  1039. final_size_override = viewport_size / content_scale_factor;
  1040. attach_to_screen_rect = Rect2(margin, screen_size);
  1041. font_oversampling = (screen_size.x / viewport_size.x) * content_scale_factor;
  1042. window_transform.translate_local(margin);
  1043. } break;
  1044. case CONTENT_SCALE_MODE_VIEWPORT: {
  1045. final_size = (viewport_size / content_scale_factor).floor();
  1046. attach_to_screen_rect = Rect2(margin, screen_size);
  1047. window_transform.translate_local(margin);
  1048. if (final_size.x != 0 && final_size.y != 0) {
  1049. Transform2D scale_transform;
  1050. scale_transform.scale(Vector2(attach_to_screen_rect.size) / Vector2(final_size));
  1051. window_transform *= scale_transform;
  1052. }
  1053. } break;
  1054. }
  1055. }
  1056. bool allocate = is_inside_tree() && visible && (window_id != DisplayServer::INVALID_WINDOW_ID || embedder != nullptr);
  1057. bool ci_updated = _set_size(final_size, final_size_override, allocate);
  1058. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  1059. RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), attach_to_screen_rect, window_id);
  1060. } else {
  1061. RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), Rect2i(), DisplayServer::INVALID_WINDOW_ID);
  1062. }
  1063. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  1064. if (!use_font_oversampling) {
  1065. font_oversampling = 1.0;
  1066. }
  1067. if (!Math::is_equal_approx(TS->font_get_global_oversampling(), font_oversampling)) {
  1068. TS->font_set_global_oversampling(font_oversampling);
  1069. if (!ci_updated) {
  1070. update_canvas_items();
  1071. emit_signal(SNAME("size_changed"));
  1072. }
  1073. }
  1074. }
  1075. notification(NOTIFICATION_WM_SIZE_CHANGED);
  1076. if (embedder) {
  1077. embedder->_sub_window_update(this);
  1078. }
  1079. }
  1080. void Window::_update_window_callbacks() {
  1081. DisplayServer::get_singleton()->window_set_rect_changed_callback(callable_mp(this, &Window::_rect_changed_callback), window_id);
  1082. DisplayServer::get_singleton()->window_set_window_event_callback(callable_mp(this, &Window::_event_callback), window_id);
  1083. DisplayServer::get_singleton()->window_set_input_event_callback(callable_mp(this, &Window::_window_input), window_id);
  1084. DisplayServer::get_singleton()->window_set_input_text_callback(callable_mp(this, &Window::_window_input_text), window_id);
  1085. DisplayServer::get_singleton()->window_set_drop_files_callback(callable_mp(this, &Window::_window_drop_files), window_id);
  1086. }
  1087. void Window::set_force_native(bool p_force_native) {
  1088. if (force_native == p_force_native) {
  1089. return;
  1090. }
  1091. if (is_visible() && !is_in_edited_scene_root()) {
  1092. ERR_FAIL_MSG("Can't change \"force_native\" while a window is displayed. Consider hiding window before changing this value.");
  1093. }
  1094. force_native = p_force_native;
  1095. }
  1096. bool Window::get_force_native() const {
  1097. return force_native;
  1098. }
  1099. Viewport *Window::get_embedder() const {
  1100. ERR_READ_THREAD_GUARD_V(nullptr);
  1101. if (force_native && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SUBWINDOWS) && !is_in_edited_scene_root()) {
  1102. return nullptr;
  1103. }
  1104. Viewport *vp = get_parent_viewport();
  1105. while (vp) {
  1106. if (vp->is_embedding_subwindows()) {
  1107. return vp;
  1108. }
  1109. if (vp->get_parent()) {
  1110. vp = vp->get_parent()->get_viewport();
  1111. } else {
  1112. vp = nullptr;
  1113. }
  1114. }
  1115. return nullptr;
  1116. }
  1117. void Window::_notification(int p_what) {
  1118. ERR_MAIN_THREAD_GUARD;
  1119. switch (p_what) {
  1120. case NOTIFICATION_POSTINITIALIZE: {
  1121. initialized = true;
  1122. _invalidate_theme_cache();
  1123. _update_theme_item_cache();
  1124. } break;
  1125. case NOTIFICATION_PARENTED: {
  1126. theme_owner->assign_theme_on_parented(this);
  1127. } break;
  1128. case NOTIFICATION_UNPARENTED: {
  1129. theme_owner->clear_theme_on_unparented(this);
  1130. } break;
  1131. case NOTIFICATION_ENTER_TREE: {
  1132. if (is_in_edited_scene_root()) {
  1133. if (!ProjectSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &Window::_settings_changed))) {
  1134. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Window::_settings_changed));
  1135. }
  1136. }
  1137. bool embedded = false;
  1138. {
  1139. embedder = get_embedder();
  1140. if (embedder) {
  1141. embedded = true;
  1142. if (!visible) {
  1143. embedder = nullptr; // Not yet since not visible.
  1144. }
  1145. }
  1146. }
  1147. if (embedded) {
  1148. // Create as embedded.
  1149. if (embedder) {
  1150. if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
  1151. if (is_in_edited_scene_root()) {
  1152. Size2 screen_size = Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
  1153. position = (screen_size - size) / 2;
  1154. } else {
  1155. position = (embedder->get_visible_rect().size - size) / 2;
  1156. }
  1157. }
  1158. embedder->_sub_window_register(this);
  1159. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
  1160. _update_window_size();
  1161. }
  1162. } else {
  1163. if (!get_parent()) {
  1164. // It's the root window!
  1165. visible = true; // Always visible.
  1166. window_id = DisplayServer::MAIN_WINDOW_ID;
  1167. DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
  1168. _update_from_window();
  1169. // Since this window already exists (created on start), we must update pos and size from it.
  1170. {
  1171. position = DisplayServer::get_singleton()->window_get_position(window_id);
  1172. size = DisplayServer::get_singleton()->window_get_size(window_id);
  1173. focused = DisplayServer::get_singleton()->window_is_focused(window_id);
  1174. }
  1175. _update_window_size(); // Inform DisplayServer of minimum and maximum size.
  1176. _update_viewport_size(); // Then feed back to the viewport.
  1177. _update_window_callbacks();
  1178. // Simulate mouse-enter event when mouse is over the window, since OS event might arrive before setting callbacks.
  1179. if (!mouse_in_window && Rect2(position, size).has_point(DisplayServer::get_singleton()->mouse_get_position())) {
  1180. _event_callback(DisplayServer::WINDOW_EVENT_MOUSE_ENTER);
  1181. }
  1182. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  1183. if (DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(FLAG_TRANSPARENT), window_id)) {
  1184. set_transparent_background(true);
  1185. }
  1186. } else {
  1187. // Create.
  1188. if (visible) {
  1189. _make_window();
  1190. }
  1191. }
  1192. }
  1193. if (transient && !transient_to_focused) {
  1194. _make_transient();
  1195. }
  1196. if (visible) {
  1197. notification(NOTIFICATION_VISIBILITY_CHANGED);
  1198. emit_signal(SceneStringName(visibility_changed));
  1199. RS::get_singleton()->viewport_set_active(get_viewport_rid(), true);
  1200. }
  1201. // Emits NOTIFICATION_THEME_CHANGED internally.
  1202. set_theme_context(ThemeDB::get_singleton()->get_nearest_theme_context(this));
  1203. } break;
  1204. case NOTIFICATION_READY: {
  1205. if (wrap_controls) {
  1206. // Finish any resizing immediately so it doesn't interfere on stuff overriding _ready().
  1207. _update_child_controls();
  1208. }
  1209. } break;
  1210. case NOTIFICATION_THEME_CHANGED: {
  1211. emit_signal(SceneStringName(theme_changed));
  1212. _invalidate_theme_cache();
  1213. _update_theme_item_cache();
  1214. } break;
  1215. case NOTIFICATION_TRANSLATION_CHANGED: {
  1216. _invalidate_theme_cache();
  1217. _update_theme_item_cache();
  1218. tr_title = atr(title);
  1219. #ifdef DEBUG_ENABLED
  1220. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  1221. // Append a suffix to the window title to denote that the project is running
  1222. // from a debug build (including the editor). Since this results in lower performance,
  1223. // this should be clearly presented to the user.
  1224. tr_title = vformat("%s (DEBUG)", tr_title);
  1225. }
  1226. #endif
  1227. if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID) {
  1228. DisplayServer::get_singleton()->window_set_title(tr_title, window_id);
  1229. if (keep_title_visible) {
  1230. Size2i title_size = DisplayServer::get_singleton()->window_get_title_size(tr_title, window_id);
  1231. Size2i size_limit = get_clamped_minimum_size();
  1232. if (title_size.x > size_limit.x || title_size.y > size_limit.y) {
  1233. _update_window_size();
  1234. }
  1235. }
  1236. }
  1237. } break;
  1238. case NOTIFICATION_VISIBILITY_CHANGED: {
  1239. if (unparent_when_invisible && !is_visible()) {
  1240. Node *p = get_parent();
  1241. if (p) {
  1242. p->remove_child(this);
  1243. }
  1244. }
  1245. } break;
  1246. case NOTIFICATION_EXIT_TREE: {
  1247. if (ProjectSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &Window::_settings_changed))) {
  1248. ProjectSettings::get_singleton()->disconnect("settings_changed", callable_mp(this, &Window::_settings_changed));
  1249. }
  1250. set_theme_context(nullptr, false);
  1251. if (transient) {
  1252. _clear_transient();
  1253. }
  1254. if (!is_embedded() && window_id != DisplayServer::INVALID_WINDOW_ID) {
  1255. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  1256. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  1257. _update_window_callbacks();
  1258. } else {
  1259. _clear_window();
  1260. }
  1261. } else {
  1262. if (embedder) {
  1263. embedder->_sub_window_remove(this);
  1264. embedder = nullptr;
  1265. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  1266. }
  1267. _update_viewport_size(); //called by clear and make, which does not happen here
  1268. }
  1269. RS::get_singleton()->viewport_set_active(get_viewport_rid(), false);
  1270. } break;
  1271. case NOTIFICATION_VP_MOUSE_ENTER: {
  1272. emit_signal(SceneStringName(mouse_entered));
  1273. } break;
  1274. case NOTIFICATION_VP_MOUSE_EXIT: {
  1275. emit_signal(SceneStringName(mouse_exited));
  1276. } break;
  1277. }
  1278. }
  1279. void Window::set_content_scale_size(const Size2i &p_size) {
  1280. ERR_MAIN_THREAD_GUARD;
  1281. ERR_FAIL_COND(p_size.x < 0);
  1282. ERR_FAIL_COND(p_size.y < 0);
  1283. content_scale_size = p_size;
  1284. _update_viewport_size();
  1285. }
  1286. Size2i Window::get_content_scale_size() const {
  1287. ERR_READ_THREAD_GUARD_V(Size2i());
  1288. return content_scale_size;
  1289. }
  1290. void Window::set_content_scale_mode(ContentScaleMode p_mode) {
  1291. ERR_MAIN_THREAD_GUARD;
  1292. content_scale_mode = p_mode;
  1293. _update_viewport_size();
  1294. }
  1295. Window::ContentScaleMode Window::get_content_scale_mode() const {
  1296. ERR_READ_THREAD_GUARD_V(CONTENT_SCALE_MODE_DISABLED);
  1297. return content_scale_mode;
  1298. }
  1299. void Window::set_content_scale_aspect(ContentScaleAspect p_aspect) {
  1300. ERR_MAIN_THREAD_GUARD;
  1301. content_scale_aspect = p_aspect;
  1302. _update_viewport_size();
  1303. }
  1304. Window::ContentScaleAspect Window::get_content_scale_aspect() const {
  1305. ERR_READ_THREAD_GUARD_V(CONTENT_SCALE_ASPECT_IGNORE);
  1306. return content_scale_aspect;
  1307. }
  1308. void Window::set_content_scale_stretch(ContentScaleStretch p_stretch) {
  1309. content_scale_stretch = p_stretch;
  1310. _update_viewport_size();
  1311. }
  1312. Window::ContentScaleStretch Window::get_content_scale_stretch() const {
  1313. return content_scale_stretch;
  1314. }
  1315. void Window::set_keep_title_visible(bool p_title_visible) {
  1316. if (keep_title_visible == p_title_visible) {
  1317. return;
  1318. }
  1319. keep_title_visible = p_title_visible;
  1320. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  1321. _update_window_size();
  1322. }
  1323. }
  1324. bool Window::get_keep_title_visible() const {
  1325. return keep_title_visible;
  1326. }
  1327. void Window::set_content_scale_factor(real_t p_factor) {
  1328. ERR_MAIN_THREAD_GUARD;
  1329. ERR_FAIL_COND(p_factor <= 0);
  1330. content_scale_factor = p_factor;
  1331. _update_viewport_size();
  1332. }
  1333. real_t Window::get_content_scale_factor() const {
  1334. ERR_READ_THREAD_GUARD_V(0);
  1335. return content_scale_factor;
  1336. }
  1337. void Window::set_use_font_oversampling(bool p_oversampling) {
  1338. ERR_MAIN_THREAD_GUARD;
  1339. if (is_inside_tree() && window_id != DisplayServer::MAIN_WINDOW_ID) {
  1340. ERR_FAIL_MSG("Only the root window can set and use font oversampling.");
  1341. }
  1342. use_font_oversampling = p_oversampling;
  1343. _update_viewport_size();
  1344. }
  1345. bool Window::is_using_font_oversampling() const {
  1346. ERR_READ_THREAD_GUARD_V(false);
  1347. return use_font_oversampling;
  1348. }
  1349. DisplayServer::WindowID Window::get_window_id() const {
  1350. ERR_READ_THREAD_GUARD_V(DisplayServer::INVALID_WINDOW_ID);
  1351. if (embedder) {
  1352. return parent->get_window_id();
  1353. }
  1354. return window_id;
  1355. }
  1356. void Window::set_mouse_passthrough_polygon(const Vector<Vector2> &p_region) {
  1357. ERR_MAIN_THREAD_GUARD;
  1358. mpath = p_region;
  1359. if (window_id == DisplayServer::INVALID_WINDOW_ID) {
  1360. return;
  1361. }
  1362. DisplayServer::get_singleton()->window_set_mouse_passthrough(mpath, window_id);
  1363. }
  1364. Vector<Vector2> Window::get_mouse_passthrough_polygon() const {
  1365. return mpath;
  1366. }
  1367. void Window::set_wrap_controls(bool p_enable) {
  1368. ERR_MAIN_THREAD_GUARD;
  1369. wrap_controls = p_enable;
  1370. if (!is_inside_tree()) {
  1371. return;
  1372. }
  1373. if (updating_child_controls) {
  1374. _update_child_controls();
  1375. } else {
  1376. _update_window_size();
  1377. }
  1378. }
  1379. bool Window::is_wrapping_controls() const {
  1380. ERR_READ_THREAD_GUARD_V(false);
  1381. return wrap_controls;
  1382. }
  1383. Size2 Window::_get_contents_minimum_size() const {
  1384. Size2 max;
  1385. for (int i = 0; i < get_child_count(); i++) {
  1386. Control *c = Object::cast_to<Control>(get_child(i));
  1387. if (c) {
  1388. Point2i pos = c->get_position();
  1389. Size2i min = c->get_combined_minimum_size();
  1390. max = max.max(pos + min);
  1391. }
  1392. }
  1393. return max * content_scale_factor;
  1394. }
  1395. void Window::child_controls_changed() {
  1396. ERR_MAIN_THREAD_GUARD;
  1397. if (!is_inside_tree() || !visible || updating_child_controls) {
  1398. return;
  1399. }
  1400. updating_child_controls = true;
  1401. callable_mp(this, &Window::_update_child_controls).call_deferred();
  1402. }
  1403. void Window::_update_child_controls() {
  1404. if (!updating_child_controls) {
  1405. return;
  1406. }
  1407. _update_window_size();
  1408. updating_child_controls = false;
  1409. }
  1410. bool Window::_can_consume_input_events() const {
  1411. return exclusive_child == nullptr;
  1412. }
  1413. void Window::_window_input(const Ref<InputEvent> &p_ev) {
  1414. ERR_MAIN_THREAD_GUARD;
  1415. if (exclusive_child != nullptr) {
  1416. if (!is_embedding_subwindows()) { // Not embedding, no need for event.
  1417. return;
  1418. }
  1419. }
  1420. // If the event needs to be handled in a Window-derived class, then it should overwrite
  1421. // `_input_from_window` instead of subscribing to the `window_input` signal, because the signal
  1422. // filters out internal events.
  1423. _input_from_window(p_ev);
  1424. if (p_ev->get_device() != InputEvent::DEVICE_ID_INTERNAL && is_inside_tree()) {
  1425. emit_signal(SceneStringName(window_input), p_ev);
  1426. }
  1427. if (is_inside_tree()) {
  1428. push_input(p_ev);
  1429. }
  1430. }
  1431. void Window::_window_input_text(const String &p_text) {
  1432. push_text_input(p_text);
  1433. }
  1434. void Window::_window_drop_files(const Vector<String> &p_files) {
  1435. emit_signal(SNAME("files_dropped"), p_files);
  1436. }
  1437. Viewport *Window::get_parent_viewport() const {
  1438. ERR_READ_THREAD_GUARD_V(nullptr);
  1439. if (get_parent()) {
  1440. return get_parent()->get_viewport();
  1441. } else {
  1442. return nullptr;
  1443. }
  1444. }
  1445. Window *Window::get_parent_visible_window() const {
  1446. ERR_READ_THREAD_GUARD_V(nullptr);
  1447. Viewport *vp = get_parent_viewport();
  1448. Window *window = nullptr;
  1449. while (vp) {
  1450. window = Object::cast_to<Window>(vp);
  1451. if (window && window->visible) {
  1452. break;
  1453. }
  1454. if (!vp->get_parent()) {
  1455. break;
  1456. }
  1457. vp = vp->get_parent()->get_viewport();
  1458. }
  1459. return window;
  1460. }
  1461. void Window::popup_on_parent(const Rect2i &p_parent_rect) {
  1462. ERR_MAIN_THREAD_GUARD;
  1463. ERR_FAIL_COND(!is_inside_tree());
  1464. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  1465. if (!is_embedded()) {
  1466. Window *window = get_parent_visible_window();
  1467. if (!window) {
  1468. popup(p_parent_rect);
  1469. } else {
  1470. popup(Rect2i(window->get_position() + p_parent_rect.position, p_parent_rect.size));
  1471. }
  1472. } else {
  1473. popup(p_parent_rect);
  1474. }
  1475. }
  1476. void Window::popup_centered_clamped(const Size2i &p_size, float p_fallback_ratio) {
  1477. ERR_MAIN_THREAD_GUARD;
  1478. ERR_FAIL_COND(!is_inside_tree());
  1479. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  1480. // Consider the current size when calling with the default value.
  1481. Size2i expected_size = p_size == Size2i() ? size : p_size;
  1482. Rect2 parent_rect;
  1483. if (is_embedded()) {
  1484. parent_rect = get_embedder()->get_visible_rect();
  1485. } else {
  1486. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  1487. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  1488. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  1489. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  1490. }
  1491. Vector2i size_ratio = parent_rect.size * p_fallback_ratio;
  1492. Rect2i popup_rect;
  1493. popup_rect.size = size_ratio.min(expected_size);
  1494. popup_rect.size = _clamp_window_size(popup_rect.size);
  1495. if (parent_rect != Rect2()) {
  1496. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  1497. }
  1498. popup(popup_rect);
  1499. }
  1500. void Window::popup_centered(const Size2i &p_minsize) {
  1501. ERR_MAIN_THREAD_GUARD;
  1502. ERR_FAIL_COND(!is_inside_tree());
  1503. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  1504. // Consider the current size when calling with the default value.
  1505. Size2i expected_size = p_minsize == Size2i() ? size : p_minsize;
  1506. Rect2 parent_rect;
  1507. if (is_embedded()) {
  1508. parent_rect = get_embedder()->get_visible_rect();
  1509. } else {
  1510. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  1511. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  1512. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  1513. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  1514. }
  1515. Rect2i popup_rect;
  1516. popup_rect.size = _clamp_window_size(expected_size);
  1517. if (parent_rect != Rect2()) {
  1518. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  1519. }
  1520. popup(popup_rect);
  1521. }
  1522. void Window::popup_centered_ratio(float p_ratio) {
  1523. ERR_MAIN_THREAD_GUARD;
  1524. ERR_FAIL_COND(!is_inside_tree());
  1525. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  1526. ERR_FAIL_COND_MSG(p_ratio <= 0.0 || p_ratio > 1.0, "Ratio must be between 0.0 and 1.0!");
  1527. Rect2 parent_rect;
  1528. if (is_embedded()) {
  1529. parent_rect = get_embedder()->get_visible_rect();
  1530. } else {
  1531. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  1532. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  1533. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  1534. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  1535. }
  1536. Rect2i popup_rect;
  1537. if (parent_rect != Rect2()) {
  1538. popup_rect.size = parent_rect.size * p_ratio;
  1539. popup_rect.size = _clamp_window_size(popup_rect.size);
  1540. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  1541. }
  1542. popup(popup_rect);
  1543. }
  1544. void Window::popup(const Rect2i &p_screen_rect) {
  1545. ERR_MAIN_THREAD_GUARD;
  1546. emit_signal(SNAME("about_to_popup"));
  1547. if (!get_embedder() && get_flag(FLAG_POPUP)) {
  1548. // Send a focus-out notification when opening a Window Manager Popup.
  1549. SceneTree *scene_tree = get_tree();
  1550. if (scene_tree) {
  1551. scene_tree->notify_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_viewports", NOTIFICATION_WM_WINDOW_FOCUS_OUT);
  1552. }
  1553. }
  1554. // Update window size to calculate the actual window size based on contents minimum size and minimum size.
  1555. _update_window_size();
  1556. if (p_screen_rect != Rect2i()) {
  1557. set_position(p_screen_rect.position);
  1558. int screen_id = DisplayServer::get_singleton()->get_screen_from_rect(p_screen_rect);
  1559. Size2i screen_size = DisplayServer::get_singleton()->screen_get_usable_rect(screen_id).size;
  1560. Size2i new_size = p_screen_rect.size.min(screen_size);
  1561. set_size(new_size);
  1562. }
  1563. Rect2i adjust = _popup_adjust_rect();
  1564. if (adjust != Rect2i()) {
  1565. set_position(adjust.position);
  1566. set_size(adjust.size);
  1567. }
  1568. int scr = DisplayServer::get_singleton()->get_screen_count();
  1569. for (int i = 0; i < scr; i++) {
  1570. Rect2i r = DisplayServer::get_singleton()->screen_get_usable_rect(i);
  1571. if (r.has_point(position)) {
  1572. current_screen = i;
  1573. break;
  1574. }
  1575. }
  1576. set_transient(true);
  1577. set_visible(true);
  1578. Rect2i parent_rect;
  1579. if (is_embedded()) {
  1580. parent_rect = get_embedder()->get_visible_rect();
  1581. } else {
  1582. int screen_id = DisplayServer::get_singleton()->window_get_current_screen(get_window_id());
  1583. parent_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen_id);
  1584. }
  1585. if (parent_rect != Rect2i() && !parent_rect.intersects(Rect2i(position, size))) {
  1586. ERR_PRINT(vformat("Window %d spawned at invalid position: %s.", get_window_id(), position));
  1587. set_position((parent_rect.size - size) / 2);
  1588. }
  1589. if (parent_rect != Rect2i() && is_clamped_to_embedder() && is_embedded()) {
  1590. Rect2i new_rect = fit_rect_in_parent(Rect2i(position, size), parent_rect);
  1591. set_position(new_rect.position);
  1592. set_size(new_rect.size);
  1593. }
  1594. _post_popup();
  1595. notification(NOTIFICATION_POST_POPUP);
  1596. }
  1597. bool Window::_try_parent_dialog(Node *p_from_node) {
  1598. ERR_FAIL_NULL_V(p_from_node, false);
  1599. ERR_FAIL_COND_V_MSG(is_inside_tree(), false, "Attempting to parent and popup a dialog that already has a parent.");
  1600. Window *w = p_from_node->get_last_exclusive_window();
  1601. if (w && w != this) {
  1602. w->add_child(this);
  1603. return true;
  1604. }
  1605. return false;
  1606. }
  1607. void Window::popup_exclusive(Node *p_from_node, const Rect2i &p_screen_rect) {
  1608. if (_try_parent_dialog(p_from_node)) {
  1609. popup(p_screen_rect);
  1610. }
  1611. }
  1612. void Window::popup_exclusive_on_parent(Node *p_from_node, const Rect2i &p_parent_rect) {
  1613. if (_try_parent_dialog(p_from_node)) {
  1614. popup_on_parent(p_parent_rect);
  1615. }
  1616. }
  1617. void Window::popup_exclusive_centered(Node *p_from_node, const Size2i &p_minsize) {
  1618. if (_try_parent_dialog(p_from_node)) {
  1619. popup_centered(p_minsize);
  1620. }
  1621. }
  1622. void Window::popup_exclusive_centered_ratio(Node *p_from_node, float p_ratio) {
  1623. if (_try_parent_dialog(p_from_node)) {
  1624. popup_centered_ratio(p_ratio);
  1625. }
  1626. }
  1627. void Window::popup_exclusive_centered_clamped(Node *p_from_node, const Size2i &p_size, float p_fallback_ratio) {
  1628. if (_try_parent_dialog(p_from_node)) {
  1629. popup_centered_clamped(p_size, p_fallback_ratio);
  1630. }
  1631. }
  1632. Rect2i Window::fit_rect_in_parent(Rect2i p_rect, const Rect2i &p_parent_rect) const {
  1633. ERR_READ_THREAD_GUARD_V(Rect2i());
  1634. Size2i limit = p_parent_rect.size;
  1635. if (p_rect.position.x + p_rect.size.x > limit.x) {
  1636. p_rect.position.x = limit.x - p_rect.size.x;
  1637. }
  1638. if (p_rect.position.y + p_rect.size.y > limit.y) {
  1639. p_rect.position.y = limit.y - p_rect.size.y;
  1640. }
  1641. if (p_rect.position.x < 0) {
  1642. p_rect.position.x = 0;
  1643. }
  1644. int title_height = get_flag(Window::FLAG_BORDERLESS) ? 0 : theme_cache.title_height;
  1645. if (p_rect.position.y < title_height) {
  1646. p_rect.position.y = title_height;
  1647. }
  1648. return p_rect;
  1649. }
  1650. Size2 Window::get_contents_minimum_size() const {
  1651. ERR_READ_THREAD_GUARD_V(Size2());
  1652. Vector2 ms;
  1653. if (GDVIRTUAL_CALL(_get_contents_minimum_size, ms)) {
  1654. return ms;
  1655. }
  1656. return _get_contents_minimum_size();
  1657. }
  1658. Size2 Window::get_clamped_minimum_size() const {
  1659. ERR_READ_THREAD_GUARD_V(Size2());
  1660. if (!wrap_controls) {
  1661. return min_size;
  1662. }
  1663. return min_size.max(get_contents_minimum_size());
  1664. }
  1665. void Window::grab_focus() {
  1666. ERR_MAIN_THREAD_GUARD;
  1667. if (embedder) {
  1668. embedder->_sub_window_grab_focus(this);
  1669. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  1670. DisplayServer::get_singleton()->window_move_to_foreground(window_id);
  1671. }
  1672. }
  1673. bool Window::has_focus() const {
  1674. ERR_READ_THREAD_GUARD_V(false);
  1675. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  1676. return DisplayServer::get_singleton()->window_is_focused(window_id);
  1677. }
  1678. return focused;
  1679. }
  1680. Rect2i Window::get_usable_parent_rect() const {
  1681. ERR_READ_THREAD_GUARD_V(Rect2i());
  1682. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  1683. Rect2i parent_rect;
  1684. if (is_embedded()) {
  1685. parent_rect = get_embedder()->get_visible_rect();
  1686. } else {
  1687. const Window *w = is_visible() ? this : get_parent_visible_window();
  1688. //find a parent that can contain us
  1689. ERR_FAIL_NULL_V(w, Rect2());
  1690. parent_rect = DisplayServer::get_singleton()->screen_get_usable_rect(DisplayServer::get_singleton()->window_get_current_screen(w->get_window_id()));
  1691. }
  1692. return parent_rect;
  1693. }
  1694. void Window::add_child_notify(Node *p_child) {
  1695. if (is_inside_tree() && wrap_controls) {
  1696. child_controls_changed();
  1697. }
  1698. }
  1699. void Window::remove_child_notify(Node *p_child) {
  1700. if (is_inside_tree() && wrap_controls) {
  1701. child_controls_changed();
  1702. }
  1703. }
  1704. // Theming.
  1705. void Window::set_theme_owner_node(Node *p_node) {
  1706. ERR_MAIN_THREAD_GUARD;
  1707. theme_owner->set_owner_node(p_node);
  1708. }
  1709. Node *Window::get_theme_owner_node() const {
  1710. ERR_READ_THREAD_GUARD_V(nullptr);
  1711. return theme_owner->get_owner_node();
  1712. }
  1713. bool Window::has_theme_owner_node() const {
  1714. ERR_READ_THREAD_GUARD_V(false);
  1715. return theme_owner->has_owner_node();
  1716. }
  1717. void Window::set_theme_context(ThemeContext *p_context, bool p_propagate) {
  1718. ERR_MAIN_THREAD_GUARD;
  1719. theme_owner->set_owner_context(p_context, p_propagate);
  1720. }
  1721. void Window::set_theme(const Ref<Theme> &p_theme) {
  1722. ERR_MAIN_THREAD_GUARD;
  1723. if (theme == p_theme) {
  1724. return;
  1725. }
  1726. if (theme.is_valid()) {
  1727. theme->disconnect_changed(callable_mp(this, &Window::_theme_changed));
  1728. }
  1729. theme = p_theme;
  1730. if (theme.is_valid()) {
  1731. theme_owner->propagate_theme_changed(this, this, is_inside_tree(), true);
  1732. theme->connect_changed(callable_mp(this, &Window::_theme_changed), CONNECT_DEFERRED);
  1733. return;
  1734. }
  1735. Control *parent_c = Object::cast_to<Control>(get_parent());
  1736. if (parent_c && parent_c->has_theme_owner_node()) {
  1737. theme_owner->propagate_theme_changed(this, parent_c->get_theme_owner_node(), is_inside_tree(), true);
  1738. return;
  1739. }
  1740. Window *parent_w = cast_to<Window>(get_parent());
  1741. if (parent_w && parent_w->has_theme_owner_node()) {
  1742. theme_owner->propagate_theme_changed(this, parent_w->get_theme_owner_node(), is_inside_tree(), true);
  1743. return;
  1744. }
  1745. theme_owner->propagate_theme_changed(this, nullptr, is_inside_tree(), true);
  1746. }
  1747. Ref<Theme> Window::get_theme() const {
  1748. ERR_READ_THREAD_GUARD_V(Ref<Theme>());
  1749. return theme;
  1750. }
  1751. void Window::_theme_changed() {
  1752. if (is_inside_tree()) {
  1753. theme_owner->propagate_theme_changed(this, this, true, false);
  1754. }
  1755. }
  1756. void Window::_notify_theme_override_changed() {
  1757. if (!bulk_theme_override && is_inside_tree()) {
  1758. notification(NOTIFICATION_THEME_CHANGED);
  1759. }
  1760. }
  1761. void Window::_invalidate_theme_cache() {
  1762. theme_icon_cache.clear();
  1763. theme_style_cache.clear();
  1764. theme_font_cache.clear();
  1765. theme_font_size_cache.clear();
  1766. theme_color_cache.clear();
  1767. theme_constant_cache.clear();
  1768. }
  1769. void Window::_update_theme_item_cache() {
  1770. // Request an update on the next frame to reflect theme changes.
  1771. // Updating without a delay can cause a lot of lag.
  1772. if (!wrap_controls) {
  1773. updating_embedded_window = true;
  1774. callable_mp(this, &Window::_update_embedded_window).call_deferred();
  1775. } else {
  1776. child_controls_changed();
  1777. }
  1778. ThemeDB::get_singleton()->update_class_instance_items(this);
  1779. }
  1780. void Window::_update_embedded_window() {
  1781. if (!updating_embedded_window) {
  1782. return;
  1783. }
  1784. if (embedder) {
  1785. embedder->_sub_window_update(this);
  1786. };
  1787. updating_embedded_window = false;
  1788. }
  1789. void Window::set_theme_type_variation(const StringName &p_theme_type) {
  1790. ERR_MAIN_THREAD_GUARD;
  1791. theme_type_variation = p_theme_type;
  1792. if (is_inside_tree()) {
  1793. notification(NOTIFICATION_THEME_CHANGED);
  1794. }
  1795. }
  1796. StringName Window::get_theme_type_variation() const {
  1797. ERR_READ_THREAD_GUARD_V(StringName());
  1798. return theme_type_variation;
  1799. }
  1800. /// Theme property lookup.
  1801. Ref<Texture2D> Window::get_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
  1802. ERR_READ_THREAD_GUARD_V(Ref<Texture2D>());
  1803. if (!initialized) {
  1804. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  1805. }
  1806. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1807. const Ref<Texture2D> *tex = theme_icon_override.getptr(p_name);
  1808. if (tex) {
  1809. return *tex;
  1810. }
  1811. }
  1812. if (theme_icon_cache.has(p_theme_type) && theme_icon_cache[p_theme_type].has(p_name)) {
  1813. return theme_icon_cache[p_theme_type][p_name];
  1814. }
  1815. Vector<StringName> theme_types;
  1816. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  1817. Ref<Texture2D> icon = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
  1818. theme_icon_cache[p_theme_type][p_name] = icon;
  1819. return icon;
  1820. }
  1821. Ref<StyleBox> Window::get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  1822. ERR_READ_THREAD_GUARD_V(Ref<StyleBox>());
  1823. if (!initialized) {
  1824. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  1825. }
  1826. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1827. const Ref<StyleBox> *style = theme_style_override.getptr(p_name);
  1828. if (style) {
  1829. return *style;
  1830. }
  1831. }
  1832. if (theme_style_cache.has(p_theme_type) && theme_style_cache[p_theme_type].has(p_name)) {
  1833. return theme_style_cache[p_theme_type][p_name];
  1834. }
  1835. Vector<StringName> theme_types;
  1836. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  1837. Ref<StyleBox> style = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
  1838. theme_style_cache[p_theme_type][p_name] = style;
  1839. return style;
  1840. }
  1841. Ref<Font> Window::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
  1842. ERR_READ_THREAD_GUARD_V(Ref<Font>());
  1843. if (!initialized) {
  1844. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  1845. }
  1846. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1847. const Ref<Font> *font = theme_font_override.getptr(p_name);
  1848. if (font) {
  1849. return *font;
  1850. }
  1851. }
  1852. if (theme_font_cache.has(p_theme_type) && theme_font_cache[p_theme_type].has(p_name)) {
  1853. return theme_font_cache[p_theme_type][p_name];
  1854. }
  1855. Vector<StringName> theme_types;
  1856. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  1857. Ref<Font> font = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
  1858. theme_font_cache[p_theme_type][p_name] = font;
  1859. return font;
  1860. }
  1861. int Window::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  1862. ERR_READ_THREAD_GUARD_V(0);
  1863. if (!initialized) {
  1864. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  1865. }
  1866. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1867. const int *font_size = theme_font_size_override.getptr(p_name);
  1868. if (font_size && (*font_size) > 0) {
  1869. return *font_size;
  1870. }
  1871. }
  1872. if (theme_font_size_cache.has(p_theme_type) && theme_font_size_cache[p_theme_type].has(p_name)) {
  1873. return theme_font_size_cache[p_theme_type][p_name];
  1874. }
  1875. Vector<StringName> theme_types;
  1876. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  1877. int font_size = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
  1878. theme_font_size_cache[p_theme_type][p_name] = font_size;
  1879. return font_size;
  1880. }
  1881. Color Window::get_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
  1882. ERR_READ_THREAD_GUARD_V(Color());
  1883. if (!initialized) {
  1884. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  1885. }
  1886. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1887. const Color *color = theme_color_override.getptr(p_name);
  1888. if (color) {
  1889. return *color;
  1890. }
  1891. }
  1892. if (theme_color_cache.has(p_theme_type) && theme_color_cache[p_theme_type].has(p_name)) {
  1893. return theme_color_cache[p_theme_type][p_name];
  1894. }
  1895. Vector<StringName> theme_types;
  1896. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  1897. Color color = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
  1898. theme_color_cache[p_theme_type][p_name] = color;
  1899. return color;
  1900. }
  1901. int Window::get_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
  1902. ERR_READ_THREAD_GUARD_V(0);
  1903. if (!initialized) {
  1904. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  1905. }
  1906. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1907. const int *constant = theme_constant_override.getptr(p_name);
  1908. if (constant) {
  1909. return *constant;
  1910. }
  1911. }
  1912. if (theme_constant_cache.has(p_theme_type) && theme_constant_cache[p_theme_type].has(p_name)) {
  1913. return theme_constant_cache[p_theme_type][p_name];
  1914. }
  1915. Vector<StringName> theme_types;
  1916. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  1917. int constant = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
  1918. theme_constant_cache[p_theme_type][p_name] = constant;
  1919. return constant;
  1920. }
  1921. Variant Window::get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
  1922. switch (p_data_type) {
  1923. case Theme::DATA_TYPE_COLOR:
  1924. return get_theme_color(p_name, p_theme_type);
  1925. case Theme::DATA_TYPE_CONSTANT:
  1926. return get_theme_constant(p_name, p_theme_type);
  1927. case Theme::DATA_TYPE_FONT:
  1928. return get_theme_font(p_name, p_theme_type);
  1929. case Theme::DATA_TYPE_FONT_SIZE:
  1930. return get_theme_font_size(p_name, p_theme_type);
  1931. case Theme::DATA_TYPE_ICON:
  1932. return get_theme_icon(p_name, p_theme_type);
  1933. case Theme::DATA_TYPE_STYLEBOX:
  1934. return get_theme_stylebox(p_name, p_theme_type);
  1935. case Theme::DATA_TYPE_MAX:
  1936. break; // Can't happen, but silences warning.
  1937. }
  1938. return Variant();
  1939. }
  1940. #ifdef TOOLS_ENABLED
  1941. Ref<Texture2D> Window::get_editor_theme_icon(const StringName &p_name) const {
  1942. return get_theme_icon(p_name, SNAME("EditorIcons"));
  1943. }
  1944. #endif
  1945. bool Window::has_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
  1946. ERR_READ_THREAD_GUARD_V(false);
  1947. if (!initialized) {
  1948. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  1949. }
  1950. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1951. if (has_theme_icon_override(p_name)) {
  1952. return true;
  1953. }
  1954. }
  1955. Vector<StringName> theme_types;
  1956. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  1957. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
  1958. }
  1959. bool Window::has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  1960. ERR_READ_THREAD_GUARD_V(false);
  1961. if (!initialized) {
  1962. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  1963. }
  1964. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1965. if (has_theme_stylebox_override(p_name)) {
  1966. return true;
  1967. }
  1968. }
  1969. Vector<StringName> theme_types;
  1970. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  1971. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
  1972. }
  1973. bool Window::has_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
  1974. ERR_READ_THREAD_GUARD_V(false);
  1975. if (!initialized) {
  1976. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  1977. }
  1978. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1979. if (has_theme_font_override(p_name)) {
  1980. return true;
  1981. }
  1982. }
  1983. Vector<StringName> theme_types;
  1984. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  1985. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
  1986. }
  1987. bool Window::has_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  1988. ERR_READ_THREAD_GUARD_V(false);
  1989. if (!initialized) {
  1990. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  1991. }
  1992. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1993. if (has_theme_font_size_override(p_name)) {
  1994. return true;
  1995. }
  1996. }
  1997. Vector<StringName> theme_types;
  1998. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  1999. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
  2000. }
  2001. bool Window::has_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
  2002. ERR_READ_THREAD_GUARD_V(false);
  2003. if (!initialized) {
  2004. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2005. }
  2006. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  2007. if (has_theme_color_override(p_name)) {
  2008. return true;
  2009. }
  2010. }
  2011. Vector<StringName> theme_types;
  2012. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2013. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
  2014. }
  2015. bool Window::has_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
  2016. ERR_READ_THREAD_GUARD_V(false);
  2017. if (!initialized) {
  2018. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2019. }
  2020. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  2021. if (has_theme_constant_override(p_name)) {
  2022. return true;
  2023. }
  2024. }
  2025. Vector<StringName> theme_types;
  2026. theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2027. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
  2028. }
  2029. /// Local property overrides.
  2030. void Window::add_theme_icon_override(const StringName &p_name, const Ref<Texture2D> &p_icon) {
  2031. ERR_MAIN_THREAD_GUARD;
  2032. ERR_FAIL_COND(!p_icon.is_valid());
  2033. if (theme_icon_override.has(p_name)) {
  2034. theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2035. }
  2036. theme_icon_override[p_name] = p_icon;
  2037. theme_icon_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  2038. _notify_theme_override_changed();
  2039. }
  2040. void Window::add_theme_style_override(const StringName &p_name, const Ref<StyleBox> &p_style) {
  2041. ERR_MAIN_THREAD_GUARD;
  2042. ERR_FAIL_COND(!p_style.is_valid());
  2043. if (theme_style_override.has(p_name)) {
  2044. theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2045. }
  2046. theme_style_override[p_name] = p_style;
  2047. theme_style_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  2048. _notify_theme_override_changed();
  2049. }
  2050. void Window::add_theme_font_override(const StringName &p_name, const Ref<Font> &p_font) {
  2051. ERR_MAIN_THREAD_GUARD;
  2052. ERR_FAIL_COND(!p_font.is_valid());
  2053. if (theme_font_override.has(p_name)) {
  2054. theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2055. }
  2056. theme_font_override[p_name] = p_font;
  2057. theme_font_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  2058. _notify_theme_override_changed();
  2059. }
  2060. void Window::add_theme_font_size_override(const StringName &p_name, int p_font_size) {
  2061. ERR_MAIN_THREAD_GUARD;
  2062. theme_font_size_override[p_name] = p_font_size;
  2063. _notify_theme_override_changed();
  2064. }
  2065. void Window::add_theme_color_override(const StringName &p_name, const Color &p_color) {
  2066. ERR_MAIN_THREAD_GUARD;
  2067. theme_color_override[p_name] = p_color;
  2068. _notify_theme_override_changed();
  2069. }
  2070. void Window::add_theme_constant_override(const StringName &p_name, int p_constant) {
  2071. ERR_MAIN_THREAD_GUARD;
  2072. theme_constant_override[p_name] = p_constant;
  2073. _notify_theme_override_changed();
  2074. }
  2075. void Window::remove_theme_icon_override(const StringName &p_name) {
  2076. ERR_MAIN_THREAD_GUARD;
  2077. if (theme_icon_override.has(p_name)) {
  2078. theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2079. }
  2080. theme_icon_override.erase(p_name);
  2081. _notify_theme_override_changed();
  2082. }
  2083. void Window::remove_theme_style_override(const StringName &p_name) {
  2084. ERR_MAIN_THREAD_GUARD;
  2085. if (theme_style_override.has(p_name)) {
  2086. theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2087. }
  2088. theme_style_override.erase(p_name);
  2089. _notify_theme_override_changed();
  2090. }
  2091. void Window::remove_theme_font_override(const StringName &p_name) {
  2092. ERR_MAIN_THREAD_GUARD;
  2093. if (theme_font_override.has(p_name)) {
  2094. theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2095. }
  2096. theme_font_override.erase(p_name);
  2097. _notify_theme_override_changed();
  2098. }
  2099. void Window::remove_theme_font_size_override(const StringName &p_name) {
  2100. ERR_MAIN_THREAD_GUARD;
  2101. theme_font_size_override.erase(p_name);
  2102. _notify_theme_override_changed();
  2103. }
  2104. void Window::remove_theme_color_override(const StringName &p_name) {
  2105. ERR_MAIN_THREAD_GUARD;
  2106. theme_color_override.erase(p_name);
  2107. _notify_theme_override_changed();
  2108. }
  2109. void Window::remove_theme_constant_override(const StringName &p_name) {
  2110. ERR_MAIN_THREAD_GUARD;
  2111. theme_constant_override.erase(p_name);
  2112. _notify_theme_override_changed();
  2113. }
  2114. bool Window::has_theme_icon_override(const StringName &p_name) const {
  2115. ERR_READ_THREAD_GUARD_V(false);
  2116. const Ref<Texture2D> *tex = theme_icon_override.getptr(p_name);
  2117. return tex != nullptr;
  2118. }
  2119. bool Window::has_theme_stylebox_override(const StringName &p_name) const {
  2120. ERR_READ_THREAD_GUARD_V(false);
  2121. const Ref<StyleBox> *style = theme_style_override.getptr(p_name);
  2122. return style != nullptr;
  2123. }
  2124. bool Window::has_theme_font_override(const StringName &p_name) const {
  2125. ERR_READ_THREAD_GUARD_V(false);
  2126. const Ref<Font> *font = theme_font_override.getptr(p_name);
  2127. return font != nullptr;
  2128. }
  2129. bool Window::has_theme_font_size_override(const StringName &p_name) const {
  2130. ERR_READ_THREAD_GUARD_V(false);
  2131. const int *font_size = theme_font_size_override.getptr(p_name);
  2132. return font_size != nullptr;
  2133. }
  2134. bool Window::has_theme_color_override(const StringName &p_name) const {
  2135. ERR_READ_THREAD_GUARD_V(false);
  2136. const Color *color = theme_color_override.getptr(p_name);
  2137. return color != nullptr;
  2138. }
  2139. bool Window::has_theme_constant_override(const StringName &p_name) const {
  2140. ERR_READ_THREAD_GUARD_V(false);
  2141. const int *constant = theme_constant_override.getptr(p_name);
  2142. return constant != nullptr;
  2143. }
  2144. /// Default theme properties.
  2145. float Window::get_theme_default_base_scale() const {
  2146. ERR_READ_THREAD_GUARD_V(0);
  2147. return theme_owner->get_theme_default_base_scale();
  2148. }
  2149. Ref<Font> Window::get_theme_default_font() const {
  2150. ERR_READ_THREAD_GUARD_V(Ref<Font>());
  2151. return theme_owner->get_theme_default_font();
  2152. }
  2153. int Window::get_theme_default_font_size() const {
  2154. ERR_READ_THREAD_GUARD_V(0);
  2155. return theme_owner->get_theme_default_font_size();
  2156. }
  2157. /// Bulk actions.
  2158. void Window::begin_bulk_theme_override() {
  2159. ERR_MAIN_THREAD_GUARD;
  2160. bulk_theme_override = true;
  2161. }
  2162. void Window::end_bulk_theme_override() {
  2163. ERR_MAIN_THREAD_GUARD;
  2164. ERR_FAIL_COND(!bulk_theme_override);
  2165. bulk_theme_override = false;
  2166. _notify_theme_override_changed();
  2167. }
  2168. //
  2169. Rect2i Window::get_parent_rect() const {
  2170. ERR_READ_THREAD_GUARD_V(Rect2i());
  2171. ERR_FAIL_COND_V(!is_inside_tree(), Rect2i());
  2172. if (is_embedded()) {
  2173. //viewport
  2174. Node *n = get_parent();
  2175. ERR_FAIL_NULL_V(n, Rect2i());
  2176. Viewport *p = n->get_viewport();
  2177. ERR_FAIL_NULL_V(p, Rect2i());
  2178. return p->get_visible_rect();
  2179. } else {
  2180. int x = get_position().x;
  2181. int closest_dist = 0x7FFFFFFF;
  2182. Rect2i closest_rect;
  2183. for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
  2184. Rect2i s(DisplayServer::get_singleton()->screen_get_position(i), DisplayServer::get_singleton()->screen_get_size(i));
  2185. int d;
  2186. if (x >= s.position.x && x < s.size.x) {
  2187. //contained
  2188. closest_rect = s;
  2189. break;
  2190. } else if (x < s.position.x) {
  2191. d = s.position.x - x;
  2192. } else {
  2193. d = x - (s.position.x + s.size.x);
  2194. }
  2195. if (d < closest_dist) {
  2196. closest_dist = d;
  2197. closest_rect = s;
  2198. }
  2199. }
  2200. return closest_rect;
  2201. }
  2202. }
  2203. void Window::set_clamp_to_embedder(bool p_enable) {
  2204. ERR_MAIN_THREAD_GUARD;
  2205. clamp_to_embedder = p_enable;
  2206. }
  2207. bool Window::is_clamped_to_embedder() const {
  2208. ERR_READ_THREAD_GUARD_V(false);
  2209. return clamp_to_embedder;
  2210. }
  2211. void Window::set_unparent_when_invisible(bool p_unparent) {
  2212. unparent_when_invisible = p_unparent;
  2213. }
  2214. void Window::set_layout_direction(Window::LayoutDirection p_direction) {
  2215. ERR_MAIN_THREAD_GUARD;
  2216. ERR_FAIL_INDEX(p_direction, LAYOUT_DIRECTION_MAX);
  2217. layout_dir = p_direction;
  2218. propagate_notification(Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
  2219. }
  2220. Window::LayoutDirection Window::get_layout_direction() const {
  2221. ERR_READ_THREAD_GUARD_V(LAYOUT_DIRECTION_INHERITED);
  2222. return layout_dir;
  2223. }
  2224. bool Window::is_layout_rtl() const {
  2225. ERR_READ_THREAD_GUARD_V(false);
  2226. if (layout_dir == LAYOUT_DIRECTION_INHERITED) {
  2227. #ifdef TOOLS_ENABLED
  2228. if (is_part_of_edited_scene() && GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  2229. return true;
  2230. }
  2231. if (is_inside_tree()) {
  2232. Node *edited_scene_root = get_tree()->get_edited_scene_root();
  2233. if (edited_scene_root == this) {
  2234. int proj_root_layout_direction = GLOBAL_GET(SNAME("internationalization/rendering/root_node_layout_direction"));
  2235. if (proj_root_layout_direction == 1) {
  2236. return false;
  2237. } else if (proj_root_layout_direction == 2) {
  2238. return true;
  2239. } else if (proj_root_layout_direction == 3) {
  2240. String locale = OS::get_singleton()->get_locale();
  2241. return TS->is_locale_right_to_left(locale);
  2242. } else {
  2243. String locale = TranslationServer::get_singleton()->get_tool_locale();
  2244. return TS->is_locale_right_to_left(locale);
  2245. }
  2246. }
  2247. }
  2248. #else
  2249. if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  2250. return true;
  2251. }
  2252. #endif
  2253. Node *parent_node = get_parent();
  2254. while (parent_node) {
  2255. Control *parent_control = Object::cast_to<Control>(parent_node);
  2256. if (parent_control) {
  2257. return parent_control->is_layout_rtl();
  2258. }
  2259. Window *parent_window = Object::cast_to<Window>(parent_node);
  2260. if (parent_window) {
  2261. return parent_window->is_layout_rtl();
  2262. }
  2263. parent_node = parent_node->get_parent();
  2264. }
  2265. if (root_layout_direction == 1) {
  2266. return false;
  2267. } else if (root_layout_direction == 2) {
  2268. return true;
  2269. } else if (root_layout_direction == 3) {
  2270. String locale = OS::get_singleton()->get_locale();
  2271. return TS->is_locale_right_to_left(locale);
  2272. } else {
  2273. String locale = TranslationServer::get_singleton()->get_tool_locale();
  2274. return TS->is_locale_right_to_left(locale);
  2275. }
  2276. } else if (layout_dir == LAYOUT_DIRECTION_APPLICATION_LOCALE) {
  2277. if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  2278. return true;
  2279. } else {
  2280. String locale = TranslationServer::get_singleton()->get_tool_locale();
  2281. return TS->is_locale_right_to_left(locale);
  2282. }
  2283. } else if (layout_dir == LAYOUT_DIRECTION_SYSTEM_LOCALE) {
  2284. if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  2285. return true;
  2286. } else {
  2287. String locale = OS::get_singleton()->get_locale();
  2288. return TS->is_locale_right_to_left(locale);
  2289. }
  2290. } else {
  2291. return (layout_dir == LAYOUT_DIRECTION_RTL);
  2292. }
  2293. }
  2294. #ifndef DISABLE_DEPRECATED
  2295. void Window::set_auto_translate(bool p_enable) {
  2296. ERR_MAIN_THREAD_GUARD;
  2297. set_auto_translate_mode(p_enable ? AUTO_TRANSLATE_MODE_ALWAYS : AUTO_TRANSLATE_MODE_DISABLED);
  2298. }
  2299. bool Window::is_auto_translating() const {
  2300. ERR_READ_THREAD_GUARD_V(false);
  2301. return can_auto_translate();
  2302. }
  2303. #endif
  2304. Transform2D Window::get_final_transform() const {
  2305. ERR_READ_THREAD_GUARD_V(Transform2D());
  2306. return window_transform * stretch_transform * global_canvas_transform;
  2307. }
  2308. Transform2D Window::get_screen_transform_internal(bool p_absolute_position) const {
  2309. ERR_READ_THREAD_GUARD_V(Transform2D());
  2310. Transform2D embedder_transform;
  2311. if (get_embedder()) {
  2312. embedder_transform.translate_local(get_position());
  2313. embedder_transform = get_embedder()->get_screen_transform_internal(p_absolute_position) * embedder_transform;
  2314. } else if (p_absolute_position) {
  2315. embedder_transform.translate_local(get_position());
  2316. }
  2317. return embedder_transform * get_final_transform();
  2318. }
  2319. Transform2D Window::get_popup_base_transform() const {
  2320. ERR_READ_THREAD_GUARD_V(Transform2D());
  2321. if (is_embedding_subwindows()) {
  2322. return Transform2D();
  2323. }
  2324. Transform2D popup_base_transform;
  2325. popup_base_transform.set_origin(get_position());
  2326. popup_base_transform *= get_final_transform();
  2327. if (get_embedder()) {
  2328. return get_embedder()->get_popup_base_transform() * popup_base_transform;
  2329. }
  2330. return popup_base_transform;
  2331. }
  2332. Viewport *Window::get_section_root_viewport() const {
  2333. if (get_embedder()) {
  2334. return get_embedder()->get_section_root_viewport();
  2335. }
  2336. if (is_inside_tree()) {
  2337. // Native window.
  2338. return SceneTree::get_singleton()->get_root();
  2339. }
  2340. Window *vp = const_cast<Window *>(this);
  2341. return vp;
  2342. }
  2343. bool Window::is_attached_in_viewport() const {
  2344. return get_embedder();
  2345. }
  2346. void Window::_update_mouse_over(Vector2 p_pos) {
  2347. if (!mouse_in_window) {
  2348. if (is_embedded()) {
  2349. mouse_in_window = true;
  2350. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
  2351. }
  2352. }
  2353. bool new_in = get_visible_rect().has_point(p_pos);
  2354. if (new_in == gui.mouse_in_viewport) {
  2355. if (new_in) {
  2356. Viewport::_update_mouse_over(p_pos);
  2357. }
  2358. return;
  2359. }
  2360. if (new_in) {
  2361. notification(NOTIFICATION_VP_MOUSE_ENTER);
  2362. Viewport::_update_mouse_over(p_pos);
  2363. } else {
  2364. Viewport::_mouse_leave_viewport();
  2365. }
  2366. }
  2367. void Window::_mouse_leave_viewport() {
  2368. Viewport::_mouse_leave_viewport();
  2369. if (is_embedded()) {
  2370. mouse_in_window = false;
  2371. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
  2372. }
  2373. }
  2374. void Window::_bind_methods() {
  2375. ClassDB::bind_method(D_METHOD("set_title", "title"), &Window::set_title);
  2376. ClassDB::bind_method(D_METHOD("get_title"), &Window::get_title);
  2377. ClassDB::bind_method(D_METHOD("get_window_id"), &Window::get_window_id);
  2378. ClassDB::bind_method(D_METHOD("set_initial_position", "initial_position"), &Window::set_initial_position);
  2379. ClassDB::bind_method(D_METHOD("get_initial_position"), &Window::get_initial_position);
  2380. ClassDB::bind_method(D_METHOD("set_current_screen", "index"), &Window::set_current_screen);
  2381. ClassDB::bind_method(D_METHOD("get_current_screen"), &Window::get_current_screen);
  2382. ClassDB::bind_method(D_METHOD("set_position", "position"), &Window::set_position);
  2383. ClassDB::bind_method(D_METHOD("get_position"), &Window::get_position);
  2384. ClassDB::bind_method(D_METHOD("move_to_center"), &Window::move_to_center);
  2385. ClassDB::bind_method(D_METHOD("set_size", "size"), &Window::set_size);
  2386. ClassDB::bind_method(D_METHOD("get_size"), &Window::get_size);
  2387. ClassDB::bind_method(D_METHOD("reset_size"), &Window::reset_size);
  2388. ClassDB::bind_method(D_METHOD("get_position_with_decorations"), &Window::get_position_with_decorations);
  2389. ClassDB::bind_method(D_METHOD("get_size_with_decorations"), &Window::get_size_with_decorations);
  2390. ClassDB::bind_method(D_METHOD("set_max_size", "max_size"), &Window::set_max_size);
  2391. ClassDB::bind_method(D_METHOD("get_max_size"), &Window::get_max_size);
  2392. ClassDB::bind_method(D_METHOD("set_min_size", "min_size"), &Window::set_min_size);
  2393. ClassDB::bind_method(D_METHOD("get_min_size"), &Window::get_min_size);
  2394. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &Window::set_mode);
  2395. ClassDB::bind_method(D_METHOD("get_mode"), &Window::get_mode);
  2396. ClassDB::bind_method(D_METHOD("set_flag", "flag", "enabled"), &Window::set_flag);
  2397. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &Window::get_flag);
  2398. ClassDB::bind_method(D_METHOD("is_maximize_allowed"), &Window::is_maximize_allowed);
  2399. ClassDB::bind_method(D_METHOD("request_attention"), &Window::request_attention);
  2400. #ifndef DISABLE_DEPRECATED
  2401. ClassDB::bind_method(D_METHOD("move_to_foreground"), &Window::move_to_foreground);
  2402. #endif // DISABLE_DEPRECATED
  2403. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Window::set_visible);
  2404. ClassDB::bind_method(D_METHOD("is_visible"), &Window::is_visible);
  2405. ClassDB::bind_method(D_METHOD("hide"), &Window::hide);
  2406. ClassDB::bind_method(D_METHOD("show"), &Window::show);
  2407. ClassDB::bind_method(D_METHOD("set_transient", "transient"), &Window::set_transient);
  2408. ClassDB::bind_method(D_METHOD("is_transient"), &Window::is_transient);
  2409. ClassDB::bind_method(D_METHOD("set_transient_to_focused", "enable"), &Window::set_transient_to_focused);
  2410. ClassDB::bind_method(D_METHOD("is_transient_to_focused"), &Window::is_transient_to_focused);
  2411. ClassDB::bind_method(D_METHOD("set_exclusive", "exclusive"), &Window::set_exclusive);
  2412. ClassDB::bind_method(D_METHOD("is_exclusive"), &Window::is_exclusive);
  2413. ClassDB::bind_method(D_METHOD("set_unparent_when_invisible", "unparent"), &Window::set_unparent_when_invisible);
  2414. ClassDB::bind_method(D_METHOD("can_draw"), &Window::can_draw);
  2415. ClassDB::bind_method(D_METHOD("has_focus"), &Window::has_focus);
  2416. ClassDB::bind_method(D_METHOD("grab_focus"), &Window::grab_focus);
  2417. ClassDB::bind_method(D_METHOD("set_ime_active", "active"), &Window::set_ime_active);
  2418. ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &Window::set_ime_position);
  2419. ClassDB::bind_method(D_METHOD("is_embedded"), &Window::is_embedded);
  2420. ClassDB::bind_method(D_METHOD("get_contents_minimum_size"), &Window::get_contents_minimum_size);
  2421. ClassDB::bind_method(D_METHOD("set_force_native", "force_native"), &Window::set_force_native);
  2422. ClassDB::bind_method(D_METHOD("get_force_native"), &Window::get_force_native);
  2423. ClassDB::bind_method(D_METHOD("set_content_scale_size", "size"), &Window::set_content_scale_size);
  2424. ClassDB::bind_method(D_METHOD("get_content_scale_size"), &Window::get_content_scale_size);
  2425. ClassDB::bind_method(D_METHOD("set_content_scale_mode", "mode"), &Window::set_content_scale_mode);
  2426. ClassDB::bind_method(D_METHOD("get_content_scale_mode"), &Window::get_content_scale_mode);
  2427. ClassDB::bind_method(D_METHOD("set_content_scale_aspect", "aspect"), &Window::set_content_scale_aspect);
  2428. ClassDB::bind_method(D_METHOD("get_content_scale_aspect"), &Window::get_content_scale_aspect);
  2429. ClassDB::bind_method(D_METHOD("set_content_scale_stretch", "stretch"), &Window::set_content_scale_stretch);
  2430. ClassDB::bind_method(D_METHOD("get_content_scale_stretch"), &Window::get_content_scale_stretch);
  2431. ClassDB::bind_method(D_METHOD("set_keep_title_visible", "title_visible"), &Window::set_keep_title_visible);
  2432. ClassDB::bind_method(D_METHOD("get_keep_title_visible"), &Window::get_keep_title_visible);
  2433. ClassDB::bind_method(D_METHOD("set_content_scale_factor", "factor"), &Window::set_content_scale_factor);
  2434. ClassDB::bind_method(D_METHOD("get_content_scale_factor"), &Window::get_content_scale_factor);
  2435. ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &Window::set_use_font_oversampling);
  2436. ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &Window::is_using_font_oversampling);
  2437. ClassDB::bind_method(D_METHOD("set_mouse_passthrough_polygon", "polygon"), &Window::set_mouse_passthrough_polygon);
  2438. ClassDB::bind_method(D_METHOD("get_mouse_passthrough_polygon"), &Window::get_mouse_passthrough_polygon);
  2439. ClassDB::bind_method(D_METHOD("set_wrap_controls", "enable"), &Window::set_wrap_controls);
  2440. ClassDB::bind_method(D_METHOD("is_wrapping_controls"), &Window::is_wrapping_controls);
  2441. ClassDB::bind_method(D_METHOD("child_controls_changed"), &Window::child_controls_changed);
  2442. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Window::set_theme);
  2443. ClassDB::bind_method(D_METHOD("get_theme"), &Window::get_theme);
  2444. ClassDB::bind_method(D_METHOD("set_theme_type_variation", "theme_type"), &Window::set_theme_type_variation);
  2445. ClassDB::bind_method(D_METHOD("get_theme_type_variation"), &Window::get_theme_type_variation);
  2446. ClassDB::bind_method(D_METHOD("begin_bulk_theme_override"), &Window::begin_bulk_theme_override);
  2447. ClassDB::bind_method(D_METHOD("end_bulk_theme_override"), &Window::end_bulk_theme_override);
  2448. ClassDB::bind_method(D_METHOD("add_theme_icon_override", "name", "texture"), &Window::add_theme_icon_override);
  2449. ClassDB::bind_method(D_METHOD("add_theme_stylebox_override", "name", "stylebox"), &Window::add_theme_style_override);
  2450. ClassDB::bind_method(D_METHOD("add_theme_font_override", "name", "font"), &Window::add_theme_font_override);
  2451. ClassDB::bind_method(D_METHOD("add_theme_font_size_override", "name", "font_size"), &Window::add_theme_font_size_override);
  2452. ClassDB::bind_method(D_METHOD("add_theme_color_override", "name", "color"), &Window::add_theme_color_override);
  2453. ClassDB::bind_method(D_METHOD("add_theme_constant_override", "name", "constant"), &Window::add_theme_constant_override);
  2454. ClassDB::bind_method(D_METHOD("remove_theme_icon_override", "name"), &Window::remove_theme_icon_override);
  2455. ClassDB::bind_method(D_METHOD("remove_theme_stylebox_override", "name"), &Window::remove_theme_style_override);
  2456. ClassDB::bind_method(D_METHOD("remove_theme_font_override", "name"), &Window::remove_theme_font_override);
  2457. ClassDB::bind_method(D_METHOD("remove_theme_font_size_override", "name"), &Window::remove_theme_font_size_override);
  2458. ClassDB::bind_method(D_METHOD("remove_theme_color_override", "name"), &Window::remove_theme_color_override);
  2459. ClassDB::bind_method(D_METHOD("remove_theme_constant_override", "name"), &Window::remove_theme_constant_override);
  2460. ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "theme_type"), &Window::get_theme_icon, DEFVAL(StringName()));
  2461. ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "theme_type"), &Window::get_theme_stylebox, DEFVAL(StringName()));
  2462. ClassDB::bind_method(D_METHOD("get_theme_font", "name", "theme_type"), &Window::get_theme_font, DEFVAL(StringName()));
  2463. ClassDB::bind_method(D_METHOD("get_theme_font_size", "name", "theme_type"), &Window::get_theme_font_size, DEFVAL(StringName()));
  2464. ClassDB::bind_method(D_METHOD("get_theme_color", "name", "theme_type"), &Window::get_theme_color, DEFVAL(StringName()));
  2465. ClassDB::bind_method(D_METHOD("get_theme_constant", "name", "theme_type"), &Window::get_theme_constant, DEFVAL(StringName()));
  2466. ClassDB::bind_method(D_METHOD("has_theme_icon_override", "name"), &Window::has_theme_icon_override);
  2467. ClassDB::bind_method(D_METHOD("has_theme_stylebox_override", "name"), &Window::has_theme_stylebox_override);
  2468. ClassDB::bind_method(D_METHOD("has_theme_font_override", "name"), &Window::has_theme_font_override);
  2469. ClassDB::bind_method(D_METHOD("has_theme_font_size_override", "name"), &Window::has_theme_font_size_override);
  2470. ClassDB::bind_method(D_METHOD("has_theme_color_override", "name"), &Window::has_theme_color_override);
  2471. ClassDB::bind_method(D_METHOD("has_theme_constant_override", "name"), &Window::has_theme_constant_override);
  2472. ClassDB::bind_method(D_METHOD("has_theme_icon", "name", "theme_type"), &Window::has_theme_icon, DEFVAL(StringName()));
  2473. ClassDB::bind_method(D_METHOD("has_theme_stylebox", "name", "theme_type"), &Window::has_theme_stylebox, DEFVAL(StringName()));
  2474. ClassDB::bind_method(D_METHOD("has_theme_font", "name", "theme_type"), &Window::has_theme_font, DEFVAL(StringName()));
  2475. ClassDB::bind_method(D_METHOD("has_theme_font_size", "name", "theme_type"), &Window::has_theme_font_size, DEFVAL(StringName()));
  2476. ClassDB::bind_method(D_METHOD("has_theme_color", "name", "theme_type"), &Window::has_theme_color, DEFVAL(StringName()));
  2477. ClassDB::bind_method(D_METHOD("has_theme_constant", "name", "theme_type"), &Window::has_theme_constant, DEFVAL(StringName()));
  2478. ClassDB::bind_method(D_METHOD("get_theme_default_base_scale"), &Window::get_theme_default_base_scale);
  2479. ClassDB::bind_method(D_METHOD("get_theme_default_font"), &Window::get_theme_default_font);
  2480. ClassDB::bind_method(D_METHOD("get_theme_default_font_size"), &Window::get_theme_default_font_size);
  2481. ClassDB::bind_method(D_METHOD("set_layout_direction", "direction"), &Window::set_layout_direction);
  2482. ClassDB::bind_method(D_METHOD("get_layout_direction"), &Window::get_layout_direction);
  2483. ClassDB::bind_method(D_METHOD("is_layout_rtl"), &Window::is_layout_rtl);
  2484. #ifndef DISABLE_DEPRECATED
  2485. ClassDB::bind_method(D_METHOD("set_auto_translate", "enable"), &Window::set_auto_translate);
  2486. ClassDB::bind_method(D_METHOD("is_auto_translating"), &Window::is_auto_translating);
  2487. #endif
  2488. ClassDB::bind_method(D_METHOD("popup", "rect"), &Window::popup, DEFVAL(Rect2i()));
  2489. ClassDB::bind_method(D_METHOD("popup_on_parent", "parent_rect"), &Window::popup_on_parent);
  2490. ClassDB::bind_method(D_METHOD("popup_centered", "minsize"), &Window::popup_centered, DEFVAL(Size2i()));
  2491. ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Window::popup_centered_ratio, DEFVAL(0.8));
  2492. ClassDB::bind_method(D_METHOD("popup_centered_clamped", "minsize", "fallback_ratio"), &Window::popup_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
  2493. ClassDB::bind_method(D_METHOD("popup_exclusive", "from_node", "rect"), &Window::popup_exclusive, DEFVAL(Rect2i()));
  2494. ClassDB::bind_method(D_METHOD("popup_exclusive_on_parent", "from_node", "parent_rect"), &Window::popup_exclusive_on_parent);
  2495. ClassDB::bind_method(D_METHOD("popup_exclusive_centered", "from_node", "minsize"), &Window::popup_exclusive_centered, DEFVAL(Size2i()));
  2496. ClassDB::bind_method(D_METHOD("popup_exclusive_centered_ratio", "from_node", "ratio"), &Window::popup_exclusive_centered_ratio, DEFVAL(0.8));
  2497. ClassDB::bind_method(D_METHOD("popup_exclusive_centered_clamped", "from_node", "minsize", "fallback_ratio"), &Window::popup_exclusive_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
  2498. // Keep the enum values in sync with the `Mode` enum.
  2499. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Windowed,Minimized,Maximized,Fullscreen,Exclusive Fullscreen"), "set_mode", "get_mode");
  2500. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  2501. // Keep the enum values in sync with the `WindowInitialPosition` enum.
  2502. ADD_PROPERTY(PropertyInfo(Variant::INT, "initial_position", PROPERTY_HINT_ENUM, "Absolute,Center of Primary Screen,Center of Main Window Screen,Center of Other Screen,Center of Screen With Mouse Pointer,Center of Screen With Keyboard Focus"), "set_initial_position", "get_initial_position");
  2503. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
  2504. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
  2505. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_screen", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), "set_current_screen", "get_current_screen");
  2506. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "mouse_passthrough_polygon"), "set_mouse_passthrough_polygon", "get_mouse_passthrough_polygon");
  2507. ADD_GROUP("Flags", "");
  2508. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  2509. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_controls"), "set_wrap_controls", "is_wrapping_controls");
  2510. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient"), "set_transient", "is_transient");
  2511. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient_to_focused"), "set_transient_to_focused", "is_transient_to_focused");
  2512. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclusive"), "set_exclusive", "is_exclusive");
  2513. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unresizable"), "set_flag", "get_flag", FLAG_RESIZE_DISABLED);
  2514. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "borderless"), "set_flag", "get_flag", FLAG_BORDERLESS);
  2515. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "always_on_top"), "set_flag", "get_flag", FLAG_ALWAYS_ON_TOP);
  2516. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_flag", "get_flag", FLAG_TRANSPARENT);
  2517. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unfocusable"), "set_flag", "get_flag", FLAG_NO_FOCUS);
  2518. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "popup_window"), "set_flag", "get_flag", FLAG_POPUP);
  2519. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "extend_to_title"), "set_flag", "get_flag", FLAG_EXTEND_TO_TITLE);
  2520. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "mouse_passthrough"), "set_flag", "get_flag", FLAG_MOUSE_PASSTHROUGH);
  2521. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "sharp_corners"), "set_flag", "get_flag", FLAG_SHARP_CORNERS);
  2522. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_native"), "set_force_native", "get_force_native");
  2523. ADD_GROUP("Limits", "");
  2524. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "min_size", PROPERTY_HINT_NONE, "suffix:px"), "set_min_size", "get_min_size");
  2525. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "max_size", PROPERTY_HINT_NONE, "suffix:px"), "set_max_size", "get_max_size");
  2526. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_title_visible"), "set_keep_title_visible", "get_keep_title_visible");
  2527. ADD_GROUP("Content Scale", "content_scale_");
  2528. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "content_scale_size"), "set_content_scale_size", "get_content_scale_size");
  2529. ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_mode", PROPERTY_HINT_ENUM, "Disabled,Canvas Items,Viewport"), "set_content_scale_mode", "get_content_scale_mode");
  2530. ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_aspect", PROPERTY_HINT_ENUM, "Ignore,Keep,Keep Width,Keep Height,Expand"), "set_content_scale_aspect", "get_content_scale_aspect");
  2531. ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_stretch", PROPERTY_HINT_ENUM, "Fractional,Integer"), "set_content_scale_stretch", "get_content_scale_stretch");
  2532. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "content_scale_factor", PROPERTY_HINT_RANGE, "0.5,8.0,0.01"), "set_content_scale_factor", "get_content_scale_factor");
  2533. #ifndef DISABLE_DEPRECATED
  2534. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_auto_translate", "is_auto_translating");
  2535. #endif
  2536. ADD_GROUP("Theme", "theme_");
  2537. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
  2538. ADD_PROPERTY(PropertyInfo(Variant::STRING, "theme_type_variation", PROPERTY_HINT_ENUM_SUGGESTION), "set_theme_type_variation", "get_theme_type_variation");
  2539. ADD_SIGNAL(MethodInfo("window_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  2540. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
  2541. ADD_SIGNAL(MethodInfo("mouse_entered"));
  2542. ADD_SIGNAL(MethodInfo("mouse_exited"));
  2543. ADD_SIGNAL(MethodInfo("focus_entered"));
  2544. ADD_SIGNAL(MethodInfo("focus_exited"));
  2545. ADD_SIGNAL(MethodInfo("close_requested"));
  2546. ADD_SIGNAL(MethodInfo("go_back_requested"));
  2547. ADD_SIGNAL(MethodInfo("visibility_changed"));
  2548. ADD_SIGNAL(MethodInfo("about_to_popup"));
  2549. ADD_SIGNAL(MethodInfo("theme_changed"));
  2550. ADD_SIGNAL(MethodInfo("dpi_changed"));
  2551. ADD_SIGNAL(MethodInfo("titlebar_changed"));
  2552. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  2553. BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);
  2554. BIND_ENUM_CONSTANT(MODE_WINDOWED);
  2555. BIND_ENUM_CONSTANT(MODE_MINIMIZED);
  2556. BIND_ENUM_CONSTANT(MODE_MAXIMIZED);
  2557. BIND_ENUM_CONSTANT(MODE_FULLSCREEN);
  2558. BIND_ENUM_CONSTANT(MODE_EXCLUSIVE_FULLSCREEN);
  2559. BIND_ENUM_CONSTANT(FLAG_RESIZE_DISABLED);
  2560. BIND_ENUM_CONSTANT(FLAG_BORDERLESS);
  2561. BIND_ENUM_CONSTANT(FLAG_ALWAYS_ON_TOP);
  2562. BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
  2563. BIND_ENUM_CONSTANT(FLAG_NO_FOCUS);
  2564. BIND_ENUM_CONSTANT(FLAG_POPUP);
  2565. BIND_ENUM_CONSTANT(FLAG_EXTEND_TO_TITLE);
  2566. BIND_ENUM_CONSTANT(FLAG_MOUSE_PASSTHROUGH);
  2567. BIND_ENUM_CONSTANT(FLAG_SHARP_CORNERS);
  2568. BIND_ENUM_CONSTANT(FLAG_MAX);
  2569. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_DISABLED);
  2570. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_CANVAS_ITEMS);
  2571. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_VIEWPORT);
  2572. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_IGNORE);
  2573. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP);
  2574. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_WIDTH);
  2575. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_HEIGHT);
  2576. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_EXPAND);
  2577. BIND_ENUM_CONSTANT(CONTENT_SCALE_STRETCH_FRACTIONAL);
  2578. BIND_ENUM_CONSTANT(CONTENT_SCALE_STRETCH_INTEGER);
  2579. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_INHERITED);
  2580. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_APPLICATION_LOCALE);
  2581. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LTR);
  2582. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_RTL);
  2583. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_SYSTEM_LOCALE);
  2584. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_MAX);
  2585. #ifndef DISABLE_DEPRECATED
  2586. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LOCALE);
  2587. #endif // DISABLE_DEPRECATED
  2588. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_ABSOLUTE);
  2589. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN);
  2590. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN);
  2591. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN);
  2592. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS);
  2593. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS);
  2594. GDVIRTUAL_BIND(_get_contents_minimum_size);
  2595. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Window, embedded_border);
  2596. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Window, embedded_unfocused_border);
  2597. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Window, title_font);
  2598. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Window, title_font_size);
  2599. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Window, title_color);
  2600. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, title_height);
  2601. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Window, title_outline_modulate);
  2602. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, title_outline_size);
  2603. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Window, close);
  2604. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Window, close_pressed);
  2605. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, close_h_offset);
  2606. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, close_v_offset);
  2607. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, resize_margin);
  2608. }
  2609. Window::Window() {
  2610. RenderingServer *rendering_server = RenderingServer::get_singleton();
  2611. if (rendering_server) {
  2612. max_size = rendering_server->get_maximum_viewport_size();
  2613. max_size_used = max_size; // Update max_size_used.
  2614. }
  2615. theme_owner = memnew(ThemeOwner(this));
  2616. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  2617. }
  2618. Window::~Window() {
  2619. memdelete(theme_owner);
  2620. // Resources need to be disconnected.
  2621. for (KeyValue<StringName, Ref<Texture2D>> &E : theme_icon_override) {
  2622. E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2623. }
  2624. for (KeyValue<StringName, Ref<StyleBox>> &E : theme_style_override) {
  2625. E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2626. }
  2627. for (KeyValue<StringName, Ref<Font>> &E : theme_font_override) {
  2628. E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2629. }
  2630. // Then override maps can be simply cleared.
  2631. theme_icon_override.clear();
  2632. theme_style_override.clear();
  2633. theme_font_override.clear();
  2634. theme_font_size_override.clear();
  2635. theme_color_override.clear();
  2636. theme_constant_override.clear();
  2637. }