Debian Codegraph @

scene/main/window.cpp

raw symbols SCIP
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
31 #include "window.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/debugger/engine_debugger.h"
35 #include "scene/gui/control.h"
36 #include "scene/theme/theme_db.h"
37 #include "scene/theme/theme_owner.h"
38
39 // Editor integration.
40
41 int Window::root_layout_direction = 0;
42
43 void Window::set_root_layout_direction(int p_root_dir) {
44 root_layout_direction = p_root_dir;
45 }
46
47 // Dynamic properties.
48
49 Window *Window::focused_window = nullptr;
50
51 bool Window::_set(const StringName &p_name, const Variant &p_value) {
52 ERR_MAIN_THREAD_GUARD_V(false);
53 String name = p_name;
54
55 if (!name.begins_with("theme_override")) {
56 return false;
57 }
58
59 if (p_value.get_type() == Variant::NIL || (p_value.get_type() == Variant::OBJECT && (Object *)p_value == nullptr)) {
60 if (name.begins_with("theme_override_icons/")) {
61 String dname = name.get_slicec('/', 1);
62 if (theme_icon_override.has(dname)) {
63 theme_icon_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
64 }
65 theme_icon_override.erase(dname);
66 _notify_theme_override_changed();
67 } else if (name.begins_with("theme_override_styles/")) {
68 String dname = name.get_slicec('/', 1);
69 if (theme_style_override.has(dname)) {
70 theme_style_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
71 }
72 theme_style_override.erase(dname);
73 _notify_theme_override_changed();
74 } else if (name.begins_with("theme_override_fonts/")) {
75 String dname = name.get_slicec('/', 1);
76 if (theme_font_override.has(dname)) {
77 theme_font_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
78 }
79 theme_font_override.erase(dname);
80 _notify_theme_override_changed();
81 } else if (name.begins_with("theme_override_font_sizes/")) {
82 String dname = name.get_slicec('/', 1);
83 theme_font_size_override.erase(dname);
84 _notify_theme_override_changed();
85 } else if (name.begins_with("theme_override_colors/")) {
86 String dname = name.get_slicec('/', 1);
87 theme_color_override.erase(dname);
88 _notify_theme_override_changed();
89 } else if (name.begins_with("theme_override_constants/")) {
90 String dname = name.get_slicec('/', 1);
91 theme_constant_override.erase(dname);
92 _notify_theme_override_changed();
93 } else {
94 return false;
95 }
96 } else {
97 if (name.begins_with("theme_override_icons/")) {
98 String dname = name.get_slicec('/', 1);
99 add_theme_icon_override(dname, p_value);
100 } else if (name.begins_with("theme_override_styles/")) {
101 String dname = name.get_slicec('/', 1);
102 add_theme_style_override(dname, p_value);
103 } else if (name.begins_with("theme_override_fonts/")) {
104 String dname = name.get_slicec('/', 1);
105 add_theme_font_override(dname, p_value);
106 } else if (name.begins_with("theme_override_font_sizes/")) {
107 String dname = name.get_slicec('/', 1);
108 add_theme_font_size_override(dname, p_value);
109 } else if (name.begins_with("theme_override_colors/")) {
110 String dname = name.get_slicec('/', 1);
111 add_theme_color_override(dname, p_value);
112 } else if (name.begins_with("theme_override_constants/")) {
113 String dname = name.get_slicec('/', 1);
114 add_theme_constant_override(dname, p_value);
115 } else {
116 return false;
117 }
118 }
119 return true;
120 }
121
122 bool Window::_get(const StringName &p_name, Variant &r_ret) const {
123 ERR_READ_THREAD_GUARD_V(false);
124 String sname = p_name;
125
126 if (!sname.begins_with("theme_override")) {
127 return false;
128 }
129
130 if (sname.begins_with("theme_override_icons/")) {
131 String name = sname.get_slicec('/', 1);
132 r_ret = theme_icon_override.has(name) ? Variant(theme_icon_override[name]) : Variant();
133 } else if (sname.begins_with("theme_override_styles/")) {
134 String name = sname.get_slicec('/', 1);
135 r_ret = theme_style_override.has(name) ? Variant(theme_style_override[name]) : Variant();
136 } else if (sname.begins_with("theme_override_fonts/")) {
137 String name = sname.get_slicec('/', 1);
138 r_ret = theme_font_override.has(name) ? Variant(theme_font_override[name]) : Variant();
139 } else if (sname.begins_with("theme_override_font_sizes/")) {
140 String name = sname.get_slicec('/', 1);
141 r_ret = theme_font_size_override.has(name) ? Variant(theme_font_size_override[name]) : Variant();
142 } else if (sname.begins_with("theme_override_colors/")) {
143 String name = sname.get_slicec('/', 1);
144 r_ret = theme_color_override.has(name) ? Variant(theme_color_override[name]) : Variant();
145 } else if (sname.begins_with("theme_override_constants/")) {
146 String name = sname.get_slicec('/', 1);
147 r_ret = theme_constant_override.has(name) ? Variant(theme_constant_override[name]) : Variant();
148 } else {
149 return false;
150 }
151
152 return true;
153 }
154
155 void Window::_get_property_list(List<PropertyInfo> *p_list) const {
156 ERR_READ_THREAD_GUARD;
157
158 Ref<Theme> default_theme = ThemeDB::get_singleton()->get_default_theme();
159
160 p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Theme Overrides", "theme_override_"), PROPERTY_HINT_NONE, "theme_override_", PROPERTY_USAGE_GROUP));
161
162 {
163 List<StringName> names;
164 default_theme->get_color_list(get_class_name(), &names);
165 for (const StringName &E : names) {
166 uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
167 if (theme_color_override.has(E)) {
168 usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
169 }
170
171 p_list->push_back(PropertyInfo(Variant::COLOR, PNAME("theme_override_colors") + String("/") + E, PROPERTY_HINT_NONE, "", usage));
172 }
173 }
174 {
175 List<StringName> names;
176 default_theme->get_constant_list(get_class_name(), &names);
177 for (const StringName &E : names) {
178 uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
179 if (theme_constant_override.has(E)) {
180 usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
181 }
182
183 p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_constants") + String("/") + E, PROPERTY_HINT_RANGE, "-16384,16384", usage));
184 }
185 }
186 {
187 List<StringName> names;
188 default_theme->get_font_list(get_class_name(), &names);
189 for (const StringName &E : names) {
190 uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
191 if (theme_font_override.has(E)) {
192 usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
193 }
194
195 p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_fonts") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "Font", usage));
196 }
197 }
198 {
199 List<StringName> names;
200 default_theme->get_font_size_list(get_class_name(), &names);
201 for (const StringName &E : names) {
202 uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
203 if (theme_font_size_override.has(E)) {
204 usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
205 }
206
207 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));
208 }
209 }
210 {
211 List<StringName> names;
212 default_theme->get_icon_list(get_class_name(), &names);
213 for (const StringName &E : names) {
214 uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
215 if (theme_icon_override.has(E)) {
216 usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
217 }
218
219 p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_icons") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", usage));
220 }
221 }
222 {
223 List<StringName> names;
224 default_theme->get_stylebox_list(get_class_name(), &names);
225 for (const StringName &E : names) {
226 uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
227 if (theme_style_override.has(E)) {
228 usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
229 }
230
231 p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_styles") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", usage));
232 }
233 }
234 }
235
236 void Window::_validate_property(PropertyInfo &p_property) const {
237 if (p_property.name == "position") {
238 if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
239 p_property.usage = PROPERTY_USAGE_NONE;
240 }
241 } else if (p_property.name == "current_screen") {
242 if (initial_position != WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN) {
243 p_property.usage = PROPERTY_USAGE_NONE;
244 }
245 } else if (Engine::get_singleton()->is_editor_hint() && p_property.name == "theme_type_variation") {
246 List<StringName> names;
247
248 ThemeDB::get_singleton()->get_default_theme()->get_type_variation_list(get_class_name(), &names);
249
250 // Iterate to find all themes.
251 Control *tmp_control = Object::cast_to<Control>(get_parent());
252 Window *tmp_window = Object::cast_to<Window>(get_parent());
253 while (tmp_control || tmp_window) {
254 // We go up and any non Control/Window will break the chain.
255 if (tmp_control) {
256 if (tmp_control->get_theme().is_valid()) {
257 tmp_control->get_theme()->get_type_variation_list(get_class_name(), &names);
258 }
259 tmp_window = Object::cast_to<Window>(tmp_control->get_parent());
260 tmp_control = Object::cast_to<Control>(tmp_control->get_parent());
261 } else { // Window.
262 if (tmp_window->get_theme().is_valid()) {
263 tmp_window->get_theme()->get_type_variation_list(get_class_name(), &names);
264 }
265 tmp_control = Object::cast_to<Control>(tmp_window->get_parent());
266 tmp_window = Object::cast_to<Window>(tmp_window->get_parent());
267 }
268 }
269 if (get_theme().is_valid()) {
270 get_theme()->get_type_variation_list(get_class_name(), &names);
271 }
272 if (ThemeDB::get_singleton()->get_project_theme().is_valid()) {
273 ThemeDB::get_singleton()->get_project_theme()->get_type_variation_list(get_class_name(), &names);
274 }
275 names.sort_custom<StringName::AlphCompare>();
276
277 Vector<StringName> unique_names;
278 String hint_string;
279 for (const StringName &E : names) {
280 // Skip duplicate values.
281 if (unique_names.has(E)) {
282 continue;
283 }
284
285 hint_string += String(E) + ",";
286 unique_names.append(E);
287 }
288
289 p_property.hint_string = hint_string;
290 }
291 }
292
293 //
294
295 Window *Window::get_from_id(DisplayServer::WindowID p_window_id) {
296 if (p_window_id == DisplayServer::INVALID_WINDOW_ID) {
297 return nullptr;
298 }
299 return ObjectDB::get_instance<Window>(DisplayServer::get_singleton()->window_get_attached_instance_id(p_window_id));
300 }
301
302 void Window::set_title(const String &p_title) {
303 ERR_MAIN_THREAD_GUARD;
304
305 title = p_title;
306 _update_displayed_title();
307
308 emit_signal("title_changed");
309 }
310
311 String Window::get_title() const {
312 ERR_READ_THREAD_GUARD_V(String());
313 return title;
314 }
315
316 String Window::get_displayed_title() const {
317 ERR_READ_THREAD_GUARD_V(String());
318 return displayed_title;
319 }
320
321 void Window::_settings_changed() {
322 if (visible && initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE && is_in_edited_scene_root()) {
323 Size2 screen_size = Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
324 position = (screen_size - size) / 2;
325 if (embedder) {
326 embedder->_sub_window_update(this);
327 }
328 }
329 }
330
331 void Window::set_initial_position(Window::WindowInitialPosition p_initial_position) {
332 ERR_MAIN_THREAD_GUARD;
333
334 initial_position = p_initial_position;
335 _settings_changed();
336 notify_property_list_changed();
337 }
338
339 Window::WindowInitialPosition Window::get_initial_position() const {
340 ERR_READ_THREAD_GUARD_V(WINDOW_INITIAL_POSITION_ABSOLUTE);
341 return initial_position;
342 }
343
344 void Window::set_current_screen(int p_screen) {
345 ERR_MAIN_THREAD_GUARD;
346
347 current_screen = p_screen;
348 if (window_id == DisplayServer::INVALID_WINDOW_ID) {
349 return;
350 }
351 DisplayServer::get_singleton()->window_set_current_screen(p_screen, window_id);
352 }
353
354 int Window::get_current_screen() const {
355 ERR_READ_THREAD_GUARD_V(0);
356
357 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
358 current_screen = DisplayServer::get_singleton()->window_get_current_screen(window_id);
359 }
360 return current_screen;
361 }
362
363 void Window::set_position(const Point2i &p_position) {
364 ERR_MAIN_THREAD_GUARD;
365
366 position = p_position;
367
368 if (embedder) {
369 embedder->_sub_window_update(this);
370
371 } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
372 DisplayServer::get_singleton()->window_set_position(p_position, window_id);
373 }
374 }
375
376 Point2i Window::get_position() const {
377 ERR_READ_THREAD_GUARD_V(Point2i());
378
379 return position;
380 }
381
382 void Window::move_to_center() {
383 ERR_MAIN_THREAD_GUARD;
384 ERR_FAIL_COND(!is_inside_tree());
385
386 Rect2 parent_rect;
387
388 if (is_embedded()) {
389 parent_rect = get_embedder()->get_visible_rect();
390 } else {
391 int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(get_window_id());
392 parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
393 parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
394 }
395
396 if (parent_rect != Rect2()) {
397 set_position(parent_rect.position + (parent_rect.size - get_size()) / 2);
398 }
399 }
400
401 void Window::set_size(const Size2i &p_size) {
402 ERR_MAIN_THREAD_GUARD;
403 #if defined(ANDROID_ENABLED)
404 if (!get_parent() && is_inside_tree()) {
405 // Can't set root window size on Android.
406 return;
407 }
408 #endif
409
410 size = p_size;
411 _update_window_size();
412 _settings_changed();
413 }
414
415 Size2i Window::get_size() const {
416 ERR_READ_THREAD_GUARD_V(Size2i());
417 return size;
418 }
419
420 void Window::reset_size() {
421 ERR_MAIN_THREAD_GUARD;
422 set_size(Size2i());
423 }
424
425 Point2i Window::get_position_with_decorations() const {
426 ERR_READ_THREAD_GUARD_V(Point2i());
427 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
428 return DisplayServer::get_singleton()->window_get_position_with_decorations(window_id);
429 }
430 if (visible && is_embedded() && !get_flag(Window::FLAG_BORDERLESS)) {
431 Size2 border_offset;
432 if (theme_cache.embedded_border.is_valid()) {
433 border_offset = theme_cache.embedded_border->get_offset();
434 }
435 if (theme_cache.embedded_unfocused_border.is_valid()) {
436 border_offset = border_offset.max(theme_cache.embedded_unfocused_border->get_offset());
437 }
438 return position - border_offset;
439 }
440 return position;
441 }
442
443 Size2i Window::get_size_with_decorations() const {
444 ERR_READ_THREAD_GUARD_V(Size2i());
445 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
446 return DisplayServer::get_singleton()->window_get_size_with_decorations(window_id);
447 }
448 if (visible && is_embedded() && !get_flag(Window::FLAG_BORDERLESS)) {
449 Size2 border_size;
450 if (theme_cache.embedded_border.is_valid()) {
451 border_size = theme_cache.embedded_border->get_minimum_size();
452 }
453 if (theme_cache.embedded_unfocused_border.is_valid()) {
454 border_size = border_size.max(theme_cache.embedded_unfocused_border->get_minimum_size());
455 }
456 return size + border_size;
457 }
458 return size;
459 }
460
461 Size2i Window::_clamp_limit_size(const Size2i &p_limit_size) {
462 // Force window limits to respect size limitations of rendering server.
463 Size2i max_window_size = RS::get_singleton()->get_maximum_viewport_size();
464 if (max_window_size != Size2i()) {
465 return p_limit_size.clamp(Vector2i(), max_window_size);
466 } else {
467 return p_limit_size.maxi(0);
468 }
469 }
470
471 void Window::_validate_limit_size() {
472 // When max_size is invalid, max_size_used falls back to respect size limitations of rendering server.
473 bool max_size_valid = (max_size.x > 0 || max_size.y > 0) && max_size.x >= min_size.x && max_size.y >= min_size.y;
474 max_size_used = max_size_valid ? max_size : RS::get_singleton()->get_maximum_viewport_size();
475 }
476
477 void Window::set_max_size(const Size2i &p_max_size) {
478 ERR_MAIN_THREAD_GUARD;
479 #if defined(ANDROID_ENABLED)
480 if (!get_parent() && is_inside_tree()) {
481 // Can't set root window size on Android.
482 return;
483 }
484 #endif
485 Size2i max_size_clamped = _clamp_limit_size(p_max_size);
486 if (max_size == max_size_clamped) {
487 return;
488 }
489 max_size = max_size_clamped;
490
491 _validate_limit_size();
492 _update_window_size();
493 }
494
495 Size2i Window::get_max_size() const {
496 ERR_READ_THREAD_GUARD_V(Size2i());
497 return max_size;
498 }
499
500 void Window::set_min_size(const Size2i &p_min_size) {
501 ERR_MAIN_THREAD_GUARD;
502 #if defined(ANDROID_ENABLED)
503 if (!get_parent() && is_inside_tree()) {
504 // Can't set root window size on Android.
505 return;
506 }
507 #endif
508 Size2i min_size_clamped = _clamp_limit_size(p_min_size);
509 if (min_size == min_size_clamped) {
510 return;
511 }
512 min_size = min_size_clamped;
513
514 _validate_limit_size();
515 _update_window_size();
516 }
517
518 Size2i Window::get_min_size() const {
519 ERR_READ_THREAD_GUARD_V(Size2i());
520 return min_size;
521 }
522
523 void Window::set_mode(Mode p_mode) {
524 ERR_MAIN_THREAD_GUARD;
525 mode = p_mode;
526
527 if (embedder) {
528 embedder->_sub_window_update(this);
529
530 } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
531 DisplayServer::get_singleton()->window_set_mode(DisplayServer::WindowMode(p_mode), window_id);
532 }
533 }
534
535 Window::Mode Window::get_mode() const {
536 ERR_READ_THREAD_GUARD_V(MODE_WINDOWED);
537 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
538 mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
539 }
540 return mode;
541 }
542
543 void Window::set_flag(Flags p_flag, bool p_enabled) {
544 ERR_MAIN_THREAD_GUARD;
545 ERR_FAIL_INDEX(p_flag, FLAG_MAX);
546 flags[p_flag] = p_enabled;
547
548 if (p_flag == FLAG_TRANSPARENT) {
549 set_transparent_background(p_enabled);
550 }
551
552 if (embedder) {
553 embedder->_sub_window_update(this);
554 } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
555 if (!is_in_edited_scene_root()) {
556 DisplayServer::get_singleton()->window_set_flag(DisplayServer::WindowFlags(p_flag), p_enabled, window_id);
557 }
558 }
559 }
560
561 bool Window::get_flag(Flags p_flag) const {
562 ERR_READ_THREAD_GUARD_V(false);
563 ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
564 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
565 if (!is_in_edited_scene_root()) {
566 flags[p_flag] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(p_flag), window_id);
567 }
568 }
569 return flags[p_flag];
570 }
571
572 bool Window::is_popup() const {
573 return get_flag(Window::FLAG_POPUP) || get_flag(Window::FLAG_NO_FOCUS);
574 }
575
576 bool Window::is_maximize_allowed() const {
577 ERR_READ_THREAD_GUARD_V(false);
578 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
579 return DisplayServer::get_singleton()->window_is_maximize_allowed(window_id);
580 }
581 return true;
582 }
583
584 void Window::request_attention() {
585 ERR_MAIN_THREAD_GUARD;
586 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
587 DisplayServer::get_singleton()->window_request_attention(window_id);
588 }
589 }
590
591 #ifndef DISABLE_DEPRECATED
592 void Window::move_to_foreground() {
593 WARN_DEPRECATED_MSG(R"*(The "move_to_foreground()" method is deprecated, use "grab_focus()" instead.)*");
594 grab_focus();
595 }
596 #endif // DISABLE_DEPRECATED
597
598 bool Window::can_draw() const {
599 ERR_READ_THREAD_GUARD_V(false);
600 if (!is_inside_tree()) {
601 return false;
602 }
603 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
604 return DisplayServer::get_singleton()->window_can_draw(window_id);
605 }
606
607 return visible;
608 }
609
610 void Window::set_ime_active(bool p_active) {
611 ERR_MAIN_THREAD_GUARD;
612 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
613 DisplayServer::get_singleton()->window_set_ime_active(p_active, window_id);
614 }
615 }
616
617 void Window::set_ime_position(const Point2i &p_pos) {
618 ERR_MAIN_THREAD_GUARD;
619 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
620 DisplayServer::get_singleton()->window_set_ime_position(p_pos, window_id);
621 }
622 }
623
624 bool Window::is_embedded() const {
625 ERR_READ_THREAD_GUARD_V(false);
626 return get_embedder() != nullptr;
627 }
628
629 bool Window::is_in_edited_scene_root() const {
630 ERR_READ_THREAD_GUARD_V(false);
631 #ifdef TOOLS_ENABLED
632 return is_part_of_edited_scene();
633 #else
634 return false;
635 #endif
636 }
637
638 void Window::_make_window() {
639 ERR_FAIL_COND(window_id != DisplayServer::INVALID_WINDOW_ID);
640
641 if (transient && transient_to_focused) {
642 _make_transient();
643 }
644
645 uint32_t f = 0;
646 for (int i = 0; i < FLAG_MAX; i++) {
647 if (flags[i]) {
648 f |= (1 << i);
649 }
650 }
651
652 DisplayServer::VSyncMode vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
653 Rect2i window_rect;
654 if (initial_position == WINDOW_INITIAL_POSITION_ABSOLUTE) {
655 window_rect = Rect2i(position, size);
656 } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN) {
657 window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_PRIMARY) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_PRIMARY) - size) / 2, size);
658 } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN) {
659 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);
660 } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN) {
661 window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(current_screen) + (DisplayServer::get_singleton()->screen_get_size(current_screen) - size) / 2, size);
662 } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS) {
663 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);
664 } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS) {
665 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);
666 }
667
668 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);
669 ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
670 DisplayServer::get_singleton()->window_set_max_size(Size2i(), window_id);
671 DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
672 DisplayServer::get_singleton()->window_set_mouse_passthrough(mpath, window_id);
673 DisplayServer::get_singleton()->window_set_title(displayed_title, window_id);
674 DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
675 DisplayServer::get_singleton()->accessibility_set_window_callbacks(window_id, callable_mp(this, &Window::_accessibility_activate), callable_mp(this, &Window::_accessibility_deactivate));
676
677 _update_window_size();
678
679 if (transient_parent) {
680 for (const Window *E : transient_children) {
681 if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
682 DisplayServer::get_singleton()->window_set_transient(E->window_id, transient_parent->window_id);
683 }
684 }
685 }
686
687 _update_window_callbacks();
688
689 RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
690 DisplayServer::get_singleton()->show_window(window_id);
691 }
692
693 void Window::_update_from_window() {
694 ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
695 mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
696 for (int i = 0; i < FLAG_MAX; i++) {
697 flags[i] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(i), window_id);
698 }
699 }
700
701 void Window::_clear_window() {
702 ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
703
704 bool had_focus = has_focus();
705
706 if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS)) {
707 float win_scale = DisplayServer::get_singleton()->window_get_scale(window_id);
708
709 Size2i adjusted_size = Size2i(size.width / win_scale, size.height / win_scale);
710 Size2i adjusted_pos = Size2i(position.x / win_scale, position.y / win_scale);
711
712 _rect_changed_callback(Rect2i(adjusted_pos, adjusted_size));
713 }
714
715 if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
716 DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
717 }
718
719 for (const Window *E : transient_children) {
720 if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
721 DisplayServer::get_singleton()->window_set_transient(E->window_id, DisplayServer::INVALID_WINDOW_ID);
722 }
723 }
724
725 _update_from_window();
726
727 DisplayServer::get_singleton()->delete_sub_window(window_id);
728 window_id = DisplayServer::INVALID_WINDOW_ID;
729
730 // If closing window was focused and has a parent, return focus.
731 if (had_focus && transient_parent) {
732 transient_parent->grab_focus();
733 }
734
735 _update_viewport_size();
736 RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
737
738 if (transient && transient_to_focused) {
739 _clear_transient();
740 }
741 }
742
743 void Window::_rect_changed_callback(const Rect2i &p_callback) {
744 //we must always accept this as the truth
745 if (size == p_callback.size && position == p_callback.position) {
746 return;
747 }
748
749 if (position != p_callback.position) {
750 position = p_callback.position;
751 _propagate_window_notification(this, NOTIFICATION_WM_POSITION_CHANGED);
752 }
753
754 if (size != p_callback.size) {
755 size = p_callback.size;
756 _update_viewport_size();
757 }
758 if (window_id != DisplayServer::INVALID_WINDOW_ID && !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS)) {
759 Vector2 sz_out = DisplayServer::get_singleton()->window_get_size_with_decorations(window_id);
760 Vector2 pos_out = DisplayServer::get_singleton()->window_get_position_with_decorations(window_id);
761 Vector2 sz_in = DisplayServer::get_singleton()->window_get_size(window_id);
762 Vector2 pos_in = DisplayServer::get_singleton()->window_get_position(window_id);
763 DisplayServer::get_singleton()->accessibility_set_window_rect(window_id, Rect2(pos_out, sz_out), Rect2(pos_in, sz_in));
764 }
765 queue_accessibility_update();
766 }
767
768 void Window::_propagate_window_notification(Node *p_node, int p_notification) {
769 p_node->notification(p_notification);
770 for (int i = 0; i < p_node->get_child_count(); i++) {
771 Node *child = p_node->get_child(i);
772 Window *window = Object::cast_to<Window>(child);
773 if (window) {
774 continue;
775 }
776 _propagate_window_notification(child, p_notification);
777 }
778 }
779
780 void Window::_event_callback(DisplayServer::WindowEvent p_event) {
781 switch (p_event) {
782 case DisplayServer::WINDOW_EVENT_MOUSE_ENTER: {
783 if (!is_inside_tree()) {
784 return;
785 }
786 Window *root = get_tree()->get_root();
787 if (mouse_in_window && root->gui.windowmanager_window_over == this) {
788 return;
789 }
790 if (root->gui.windowmanager_window_over) {
791 #ifdef DEV_ENABLED
792 WARN_PRINT_ONCE("Entering a window while a window is hovered should never happen in DisplayServer.");
793 #endif // DEV_ENABLED
794 root->gui.windowmanager_window_over->_event_callback(DisplayServer::WINDOW_EVENT_MOUSE_EXIT);
795 }
796 _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
797 root->gui.windowmanager_window_over = this;
798 mouse_in_window = true;
799 if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CURSOR_SHAPE)) {
800 DisplayServer::get_singleton()->cursor_set_shape(DisplayServer::CURSOR_ARROW); //restore cursor shape
801 }
802 } break;
803 case DisplayServer::WINDOW_EVENT_MOUSE_EXIT: {
804 if (!is_inside_tree()) {
805 return;
806 }
807 // Ensure keeping the order of input events and window events when input events are buffered or accumulated.
808 Input::get_singleton()->flush_buffered_events();
809
810 Window *root = get_tree()->get_root();
811 if (!root->gui.windowmanager_window_over) {
812 #ifdef DEV_ENABLED
813 WARN_PRINT_ONCE("Exiting a window while no window is hovered should never happen in DisplayServer.");
814 #endif // DEV_ENABLED
815 return;
816 }
817 mouse_in_window = false;
818 root->gui.windowmanager_window_over->_mouse_leave_viewport();
819 root->gui.windowmanager_window_over = nullptr;
820 _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
821 } break;
822 case DisplayServer::WINDOW_EVENT_FOCUS_IN: {
823 focused = true;
824 focused_window = this;
825 _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_IN);
826 emit_signal(SceneStringName(focus_entered));
827 if (get_tree() && get_tree()->is_accessibility_enabled()) {
828 Window *w = exclusive_child;
829 while (w) {
830 if (w->exclusive_child) {
831 w = w->exclusive_child;
832 } else {
833 w->grab_focus();
834 break;
835 }
836 }
837 }
838 } break;
839 case DisplayServer::WINDOW_EVENT_FOCUS_OUT: {
840 focused = false;
841 if (focused_window == this) {
842 focused_window = nullptr;
843 }
844 _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_OUT);
845 emit_signal(SceneStringName(focus_exited));
846 } break;
847 case DisplayServer::WINDOW_EVENT_CLOSE_REQUEST: {
848 if (exclusive_child != nullptr) {
849 break; //has an exclusive child, can't get events until child is closed
850 }
851 _propagate_window_notification(this, NOTIFICATION_WM_CLOSE_REQUEST);
852 emit_signal(SNAME("close_requested"));
853 } break;
854 case DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST: {
855 _propagate_window_notification(this, NOTIFICATION_WM_GO_BACK_REQUEST);
856 emit_signal(SNAME("go_back_requested"));
857 } break;
858 case DisplayServer::WINDOW_EVENT_DPI_CHANGE: {
859 _update_viewport_size();
860 _propagate_window_notification(this, NOTIFICATION_WM_DPI_CHANGE);
861 emit_signal(SNAME("dpi_changed"));
862 } break;
863 case DisplayServer::WINDOW_EVENT_TITLEBAR_CHANGE: {
864 emit_signal(SNAME("titlebar_changed"));
865 } break;
866 case DisplayServer::WINDOW_EVENT_FORCE_CLOSE: {
867 hide();
868 } break;
869 }
870 }
871
872 void Window::update_mouse_cursor_state() {
873 ERR_MAIN_THREAD_GUARD;
874 // Update states based on mouse cursor position.
875 // This includes updated mouse_enter or mouse_exit signals or the current mouse cursor shape.
876 // These details are set in Viewport::_gui_input_event. To instantly
877 // see the changes in the viewport, we need to trigger a mouse motion event.
878 // This function should be called whenever scene tree changes affect the mouse cursor.
879 Ref<InputEventMouseMotion> mm;
880 Vector2 pos = get_mouse_position();
881 Transform2D xform = get_global_canvas_transform().affine_inverse();
882 mm.instantiate();
883 mm->set_position(pos);
884 mm->set_global_position(xform.xform(pos));
885 mm->set_device(InputEvent::DEVICE_ID_INTERNAL);
886 push_input(mm, true);
887 }
888
889 void Window::show() {
890 ERR_MAIN_THREAD_GUARD;
891 set_visible(true);
892 }
893
894 void Window::hide() {
895 ERR_MAIN_THREAD_GUARD;
896 set_visible(false);
897 }
898
899 void Window::_accessibility_activate() {
900 _accessibility_notify_enter(this);
901 if (!get_embedder()) {
902 DisplayServer::get_singleton()->accessibility_window_activation_completed(get_window_id());
903 }
904 }
905
906 void Window::_accessibility_deactivate() {
907 _accessibility_notify_exit(this);
908 if (!get_embedder()) {
909 DisplayServer::get_singleton()->accessibility_window_deactivation_completed(get_window_id());
910 }
911 }
912
913 void Window::_accessibility_notify_enter(Node *p_node) {
914 p_node->queue_accessibility_update();
915
916 if (p_node != this) {
917 const Window *window = Object::cast_to<Window>(p_node);
918 if (window) {
919 return;
920 }
921 }
922
923 for (int i = 0; i < p_node->get_child_count(); i++) {
924 _accessibility_notify_enter(p_node->get_child(i));
925 }
926 }
927
928 void Window::_accessibility_notify_exit(Node *p_node) {
929 p_node->notification(Node::NOTIFICATION_ACCESSIBILITY_INVALIDATE);
930
931 if (p_node != this) {
932 const Window *window = Object::cast_to<Window>(p_node);
933 if (window) {
934 return;
935 }
936 }
937
938 for (int i = 0; i < p_node->get_child_count(); i++) {
939 _accessibility_notify_exit(p_node->get_child(i));
940 }
941 }
942
943 void Window::set_visible(bool p_visible) {
944 ERR_MAIN_THREAD_GUARD;
945 if (visible == p_visible) {
946 return;
947 }
948
949 if (!is_inside_tree()) {
950 visible = p_visible;
951 return;
952 }
953
954 ERR_FAIL_NULL_MSG(get_parent(), "Can't change visibility of main window.");
955
956 visible = p_visible;
957
958 // Stop any queued resizing, as the window will be resized right now.
959 updating_child_controls = false;
960
961 Viewport *embedder_vp = get_embedder();
962
963 if (!embedder_vp) {
964 if (!p_visible && window_id != DisplayServer::INVALID_WINDOW_ID) {
965 _clear_window();
966 }
967 if (p_visible && window_id == DisplayServer::INVALID_WINDOW_ID) {
968 _make_window();
969 }
970 } else {
971 if (visible) {
972 embedder = embedder_vp;
973 if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
974 if (is_in_edited_scene_root()) {
975 Size2 screen_size = Size2(GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_width"), GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_height"));
976 position = (screen_size - size) / 2;
977 } else {
978 position = (embedder->get_visible_rect().size - size) / 2;
979 }
980 }
981 embedder->_sub_window_register(this);
982 RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
983 } else {
984 embedder->_sub_window_remove(this);
985 embedder = nullptr;
986 RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
987 }
988 _update_window_size();
989 }
990
991 if (visible) {
992 if (get_tree() && get_tree()->is_accessibility_supported()) {
993 get_tree()->_accessibility_force_update();
994 _accessibility_notify_enter(this);
995 if (!embedder_vp) {
996 DisplayServer::get_singleton()->accessibility_window_activation_completed(get_window_id());
997 }
998 }
999 } else {
1000 if (get_tree() && get_tree()->is_accessibility_supported()) {
1001 _accessibility_notify_exit(this);
1002 if (!embedder_vp) {
1003 DisplayServer::get_singleton()->accessibility_window_deactivation_completed(get_window_id());
1004 }
1005 }
1006 focused = false;
1007 if (focused_window == this) {
1008 focused_window = nullptr;
1009 }
1010 }
1011 if (get_parent()) {
1012 get_parent()->queue_accessibility_update();
1013 }
1014 if (embedder) {
1015 embedder->queue_accessibility_update();
1016 }
1017
1018 notification(NOTIFICATION_VISIBILITY_CHANGED);
1019 emit_signal(SceneStringName(visibility_changed));
1020
1021 RS::get_singleton()->viewport_set_active(get_viewport_rid(), visible);
1022
1023 //update transient exclusive
1024 if (transient_parent) {
1025 _set_transient_exclusive_child(true);
1026 }
1027 }
1028
1029 void Window::_clear_transient() {
1030 if (transient_parent) {
1031 if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
1032 DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
1033 }
1034 transient_parent->transient_children.erase(this);
1035 if (transient_parent->exclusive_child == this) {
1036 transient_parent->exclusive_child = nullptr;
1037 }
1038 transient_parent = nullptr;
1039 }
1040 }
1041
1042 void Window::_make_transient() {
1043 if (!get_parent()) {
1044 //main window, can't be transient
1045 return;
1046 }
1047 //find transient parent
1048
1049 Window *window = nullptr;
1050
1051 if (!is_embedded() && transient_to_focused) {
1052 DisplayServer::WindowID focused_window_id = DisplayServer::get_singleton()->get_focused_window();
1053 if (focused_window_id != DisplayServer::INVALID_WINDOW_ID) {
1054 window = Window::get_from_id(focused_window_id);
1055 }
1056 }
1057
1058 if (!window) {
1059 Viewport *vp = get_parent()->get_viewport();
1060 while (vp) {
1061 window = Object::cast_to<Window>(vp);
1062 if (window) {
1063 break;
1064 }
1065 if (!vp->get_parent()) {
1066 break;
1067 }
1068
1069 vp = vp->get_parent()->get_viewport();
1070 }
1071 }
1072
1073 if (window) {
1074 transient_parent = window;
1075 window->transient_children.insert(this);
1076 _set_transient_exclusive_child();
1077 }
1078
1079 //see if we can make transient
1080 if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
1081 DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
1082 }
1083 }
1084
1085 void Window::_set_transient_exclusive_child(bool p_clear_invalid) {
1086 if (exclusive && visible && is_inside_tree()) {
1087 if (!is_in_edited_scene_root()) {
1088 // Transient parent has another exclusive child.
1089 if (transient_parent->exclusive_child && transient_parent->exclusive_child != this) {
1090 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()));
1091 }
1092 transient_parent->exclusive_child = this;
1093 }
1094 } else if (p_clear_invalid) {
1095 if (transient_parent->exclusive_child == this) {
1096 transient_parent->exclusive_child = nullptr;
1097 }
1098 }
1099 }
1100
1101 void Window::set_transient(bool p_transient) {
1102 ERR_MAIN_THREAD_GUARD;
1103 if (transient == p_transient) {
1104 return;
1105 }
1106
1107 transient = p_transient;
1108
1109 if (!is_inside_tree()) {
1110 return;
1111 }
1112
1113 if (transient) {
1114 if (!transient_to_focused) {
1115 _make_transient();
1116 }
1117 } else {
1118 _clear_transient();
1119 }
1120 }
1121
1122 bool Window::is_transient() const {
1123 return transient;
1124 }
1125
1126 void Window::set_transient_to_focused(bool p_transient_to_focused) {
1127 ERR_MAIN_THREAD_GUARD;
1128 if (transient_to_focused == p_transient_to_focused) {
1129 return;
1130 }
1131
1132 transient_to_focused = p_transient_to_focused;
1133 }
1134
1135 bool Window::is_transient_to_focused() const {
1136 ERR_READ_THREAD_GUARD_V(false);
1137 return transient_to_focused;
1138 }
1139
1140 void Window::set_exclusive(bool p_exclusive) {
1141 ERR_MAIN_THREAD_GUARD;
1142 if (exclusive == p_exclusive) {
1143 return;
1144 }
1145
1146 exclusive = p_exclusive;
1147
1148 if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID) {
1149 if (is_in_edited_scene_root()) {
1150 DisplayServer::get_singleton()->window_set_exclusive(window_id, false);
1151 } else {
1152 DisplayServer::get_singleton()->window_set_exclusive(window_id, exclusive);
1153 }
1154 }
1155
1156 if (transient_parent) {
1157 _set_transient_exclusive_child(true);
1158 }
1159 }
1160
1161 bool Window::is_exclusive() const {
1162 ERR_READ_THREAD_GUARD_V(false);
1163 return exclusive;
1164 }
1165
1166 bool Window::is_visible() const {
1167 ERR_READ_THREAD_GUARD_V(false);
1168 return visible;
1169 }
1170
1171 Size2i Window::_clamp_window_size(const Size2i &p_size) {
1172 Size2i window_size_clamped = p_size;
1173 Size2 minsize = get_clamped_minimum_size();
1174 window_size_clamped = window_size_clamped.max(minsize);
1175
1176 if (max_size_used != Size2i()) {
1177 window_size_clamped = window_size_clamped.min(max_size_used);
1178 }
1179
1180 return window_size_clamped;
1181 }
1182
1183 void Window::_update_window_size() {
1184 Size2i size_limit = get_clamped_minimum_size();
1185 if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID && keep_title_visible) {
1186 Size2i title_size = DisplayServer::get_singleton()->window_get_title_size(displayed_title, window_id);
1187 size_limit = size_limit.max(title_size);
1188 }
1189
1190 size = size.max(size_limit);
1191
1192 bool reset_min_first = false;
1193
1194 if (max_size_used != Size2i()) {
1195 // Force window size to respect size limitations of max_size_used.
1196 size = size.min(max_size_used);
1197
1198 if (size_limit.x > max_size_used.x) {
1199 size_limit.x = max_size_used.x;
1200 reset_min_first = true;
1201 }
1202 if (size_limit.y > max_size_used.y) {
1203 size_limit.y = max_size_used.y;
1204 reset_min_first = true;
1205 }
1206 }
1207
1208 if (embedder) {
1209 size = size.maxi(1);
1210
1211 embedder->_sub_window_update(this);
1212 } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1213 // When main window embedded in the editor, we can't resize the main window.
1214 if (window_id != DisplayServer::MAIN_WINDOW_ID || !Engine::get_singleton()->is_embedded_in_editor()) {
1215 if (reset_min_first && wrap_controls) {
1216 // Avoid an error if setting max_size to a value between min_size and the previous size_limit.
1217 DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
1218 }
1219
1220 DisplayServer::get_singleton()->window_set_max_size(max_size_used, window_id);
1221 DisplayServer::get_singleton()->window_set_min_size(size_limit, window_id);
1222 DisplayServer::get_singleton()->window_set_size(size, window_id);
1223 } else if (Engine::get_singleton()->is_embedded_in_editor()) {
1224 size = DisplayServer::get_singleton()->window_get_size(window_id); // Reset size.
1225 }
1226 }
1227
1228 //update the viewport
1229 _update_viewport_size();
1230 }
1231
1232 void Window::_update_viewport_size() {
1233 //update the viewport part
1234
1235 Size2i final_size;
1236 Size2 final_size_override;
1237 Rect2i attach_to_screen_rect(Point2i(), size);
1238 window_transform = Transform2D();
1239
1240 if (content_scale_stretch == Window::CONTENT_SCALE_STRETCH_INTEGER) {
1241 // We always want to make sure that the content scale factor is a whole
1242 // number, else there will be pixel wobble no matter what.
1243 content_scale_factor = Math::floor(content_scale_factor);
1244
1245 // A content scale factor of zero is pretty useless.
1246 if (content_scale_factor < 1) {
1247 content_scale_factor = 1;
1248 }
1249 }
1250
1251 if (content_scale_mode == CONTENT_SCALE_MODE_DISABLED || content_scale_size.x == 0 || content_scale_size.y == 0) {
1252 final_size = size;
1253 final_size_override = Size2(size) / content_scale_factor;
1254 } else {
1255 //actual screen video mode
1256 Size2 video_mode = size;
1257 Size2 desired_res = content_scale_size;
1258
1259 Size2 viewport_size;
1260 Size2 screen_size;
1261
1262 float viewport_aspect = desired_res.aspect();
1263 float video_mode_aspect = video_mode.aspect();
1264
1265 if (content_scale_aspect == CONTENT_SCALE_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
1266 //same aspect or ignore aspect
1267 viewport_size = desired_res;
1268 screen_size = video_mode;
1269 } else if (viewport_aspect < video_mode_aspect) {
1270 // screen ratio is smaller vertically
1271
1272 if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_HEIGHT || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
1273 //will stretch horizontally
1274 viewport_size.x = desired_res.y * video_mode_aspect;
1275 viewport_size.y = desired_res.y;
1276 screen_size = video_mode;
1277
1278 } else {
1279 //will need black bars
1280 viewport_size = desired_res;
1281 screen_size.x = video_mode.y * viewport_aspect;
1282 screen_size.y = video_mode.y;
1283 }
1284 } else {
1285 //screen ratio is smaller horizontally
1286 if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_WIDTH || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
1287 //will stretch horizontally
1288 viewport_size.x = desired_res.x;
1289 viewport_size.y = desired_res.x / video_mode_aspect;
1290 screen_size = video_mode;
1291
1292 } else {
1293 //will need black bars
1294 viewport_size = desired_res;
1295 screen_size.x = video_mode.x;
1296 screen_size.y = video_mode.x / viewport_aspect;
1297 }
1298 }
1299
1300 screen_size = screen_size.floor();
1301 viewport_size = viewport_size.floor();
1302
1303 if (content_scale_stretch == Window::CONTENT_SCALE_STRETCH_INTEGER) {
1304 Size2i screen_scale = (screen_size / viewport_size).floor();
1305 int scale_factor = MIN(screen_scale.x, screen_scale.y);
1306
1307 if (scale_factor < 1) {
1308 scale_factor = 1;
1309 }
1310
1311 screen_size = viewport_size * scale_factor;
1312 }
1313
1314 Size2 margin;
1315 Size2 offset;
1316
1317 if (screen_size.x < video_mode.x) {
1318 margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
1319 offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
1320 }
1321
1322 if (screen_size.y < video_mode.y) {
1323 margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
1324 offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
1325 }
1326
1327 switch (content_scale_mode) {
1328 case CONTENT_SCALE_MODE_DISABLED: {
1329 } break;
1330 case CONTENT_SCALE_MODE_CANVAS_ITEMS: {
1331 final_size = screen_size;
1332 final_size_override = viewport_size / content_scale_factor;
1333 attach_to_screen_rect = Rect2(margin, screen_size);
1334
1335 window_transform.translate_local(margin);
1336 } break;
1337 case CONTENT_SCALE_MODE_VIEWPORT: {
1338 final_size = (viewport_size / content_scale_factor).floor();
1339 attach_to_screen_rect = Rect2(margin, screen_size);
1340
1341 window_transform.translate_local(margin);
1342 if (final_size.x != 0 && final_size.y != 0) {
1343 Transform2D scale_transform;
1344 scale_transform.scale(Vector2(attach_to_screen_rect.size) / Vector2(final_size));
1345 window_transform *= scale_transform;
1346 }
1347 } break;
1348 }
1349 }
1350
1351 bool allocate = is_inside_tree() && visible && (window_id != DisplayServer::INVALID_WINDOW_ID || embedder != nullptr);
1352 _set_size(final_size, final_size_override, allocate);
1353
1354 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1355 RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), attach_to_screen_rect, window_id);
1356 } else if (!is_embedded()) {
1357 RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), Rect2i(), DisplayServer::INVALID_WINDOW_ID);
1358 }
1359
1360 notification(NOTIFICATION_WM_SIZE_CHANGED);
1361
1362 if (embedder) {
1363 float scale = MIN(embedder->stretch_transform.get_scale().width, embedder->stretch_transform.get_scale().height);
1364 Viewport::set_oversampling_override(scale);
1365 Size2 s = Size2(final_size.width * scale, final_size.height * scale).ceil();
1366 RS::get_singleton()->viewport_set_global_canvas_transform(get_viewport_rid(), global_canvas_transform * scale * content_scale_factor);
1367 RS::get_singleton()->viewport_set_size(get_viewport_rid(), s.width, s.height);
1368 embedder->_sub_window_update(this);
1369 }
1370 }
1371
1372 void Window::_update_window_callbacks() {
1373 DisplayServer::get_singleton()->window_set_rect_changed_callback(callable_mp(this, &Window::_rect_changed_callback), window_id);
1374 DisplayServer::get_singleton()->window_set_window_event_callback(callable_mp(this, &Window::_event_callback), window_id);
1375 DisplayServer::get_singleton()->window_set_input_event_callback(callable_mp(this, &Window::_window_input), window_id);
1376 DisplayServer::get_singleton()->window_set_input_text_callback(callable_mp(this, &Window::_window_input_text), window_id);
1377 DisplayServer::get_singleton()->window_set_drop_files_callback(callable_mp(this, &Window::_window_drop_files), window_id);
1378 }
1379
1380 void Window::set_force_native(bool p_force_native) {
1381 if (force_native == p_force_native) {
1382 return;
1383 }
1384 if (is_visible() && !is_in_edited_scene_root()) {
1385 ERR_FAIL_MSG("Can't change \"force_native\" while a window is displayed. Consider hiding window before changing this value.");
1386 }
1387 if (window_id == DisplayServer::MAIN_WINDOW_ID) {
1388 return;
1389 }
1390 force_native = p_force_native;
1391 if (!is_in_edited_scene_root() && is_inside_tree() && get_tree()->get_root()->is_embedding_subwindows()) {
1392 set_embedding_subwindows(force_native);
1393 }
1394 }
1395
1396 bool Window::get_force_native() const {
1397 return force_native;
1398 }
1399
1400 Viewport *Window::get_embedder() const {
1401 ERR_READ_THREAD_GUARD_V(nullptr);
1402 if (force_native && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SUBWINDOWS) && !is_in_edited_scene_root()) {
1403 return nullptr;
1404 }
1405
1406 Viewport *vp = get_parent_viewport();
1407
1408 while (vp) {
1409 if (vp->is_embedding_subwindows()) {
1410 return vp;
1411 }
1412
1413 if (vp->get_parent()) {
1414 vp = vp->get_parent()->get_viewport();
1415 } else {
1416 vp = nullptr;
1417 }
1418 }
1419 return nullptr;
1420 }
1421
1422 RID Window::get_accessibility_element() const {
1423 if (!visible || is_part_of_edited_scene()) {
1424 return RID();
1425 }
1426 if (get_embedder() || is_popup()) {
1427 return Node::get_accessibility_element();
1428 } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1429 return DisplayServer::get_singleton()->accessibility_get_window_root(window_id);
1430 } else {
1431 return RID();
1432 }
1433 }
1434
1435 RID Window::get_focused_accessibility_element() const {
1436 if (window_id == DisplayServer::MAIN_WINDOW_ID) {
1437 if (get_child_count() > 0) {
1438 return get_child(0)->get_focused_accessibility_element(); // Try scene tree root node.
1439 }
1440 }
1441 return Node::get_focused_accessibility_element();
1442 }
1443
1444 void Window::_notification(int p_what) {
1445 ERR_MAIN_THREAD_GUARD;
1446 switch (p_what) {
1447 case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
1448 if (accessibility_title_element.is_valid()) {
1449 DisplayServer::get_singleton()->accessibility_free_element(accessibility_title_element);
1450 accessibility_title_element = RID();
1451 }
1452 if (accessibility_announcement_element.is_valid()) {
1453 DisplayServer::get_singleton()->accessibility_free_element(accessibility_announcement_element);
1454 accessibility_announcement_element = RID();
1455 }
1456 } break;
1457
1458 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
1459 RID ae = get_accessibility_element();
1460 ERR_FAIL_COND(ae.is_null());
1461
1462 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_WINDOW);
1463 if (accessibility_name.is_empty()) {
1464 DisplayServer::get_singleton()->accessibility_update_set_name(ae, displayed_title);
1465 } else {
1466 DisplayServer::get_singleton()->accessibility_update_set_name(ae, accessibility_name);
1467 }
1468 DisplayServer::get_singleton()->accessibility_update_set_description(ae, accessibility_description);
1469 DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_MODAL, exclusive);
1470 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &Window::_accessibility_action_grab_focus));
1471 DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, !visible);
1472
1473 if (get_embedder() || is_popup()) {
1474 Control *parent_ctrl = Object::cast_to<Control>(get_parent());
1475 Transform2D parent_tr = parent_ctrl ? parent_ctrl->get_global_transform() : Transform2D();
1476 Transform2D tr;
1477 if (window_id == DisplayServer::INVALID_WINDOW_ID) {
1478 tr.set_origin(position);
1479 } else {
1480 Window *np = get_non_popup_window();
1481 if (np) {
1482 tr.set_origin(get_position() - np->get_position());
1483 }
1484 }
1485 DisplayServer::get_singleton()->accessibility_update_set_transform(ae, parent_tr.affine_inverse() * tr);
1486 DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, Rect2(Point2(), size));
1487
1488 if (accessibility_title_element.is_null()) {
1489 accessibility_title_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_TITLE_BAR);
1490 }
1491
1492 int w = get_theme_constant(SNAME("title_height"));
1493 DisplayServer::get_singleton()->accessibility_update_set_name(accessibility_title_element, displayed_title);
1494 DisplayServer::get_singleton()->accessibility_update_set_bounds(accessibility_title_element, Rect2(Vector2(0, -w), Size2(size.x, w)));
1495 } else {
1496 DisplayServer::get_singleton()->accessibility_update_set_transform(ae, get_final_transform());
1497 if (_get_size_2d_override() != Size2()) {
1498 DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, Rect2(Point2(), _get_size_2d_override()));
1499 } else {
1500 DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, Rect2(Point2(), _get_size()));
1501 }
1502
1503 if (accessibility_announcement_element.is_null()) {
1504 accessibility_announcement_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
1505 }
1506
1507 if (announcement.is_empty()) {
1508 DisplayServer::get_singleton()->accessibility_update_set_live(accessibility_announcement_element, DisplayServer::LIVE_OFF);
1509 } else {
1510 DisplayServer::get_singleton()->accessibility_update_set_name(accessibility_announcement_element, announcement);
1511 DisplayServer::get_singleton()->accessibility_update_set_live(accessibility_announcement_element, DisplayServer::LIVE_ASSERTIVE);
1512 }
1513 }
1514 } break;
1515
1516 case NOTIFICATION_POSTINITIALIZE: {
1517 initialized = true;
1518
1519 _invalidate_theme_cache();
1520 _update_theme_item_cache();
1521 } break;
1522
1523 case NOTIFICATION_PARENTED: {
1524 theme_owner->assign_theme_on_parented(this);
1525 } break;
1526
1527 case NOTIFICATION_UNPARENTED: {
1528 theme_owner->clear_theme_on_unparented(this);
1529 } break;
1530
1531 case NOTIFICATION_ENTER_TREE: {
1532 if (is_in_edited_scene_root()) {
1533 if (!ProjectSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &Window::_settings_changed))) {
1534 ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Window::_settings_changed));
1535 }
1536 } else if (get_parent() && get_tree()->get_root()->is_embedding_subwindows()) {
1537 // Is not the main window and main window is embedding.
1538 set_embedding_subwindows(force_native);
1539 }
1540
1541 bool embedded = false;
1542 {
1543 embedder = get_embedder();
1544 if (embedder) {
1545 embedded = true;
1546 if (!visible) {
1547 embedder = nullptr; // Not yet since not visible.
1548 }
1549 }
1550 }
1551
1552 if (embedded) {
1553 // Create as embedded.
1554 if (embedder) {
1555 if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
1556 if (is_in_edited_scene_root()) {
1557 Size2 screen_size = Size2(GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_width"), GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_height"));
1558 position = (screen_size - size) / 2;
1559 } else {
1560 position = (embedder->get_visible_rect().size - size) / 2;
1561 }
1562 }
1563 embedder->_sub_window_register(this);
1564 RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
1565 _update_window_size();
1566 }
1567
1568 } else {
1569 if (!get_parent()) {
1570 // It's the root window!
1571 visible = true; // Always visible.
1572 window_id = DisplayServer::MAIN_WINDOW_ID;
1573 focused_window = this;
1574 DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
1575 DisplayServer::get_singleton()->accessibility_set_window_callbacks(window_id, callable_mp(this, &Window::_accessibility_activate), callable_mp(this, &Window::_accessibility_deactivate));
1576 _update_from_window();
1577 // Since this window already exists (created on start), we must update pos and size from it.
1578 {
1579 position = DisplayServer::get_singleton()->window_get_position(window_id);
1580 size = DisplayServer::get_singleton()->window_get_size(window_id);
1581 focused = DisplayServer::get_singleton()->window_is_focused(window_id);
1582 }
1583 _update_window_size(); // Inform DisplayServer of minimum and maximum size.
1584 _update_viewport_size(); // Then feed back to the viewport.
1585 _update_window_callbacks();
1586 // Simulate mouse-enter event when mouse is over the window, since OS event might arrive before setting callbacks.
1587 if (!mouse_in_window && Rect2(position, size).has_point(DisplayServer::get_singleton()->mouse_get_position())) {
1588 _event_callback(DisplayServer::WINDOW_EVENT_MOUSE_ENTER);
1589 }
1590 RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
1591 if (DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(FLAG_TRANSPARENT), window_id)) {
1592 set_transparent_background(true);
1593 }
1594 } else {
1595 // Create.
1596 if (visible) {
1597 _make_window();
1598 }
1599 }
1600 }
1601
1602 if (transient && !transient_to_focused) {
1603 _make_transient();
1604 }
1605 if (visible) {
1606 if (window_id != DisplayServer::MAIN_WINDOW_ID && get_tree() && get_tree()->is_accessibility_supported()) {
1607 get_tree()->_accessibility_force_update();
1608 _accessibility_notify_enter(this);
1609 if (!embedder) {
1610 DisplayServer::get_singleton()->accessibility_window_activation_completed(get_window_id());
1611 }
1612 }
1613 notification(NOTIFICATION_VISIBILITY_CHANGED);
1614 emit_signal(SceneStringName(visibility_changed));
1615 RS::get_singleton()->viewport_set_active(get_viewport_rid(), true);
1616 if (get_parent()) {
1617 get_parent()->queue_accessibility_update();
1618 }
1619 if (embedder) {
1620 embedder->queue_accessibility_update();
1621 }
1622 }
1623
1624 // Emits NOTIFICATION_THEME_CHANGED internally.
1625 set_theme_context(ThemeDB::get_singleton()->get_nearest_theme_context(this));
1626 } break;
1627
1628 case NOTIFICATION_READY: {
1629 if (wrap_controls) {
1630 // Finish any resizing immediately so it doesn't interfere on stuff overriding _ready().
1631 _update_child_controls();
1632 }
1633 } break;
1634
1635 case NOTIFICATION_THEME_CHANGED: {
1636 emit_signal(SceneStringName(theme_changed));
1637 _invalidate_theme_cache();
1638 _update_theme_item_cache();
1639 } break;
1640
1641 case NOTIFICATION_TRANSLATION_CHANGED: {
1642 _invalidate_theme_cache();
1643 _update_theme_item_cache();
1644 _update_displayed_title();
1645 } break;
1646
1647 case NOTIFICATION_VISIBILITY_CHANGED: {
1648 if (unparent_when_invisible && !is_visible()) {
1649 Node *p = get_parent();
1650 if (p) {
1651 p->remove_child(this);
1652 }
1653 }
1654 } break;
1655
1656 case NOTIFICATION_EXIT_TREE: {
1657 if (ProjectSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &Window::_settings_changed))) {
1658 ProjectSettings::get_singleton()->disconnect("settings_changed", callable_mp(this, &Window::_settings_changed));
1659 }
1660
1661 set_theme_context(nullptr, false);
1662
1663 if (visible && window_id != DisplayServer::MAIN_WINDOW_ID) {
1664 if (get_tree() && get_tree()->is_accessibility_supported()) {
1665 _accessibility_notify_exit(this);
1666 if (!embedder) {
1667 DisplayServer::get_singleton()->accessibility_window_deactivation_completed(get_window_id());
1668 }
1669 if (get_parent()) {
1670 get_parent()->queue_accessibility_update();
1671 }
1672 if (embedder) {
1673 embedder->queue_accessibility_update();
1674 }
1675 }
1676 }
1677
1678 accessibility_title_element = RID();
1679 accessibility_announcement_element = RID();
1680
1681 if (transient) {
1682 _clear_transient();
1683 }
1684
1685 if (!is_embedded() && window_id != DisplayServer::INVALID_WINDOW_ID) {
1686 if (window_id == DisplayServer::MAIN_WINDOW_ID) {
1687 RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
1688 _update_window_callbacks();
1689 } else {
1690 _clear_window();
1691 }
1692 } else {
1693 if (embedder) {
1694 embedder->_sub_window_remove(this);
1695 embedder = nullptr;
1696 RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
1697 }
1698 _update_viewport_size(); //called by clear and make, which does not happen here
1699 }
1700
1701 RS::get_singleton()->viewport_set_active(get_viewport_rid(), false);
1702 } break;
1703
1704 case NOTIFICATION_VP_MOUSE_ENTER: {
1705 emit_signal(SceneStringName(mouse_entered));
1706 } break;
1707
1708 case NOTIFICATION_VP_MOUSE_EXIT: {
1709 emit_signal(SceneStringName(mouse_exited));
1710 } break;
1711 }
1712 }
1713
1714 void Window::set_content_scale_size(const Size2i &p_size) {
1715 ERR_MAIN_THREAD_GUARD;
1716 ERR_FAIL_COND(p_size.x < 0);
1717 ERR_FAIL_COND(p_size.y < 0);
1718 content_scale_size = p_size;
1719 _update_viewport_size();
1720 }
1721
1722 Size2i Window::get_content_scale_size() const {
1723 ERR_READ_THREAD_GUARD_V(Size2i());
1724 return content_scale_size;
1725 }
1726
1727 void Window::set_content_scale_mode(ContentScaleMode p_mode) {
1728 ERR_MAIN_THREAD_GUARD;
1729 content_scale_mode = p_mode;
1730 _update_viewport_size();
1731 }
1732
1733 Window::ContentScaleMode Window::get_content_scale_mode() const {
1734 ERR_READ_THREAD_GUARD_V(CONTENT_SCALE_MODE_DISABLED);
1735 return content_scale_mode;
1736 }
1737
1738 void Window::set_content_scale_aspect(ContentScaleAspect p_aspect) {
1739 ERR_MAIN_THREAD_GUARD;
1740 content_scale_aspect = p_aspect;
1741 _update_viewport_size();
1742 }
1743
1744 Window::ContentScaleAspect Window::get_content_scale_aspect() const {
1745 ERR_READ_THREAD_GUARD_V(CONTENT_SCALE_ASPECT_IGNORE);
1746 return content_scale_aspect;
1747 }
1748
1749 void Window::set_content_scale_stretch(ContentScaleStretch p_stretch) {
1750 content_scale_stretch = p_stretch;
1751 _update_viewport_size();
1752 }
1753
1754 Window::ContentScaleStretch Window::get_content_scale_stretch() const {
1755 return content_scale_stretch;
1756 }
1757
1758 void Window::set_keep_title_visible(bool p_title_visible) {
1759 if (keep_title_visible == p_title_visible) {
1760 return;
1761 }
1762 keep_title_visible = p_title_visible;
1763 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1764 _update_window_size();
1765 }
1766 }
1767
1768 bool Window::get_keep_title_visible() const {
1769 return keep_title_visible;
1770 }
1771
1772 void Window::set_content_scale_factor(real_t p_factor) {
1773 ERR_MAIN_THREAD_GUARD;
1774 ERR_FAIL_COND(p_factor <= 0);
1775 content_scale_factor = p_factor;
1776 _update_viewport_size();
1777 }
1778
1779 real_t Window::get_content_scale_factor() const {
1780 ERR_READ_THREAD_GUARD_V(0);
1781 return content_scale_factor;
1782 }
1783
1784 void Window::set_nonclient_area(const Rect2i &p_rect) {
1785 ERR_MAIN_THREAD_GUARD;
1786 nonclient_area = p_rect;
1787 }
1788
1789 Rect2i Window::get_nonclient_area() const {
1790 return nonclient_area;
1791 }
1792
1793 DisplayServer::WindowID Window::get_window_id() const {
1794 ERR_READ_THREAD_GUARD_V(DisplayServer::INVALID_WINDOW_ID);
1795 if (get_embedder()) {
1796 #ifdef TOOLS_ENABLED
1797 if (is_part_of_edited_scene()) {
1798 return DisplayServer::MAIN_WINDOW_ID;
1799 }
1800 #endif
1801 return parent->get_window_id();
1802 }
1803 return window_id;
1804 }
1805
1806 void Window::set_mouse_passthrough_polygon(const Vector<Vector2> &p_region) {
1807 ERR_MAIN_THREAD_GUARD;
1808 mpath = p_region;
1809 if (window_id == DisplayServer::INVALID_WINDOW_ID) {
1810 return;
1811 }
1812 DisplayServer::get_singleton()->window_set_mouse_passthrough(mpath, window_id);
1813 }
1814
1815 Vector<Vector2> Window::get_mouse_passthrough_polygon() const {
1816 return mpath;
1817 }
1818
1819 void Window::set_wrap_controls(bool p_enable) {
1820 ERR_MAIN_THREAD_GUARD;
1821 wrap_controls = p_enable;
1822
1823 if (!is_inside_tree()) {
1824 return;
1825 }
1826
1827 if (updating_child_controls) {
1828 _update_child_controls();
1829 } else {
1830 _update_window_size();
1831 }
1832 }
1833
1834 bool Window::is_wrapping_controls() const {
1835 ERR_READ_THREAD_GUARD_V(false);
1836 return wrap_controls;
1837 }
1838
1839 Size2 Window::_get_contents_minimum_size() const {
1840 Size2 max;
1841
1842 for (int i = 0; i < get_child_count(); i++) {
1843 Control *c = Object::cast_to<Control>(get_child(i));
1844 if (c) {
1845 Point2i pos = c->get_position();
1846 Size2i min = c->get_combined_minimum_size();
1847
1848 max = max.max(pos + min);
1849 }
1850 }
1851
1852 return max;
1853 }
1854
1855 void Window::child_controls_changed() {
1856 ERR_MAIN_THREAD_GUARD;
1857 if (!is_inside_tree() || !visible || updating_child_controls) {
1858 return;
1859 }
1860
1861 updating_child_controls = true;
1862 callable_mp(this, &Window::_update_child_controls).call_deferred();
1863 }
1864
1865 void Window::_update_child_controls() {
1866 if (!updating_child_controls) {
1867 return;
1868 }
1869
1870 _update_window_size();
1871
1872 updating_child_controls = false;
1873 }
1874
1875 bool Window::_can_consume_input_events() const {
1876 return exclusive_child == nullptr;
1877 }
1878
1879 void Window::_window_input(const Ref<InputEvent> &p_ev) {
1880 ERR_MAIN_THREAD_GUARD;
1881
1882 if (exclusive_child != nullptr) {
1883 if (nonclient_area.has_area() && is_inside_tree()) {
1884 Ref<InputEventMouse> me = p_ev;
1885 if (me.is_valid() && nonclient_area.has_point(me->get_position())) {
1886 emit_signal(SceneStringName(nonclient_window_input), p_ev);
1887 }
1888 }
1889 if (!is_embedding_subwindows()) { // Not embedding, no need for event.
1890 return;
1891 }
1892 }
1893
1894 // If the event needs to be handled in a Window-derived class, then it should overwrite
1895 // `_input_from_window` instead of subscribing to the `window_input` signal, because the signal
1896 // filters out internal events.
1897 _input_from_window(p_ev);
1898
1899 if (p_ev->get_device() != InputEvent::DEVICE_ID_INTERNAL && is_inside_tree()) {
1900 emit_signal(SceneStringName(window_input), p_ev);
1901 }
1902
1903 if (is_inside_tree()) {
1904 push_input(p_ev);
1905 }
1906 }
1907
1908 void Window::_window_input_text(const String &p_text, bool p_emit_signal) {
1909 _push_text_input(p_text, p_emit_signal);
1910 }
1911
1912 void Window::_window_drop_files(const Vector<String> &p_files) {
1913 emit_signal(SNAME("files_dropped"), p_files);
1914 }
1915
1916 Viewport *Window::get_parent_viewport() const {
1917 ERR_READ_THREAD_GUARD_V(nullptr);
1918 if (get_parent()) {
1919 return get_parent()->get_viewport();
1920 } else {
1921 return nullptr;
1922 }
1923 }
1924
1925 Window *Window::get_non_popup_window() const {
1926 Window *w = const_cast<Window *>(this);
1927 while (w && w->is_popup()) {
1928 w = w->get_parent_visible_window();
1929 }
1930 return w;
1931 }
1932
1933 Window *Window::get_parent_visible_window() const {
1934 ERR_READ_THREAD_GUARD_V(nullptr);
1935 Viewport *vp = get_parent_viewport();
1936 Window *window = nullptr;
1937 while (vp) {
1938 window = Object::cast_to<Window>(vp);
1939 if (window && window->visible) {
1940 break;
1941 }
1942 if (!vp->get_parent()) {
1943 break;
1944 }
1945
1946 vp = vp->get_parent()->get_viewport();
1947 }
1948 return window;
1949 }
1950
1951 void Window::popup_on_parent(const Rect2i &p_parent_rect) {
1952 ERR_MAIN_THREAD_GUARD;
1953 ERR_FAIL_COND(!is_inside_tree());
1954 ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
1955
1956 if (!is_embedded()) {
1957 Window *window = get_parent_visible_window();
1958
1959 if (!window) {
1960 popup(p_parent_rect);
1961 } else {
1962 popup(Rect2i(window->get_position() + p_parent_rect.position, p_parent_rect.size));
1963 }
1964 } else {
1965 popup(p_parent_rect);
1966 }
1967 }
1968
1969 void Window::popup_centered_clamped(const Size2i &p_size, float p_fallback_ratio) {
1970 ERR_MAIN_THREAD_GUARD;
1971 ERR_FAIL_COND(!is_inside_tree());
1972 ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
1973
1974 // Consider the current size when calling with the default value.
1975 Size2i expected_size = p_size == Size2i() ? size : p_size;
1976
1977 Rect2 parent_rect;
1978
1979 if (is_embedded()) {
1980 parent_rect = get_embedder()->get_visible_rect();
1981 } else {
1982 DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
1983 int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
1984 parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
1985 parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
1986 }
1987
1988 Vector2i size_ratio = parent_rect.size * p_fallback_ratio;
1989
1990 Rect2i popup_rect;
1991 popup_rect.size = size_ratio.min(expected_size);
1992 popup_rect.size = _clamp_window_size(popup_rect.size);
1993
1994 if (parent_rect != Rect2()) {
1995 popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
1996 }
1997
1998 emit_signal(SNAME("about_to_popup"));
1999
2000 if (popup_rect != Rect2()) {
2001 set_size(popup_rect.size);
2002 }
2003 _pre_popup();
2004 if (popup_rect != Rect2i()) {
2005 popup_rect.size = get_size();
2006 popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
2007 }
2008 _popup_base(popup_rect);
2009 }
2010
2011 void Window::popup_centered(const Size2i &p_minsize) {
2012 ERR_MAIN_THREAD_GUARD;
2013 ERR_FAIL_COND(!is_inside_tree());
2014 ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
2015
2016 // Consider the current size when calling with the default value.
2017 Size2i expected_size = p_minsize == Size2i() ? size : p_minsize;
2018
2019 Rect2 parent_rect;
2020
2021 if (is_embedded()) {
2022 parent_rect = get_embedder()->get_visible_rect();
2023 } else {
2024 DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
2025 int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
2026 parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
2027 parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
2028 }
2029
2030 Rect2i popup_rect;
2031 popup_rect.size = _clamp_window_size(expected_size);
2032
2033 if (parent_rect != Rect2()) {
2034 popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
2035 }
2036
2037 emit_signal(SNAME("about_to_popup"));
2038
2039 if (popup_rect != Rect2()) {
2040 set_size(popup_rect.size);
2041 }
2042 _pre_popup();
2043 if (popup_rect != Rect2i()) {
2044 popup_rect.size = get_size();
2045 popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
2046 }
2047 _popup_base(popup_rect);
2048 }
2049
2050 void Window::popup_centered_ratio(float p_ratio) {
2051 ERR_MAIN_THREAD_GUARD;
2052 ERR_FAIL_COND(!is_inside_tree());
2053 ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
2054 ERR_FAIL_COND_MSG(p_ratio <= 0.0 || p_ratio > 1.0, "Ratio must be between 0.0 and 1.0!");
2055
2056 Rect2 parent_rect;
2057
2058 if (is_embedded()) {
2059 parent_rect = get_embedder()->get_visible_rect();
2060 } else {
2061 DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
2062 int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
2063 parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
2064 parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
2065 }
2066
2067 Rect2i popup_rect;
2068 if (parent_rect != Rect2()) {
2069 popup_rect.size = parent_rect.size * p_ratio;
2070 popup_rect.size = _clamp_window_size(popup_rect.size);
2071 popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
2072 }
2073
2074 emit_signal(SNAME("about_to_popup"));
2075
2076 if (popup_rect != Rect2()) {
2077 set_size(popup_rect.size);
2078 }
2079 _pre_popup();
2080 if (popup_rect != Rect2i()) {
2081 popup_rect.size = get_size();
2082 popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
2083 }
2084 _popup_base(popup_rect);
2085 }
2086
2087 void Window::popup(const Rect2i &p_screen_rect) {
2088 ERR_MAIN_THREAD_GUARD;
2089 emit_signal(SNAME("about_to_popup"));
2090
2091 Rect2i screen_rect = p_screen_rect;
2092 if (screen_rect != Rect2i()) {
2093 set_size(screen_rect.size);
2094 }
2095 _pre_popup();
2096 if (screen_rect != Rect2i()) {
2097 screen_rect.size = get_size();
2098 }
2099 _popup_base(screen_rect);
2100 }
2101
2102 void Window::_popup_base(const Rect2i &p_screen_rect) {
2103 ERR_MAIN_THREAD_GUARD;
2104
2105 if (!get_embedder() && get_flag(FLAG_POPUP)) {
2106 // Send a focus-out notification when opening a Window Manager Popup.
2107 SceneTree *scene_tree = get_tree();
2108 if (scene_tree) {
2109 scene_tree->notify_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_viewports", NOTIFICATION_WM_WINDOW_FOCUS_OUT);
2110 }
2111 }
2112
2113 // Update window size to calculate the actual window size based on contents minimum size and minimum size.
2114 _update_window_size();
2115
2116 bool should_fit = is_embedded() || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS);
2117
2118 if (p_screen_rect != Rect2i()) {
2119 set_position(p_screen_rect.position);
2120
2121 if (should_fit) {
2122 int screen_id = DisplayServer::get_singleton()->get_screen_from_rect(p_screen_rect);
2123 Size2i screen_size = DisplayServer::get_singleton()->screen_get_usable_rect(screen_id).size;
2124 Size2i new_size = p_screen_rect.size.min(screen_size);
2125 set_size(new_size);
2126 } else {
2127 set_size(p_screen_rect.size);
2128 }
2129 }
2130
2131 Rect2i adjust = _popup_adjust_rect();
2132 if (adjust != Rect2i()) {
2133 set_position(adjust.position);
2134 set_size(adjust.size);
2135 }
2136
2137 int scr = DisplayServer::get_singleton()->get_screen_count();
2138 for (int i = 0; i < scr; i++) {
2139 Rect2i r = DisplayServer::get_singleton()->screen_get_usable_rect(i);
2140 if (r.has_point(position)) {
2141 current_screen = i;
2142 break;
2143 }
2144 }
2145
2146 set_transient(true);
2147 set_visible(true);
2148
2149 Rect2i parent_rect;
2150 if (is_embedded()) {
2151 parent_rect = get_embedder()->get_visible_rect();
2152 } else {
2153 int screen_id = DisplayServer::get_singleton()->window_get_current_screen(get_window_id());
2154 parent_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen_id);
2155 }
2156 if (should_fit && parent_rect != Rect2i() && !parent_rect.intersects(Rect2i(position, size))) {
2157 ERR_PRINT(vformat("Window %d spawned at invalid position: %s.", get_window_id(), position));
2158 set_position((parent_rect.size - size) / 2);
2159 }
2160 if (parent_rect != Rect2i() && is_clamped_to_embedder() && is_embedded()) {
2161 Rect2i new_rect = fit_rect_in_parent(Rect2i(position, size), parent_rect);
2162 set_position(new_rect.position);
2163 set_size(new_rect.size);
2164 }
2165
2166 _post_popup();
2167 notification(NOTIFICATION_POST_POPUP);
2168 }
2169
2170 bool Window::_try_parent_dialog(Node *p_from_node) {
2171 ERR_FAIL_NULL_V(p_from_node, false);
2172 ERR_FAIL_COND_V_MSG(is_inside_tree(), false, "Attempting to parent and popup a dialog that already has a parent.");
2173
2174 Window *w = p_from_node->get_last_exclusive_window();
2175 if (w && w != this) {
2176 w->add_child(this);
2177 return true;
2178 }
2179 return false;
2180 }
2181
2182 void Window::popup_exclusive(Node *p_from_node, const Rect2i &p_screen_rect) {
2183 if (_try_parent_dialog(p_from_node)) {
2184 popup(p_screen_rect);
2185 }
2186 }
2187
2188 void Window::popup_exclusive_on_parent(Node *p_from_node, const Rect2i &p_parent_rect) {
2189 if (_try_parent_dialog(p_from_node)) {
2190 popup_on_parent(p_parent_rect);
2191 }
2192 }
2193
2194 void Window::popup_exclusive_centered(Node *p_from_node, const Size2i &p_minsize) {
2195 if (_try_parent_dialog(p_from_node)) {
2196 popup_centered(p_minsize);
2197 }
2198 }
2199
2200 void Window::popup_exclusive_centered_ratio(Node *p_from_node, float p_ratio) {
2201 if (_try_parent_dialog(p_from_node)) {
2202 popup_centered_ratio(p_ratio);
2203 }
2204 }
2205
2206 void Window::popup_exclusive_centered_clamped(Node *p_from_node, const Size2i &p_size, float p_fallback_ratio) {
2207 if (_try_parent_dialog(p_from_node)) {
2208 popup_centered_clamped(p_size, p_fallback_ratio);
2209 }
2210 }
2211
2212 Rect2i Window::fit_rect_in_parent(Rect2i p_rect, const Rect2i &p_parent_rect) const {
2213 ERR_READ_THREAD_GUARD_V(Rect2i());
2214 Size2i limit = p_parent_rect.size;
2215 if (p_rect.position.x + p_rect.size.x > limit.x) {
2216 p_rect.position.x = limit.x - p_rect.size.x;
2217 }
2218 if (p_rect.position.y + p_rect.size.y > limit.y) {
2219 p_rect.position.y = limit.y - p_rect.size.y;
2220 }
2221
2222 if (p_rect.position.x < 0) {
2223 p_rect.position.x = 0;
2224 }
2225
2226 int title_height = get_flag(Window::FLAG_BORDERLESS) ? 0 : theme_cache.title_height;
2227
2228 if (p_rect.position.y < title_height) {
2229 p_rect.position.y = title_height;
2230 }
2231
2232 return p_rect;
2233 }
2234
2235 Size2 Window::get_contents_minimum_size() const {
2236 ERR_READ_THREAD_GUARD_V(Size2());
2237 Vector2 ms;
2238 if (GDVIRTUAL_CALL(_get_contents_minimum_size, ms)) {
2239 return ms;
2240 }
2241 return _get_contents_minimum_size();
2242 }
2243
2244 Size2 Window::get_clamped_minimum_size() const {
2245 ERR_READ_THREAD_GUARD_V(Size2());
2246 if (!wrap_controls) {
2247 return min_size;
2248 }
2249
2250 return min_size.max(get_contents_minimum_size() * get_content_scale_factor());
2251 }
2252
2253 void Window::grab_focus() {
2254 ERR_MAIN_THREAD_GUARD;
2255 if (embedder) {
2256 embedder->_sub_window_grab_focus(this);
2257 } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2258 DisplayServer::get_singleton()->window_move_to_foreground(window_id);
2259 }
2260 }
2261
2262 bool Window::has_focus() const {
2263 ERR_READ_THREAD_GUARD_V(false);
2264 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2265 return DisplayServer::get_singleton()->window_is_focused(window_id);
2266 }
2267 return focused;
2268 }
2269
2270 bool Window::has_focus_or_active_popup() const {
2271 ERR_READ_THREAD_GUARD_V(false);
2272 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2273 return DisplayServer::get_singleton()->window_is_focused(window_id) || (DisplayServer::get_singleton()->window_get_active_popup() == window_id);
2274 }
2275 return focused;
2276 }
2277
2278 void Window::start_drag() {
2279 ERR_MAIN_THREAD_GUARD;
2280 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2281 DisplayServer::get_singleton()->window_start_drag(window_id);
2282 } else if (embedder) {
2283 embedder->_window_start_drag(this);
2284 }
2285 }
2286
2287 void Window::start_resize(DisplayServer::WindowResizeEdge p_edge) {
2288 ERR_MAIN_THREAD_GUARD;
2289 if (get_flag(FLAG_RESIZE_DISABLED)) {
2290 return;
2291 }
2292 if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2293 DisplayServer::get_singleton()->window_start_resize(p_edge, window_id);
2294 } else if (embedder) {
2295 switch (p_edge) {
2296 case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
2297 embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_TOP_LEFT, this);
2298 } break;
2299 case DisplayServer::WINDOW_EDGE_TOP: {
2300 embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_TOP, this);
2301 } break;
2302 case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
2303 embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_TOP_RIGHT, this);
2304 } break;
2305 case DisplayServer::WINDOW_EDGE_LEFT: {
2306 embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_LEFT, this);
2307 } break;
2308 case DisplayServer::WINDOW_EDGE_RIGHT: {
2309 embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_RIGHT, this);
2310 } break;
2311 case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
2312 embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_BOTTOM_LEFT, this);
2313 } break;
2314 case DisplayServer::WINDOW_EDGE_BOTTOM: {
2315 embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_BOTTOM, this);
2316 } break;
2317 case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
2318 embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_BOTTOM_RIGHT, this);
2319 } break;
2320 default:
2321 break;
2322 }
2323 }
2324 }
2325
2326 Rect2i Window::get_usable_parent_rect() const {
2327 ERR_READ_THREAD_GUARD_V(Rect2i());
2328 ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
2329 Rect2i parent_rect;
2330 if (is_embedded()) {
2331 parent_rect = get_embedder()->get_visible_rect();
2332 } else {
2333 const Window *w = is_visible() ? this : get_parent_visible_window();
2334 //find a parent that can contain us
2335 ERR_FAIL_NULL_V(w, Rect2());
2336
2337 parent_rect = DisplayServer::get_singleton()->screen_get_usable_rect(DisplayServer::get_singleton()->window_get_current_screen(w->get_window_id()));
2338 }
2339 return parent_rect;
2340 }
2341
2342 void Window::set_accessibility_name(const String &p_name) {
2343 ERR_MAIN_THREAD_GUARD;
2344 if (accessibility_name != p_name) {
2345 accessibility_name = p_name;
2346 queue_accessibility_update();
2347 update_configuration_warnings();
2348 }
2349 }
2350
2351 String Window::get_accessibility_name() const {
2352 return tr(accessibility_name);
2353 }
2354
2355 void Window::set_accessibility_description(const String &p_description) {
2356 ERR_MAIN_THREAD_GUARD;
2357 if (accessibility_description != p_description) {
2358 accessibility_description = p_description;
2359 queue_accessibility_update();
2360 }
2361 }
2362
2363 String Window::get_accessibility_description() const {
2364 return tr(accessibility_description);
2365 }
2366
2367 void Window::accessibility_announcement(const String &p_announcement) {
2368 ERR_MAIN_THREAD_GUARD;
2369 announcement = p_announcement;
2370 queue_accessibility_update();
2371 }
2372
2373 void Window::add_child_notify(Node *p_child) {
2374 if (is_inside_tree() && wrap_controls) {
2375 child_controls_changed();
2376 }
2377 }
2378
2379 void Window::remove_child_notify(Node *p_child) {
2380 if (is_inside_tree() && wrap_controls) {
2381 child_controls_changed();
2382 }
2383 }
2384
2385 // Theming.
2386
2387 void Window::set_theme_owner_node(Node *p_node) {
2388 ERR_MAIN_THREAD_GUARD;
2389 theme_owner->set_owner_node(p_node);
2390 }
2391
2392 Node *Window::get_theme_owner_node() const {
2393 ERR_READ_THREAD_GUARD_V(nullptr);
2394 return theme_owner->get_owner_node();
2395 }
2396
2397 bool Window::has_theme_owner_node() const {
2398 ERR_READ_THREAD_GUARD_V(false);
2399 return theme_owner->has_owner_node();
2400 }
2401
2402 void Window::set_theme_context(ThemeContext *p_context, bool p_propagate) {
2403 ERR_MAIN_THREAD_GUARD;
2404 theme_owner->set_owner_context(p_context, p_propagate);
2405 }
2406
2407 void Window::set_theme(const Ref<Theme> &p_theme) {
2408 ERR_MAIN_THREAD_GUARD;
2409 if (theme == p_theme) {
2410 return;
2411 }
2412
2413 if (theme.is_valid()) {
2414 theme->disconnect_changed(callable_mp(this, &Window::_theme_changed));
2415 }
2416
2417 theme = p_theme;
2418 if (theme.is_valid()) {
2419 theme_owner->propagate_theme_changed(this, this, is_inside_tree(), true);
2420 theme->connect_changed(callable_mp(this, &Window::_theme_changed), CONNECT_DEFERRED);
2421 return;
2422 }
2423
2424 Control *parent_c = Object::cast_to<Control>(get_parent());
2425 if (parent_c && parent_c->has_theme_owner_node()) {
2426 theme_owner->propagate_theme_changed(this, parent_c->get_theme_owner_node(), is_inside_tree(), true);
2427 return;
2428 }
2429
2430 Window *parent_w = cast_to<Window>(get_parent());
2431 if (parent_w && parent_w->has_theme_owner_node()) {
2432 theme_owner->propagate_theme_changed(this, parent_w->get_theme_owner_node(), is_inside_tree(), true);
2433 return;
2434 }
2435
2436 theme_owner->propagate_theme_changed(this, nullptr, is_inside_tree(), true);
2437 }
2438
2439 Ref<Theme> Window::get_theme() const {
2440 ERR_READ_THREAD_GUARD_V(Ref<Theme>());
2441 return theme;
2442 }
2443
2444 void Window::_theme_changed() {
2445 if (is_inside_tree()) {
2446 theme_owner->propagate_theme_changed(this, this, true, false);
2447 }
2448 }
2449
2450 void Window::_notify_theme_override_changed() {
2451 if (!bulk_theme_override && is_inside_tree()) {
2452 notification(NOTIFICATION_THEME_CHANGED);
2453 }
2454 }
2455
2456 void Window::_invalidate_theme_cache() {
2457 theme_icon_cache.clear();
2458 theme_style_cache.clear();
2459 theme_font_cache.clear();
2460 theme_font_size_cache.clear();
2461 theme_color_cache.clear();
2462 theme_constant_cache.clear();
2463 }
2464
2465 void Window::_update_theme_item_cache() {
2466 // Request an update on the next frame to reflect theme changes.
2467 // Updating without a delay can cause a lot of lag.
2468 if (!wrap_controls) {
2469 updating_embedded_window = true;
2470 callable_mp(this, &Window::_update_embedded_window).call_deferred();
2471 } else {
2472 child_controls_changed();
2473 }
2474
2475 ThemeDB::get_singleton()->update_class_instance_items(this);
2476 }
2477
2478 void Window::_update_embedded_window() {
2479 if (!updating_embedded_window) {
2480 return;
2481 }
2482
2483 if (embedder) {
2484 embedder->_sub_window_update(this);
2485 };
2486
2487 updating_embedded_window = false;
2488 }
2489
2490 void Window::set_theme_type_variation(const StringName &p_theme_type) {
2491 ERR_MAIN_THREAD_GUARD;
2492 theme_type_variation = p_theme_type;
2493 if (is_inside_tree()) {
2494 notification(NOTIFICATION_THEME_CHANGED);
2495 }
2496 }
2497
2498 StringName Window::get_theme_type_variation() const {
2499 ERR_READ_THREAD_GUARD_V(StringName());
2500 return theme_type_variation;
2501 }
2502
2503 /// Theme property lookup.
2504
2505 Ref<Texture2D> Window::get_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
2506 ERR_READ_THREAD_GUARD_V(Ref<Texture2D>());
2507 if (!initialized) {
2508 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2509 }
2510
2511 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2512 const Ref<Texture2D> *tex = theme_icon_override.getptr(p_name);
2513 if (tex) {
2514 return *tex;
2515 }
2516 }
2517
2518 if (theme_icon_cache.has(p_theme_type) && theme_icon_cache[p_theme_type].has(p_name)) {
2519 return theme_icon_cache[p_theme_type][p_name];
2520 }
2521
2522 Vector<StringName> theme_types;
2523 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2524 Ref<Texture2D> icon = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
2525 theme_icon_cache[p_theme_type][p_name] = icon;
2526 return icon;
2527 }
2528
2529 Ref<StyleBox> Window::get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
2530 ERR_READ_THREAD_GUARD_V(Ref<StyleBox>());
2531 if (!initialized) {
2532 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2533 }
2534
2535 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2536 const Ref<StyleBox> *style = theme_style_override.getptr(p_name);
2537 if (style) {
2538 return *style;
2539 }
2540 }
2541
2542 if (theme_style_cache.has(p_theme_type) && theme_style_cache[p_theme_type].has(p_name)) {
2543 return theme_style_cache[p_theme_type][p_name];
2544 }
2545
2546 Vector<StringName> theme_types;
2547 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2548 Ref<StyleBox> style = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
2549 theme_style_cache[p_theme_type][p_name] = style;
2550 return style;
2551 }
2552
2553 Ref<Font> Window::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
2554 ERR_READ_THREAD_GUARD_V(Ref<Font>());
2555 if (!initialized) {
2556 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2557 }
2558
2559 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2560 const Ref<Font> *font = theme_font_override.getptr(p_name);
2561 if (font) {
2562 return *font;
2563 }
2564 }
2565
2566 if (theme_font_cache.has(p_theme_type) && theme_font_cache[p_theme_type].has(p_name)) {
2567 return theme_font_cache[p_theme_type][p_name];
2568 }
2569
2570 Vector<StringName> theme_types;
2571 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2572 Ref<Font> font = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
2573 theme_font_cache[p_theme_type][p_name] = font;
2574 return font;
2575 }
2576
2577 int Window::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
2578 ERR_READ_THREAD_GUARD_V(0);
2579 if (!initialized) {
2580 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2581 }
2582
2583 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2584 const int *font_size = theme_font_size_override.getptr(p_name);
2585 if (font_size && (*font_size) > 0) {
2586 return *font_size;
2587 }
2588 }
2589
2590 if (theme_font_size_cache.has(p_theme_type) && theme_font_size_cache[p_theme_type].has(p_name)) {
2591 return theme_font_size_cache[p_theme_type][p_name];
2592 }
2593
2594 Vector<StringName> theme_types;
2595 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2596 int font_size = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
2597 theme_font_size_cache[p_theme_type][p_name] = font_size;
2598 return font_size;
2599 }
2600
2601 Color Window::get_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
2602 ERR_READ_THREAD_GUARD_V(Color());
2603 if (!initialized) {
2604 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2605 }
2606
2607 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2608 const Color *color = theme_color_override.getptr(p_name);
2609 if (color) {
2610 return *color;
2611 }
2612 }
2613
2614 if (theme_color_cache.has(p_theme_type) && theme_color_cache[p_theme_type].has(p_name)) {
2615 return theme_color_cache[p_theme_type][p_name];
2616 }
2617
2618 Vector<StringName> theme_types;
2619 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2620 Color color = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
2621 theme_color_cache[p_theme_type][p_name] = color;
2622 return color;
2623 }
2624
2625 int Window::get_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
2626 ERR_READ_THREAD_GUARD_V(0);
2627 if (!initialized) {
2628 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2629 }
2630
2631 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2632 const int *constant = theme_constant_override.getptr(p_name);
2633 if (constant) {
2634 return *constant;
2635 }
2636 }
2637
2638 if (theme_constant_cache.has(p_theme_type) && theme_constant_cache[p_theme_type].has(p_name)) {
2639 return theme_constant_cache[p_theme_type][p_name];
2640 }
2641
2642 Vector<StringName> theme_types;
2643 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2644 int constant = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
2645 theme_constant_cache[p_theme_type][p_name] = constant;
2646 return constant;
2647 }
2648
2649 Variant Window::get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
2650 switch (p_data_type) {
2651 case Theme::DATA_TYPE_COLOR:
2652 return get_theme_color(p_name, p_theme_type);
2653 case Theme::DATA_TYPE_CONSTANT:
2654 return get_theme_constant(p_name, p_theme_type);
2655 case Theme::DATA_TYPE_FONT:
2656 return get_theme_font(p_name, p_theme_type);
2657 case Theme::DATA_TYPE_FONT_SIZE:
2658 return get_theme_font_size(p_name, p_theme_type);
2659 case Theme::DATA_TYPE_ICON:
2660 return get_theme_icon(p_name, p_theme_type);
2661 case Theme::DATA_TYPE_STYLEBOX:
2662 return get_theme_stylebox(p_name, p_theme_type);
2663 case Theme::DATA_TYPE_MAX:
2664 break; // Can't happen, but silences warning.
2665 }
2666
2667 return Variant();
2668 }
2669
2670 #ifdef TOOLS_ENABLED
2671 Ref<Texture2D> Window::get_editor_theme_icon(const StringName &p_name) const {
2672 return get_theme_icon(p_name, SNAME("EditorIcons"));
2673 }
2674 #endif
2675
2676 bool Window::has_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
2677 ERR_READ_THREAD_GUARD_V(false);
2678 if (!initialized) {
2679 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2680 }
2681
2682 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2683 if (has_theme_icon_override(p_name)) {
2684 return true;
2685 }
2686 }
2687
2688 Vector<StringName> theme_types;
2689 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2690 return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
2691 }
2692
2693 bool Window::has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
2694 ERR_READ_THREAD_GUARD_V(false);
2695 if (!initialized) {
2696 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2697 }
2698
2699 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2700 if (has_theme_stylebox_override(p_name)) {
2701 return true;
2702 }
2703 }
2704
2705 Vector<StringName> theme_types;
2706 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2707 return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
2708 }
2709
2710 bool Window::has_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
2711 ERR_READ_THREAD_GUARD_V(false);
2712 if (!initialized) {
2713 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2714 }
2715
2716 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2717 if (has_theme_font_override(p_name)) {
2718 return true;
2719 }
2720 }
2721
2722 Vector<StringName> theme_types;
2723 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2724 return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
2725 }
2726
2727 bool Window::has_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
2728 ERR_READ_THREAD_GUARD_V(false);
2729 if (!initialized) {
2730 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2731 }
2732
2733 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2734 if (has_theme_font_size_override(p_name)) {
2735 return true;
2736 }
2737 }
2738
2739 Vector<StringName> theme_types;
2740 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2741 return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
2742 }
2743
2744 bool Window::has_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
2745 ERR_READ_THREAD_GUARD_V(false);
2746 if (!initialized) {
2747 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2748 }
2749
2750 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2751 if (has_theme_color_override(p_name)) {
2752 return true;
2753 }
2754 }
2755
2756 Vector<StringName> theme_types;
2757 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2758 return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
2759 }
2760
2761 bool Window::has_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
2762 ERR_READ_THREAD_GUARD_V(false);
2763 if (!initialized) {
2764 WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2765 }
2766
2767 if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2768 if (has_theme_constant_override(p_name)) {
2769 return true;
2770 }
2771 }
2772
2773 Vector<StringName> theme_types;
2774 theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2775 return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
2776 }
2777
2778 /// Local property overrides.
2779
2780 void Window::add_theme_icon_override(const StringName &p_name, const Ref<Texture2D> &p_icon) {
2781 ERR_MAIN_THREAD_GUARD;
2782 ERR_FAIL_COND(p_icon.is_null());
2783
2784 if (theme_icon_override.has(p_name)) {
2785 theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2786 }
2787
2788 theme_icon_override[p_name] = p_icon;
2789 theme_icon_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
2790 _notify_theme_override_changed();
2791 }
2792
2793 void Window::add_theme_style_override(const StringName &p_name, const Ref<StyleBox> &p_style) {
2794 ERR_MAIN_THREAD_GUARD;
2795 ERR_FAIL_COND(p_style.is_null());
2796
2797 if (theme_style_override.has(p_name)) {
2798 theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2799 }
2800
2801 theme_style_override[p_name] = p_style;
2802 theme_style_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
2803 _notify_theme_override_changed();
2804 }
2805
2806 void Window::add_theme_font_override(const StringName &p_name, const Ref<Font> &p_font) {
2807 ERR_MAIN_THREAD_GUARD;
2808 ERR_FAIL_COND(p_font.is_null());
2809
2810 if (theme_font_override.has(p_name)) {
2811 theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2812 }
2813
2814 theme_font_override[p_name] = p_font;
2815 theme_font_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
2816 _notify_theme_override_changed();
2817 }
2818
2819 void Window::add_theme_font_size_override(const StringName &p_name, int p_font_size) {
2820 ERR_MAIN_THREAD_GUARD;
2821 theme_font_size_override[p_name] = p_font_size;
2822 _notify_theme_override_changed();
2823 }
2824
2825 void Window::add_theme_color_override(const StringName &p_name, const Color &p_color) {
2826 ERR_MAIN_THREAD_GUARD;
2827 theme_color_override[p_name] = p_color;
2828 _notify_theme_override_changed();
2829 }
2830
2831 void Window::add_theme_constant_override(const StringName &p_name, int p_constant) {
2832 ERR_MAIN_THREAD_GUARD;
2833 theme_constant_override[p_name] = p_constant;
2834 _notify_theme_override_changed();
2835 }
2836
2837 void Window::remove_theme_icon_override(const StringName &p_name) {
2838 ERR_MAIN_THREAD_GUARD;
2839 if (theme_icon_override.has(p_name)) {
2840 theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2841 }
2842
2843 theme_icon_override.erase(p_name);
2844 _notify_theme_override_changed();
2845 }
2846
2847 void Window::remove_theme_style_override(const StringName &p_name) {
2848 ERR_MAIN_THREAD_GUARD;
2849 if (theme_style_override.has(p_name)) {
2850 theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2851 }
2852
2853 theme_style_override.erase(p_name);
2854 _notify_theme_override_changed();
2855 }
2856
2857 void Window::remove_theme_font_override(const StringName &p_name) {
2858 ERR_MAIN_THREAD_GUARD;
2859 if (theme_font_override.has(p_name)) {
2860 theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2861 }
2862
2863 theme_font_override.erase(p_name);
2864 _notify_theme_override_changed();
2865 }
2866
2867 void Window::remove_theme_font_size_override(const StringName &p_name) {
2868 ERR_MAIN_THREAD_GUARD;
2869 theme_font_size_override.erase(p_name);
2870 _notify_theme_override_changed();
2871 }
2872
2873 void Window::remove_theme_color_override(const StringName &p_name) {
2874 ERR_MAIN_THREAD_GUARD;
2875 theme_color_override.erase(p_name);
2876 _notify_theme_override_changed();
2877 }
2878
2879 void Window::remove_theme_constant_override(const StringName &p_name) {
2880 ERR_MAIN_THREAD_GUARD;
2881 theme_constant_override.erase(p_name);
2882 _notify_theme_override_changed();
2883 }
2884
2885 bool Window::has_theme_icon_override(const StringName &p_name) const {
2886 ERR_READ_THREAD_GUARD_V(false);
2887 const Ref<Texture2D> *tex = theme_icon_override.getptr(p_name);
2888 return tex != nullptr;
2889 }
2890
2891 bool Window::has_theme_stylebox_override(const StringName &p_name) const {
2892 ERR_READ_THREAD_GUARD_V(false);
2893 const Ref<StyleBox> *style = theme_style_override.getptr(p_name);
2894 return style != nullptr;
2895 }
2896
2897 bool Window::has_theme_font_override(const StringName &p_name) const {
2898 ERR_READ_THREAD_GUARD_V(false);
2899 const Ref<Font> *font = theme_font_override.getptr(p_name);
2900 return font != nullptr;
2901 }
2902
2903 bool Window::has_theme_font_size_override(const StringName &p_name) const {
2904 ERR_READ_THREAD_GUARD_V(false);
2905 const int *font_size = theme_font_size_override.getptr(p_name);
2906 return font_size != nullptr;
2907 }
2908
2909 bool Window::has_theme_color_override(const StringName &p_name) const {
2910 ERR_READ_THREAD_GUARD_V(false);
2911 const Color *color = theme_color_override.getptr(p_name);
2912 return color != nullptr;
2913 }
2914
2915 bool Window::has_theme_constant_override(const StringName &p_name) const {
2916 ERR_READ_THREAD_GUARD_V(false);
2917 const int *constant = theme_constant_override.getptr(p_name);
2918 return constant != nullptr;
2919 }
2920
2921 /// Default theme properties.
2922
2923 float Window::get_theme_default_base_scale() const {
2924 ERR_READ_THREAD_GUARD_V(0);
2925 return theme_owner->get_theme_default_base_scale();
2926 }
2927
2928 Ref<Font> Window::get_theme_default_font() const {
2929 ERR_READ_THREAD_GUARD_V(Ref<Font>());
2930 return theme_owner->get_theme_default_font();
2931 }
2932
2933 int Window::get_theme_default_font_size() const {
2934 ERR_READ_THREAD_GUARD_V(0);
2935 return theme_owner->get_theme_default_font_size();
2936 }
2937
2938 /// Bulk actions.
2939
2940 void Window::begin_bulk_theme_override() {
2941 ERR_MAIN_THREAD_GUARD;
2942 bulk_theme_override = true;
2943 }
2944
2945 void Window::end_bulk_theme_override() {
2946 ERR_MAIN_THREAD_GUARD;
2947 ERR_FAIL_COND(!bulk_theme_override);
2948
2949 bulk_theme_override = false;
2950 _notify_theme_override_changed();
2951 }
2952
2953 //
2954
2955 Rect2i Window::get_parent_rect() const {
2956 ERR_READ_THREAD_GUARD_V(Rect2i());
2957 ERR_FAIL_COND_V(!is_inside_tree(), Rect2i());
2958 if (is_embedded()) {
2959 //viewport
2960 Node *n = get_parent();
2961 ERR_FAIL_NULL_V(n, Rect2i());
2962 Viewport *p = n->get_viewport();
2963 ERR_FAIL_NULL_V(p, Rect2i());
2964
2965 return p->get_visible_rect();
2966 } else {
2967 int x = get_position().x;
2968 int closest_dist = 0x7FFFFFFF;
2969 Rect2i closest_rect;
2970 for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
2971 Rect2i s(DisplayServer::get_singleton()->screen_get_position(i), DisplayServer::get_singleton()->screen_get_size(i));
2972 int d;
2973 if (x >= s.position.x && x < s.size.x) {
2974 //contained
2975 closest_rect = s;
2976 break;
2977 } else if (x < s.position.x) {
2978 d = s.position.x - x;
2979 } else {
2980 d = x - (s.position.x + s.size.x);
2981 }
2982
2983 if (d < closest_dist) {
2984 closest_dist = d;
2985 closest_rect = s;
2986 }
2987 }
2988 return closest_rect;
2989 }
2990 }
2991
2992 void Window::set_clamp_to_embedder(bool p_enable) {
2993 ERR_MAIN_THREAD_GUARD;
2994 clamp_to_embedder = p_enable;
2995 }
2996
2997 bool Window::is_clamped_to_embedder() const {
2998 ERR_READ_THREAD_GUARD_V(false);
2999 return clamp_to_embedder;
3000 }
3001
3002 void Window::set_unparent_when_invisible(bool p_unparent) {
3003 unparent_when_invisible = p_unparent;
3004 }
3005
3006 void Window::set_layout_direction(Window::LayoutDirection p_direction) {
3007 ERR_MAIN_THREAD_GUARD;
3008 ERR_FAIL_INDEX(p_direction, LAYOUT_DIRECTION_MAX);
3009
3010 layout_dir = p_direction;
3011 propagate_notification(Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
3012 }
3013
3014 Window::LayoutDirection Window::get_layout_direction() const {
3015 ERR_READ_THREAD_GUARD_V(LAYOUT_DIRECTION_INHERITED);
3016 return layout_dir;
3017 }
3018
3019 bool Window::is_layout_rtl() const {
3020 ERR_READ_THREAD_GUARD_V(false);
3021 if (layout_dir == LAYOUT_DIRECTION_INHERITED) {
3022 #ifdef TOOLS_ENABLED
3023 if (is_part_of_edited_scene() && GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3024 return true;
3025 }
3026 if (is_inside_tree()) {
3027 Node *edited_scene_root = get_tree()->get_edited_scene_root();
3028 if (edited_scene_root == this) {
3029 int proj_root_layout_direction = GLOBAL_GET_CACHED(int, "internationalization/rendering/root_node_layout_direction");
3030 if (proj_root_layout_direction == 1) {
3031 return false;
3032 } else if (proj_root_layout_direction == 2) {
3033 return true;
3034 } else if (proj_root_layout_direction == 3) {
3035 return TS->is_locale_right_to_left(OS::get_singleton()->get_locale());
3036 } else {
3037 return TS->is_locale_right_to_left(_get_locale());
3038 }
3039 }
3040 }
3041 #else
3042 if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3043 return true;
3044 }
3045 #endif
3046 const StringName domain_name = get_translation_domain();
3047 Node *parent_node = get_parent();
3048 while (parent_node && parent_node->get_translation_domain() == domain_name) {
3049 Control *parent_control = Object::cast_to<Control>(parent_node);
3050 if (parent_control) {
3051 return parent_control->is_layout_rtl();
3052 }
3053
3054 Window *parent_window = Object::cast_to<Window>(parent_node);
3055 if (parent_window) {
3056 return parent_window->is_layout_rtl();
3057 }
3058 parent_node = parent_node->get_parent();
3059 }
3060
3061 if (root_layout_direction == 1) {
3062 return false;
3063 } else if (root_layout_direction == 2) {
3064 return true;
3065 } else if (root_layout_direction == 3) {
3066 return TS->is_locale_right_to_left(OS::get_singleton()->get_locale());
3067 } else {
3068 return TS->is_locale_right_to_left(_get_locale());
3069 }
3070 } else if (layout_dir == LAYOUT_DIRECTION_APPLICATION_LOCALE) {
3071 if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3072 return true;
3073 } else {
3074 return TS->is_locale_right_to_left(_get_locale());
3075 }
3076 } else if (layout_dir == LAYOUT_DIRECTION_SYSTEM_LOCALE) {
3077 if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3078 return true;
3079 } else {
3080 return TS->is_locale_right_to_left(OS::get_singleton()->get_locale());
3081 }
3082 } else {
3083 return (layout_dir == LAYOUT_DIRECTION_RTL);
3084 }
3085 }
3086
3087 #ifndef DISABLE_DEPRECATED
3088
3089 void Window::set_use_font_oversampling(bool p_oversampling) {
3090 Viewport::set_use_oversampling(p_oversampling);
3091 }
3092
3093 bool Window::is_using_font_oversampling() const {
3094 return Viewport::is_using_oversampling();
3095 }
3096
3097 void Window::set_auto_translate(bool p_enable) {
3098 ERR_MAIN_THREAD_GUARD;
3099 set_auto_translate_mode(p_enable ? AUTO_TRANSLATE_MODE_ALWAYS : AUTO_TRANSLATE_MODE_DISABLED);
3100 }
3101
3102 bool Window::is_auto_translating() const {
3103 ERR_READ_THREAD_GUARD_V(false);
3104 return can_auto_translate();
3105 }
3106 #endif
3107
3108 Transform2D Window::get_final_transform() const {
3109 ERR_READ_THREAD_GUARD_V(Transform2D());
3110 return window_transform * stretch_transform * global_canvas_transform;
3111 }
3112
3113 Transform2D Window::get_screen_transform_internal(bool p_absolute_position) const {
3114 ERR_READ_THREAD_GUARD_V(Transform2D());
3115 Transform2D embedder_transform;
3116 if (get_embedder()) {
3117 embedder_transform.translate_local(get_position());
3118 embedder_transform = get_embedder()->get_screen_transform_internal(p_absolute_position) * embedder_transform;
3119 } else if (p_absolute_position) {
3120 embedder_transform.translate_local(get_position());
3121 }
3122 return embedder_transform * get_final_transform();
3123 }
3124
3125 Transform2D Window::get_popup_base_transform_native() const {
3126 ERR_READ_THREAD_GUARD_V(Transform2D());
3127 if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SUBWINDOWS)) {
3128 return Transform2D();
3129 }
3130 Transform2D popup_base_transform;
3131 popup_base_transform.set_origin(get_position());
3132 popup_base_transform *= get_final_transform();
3133 if (get_embedder()) {
3134 return get_embedder()->get_popup_base_transform_native() * popup_base_transform;
3135 }
3136 return popup_base_transform;
3137 }
3138
3139 Transform2D Window::get_popup_base_transform() const {
3140 ERR_READ_THREAD_GUARD_V(Transform2D());
3141 if (is_embedding_subwindows()) {
3142 return Transform2D();
3143 }
3144 Transform2D popup_base_transform;
3145 popup_base_transform.set_origin(get_position());
3146 popup_base_transform *= get_final_transform();
3147 if (get_embedder()) {
3148 return get_embedder()->get_popup_base_transform() * popup_base_transform;
3149 }
3150 return popup_base_transform;
3151 }
3152
3153 Viewport *Window::get_section_root_viewport() const {
3154 if (get_embedder()) {
3155 return get_embedder()->get_section_root_viewport();
3156 }
3157 if (is_inside_tree()) {
3158 // Native window.
3159 return SceneTree::get_singleton()->get_root();
3160 }
3161 Window *vp = const_cast<Window *>(this);
3162 return vp;
3163 }
3164
3165 bool Window::is_attached_in_viewport() const {
3166 return get_embedder();
3167 }
3168
3169 void Window::_update_mouse_over(Vector2 p_pos) {
3170 if (!mouse_in_window) {
3171 if (is_embedded()) {
3172 mouse_in_window = true;
3173 _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
3174 }
3175 }
3176
3177 bool new_in = get_visible_rect().has_point(p_pos);
3178 if (new_in == gui.mouse_in_viewport) {
3179 if (new_in) {
3180 Viewport::_update_mouse_over(p_pos);
3181 }
3182 return;
3183 }
3184
3185 if (new_in) {
3186 notification(NOTIFICATION_VP_MOUSE_ENTER);
3187 Viewport::_update_mouse_over(p_pos);
3188 } else {
3189 Viewport::_mouse_leave_viewport();
3190 }
3191 }
3192
3193 void Window::_mouse_leave_viewport() {
3194 Viewport::_mouse_leave_viewport();
3195 if (is_embedded()) {
3196 mouse_in_window = false;
3197 _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
3198 }
3199 }
3200
3201 void Window::_update_displayed_title() {
3202 displayed_title = atr(title);
3203
3204 #ifdef DEBUG_ENABLED
3205 if (window_id == DisplayServer::MAIN_WINDOW_ID && !Engine::get_singleton()->is_project_manager_hint()) {
3206 // Append a suffix to the window title to denote that the project is running
3207 // from a debug build (including the editor, excluding the project manager).
3208 // Since this results in lower performance, this should be clearly presented
3209 // to the user.
3210 displayed_title = vformat("%s (DEBUG)", displayed_title);
3211 }
3212 #endif
3213
3214 if (embedder) {
3215 embedder->_sub_window_update(this);
3216 } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
3217 DisplayServer::get_singleton()->window_set_title(displayed_title, window_id);
3218 if (keep_title_visible) {
3219 Size2i title_size = DisplayServer::get_singleton()->window_get_title_size(displayed_title, window_id);
3220 Size2i size_limit = get_clamped_minimum_size();
3221 if (title_size.x > size_limit.x || title_size.y > size_limit.y) {
3222 _update_window_size();
3223 }
3224 }
3225 }
3226
3227 #ifdef DEBUG_ENABLED
3228 if (EngineDebugger::get_singleton() && window_id == DisplayServer::MAIN_WINDOW_ID && !Engine::get_singleton()->is_project_manager_hint()) {
3229 Array arr = { displayed_title };
3230 EngineDebugger::get_singleton()->send_message("window:title", arr);
3231 }
3232 #endif
3233
3234 queue_accessibility_update();
3235 }
3236
3237 void Window::_bind_methods() {
3238 ClassDB::bind_method(D_METHOD("set_title", "title"), &Window::set_title);
3239 ClassDB::bind_method(D_METHOD("get_title"), &Window::get_title);
3240
3241 ClassDB::bind_method(D_METHOD("set_initial_position", "initial_position"), &Window::set_initial_position);
3242 ClassDB::bind_method(D_METHOD("get_initial_position"), &Window::get_initial_position);
3243
3244 ClassDB::bind_method(D_METHOD("set_current_screen", "index"), &Window::set_current_screen);
3245 ClassDB::bind_method(D_METHOD("get_current_screen"), &Window::get_current_screen);
3246
3247 ClassDB::bind_method(D_METHOD("set_position", "position"), &Window::set_position);
3248 ClassDB::bind_method(D_METHOD("get_position"), &Window::get_position);
3249 ClassDB::bind_method(D_METHOD("move_to_center"), &Window::move_to_center);
3250
3251 ClassDB::bind_method(D_METHOD("set_size", "size"), &Window::set_size);
3252 ClassDB::bind_method(D_METHOD("get_size"), &Window::get_size);
3253 ClassDB::bind_method(D_METHOD("reset_size"), &Window::reset_size);
3254
3255 ClassDB::bind_method(D_METHOD("get_position_with_decorations"), &Window::get_position_with_decorations);
3256 ClassDB::bind_method(D_METHOD("get_size_with_decorations"), &Window::get_size_with_decorations);
3257
3258 ClassDB::bind_method(D_METHOD("set_max_size", "max_size"), &Window::set_max_size);
3259 ClassDB::bind_method(D_METHOD("get_max_size"), &Window::get_max_size);
3260
3261 ClassDB::bind_method(D_METHOD("set_min_size", "min_size"), &Window::set_min_size);
3262 ClassDB::bind_method(D_METHOD("get_min_size"), &Window::get_min_size);
3263
3264 ClassDB::bind_method(D_METHOD("set_mode", "mode"), &Window::set_mode);
3265 ClassDB::bind_method(D_METHOD("get_mode"), &Window::get_mode);
3266
3267 ClassDB::bind_method(D_METHOD("set_flag", "flag", "enabled"), &Window::set_flag);
3268 ClassDB::bind_method(D_METHOD("get_flag", "flag"), &Window::get_flag);
3269
3270 ClassDB::bind_method(D_METHOD("is_maximize_allowed"), &Window::is_maximize_allowed);
3271
3272 ClassDB::bind_method(D_METHOD("request_attention"), &Window::request_attention);
3273
3274 #ifndef DISABLE_DEPRECATED
3275 ClassDB::bind_method(D_METHOD("move_to_foreground"), &Window::move_to_foreground);
3276 #endif // DISABLE_DEPRECATED
3277
3278 ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Window::set_visible);
3279 ClassDB::bind_method(D_METHOD("is_visible"), &Window::is_visible);
3280
3281 ClassDB::bind_method(D_METHOD("hide"), &Window::hide);
3282 ClassDB::bind_method(D_METHOD("show"), &Window::show);
3283
3284 ClassDB::bind_method(D_METHOD("set_transient", "transient"), &Window::set_transient);
3285 ClassDB::bind_method(D_METHOD("is_transient"), &Window::is_transient);
3286
3287 ClassDB::bind_method(D_METHOD("set_transient_to_focused", "enable"), &Window::set_transient_to_focused);
3288 ClassDB::bind_method(D_METHOD("is_transient_to_focused"), &Window::is_transient_to_focused);
3289
3290 ClassDB::bind_method(D_METHOD("set_exclusive", "exclusive"), &Window::set_exclusive);
3291 ClassDB::bind_method(D_METHOD("is_exclusive"), &Window::is_exclusive);
3292
3293 ClassDB::bind_method(D_METHOD("set_unparent_when_invisible", "unparent"), &Window::set_unparent_when_invisible);
3294
3295 ClassDB::bind_method(D_METHOD("can_draw"), &Window::can_draw);
3296 ClassDB::bind_method(D_METHOD("has_focus"), &Window::has_focus);
3297 ClassDB::bind_method(D_METHOD("grab_focus"), &Window::grab_focus);
3298
3299 ClassDB::bind_method(D_METHOD("start_drag"), &Window::start_drag);
3300 ClassDB::bind_method(D_METHOD("start_resize", "edge"), &Window::start_resize);
3301
3302 ClassDB::bind_method(D_METHOD("set_ime_active", "active"), &Window::set_ime_active);
3303 ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &Window::set_ime_position);
3304
3305 ClassDB::bind_method(D_METHOD("is_embedded"), &Window::is_embedded);
3306
3307 ClassDB::bind_method(D_METHOD("get_contents_minimum_size"), &Window::get_contents_minimum_size);
3308
3309 ClassDB::bind_method(D_METHOD("set_force_native", "force_native"), &Window::set_force_native);
3310 ClassDB::bind_method(D_METHOD("get_force_native"), &Window::get_force_native);
3311
3312 ClassDB::bind_method(D_METHOD("set_content_scale_size", "size"), &Window::set_content_scale_size);
3313 ClassDB::bind_method(D_METHOD("get_content_scale_size"), &Window::get_content_scale_size);
3314
3315 ClassDB::bind_method(D_METHOD("set_content_scale_mode", "mode"), &Window::set_content_scale_mode);
3316 ClassDB::bind_method(D_METHOD("get_content_scale_mode"), &Window::get_content_scale_mode);
3317
3318 ClassDB::bind_method(D_METHOD("set_content_scale_aspect", "aspect"), &Window::set_content_scale_aspect);
3319 ClassDB::bind_method(D_METHOD("get_content_scale_aspect"), &Window::get_content_scale_aspect);
3320
3321 ClassDB::bind_method(D_METHOD("set_content_scale_stretch", "stretch"), &Window::set_content_scale_stretch);
3322 ClassDB::bind_method(D_METHOD("get_content_scale_stretch"), &Window::get_content_scale_stretch);
3323
3324 ClassDB::bind_method(D_METHOD("set_nonclient_area", "area"), &Window::set_nonclient_area);
3325 ClassDB::bind_method(D_METHOD("get_nonclient_area"), &Window::get_nonclient_area);
3326
3327 ClassDB::bind_method(D_METHOD("set_keep_title_visible", "title_visible"), &Window::set_keep_title_visible);
3328 ClassDB::bind_method(D_METHOD("get_keep_title_visible"), &Window::get_keep_title_visible);
3329
3330 ClassDB::bind_method(D_METHOD("set_content_scale_factor", "factor"), &Window::set_content_scale_factor);
3331 ClassDB::bind_method(D_METHOD("get_content_scale_factor"), &Window::get_content_scale_factor);
3332
3333 ClassDB::bind_method(D_METHOD("set_mouse_passthrough_polygon", "polygon"), &Window::set_mouse_passthrough_polygon);
3334 ClassDB::bind_method(D_METHOD("get_mouse_passthrough_polygon"), &Window::get_mouse_passthrough_polygon);
3335
3336 ClassDB::bind_method(D_METHOD("set_wrap_controls", "enable"), &Window::set_wrap_controls);
3337 ClassDB::bind_method(D_METHOD("is_wrapping_controls"), &Window::is_wrapping_controls);
3338 ClassDB::bind_method(D_METHOD("child_controls_changed"), &Window::child_controls_changed);
3339
3340 ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Window::set_theme);
3341 ClassDB::bind_method(D_METHOD("get_theme"), &Window::get_theme);
3342
3343 ClassDB::bind_method(D_METHOD("set_theme_type_variation", "theme_type"), &Window::set_theme_type_variation);
3344 ClassDB::bind_method(D_METHOD("get_theme_type_variation"), &Window::get_theme_type_variation);
3345
3346 ClassDB::bind_method(D_METHOD("begin_bulk_theme_override"), &Window::begin_bulk_theme_override);
3347 ClassDB::bind_method(D_METHOD("end_bulk_theme_override"), &Window::end_bulk_theme_override);
3348
3349 ClassDB::bind_method(D_METHOD("add_theme_icon_override", "name", "texture"), &Window::add_theme_icon_override);
3350 ClassDB::bind_method(D_METHOD("add_theme_stylebox_override", "name", "stylebox"), &Window::add_theme_style_override);
3351 ClassDB::bind_method(D_METHOD("add_theme_font_override", "name", "font"), &Window::add_theme_font_override);
3352 ClassDB::bind_method(D_METHOD("add_theme_font_size_override", "name", "font_size"), &Window::add_theme_font_size_override);
3353 ClassDB::bind_method(D_METHOD("add_theme_color_override", "name", "color"), &Window::add_theme_color_override);
3354 ClassDB::bind_method(D_METHOD("add_theme_constant_override", "name", "constant"), &Window::add_theme_constant_override);
3355
3356 ClassDB::bind_method(D_METHOD("remove_theme_icon_override", "name"), &Window::remove_theme_icon_override);
3357 ClassDB::bind_method(D_METHOD("remove_theme_stylebox_override", "name"), &Window::remove_theme_style_override);
3358 ClassDB::bind_method(D_METHOD("remove_theme_font_override", "name"), &Window::remove_theme_font_override);
3359 ClassDB::bind_method(D_METHOD("remove_theme_font_size_override", "name"), &Window::remove_theme_font_size_override);
3360 ClassDB::bind_method(D_METHOD("remove_theme_color_override", "name"), &Window::remove_theme_color_override);
3361 ClassDB::bind_method(D_METHOD("remove_theme_constant_override", "name"), &Window::remove_theme_constant_override);
3362
3363 ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "theme_type"), &Window::get_theme_icon, DEFVAL(StringName()));
3364 ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "theme_type"), &Window::get_theme_stylebox, DEFVAL(StringName()));
3365 ClassDB::bind_method(D_METHOD("get_theme_font", "name", "theme_type"), &Window::get_theme_font, DEFVAL(StringName()));
3366 ClassDB::bind_method(D_METHOD("get_theme_font_size", "name", "theme_type"), &Window::get_theme_font_size, DEFVAL(StringName()));
3367 ClassDB::bind_method(D_METHOD("get_theme_color", "name", "theme_type"), &Window::get_theme_color, DEFVAL(StringName()));
3368 ClassDB::bind_method(D_METHOD("get_theme_constant", "name", "theme_type"), &Window::get_theme_constant, DEFVAL(StringName()));
3369
3370 ClassDB::bind_method(D_METHOD("has_theme_icon_override", "name"), &Window::has_theme_icon_override);
3371 ClassDB::bind_method(D_METHOD("has_theme_stylebox_override", "name"), &Window::has_theme_stylebox_override);
3372 ClassDB::bind_method(D_METHOD("has_theme_font_override", "name"), &Window::has_theme_font_override);
3373 ClassDB::bind_method(D_METHOD("has_theme_font_size_override", "name"), &Window::has_theme_font_size_override);
3374 ClassDB::bind_method(D_METHOD("has_theme_color_override", "name"), &Window::has_theme_color_override);
3375 ClassDB::bind_method(D_METHOD("has_theme_constant_override", "name"), &Window::has_theme_constant_override);
3376
3377 ClassDB::bind_method(D_METHOD("has_theme_icon", "name", "theme_type"), &Window::has_theme_icon, DEFVAL(StringName()));
3378 ClassDB::bind_method(D_METHOD("has_theme_stylebox", "name", "theme_type"), &Window::has_theme_stylebox, DEFVAL(StringName()));
3379 ClassDB::bind_method(D_METHOD("has_theme_font", "name", "theme_type"), &Window::has_theme_font, DEFVAL(StringName()));
3380 ClassDB::bind_method(D_METHOD("has_theme_font_size", "name", "theme_type"), &Window::has_theme_font_size, DEFVAL(StringName()));
3381 ClassDB::bind_method(D_METHOD("has_theme_color", "name", "theme_type"), &Window::has_theme_color, DEFVAL(StringName()));
3382 ClassDB::bind_method(D_METHOD("has_theme_constant", "name", "theme_type"), &Window::has_theme_constant, DEFVAL(StringName()));
3383
3384 ClassDB::bind_method(D_METHOD("get_theme_default_base_scale"), &Window::get_theme_default_base_scale);
3385 ClassDB::bind_method(D_METHOD("get_theme_default_font"), &Window::get_theme_default_font);
3386 ClassDB::bind_method(D_METHOD("get_theme_default_font_size"), &Window::get_theme_default_font_size);
3387
3388 ClassDB::bind_method(D_METHOD("get_window_id"), &Window::get_window_id);
3389
3390 ClassDB::bind_method(D_METHOD("set_accessibility_name", "name"), &Window::set_accessibility_name);
3391 ClassDB::bind_method(D_METHOD("get_accessibility_name"), &Window::get_accessibility_name);
3392 ClassDB::bind_method(D_METHOD("set_accessibility_description", "description"), &Window::set_accessibility_description);
3393 ClassDB::bind_method(D_METHOD("get_accessibility_description"), &Window::get_accessibility_description);
3394
3395 ClassDB::bind_static_method("Window", D_METHOD("get_focused_window"), &Window::get_focused_window);
3396
3397 ClassDB::bind_method(D_METHOD("set_layout_direction", "direction"), &Window::set_layout_direction);
3398 ClassDB::bind_method(D_METHOD("get_layout_direction"), &Window::get_layout_direction);
3399 ClassDB::bind_method(D_METHOD("is_layout_rtl"), &Window::is_layout_rtl);
3400
3401 #ifndef DISABLE_DEPRECATED
3402 ClassDB::bind_method(D_METHOD("set_auto_translate", "enable"), &Window::set_auto_translate);
3403 ClassDB::bind_method(D_METHOD("is_auto_translating"), &Window::is_auto_translating);
3404
3405 ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &Window::set_use_font_oversampling);
3406 ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &Window::is_using_font_oversampling);
3407 #endif
3408
3409 ClassDB::bind_method(D_METHOD("popup", "rect"), &Window::popup, DEFVAL(Rect2i()));
3410 ClassDB::bind_method(D_METHOD("popup_on_parent", "parent_rect"), &Window::popup_on_parent);
3411 ClassDB::bind_method(D_METHOD("popup_centered", "minsize"), &Window::popup_centered, DEFVAL(Size2i()));
3412 ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Window::popup_centered_ratio, DEFVAL(0.8));
3413 ClassDB::bind_method(D_METHOD("popup_centered_clamped", "minsize", "fallback_ratio"), &Window::popup_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
3414
3415 ClassDB::bind_method(D_METHOD("popup_exclusive", "from_node", "rect"), &Window::popup_exclusive, DEFVAL(Rect2i()));
3416 ClassDB::bind_method(D_METHOD("popup_exclusive_on_parent", "from_node", "parent_rect"), &Window::popup_exclusive_on_parent);
3417 ClassDB::bind_method(D_METHOD("popup_exclusive_centered", "from_node", "minsize"), &Window::popup_exclusive_centered, DEFVAL(Size2i()));
3418 ClassDB::bind_method(D_METHOD("popup_exclusive_centered_ratio", "from_node", "ratio"), &Window::popup_exclusive_centered_ratio, DEFVAL(0.8));
3419 ClassDB::bind_method(D_METHOD("popup_exclusive_centered_clamped", "from_node", "minsize", "fallback_ratio"), &Window::popup_exclusive_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
3420
3421 // Keep the enum values in sync with the `Mode` enum.
3422 ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Windowed,Minimized,Maximized,Fullscreen,Exclusive Fullscreen"), "set_mode", "get_mode");
3423
3424 ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
3425
3426 // Keep the enum values in sync with the `WindowInitialPosition` enum.
3427 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");
3428 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
3429 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
3430 ADD_PROPERTY(PropertyInfo(Variant::INT, "current_screen", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), "set_current_screen", "get_current_screen");
3431 ADD_PROPERTY(PropertyInfo(Variant::RECT2I, "nonclient_area", PROPERTY_HINT_NONE, ""), "set_nonclient_area", "get_nonclient_area");
3432
3433 ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "mouse_passthrough_polygon"), "set_mouse_passthrough_polygon", "get_mouse_passthrough_polygon");
3434
3435 ADD_GROUP("Flags", "");
3436 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
3437 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_controls"), "set_wrap_controls", "is_wrapping_controls");
3438 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient"), "set_transient", "is_transient");
3439 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient_to_focused"), "set_transient_to_focused", "is_transient_to_focused");
3440 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclusive"), "set_exclusive", "is_exclusive");
3441 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unresizable"), "set_flag", "get_flag", FLAG_RESIZE_DISABLED);
3442 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "borderless"), "set_flag", "get_flag", FLAG_BORDERLESS);
3443 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "always_on_top"), "set_flag", "get_flag", FLAG_ALWAYS_ON_TOP);
3444 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_flag", "get_flag", FLAG_TRANSPARENT);
3445 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unfocusable"), "set_flag", "get_flag", FLAG_NO_FOCUS);
3446 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "popup_window"), "set_flag", "get_flag", FLAG_POPUP);
3447 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "extend_to_title"), "set_flag", "get_flag", FLAG_EXTEND_TO_TITLE);
3448 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "mouse_passthrough"), "set_flag", "get_flag", FLAG_MOUSE_PASSTHROUGH);
3449 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "sharp_corners"), "set_flag", "get_flag", FLAG_SHARP_CORNERS);
3450 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "exclude_from_capture"), "set_flag", "get_flag", FLAG_EXCLUDE_FROM_CAPTURE);
3451 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "popup_wm_hint"), "set_flag", "get_flag", FLAG_POPUP_WM_HINT);
3452 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "minimize_disabled"), "set_flag", "get_flag", FLAG_MINIMIZE_DISABLED);
3453 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "maximize_disabled"), "set_flag", "get_flag", FLAG_MAXIMIZE_DISABLED);
3454 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_native"), "set_force_native", "get_force_native");
3455
3456 ADD_GROUP("Limits", "");
3457 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "min_size", PROPERTY_HINT_NONE, "suffix:px"), "set_min_size", "get_min_size");
3458 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "max_size", PROPERTY_HINT_NONE, "suffix:px"), "set_max_size", "get_max_size");
3459 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_title_visible"), "set_keep_title_visible", "get_keep_title_visible");
3460
3461 ADD_GROUP("Content Scale", "content_scale_");
3462 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "content_scale_size"), "set_content_scale_size", "get_content_scale_size");
3463 ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_mode", PROPERTY_HINT_ENUM, "Disabled,Canvas Items,Viewport"), "set_content_scale_mode", "get_content_scale_mode");
3464 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");
3465 ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_stretch", PROPERTY_HINT_ENUM, "Fractional,Integer"), "set_content_scale_stretch", "get_content_scale_stretch");
3466 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");
3467
3468 #ifndef DISABLE_DEPRECATED
3469 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_auto_translate", "is_auto_translating");
3470 #endif
3471
3472 ADD_GROUP("Accessibility", "accessibility_");
3473 ADD_PROPERTY(PropertyInfo(Variant::STRING, "accessibility_name"), "set_accessibility_name", "get_accessibility_name");
3474 ADD_PROPERTY(PropertyInfo(Variant::STRING, "accessibility_description"), "set_accessibility_description", "get_accessibility_description");
3475
3476 ADD_GROUP("Theme", "theme_");
3477 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
3478 ADD_PROPERTY(PropertyInfo(Variant::STRING, "theme_type_variation", PROPERTY_HINT_ENUM_SUGGESTION), "set_theme_type_variation", "get_theme_type_variation");
3479
3480 ADD_SIGNAL(MethodInfo("window_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
3481 ADD_SIGNAL(MethodInfo("nonclient_window_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
3482 ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
3483 ADD_SIGNAL(MethodInfo("mouse_entered"));
3484 ADD_SIGNAL(MethodInfo("mouse_exited"));
3485 ADD_SIGNAL(MethodInfo("focus_entered"));
3486 ADD_SIGNAL(MethodInfo("focus_exited"));
3487 ADD_SIGNAL(MethodInfo("close_requested"));
3488 ADD_SIGNAL(MethodInfo("go_back_requested"));
3489 ADD_SIGNAL(MethodInfo("visibility_changed"));
3490 ADD_SIGNAL(MethodInfo("about_to_popup"));
3491 ADD_SIGNAL(MethodInfo("theme_changed"));
3492 ADD_SIGNAL(MethodInfo("dpi_changed"));
3493 ADD_SIGNAL(MethodInfo("titlebar_changed"));
3494 ADD_SIGNAL(MethodInfo("title_changed"));
3495
3496 BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
3497 BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);
3498
3499 BIND_ENUM_CONSTANT(MODE_WINDOWED);
3500 BIND_ENUM_CONSTANT(MODE_MINIMIZED);
3501 BIND_ENUM_CONSTANT(MODE_MAXIMIZED);
3502 BIND_ENUM_CONSTANT(MODE_FULLSCREEN);
3503 BIND_ENUM_CONSTANT(MODE_EXCLUSIVE_FULLSCREEN);
3504
3505 BIND_ENUM_CONSTANT(FLAG_RESIZE_DISABLED);
3506 BIND_ENUM_CONSTANT(FLAG_BORDERLESS);
3507 BIND_ENUM_CONSTANT(FLAG_ALWAYS_ON_TOP);
3508 BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
3509 BIND_ENUM_CONSTANT(FLAG_NO_FOCUS);
3510 BIND_ENUM_CONSTANT(FLAG_POPUP);
3511 BIND_ENUM_CONSTANT(FLAG_EXTEND_TO_TITLE);
3512 BIND_ENUM_CONSTANT(FLAG_MOUSE_PASSTHROUGH);
3513 BIND_ENUM_CONSTANT(FLAG_SHARP_CORNERS);
3514 BIND_ENUM_CONSTANT(FLAG_EXCLUDE_FROM_CAPTURE);
3515 BIND_ENUM_CONSTANT(FLAG_POPUP_WM_HINT);
3516 BIND_ENUM_CONSTANT(FLAG_MINIMIZE_DISABLED);
3517 BIND_ENUM_CONSTANT(FLAG_MAXIMIZE_DISABLED);
3518 BIND_ENUM_CONSTANT(FLAG_MAX);
3519
3520 BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_DISABLED);
3521 BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_CANVAS_ITEMS);
3522 BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_VIEWPORT);
3523
3524 BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_IGNORE);
3525 BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP);
3526 BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_WIDTH);
3527 BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_HEIGHT);
3528 BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_EXPAND);
3529
3530 BIND_ENUM_CONSTANT(CONTENT_SCALE_STRETCH_FRACTIONAL);
3531 BIND_ENUM_CONSTANT(CONTENT_SCALE_STRETCH_INTEGER);
3532
3533 BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_INHERITED);
3534 BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_APPLICATION_LOCALE);
3535 BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LTR);
3536 BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_RTL);
3537 BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_SYSTEM_LOCALE);
3538 BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_MAX);
3539 #ifndef DISABLE_DEPRECATED
3540 BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LOCALE);
3541 #endif // DISABLE_DEPRECATED
3542
3543 BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_ABSOLUTE);
3544 BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN);
3545 BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN);
3546 BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN);
3547 BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS);
3548 BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS);
3549
3550 GDVIRTUAL_BIND(_get_contents_minimum_size);
3551
3552 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Window, embedded_border);
3553 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Window, embedded_unfocused_border);
3554
3555 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Window, title_font);
3556 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Window, title_font_size);
3557 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Window, title_color);
3558 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, title_height);
3559 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Window, title_outline_modulate);
3560 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, title_outline_size);
3561
3562 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Window, close);
3563 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Window, close_pressed);
3564 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, close_h_offset);
3565 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, close_v_offset);
3566
3567 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, resize_margin);
3568 }
3569
3570 Window::Window() {
3571 RenderingServer *rendering_server = RenderingServer::get_singleton();
3572 if (rendering_server) {
3573 max_size = rendering_server->get_maximum_viewport_size();
3574 max_size_used = max_size; // Update max_size_used.
3575 }
3576
3577 theme_owner = memnew(ThemeOwner(this));
3578 RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
3579 }
3580
3581 Window::~Window() {
3582 if (focused_window == this) {
3583 focused_window = nullptr;
3584 }
3585 memdelete(theme_owner);
3586
3587 // Resources need to be disconnected.
3588 for (KeyValue<StringName, Ref<Texture2D>> &E : theme_icon_override) {
3589 E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
3590 }
3591 for (KeyValue<StringName, Ref<StyleBox>> &E : theme_style_override) {
3592 E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
3593 }
3594 for (KeyValue<StringName, Ref<Font>> &E : theme_font_override) {
3595 E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
3596 }
3597
3598 // Then override maps can be simply cleared.
3599 theme_icon_override.clear();
3600 theme_style_override.clear();
3601 theme_font_override.clear();
3602 theme_font_size_override.clear();
3603 theme_color_override.clear();
3604 theme_constant_override.clear();
3605 }