window.cpp 112 KB

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