Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix window maximize on windows #79

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ui/screen_main/screen_main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ grow_horizontal = 2
grow_vertical = 2
theme_override_constants/separation = 0

[node name="TopBar" parent="VBox" instance=ExtResource("2_xllsp")]
[node name="TopBar" parent="VBox" node_paths=PackedStringArray("resize_handles") instance=ExtResource("2_xllsp")]
layout_mode = 2
resize_handles = NodePath("../../ResizeHandles")

[node name="Content" type="Control" parent="VBox"]
unique_name_in_owner = true
Expand Down
3 changes: 0 additions & 3 deletions src/ui/screen_main/scripts/main_resize_handles.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ func _ready() -> void:
$Right.gui_input.connect(_on_gui_input.bind($Right))
$Bottom.gui_input.connect(_on_gui_input.bind($Bottom))
$Corner.gui_input.connect(_on_gui_input.bind($Corner))

get_viewport().size_changed.connect(func():
visible = get_window().mode == Window.MODE_WINDOWED)


###############################################################
Expand Down
85 changes: 78 additions & 7 deletions src/ui/screen_main/scripts/main_top_bar.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,71 @@
extends PanelContainer

###############################################################
#region Window Mode ##########################################
###############################################################

@export var resize_handles: Control

@onready var _windowed_win_size: Vector2i = get_window().size
@onready var _windowed_win_pos: Vector2i = get_window().position

var win_mode: Window.Mode:
get:
var current_mode = get_window().mode
if OS.get_name() != "Windows" or !get_window().borderless:
return current_mode

if current_mode == Window.MODE_WINDOWED:
var usable_screen := DisplayServer.screen_get_usable_rect(get_window().current_screen)
if get_window().position == usable_screen.position and get_window().size == usable_screen.size:
return Window.MODE_MAXIMIZED
return Window.MODE_WINDOWED

return current_mode

set(value):
if value == win_mode: return

var prev_mode = win_mode

if OS.get_name() != "Windows" or !get_window().borderless:
get_window().mode = value
return

# store window size in windowed mode
if prev_mode == Window.MODE_WINDOWED:
_windowed_win_pos = get_window().position
_windowed_win_size = get_window().size

if value == Window.MODE_MAXIMIZED:
get_window().borderless = false
get_window().mode = Window.MODE_MAXIMIZED
get_window().borderless = true

# adjust mismatched window size and position
var usable_screen := DisplayServer.screen_get_usable_rect(get_window().current_screen)
get_window().position = usable_screen.position
get_window().size = usable_screen.size + Vector2i(2, 2)
return

if value == Window.MODE_WINDOWED:
get_window().borderless = false
get_window().mode = prev_mode # window.mode is not set to maximized when borderless
get_window().mode = Window.MODE_WINDOWED
get_window().borderless = true

# restore window size and position
get_window().size = _windowed_win_size
get_window().position = _windowed_win_pos
return

get_window().mode = value

#endregion
###############################################################
#region Main #################################################
###############################################################

var move_window := false
var move_start: Vector2i

Expand All @@ -9,16 +75,21 @@ func _ready() -> void:
ProjectManager._on_project_loaded.connect(func():
$Margin/HBox/ProjectButton.visible = true)

assert(resize_handles, "Resize handles not assigned.")
get_viewport().size_changed.connect(func():
resize_handles.visible = win_mode == Window.MODE_WINDOWED and get_window().borderless
)

#endregion
###############################################################
#region Window Dragging ######################################
###############################################################

func _on_top_bar_dragging(event):
if event is InputEventMouseButton and event.button_index == 1:
if !move_window:
move_window = event.is_pressed() and win_mode == Window.MODE_WINDOWED
if move_window:
move_start = get_viewport().get_mouse_position()
move_window = event.is_pressed()


func _process(_delta: float) -> void:
Expand All @@ -32,14 +103,14 @@ func _process(_delta: float) -> void:
###############################################################

func _on_minimize_button_pressed():
get_window().set_mode(Window.MODE_MINIMIZED)
win_mode = Window.MODE_MINIMIZED


func _on_switch_mode_button_pressed():
if get_window().mode == Window.MODE_WINDOWED:
get_window().set_mode(Window.MODE_FULLSCREEN)
elif get_window().mode == Window.MODE_FULLSCREEN:
get_window().set_mode(Window.MODE_WINDOWED)
if win_mode == Window.MODE_WINDOWED:
win_mode = Window.MODE_MAXIMIZED
else:
win_mode = Window.MODE_WINDOWED


func _on_exit_button_pressed():
Expand Down