From cfdb9688481657f0b395dddfd7d91620979df85d Mon Sep 17 00:00:00 2001
From: bruvzg <7645683+bruvzg@users.noreply.github.com>
Date: Mon, 4 Mar 2024 20:21:10 +0200
Subject: [PATCH] [Window] Allow to override viewport and project settings and
force use of native window.
---
doc/classes/Window.xml | 3 +++
scene/main/window.cpp | 22 ++++++++++++++++++++++
scene/main/window.h | 4 ++++
3 files changed, 29 insertions(+)
diff --git a/doc/classes/Window.xml b/doc/classes/Window.xml
index 2acf0532e82..9c0e8011dc2 100644
--- a/doc/classes/Window.xml
+++ b/doc/classes/Window.xml
@@ -590,6 +590,9 @@
[b]Note:[/b] This property is implemented only on macOS.
[b]Note:[/b] This property only works with native windows.
+
+ If [code]true[/code], native window will be used regardless of parent viewport and project settings.
+
Specifies the initial type of position for the [Window]. See [enum WindowInitialPosition] constants.
diff --git a/scene/main/window.cpp b/scene/main/window.cpp
index 9c2509404c7..6632dd90339 100644
--- a/scene/main/window.cpp
+++ b/scene/main/window.cpp
@@ -1209,8 +1209,26 @@ void Window::_update_window_callbacks() {
DisplayServer::get_singleton()->window_set_drop_files_callback(callable_mp(this, &Window::_window_drop_files), window_id);
}
+void Window::set_force_native(bool p_force_native) {
+ if (force_native == p_force_native) {
+ return;
+ }
+ force_native = p_force_native;
+ if (is_visible()) {
+ WARN_PRINT("Can't change \"force_native\" while a window is displayed. Consider hiding window before changing this value.");
+ }
+}
+
+bool Window::get_force_native() const {
+ return force_native;
+}
+
Viewport *Window::get_embedder() const {
ERR_READ_THREAD_GUARD_V(nullptr);
+ if (force_native && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SUBWINDOWS)) {
+ return nullptr;
+ }
+
Viewport *vp = get_parent_viewport();
while (vp) {
@@ -2802,6 +2820,9 @@ void Window::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_contents_minimum_size"), &Window::get_contents_minimum_size);
+ ClassDB::bind_method(D_METHOD("set_force_native", "force_native"), &Window::set_force_native);
+ ClassDB::bind_method(D_METHOD("get_force_native"), &Window::get_force_native);
+
ClassDB::bind_method(D_METHOD("set_content_scale_size", "size"), &Window::set_content_scale_size);
ClassDB::bind_method(D_METHOD("get_content_scale_size"), &Window::get_content_scale_size);
@@ -2926,6 +2947,7 @@ void Window::_bind_methods() {
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "popup_window"), "set_flag", "get_flag", FLAG_POPUP);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "extend_to_title"), "set_flag", "get_flag", FLAG_EXTEND_TO_TITLE);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "mouse_passthrough"), "set_flag", "get_flag", FLAG_MOUSE_PASSTHROUGH);
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_native"), "set_force_native", "get_force_native");
ADD_GROUP("Limits", "");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "min_size", PROPERTY_HINT_NONE, "suffix:px"), "set_min_size", "get_min_size");
diff --git a/scene/main/window.h b/scene/main/window.h
index e37a98bd2dc..ffcf50ccdd5 100644
--- a/scene/main/window.h
+++ b/scene/main/window.h
@@ -122,6 +122,7 @@ private:
bool visible = true;
bool focused = false;
WindowInitialPosition initial_position = WINDOW_INITIAL_POSITION_ABSOLUTE;
+ bool force_native = false;
bool use_font_oversampling = false;
bool transient = false;
@@ -273,6 +274,9 @@ public:
void set_initial_position(WindowInitialPosition p_initial_position);
WindowInitialPosition get_initial_position() const;
+ void set_force_native(bool p_force_native);
+ bool get_force_native() const;
+
void set_current_screen(int p_screen);
int get_current_screen() const;