Stop fighting the WM on resize events

This commit is contained in:
Luke Hubmayer-Werner 2023-08-04 14:36:43 +09:30
parent 6ec484b679
commit 0514011cd2
1 changed files with 12 additions and 2 deletions

View File

@ -2,6 +2,7 @@ extends Node
var base_resolution: Vector2 = Vector2(ProjectSettings.get_setting('display/window/size/width'), ProjectSettings.get_setting('display/window/size/height')) # Vector2(640, 360)
var shrink_timer := Timer.new()
var last_resolution_pre_shrink := Vector2.ZERO
static func load_json(filename: String): # Valid JSON will return Array or Dictionary. int error code for anything else.
var file := File.new()
@ -145,9 +146,18 @@ func update_window_scale():
var scale_f := max(min(scale_vec.x, scale_vec.y), 1)
var scale := int(scale_f)
if ProjectSettings.get_setting('display/window/size/snap_to_integer'):
var target_size := base_resolution * scale
# OS.window_size = base_resolution*scale
shrink_timer.paused = false
shrink_timer.start(1)
if size == target_size:
print_debug('Config is to shrink down to integer scale, but %s is already a perfect %dx scale' % [size, scale])
elif size == self.last_resolution_pre_shrink:
# Don't fight the WM if it sets the window size back again
print_debug("Config is to shrink down to integer scale, but %s was the last external resize event, so we won't fight the window manager" % size)
else:
print_debug('Setting lastres to %s, will resize to %dx == %s in 1 second' % [self.last_resolution_pre_shrink, scale, target_size])
self.last_resolution_pre_shrink = size
shrink_timer.paused = false
shrink_timer.start(1)
get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT, SceneTree.STRETCH_ASPECT_KEEP, base_resolution*scale, scale)
func _ready():