Skip to content

Commit

Permalink
Fix UI scaling (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaxerror247 authored Feb 1, 2025
1 parent bf86d18 commit 28207cb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 74 deletions.
93 changes: 25 additions & 68 deletions src/autoload/HandlerGUI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -259,36 +259,12 @@ func update_ui_scale() -> void:
var window := get_window()
if not window.is_node_ready():
await window.ready
var old_scale_factor := window.content_scale_factor

# Get window size without the decorations.
var usable_screen_size := Vector2i(DisplayServer.screen_get_usable_rect(
DisplayServer.window_get_current_screen()).size -\
window.get_size_with_decorations() + window.size)

# Presumably the default size would always be enough for the contents.
var window_default_size := Vector2i(
ProjectSettings.get_setting("display/window/size/viewport_width"),
ProjectSettings.get_setting("display/window/size/viewport_height"))

# How much can the default size be increased before it takes all usable screen space.
var max_expansion := Vector2(usable_screen_size) / Vector2(window_default_size)
var max_scale := snappedf(minf(max_expansion.x, max_expansion.y) - 0.125, 0.25)
if OS.get_name() == "Android":
# This is a temporary fix for smaller UI scale on Android.
# TODO Update this logic after moving to Godot 4.4
max_scale *= 1.1
var final_scale := minf(Configs.savedata.ui_scale * _calculate_auto_scale(), max_scale)
var resize_factor := final_scale / old_scale_factor

if not OS.get_name() in ["Android", "Web"]:
# TODO Check later if this workaround is still necessary for Windows.
if OS.get_name() != "Windows" or window.mode == Window.MODE_WINDOWED:
# The window's minimum size can mess with the size change, so we set it to zero.
window.min_size = Vector2i.ZERO
window.size *= resize_factor
window.min_size = window_default_size * final_scale
window.content_scale_factor = final_scale
if Configs.savedata.auto_ui_scale:
window.content_scale_factor = _calculate_auto_scale()
else:
var final_scale := minf(Configs.savedata.ui_scale, 4.0)
window.content_scale_factor = final_scale


func open_update_checker() -> void:
var confirmation_dialog = ConfirmDialog.instantiate()
Expand Down Expand Up @@ -325,44 +301,25 @@ func open_export() -> void:
Translator.translate("Export"), FileUtils.open_export_dialog.bind(svg_export_data))

func _calculate_auto_scale() -> float:
if not Configs.savedata.auto_ui_scale:
return 1.0

# Credit: Godots (MIT, by MakovWait and contributors)

var screen := DisplayServer.window_get_current_screen()
if DisplayServer.screen_get_size(screen) == Vector2i():
return 1.0

# Use the smallest dimension to use a correct display scale on portrait displays.
var smallest_dimension := mini(DisplayServer.screen_get_size(screen).x,
DisplayServer.screen_get_size(screen).y)

var dpi := DisplayServer.screen_get_dpi(screen)
if dpi != 72:
if dpi < 72:
return 0.75
elif dpi <= 96:
return 1.0
elif dpi <= 120:
return 1.25
elif dpi <= 160:
return 1.5
elif dpi <= 200:
return 2.0
elif dpi <= 240:
return 2.5
elif dpi <= 320:
return 3.0
elif dpi <= 480:
return 4.0
else: # dpi > 480
return 5.0
elif smallest_dimension >= 1700:
# Likely a hiDPI display, but we aren't certain due to the returned DPI.
# Use an intermediate scale to handle this situation.
return 1.5
return 1.0
var dpi := DisplayServer.screen_get_dpi(DisplayServer.window_get_current_screen())

if dpi <= 120:
return 0.75 # ldpi
elif dpi <= 160:
return 1.0 # mdpi (baseline)
elif dpi <= 240:
return 1.5 # hdpi
elif dpi <= 320:
return 2.0 # xhdpi
elif dpi <= 480:
return 3.0 # xxhdpi
elif dpi <= 640:
return 4.0 # xxxhdpi
else:
return 5.0 # Beyond xxxhdpi

return 1.0 # Default fallback scale


# Helpers

Expand Down
8 changes: 5 additions & 3 deletions src/portrait_ui/editor_scene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ theme_override_constants/separation = 0
script = ExtResource("1_o7lif")

[node name="PanelContainer" type="PanelContainer" parent="."]
custom_minimum_size = Vector2(360, 0)
custom_minimum_size = Vector2(300, 0)
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3

[node name="HSplitContainer" type="VSplitContainer" parent="PanelContainer"]
layout_mode = 2
size_flags_horizontal = 3
theme_override_constants/separation = 40
theme_override_constants/minimum_grab_thickness = 30
theme_override_constants/separation = 30
theme_override_styles/split_bar_background = SubResource("StyleBoxFlat_mt61i")

[node name="TabContainer" type="TabContainer" parent="PanelContainer/HSplitContainer"]
Expand All @@ -50,3 +49,6 @@ metadata/_tab_index = 1
unique_name_in_owner = true
layout_mode = 2
size_flags_vertical = 3

[node name="MarginContainer" type="MarginContainer" parent="."]
layout_mode = 2
3 changes: 2 additions & 1 deletion src/ui_parts/code_editor.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ corner_radius_bottom_right = 5
corner_radius_bottom_left = 5

[node name="CodeEditor" type="VBoxContainer"]
custom_minimum_size = Vector2(0, 90)
theme_override_constants/separation = 0
script = ExtResource("1_nffk0")

Expand Down Expand Up @@ -99,7 +100,7 @@ size_flags_vertical = 3
theme_override_constants/separation = -2

[node name="SVGCodeEdit" type="TextEdit" parent="ScriptEditor"]
custom_minimum_size = Vector2(0, 96)
custom_minimum_size = Vector2(0, 80)
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3
Expand Down
2 changes: 1 addition & 1 deletion src/ui_parts/display.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ layout_mode = 2
size_flags_vertical = 3

[node name="ViewportContainer" type="SubViewportContainer" parent="ViewportPanel"]
custom_minimum_size = Vector2(450, 0)
custom_minimum_size = Vector2(300, 0)
layout_mode = 2
size_flags_vertical = 3
stretch = true
Expand Down
3 changes: 2 additions & 1 deletion src/ui_parts/inspector.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[ext_resource type="Script" uid="uid://27atmrvxgbjt" path="res://src/ui_parts/move_to_overlay.gd" id="5_otlmf"]

[node name="Inspector" type="Container"]
custom_minimum_size = Vector2(0, 90)
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
Expand Down Expand Up @@ -34,7 +35,7 @@ theme_override_constants/h_separation = 4
icon = ExtResource("3_vo6hf")

[node name="ElementContainer" type="Control" parent="."]
custom_minimum_size = Vector2(0, 240)
custom_minimum_size = Vector2(0, 80)
layout_mode = 2
size_flags_vertical = 3
script = ExtResource("3_qeptj")
Expand Down

0 comments on commit 28207cb

Please sign in to comment.