From defcd0c57e8b90177639963e958ea91be4c511bc Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Mon, 21 Feb 2022 10:17:28 +0200 Subject: [PATCH] Avoid setting sub-window or started project window positions, if it's impossible to get screen rect. --- editor/editor_run.cpp | 126 ++++++++++++++++++++----------------- editor/editor_settings.cpp | 6 ++ editor/project_manager.cpp | 16 +++-- scene/main/window.cpp | 15 +++-- 4 files changed, 93 insertions(+), 70 deletions(-) diff --git a/editor/editor_run.cpp b/editor/editor_run.cpp index 574acdff1c0..6a2ff50ee0d 100644 --- a/editor/editor_run.cpp +++ b/editor/editor_run.cpp @@ -98,67 +98,79 @@ Error EditorRun::run(const String &p_scene) { screen_rect.position = DisplayServer::get_singleton()->screen_get_position(screen); screen_rect.size = DisplayServer::get_singleton()->screen_get_size(screen); - Size2 window_size; - window_size.x = ProjectSettings::get_singleton()->get("display/window/size/viewport_width"); - window_size.y = ProjectSettings::get_singleton()->get("display/window/size/viewport_height"); - - Size2 desired_size; - desired_size.x = ProjectSettings::get_singleton()->get("display/window/size/window_width_override"); - desired_size.y = ProjectSettings::get_singleton()->get("display/window/size/window_height_override"); - if (desired_size.x > 0 && desired_size.y > 0) { - window_size = desired_size; - } - int window_placement = EditorSettings::get_singleton()->get("run/window_placement/rect"); - if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_HIDPI)) { - bool hidpi_proj = ProjectSettings::get_singleton()->get("display/window/dpi/allow_hidpi"); - int display_scale = 1; + if (screen_rect != Rect2()) { + Size2 window_size; + window_size.x = ProjectSettings::get_singleton()->get("display/window/size/viewport_width"); + window_size.y = ProjectSettings::get_singleton()->get("display/window/size/viewport_height"); - if (OS::get_singleton()->is_hidpi_allowed()) { - if (hidpi_proj) { - display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale. - } else { - display_scale = DisplayServer::get_singleton()->screen_get_max_scale(); // Editor is in hiDPI mode, project is not, scale down. - } - } else { - if (hidpi_proj) { - display_scale = (1.f / DisplayServer::get_singleton()->screen_get_max_scale()); // Editor is not in hiDPI mode, project is, scale up. - } else { - display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale. - } + Size2 desired_size; + desired_size.x = ProjectSettings::get_singleton()->get("display/window/size/window_width_override"); + desired_size.y = ProjectSettings::get_singleton()->get("display/window/size/window_height_override"); + if (desired_size.x > 0 && desired_size.y > 0) { + window_size = desired_size; } - screen_rect.position /= display_scale; - screen_rect.size /= display_scale; - } - switch (window_placement) { - case 0: { // top left - args.push_back("--position"); - args.push_back(itos(screen_rect.position.x) + "," + itos(screen_rect.position.y)); - } break; - case 1: { // centered - Vector2 pos = (screen_rect.position) + ((screen_rect.size - window_size) / 2).floor(); - args.push_back("--position"); - args.push_back(itos(pos.x) + "," + itos(pos.y)); - } break; - case 2: { // custom pos - Vector2 pos = EditorSettings::get_singleton()->get("run/window_placement/rect_custom_position"); - pos += screen_rect.position; - args.push_back("--position"); - args.push_back(itos(pos.x) + "," + itos(pos.y)); - } break; - case 3: { // force maximized - Vector2 pos = screen_rect.position; - args.push_back("--position"); - args.push_back(itos(pos.x) + "," + itos(pos.y)); - args.push_back("--maximized"); - } break; - case 4: { // force fullscreen - Vector2 pos = screen_rect.position; - args.push_back("--position"); - args.push_back(itos(pos.x) + "," + itos(pos.y)); - args.push_back("--fullscreen"); - } break; + if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_HIDPI)) { + bool hidpi_proj = ProjectSettings::get_singleton()->get("display/window/dpi/allow_hidpi"); + int display_scale = 1; + + if (OS::get_singleton()->is_hidpi_allowed()) { + if (hidpi_proj) { + display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale. + } else { + display_scale = DisplayServer::get_singleton()->screen_get_max_scale(); // Editor is in hiDPI mode, project is not, scale down. + } + } else { + if (hidpi_proj) { + display_scale = (1.f / DisplayServer::get_singleton()->screen_get_max_scale()); // Editor is not in hiDPI mode, project is, scale up. + } else { + display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale. + } + } + screen_rect.position /= display_scale; + screen_rect.size /= display_scale; + } + + switch (window_placement) { + case 0: { // top left + args.push_back("--position"); + args.push_back(itos(screen_rect.position.x) + "," + itos(screen_rect.position.y)); + } break; + case 1: { // centered + Vector2 pos = (screen_rect.position) + ((screen_rect.size - window_size) / 2).floor(); + args.push_back("--position"); + args.push_back(itos(pos.x) + "," + itos(pos.y)); + } break; + case 2: { // custom pos + Vector2 pos = EditorSettings::get_singleton()->get("run/window_placement/rect_custom_position"); + pos += screen_rect.position; + args.push_back("--position"); + args.push_back(itos(pos.x) + "," + itos(pos.y)); + } break; + case 3: { // force maximized + Vector2 pos = screen_rect.position; + args.push_back("--position"); + args.push_back(itos(pos.x) + "," + itos(pos.y)); + args.push_back("--maximized"); + } break; + case 4: { // force fullscreen + Vector2 pos = screen_rect.position; + args.push_back("--position"); + args.push_back(itos(pos.x) + "," + itos(pos.y)); + args.push_back("--fullscreen"); + } break; + } + } else { + // Unable to get screen info, skip setting position. + switch (window_placement) { + case 3: { // force maximized + args.push_back("--maximized"); + } break; + case 4: { // force fullscreen + args.push_back("--fullscreen"); + } break; + } } List breakpoints; diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 48f04694aa5..bdabff20f99 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -1365,6 +1365,12 @@ float EditorSettings::get_auto_display_scale() const { return DisplayServer::get_singleton()->screen_get_max_scale(); #else const int screen = DisplayServer::get_singleton()->window_get_current_screen(); + + if (DisplayServer::get_singleton()->screen_get_size(screen) == Vector2i()) { + // Invalid screen size, skip. + return 1.0; + } + // Use the smallest dimension to use a correct display scale on portrait displays. const int smallest_dimension = MIN(DisplayServer::get_singleton()->screen_get_size(screen).x, DisplayServer::get_singleton()->screen_get_size(screen).y); if (DisplayServer::get_singleton()->screen_get_dpi(screen) >= 192 && smallest_dimension >= 1400) { diff --git a/editor/project_manager.cpp b/editor/project_manager.cpp index 4ca0f18f0e6..da57efd1bb0 100644 --- a/editor/project_manager.cpp +++ b/editor/project_manager.cpp @@ -2877,17 +2877,15 @@ ProjectManager::ProjectManager() { Vector2i screen_size = DisplayServer::get_singleton()->screen_get_size(); Vector2i screen_position = DisplayServer::get_singleton()->screen_get_position(); - // Consider the editor display scale. - window_size.x = round((float)window_size.x * scale_factor); - window_size.y = round((float)window_size.y * scale_factor); - - // Make the window centered on the screen. - Vector2i window_position; - window_position.x = screen_position.x + (screen_size.x - window_size.x) / 2; - window_position.y = screen_position.y + (screen_size.y - window_size.y) / 2; + window_size *= scale_factor; DisplayServer::get_singleton()->window_set_size(window_size); - DisplayServer::get_singleton()->window_set_position(window_position); + if (screen_size != Vector2i()) { + Vector2i window_position; + window_position.x = screen_position.x + (screen_size.x - window_size.x) / 2; + window_position.y = screen_position.y + (screen_size.y - window_size.y) / 2; + DisplayServer::get_singleton()->window_set_position(window_position); + } } OS::get_singleton()->set_low_processor_usage_mode(true); diff --git a/scene/main/window.cpp b/scene/main/window.cpp index 8b1a4680d2a..6feccb7eecc 100644 --- a/scene/main/window.cpp +++ b/scene/main/window.cpp @@ -1045,7 +1045,9 @@ void Window::popup_centered_clamped(const Size2i &p_size, float p_fallback_ratio Rect2i popup_rect; popup_rect.size = Vector2i(MIN(size_ratio.x, p_size.x), MIN(size_ratio.y, p_size.y)); - popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2; + if (parent_rect != Rect2()) { + popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2; + } popup(popup_rect); } @@ -1069,7 +1071,10 @@ void Window::popup_centered(const Size2i &p_minsize) { Size2 contents_minsize = _get_contents_minimum_size(); popup_rect.size.x = MAX(p_minsize.x, contents_minsize.x); popup_rect.size.y = MAX(p_minsize.y, contents_minsize.y); - popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2; + + if (parent_rect != Rect2()) { + popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2; + } popup(popup_rect); } @@ -1091,8 +1096,10 @@ void Window::popup_centered_ratio(float p_ratio) { } Rect2i popup_rect; - popup_rect.size = parent_rect.size * p_ratio; - popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2; + if (parent_rect != Rect2()) { + popup_rect.size = parent_rect.size * p_ratio; + popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2; + } popup(popup_rect); }