Skip to content

Commit 28207cb

Browse files
Fix UI scaling (#9)
1 parent bf86d18 commit 28207cb

File tree

5 files changed

+35
-74
lines changed

5 files changed

+35
-74
lines changed

src/autoload/HandlerGUI.gd

Lines changed: 25 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -259,36 +259,12 @@ func update_ui_scale() -> void:
259259
var window := get_window()
260260
if not window.is_node_ready():
261261
await window.ready
262-
var old_scale_factor := window.content_scale_factor
263-
264-
# Get window size without the decorations.
265-
var usable_screen_size := Vector2i(DisplayServer.screen_get_usable_rect(
266-
DisplayServer.window_get_current_screen()).size -\
267-
window.get_size_with_decorations() + window.size)
268-
269-
# Presumably the default size would always be enough for the contents.
270-
var window_default_size := Vector2i(
271-
ProjectSettings.get_setting("display/window/size/viewport_width"),
272-
ProjectSettings.get_setting("display/window/size/viewport_height"))
273-
274-
# How much can the default size be increased before it takes all usable screen space.
275-
var max_expansion := Vector2(usable_screen_size) / Vector2(window_default_size)
276-
var max_scale := snappedf(minf(max_expansion.x, max_expansion.y) - 0.125, 0.25)
277-
if OS.get_name() == "Android":
278-
# This is a temporary fix for smaller UI scale on Android.
279-
# TODO Update this logic after moving to Godot 4.4
280-
max_scale *= 1.1
281-
var final_scale := minf(Configs.savedata.ui_scale * _calculate_auto_scale(), max_scale)
282-
var resize_factor := final_scale / old_scale_factor
283-
284-
if not OS.get_name() in ["Android", "Web"]:
285-
# TODO Check later if this workaround is still necessary for Windows.
286-
if OS.get_name() != "Windows" or window.mode == Window.MODE_WINDOWED:
287-
# The window's minimum size can mess with the size change, so we set it to zero.
288-
window.min_size = Vector2i.ZERO
289-
window.size *= resize_factor
290-
window.min_size = window_default_size * final_scale
291-
window.content_scale_factor = final_scale
262+
if Configs.savedata.auto_ui_scale:
263+
window.content_scale_factor = _calculate_auto_scale()
264+
else:
265+
var final_scale := minf(Configs.savedata.ui_scale, 4.0)
266+
window.content_scale_factor = final_scale
267+
292268

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

327303
func _calculate_auto_scale() -> float:
328-
if not Configs.savedata.auto_ui_scale:
329-
return 1.0
330-
331-
# Credit: Godots (MIT, by MakovWait and contributors)
332-
333-
var screen := DisplayServer.window_get_current_screen()
334-
if DisplayServer.screen_get_size(screen) == Vector2i():
335-
return 1.0
336-
337-
# Use the smallest dimension to use a correct display scale on portrait displays.
338-
var smallest_dimension := mini(DisplayServer.screen_get_size(screen).x,
339-
DisplayServer.screen_get_size(screen).y)
340-
341-
var dpi := DisplayServer.screen_get_dpi(screen)
342-
if dpi != 72:
343-
if dpi < 72:
344-
return 0.75
345-
elif dpi <= 96:
346-
return 1.0
347-
elif dpi <= 120:
348-
return 1.25
349-
elif dpi <= 160:
350-
return 1.5
351-
elif dpi <= 200:
352-
return 2.0
353-
elif dpi <= 240:
354-
return 2.5
355-
elif dpi <= 320:
356-
return 3.0
357-
elif dpi <= 480:
358-
return 4.0
359-
else: # dpi > 480
360-
return 5.0
361-
elif smallest_dimension >= 1700:
362-
# Likely a hiDPI display, but we aren't certain due to the returned DPI.
363-
# Use an intermediate scale to handle this situation.
364-
return 1.5
365-
return 1.0
304+
var dpi := DisplayServer.screen_get_dpi(DisplayServer.window_get_current_screen())
305+
306+
if dpi <= 120:
307+
return 0.75 # ldpi
308+
elif dpi <= 160:
309+
return 1.0 # mdpi (baseline)
310+
elif dpi <= 240:
311+
return 1.5 # hdpi
312+
elif dpi <= 320:
313+
return 2.0 # xhdpi
314+
elif dpi <= 480:
315+
return 3.0 # xxhdpi
316+
elif dpi <= 640:
317+
return 4.0 # xxxhdpi
318+
else:
319+
return 5.0 # Beyond xxxhdpi
320+
321+
return 1.0 # Default fallback scale
322+
366323

367324
# Helpers
368325

src/portrait_ui/editor_scene.tscn

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ theme_override_constants/separation = 0
1818
script = ExtResource("1_o7lif")
1919

2020
[node name="PanelContainer" type="PanelContainer" parent="."]
21-
custom_minimum_size = Vector2(360, 0)
21+
custom_minimum_size = Vector2(300, 0)
2222
layout_mode = 2
2323
size_flags_horizontal = 3
2424
size_flags_vertical = 3
2525

2626
[node name="HSplitContainer" type="VSplitContainer" parent="PanelContainer"]
2727
layout_mode = 2
2828
size_flags_horizontal = 3
29-
theme_override_constants/separation = 40
30-
theme_override_constants/minimum_grab_thickness = 30
29+
theme_override_constants/separation = 30
3130
theme_override_styles/split_bar_background = SubResource("StyleBoxFlat_mt61i")
3231

3332
[node name="TabContainer" type="TabContainer" parent="PanelContainer/HSplitContainer"]
@@ -50,3 +49,6 @@ metadata/_tab_index = 1
5049
unique_name_in_owner = true
5150
layout_mode = 2
5251
size_flags_vertical = 3
52+
53+
[node name="MarginContainer" type="MarginContainer" parent="."]
54+
layout_mode = 2

src/ui_parts/code_editor.tscn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ corner_radius_bottom_right = 5
3434
corner_radius_bottom_left = 5
3535

3636
[node name="CodeEditor" type="VBoxContainer"]
37+
custom_minimum_size = Vector2(0, 90)
3738
theme_override_constants/separation = 0
3839
script = ExtResource("1_nffk0")
3940

@@ -99,7 +100,7 @@ size_flags_vertical = 3
99100
theme_override_constants/separation = -2
100101

101102
[node name="SVGCodeEdit" type="TextEdit" parent="ScriptEditor"]
102-
custom_minimum_size = Vector2(0, 96)
103+
custom_minimum_size = Vector2(0, 80)
103104
layout_mode = 2
104105
size_flags_horizontal = 3
105106
size_flags_vertical = 3

src/ui_parts/display.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ layout_mode = 2
3939
size_flags_vertical = 3
4040

4141
[node name="ViewportContainer" type="SubViewportContainer" parent="ViewportPanel"]
42-
custom_minimum_size = Vector2(450, 0)
42+
custom_minimum_size = Vector2(300, 0)
4343
layout_mode = 2
4444
size_flags_vertical = 3
4545
stretch = true

src/ui_parts/inspector.tscn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[ext_resource type="Script" uid="uid://27atmrvxgbjt" path="res://src/ui_parts/move_to_overlay.gd" id="5_otlmf"]
88

99
[node name="Inspector" type="Container"]
10+
custom_minimum_size = Vector2(0, 90)
1011
anchors_preset = 15
1112
anchor_right = 1.0
1213
anchor_bottom = 1.0
@@ -34,7 +35,7 @@ theme_override_constants/h_separation = 4
3435
icon = ExtResource("3_vo6hf")
3536

3637
[node name="ElementContainer" type="Control" parent="."]
37-
custom_minimum_size = Vector2(0, 240)
38+
custom_minimum_size = Vector2(0, 80)
3839
layout_mode = 2
3940
size_flags_vertical = 3
4041
script = ExtResource("3_qeptj")

0 commit comments

Comments
 (0)