window.cpp 113 KB

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