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

Switch auto UI scale to use screen DPI #672

Merged
merged 7 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
79 changes: 56 additions & 23 deletions src/GlobalSettings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ func _enter_tree() -> void:
default_input_events[action] = InputMap.action_get_events(action)
load_settings()
load_user_data()
get_window().wrap_controls = true # Prevents the main window from getting too small.
update_ui_scale()
get_window().size_changed.connect(update_ui_scale)
ThemeGenerator.generate_theme()


Expand Down Expand Up @@ -249,24 +247,59 @@ func get_validity_color(error_condition: bool, warning_condition := false) -> Co
func update_ui_scale() -> void:
await get_tree().process_frame
var window := get_window()
var min_size: Vector2 = window.get_contents_minimum_size()
var actual_ui_scale: float = ui_scale
if auto_ui_scale:
actual_ui_scale = _calculate_auto_scale(min_size, window.size)
min_size *= actual_ui_scale
window.min_size = min_size
window.content_scale_factor = actual_ui_scale


func _calculate_auto_scale(min_size: Vector2, size: Vector2i) -> float:
var div: Vector2 = (Vector2(size - Vector2i(10, 10)) / min_size)
var desired: float = div[div.min_axis_index()] * 0.5 * ui_scale
if desired > 4.0: return 4.0
if desired > 3.0: return 3.0
if desired > 2.5: return 2.5
if desired > 2.0: return 2.0
if desired > 1.75: return 1.75
if desired > 1.5: return 1.5
if desired > 1.25: return 1.25
if desired > 1.0: return 1.0
return 0.75

var usable_screen_size := Vector2(
DisplayServer.screen_get_usable_rect(DisplayServer.window_get_current_screen()).size
# Subtract window decoration size.
- (window.get_size_with_decorations() - window.size)
)

# How much can window content size be multiplied by before it extends over the usable screen size.
var diff := usable_screen_size / window.get_contents_minimum_size()
var max_scale := floorf(minf(diff.x, diff.y)*4.0)/4.0
Kiisu-Master marked this conversation as resolved.
Show resolved Hide resolved

var desired_scale: float = ui_scale * _calculate_auto_scale()

if not desired_scale > max_scale:
window.min_size = window.get_contents_minimum_size() * desired_scale
window.content_scale_factor = desired_scale
else:
window.min_size = usable_screen_size
window.content_scale_factor = max_scale


func _calculate_auto_scale() -> float:
if not 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 <=160:
Kiisu-Master marked this conversation as resolved.
Show resolved Hide resolved
return 1.25
elif dpi <= 240:
return 1.5
elif dpi <= 480:
return 1.75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These values are just a bit too low. DisplayServer.screen_get_dpi(screen) returns 163 on my system with 200% scaling, so I would expect 2.0 to be returned, but this code is returning 1.5.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adjusted the values to be roughly dpi/96. That's the DPI of my laptop screen and 1.0 looks fine on it, so i used it as a base.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different perspectives I guess, I am using a high DPI desktop display.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your DPI will return 2.0 now.

I found that changing the scaling in Windows also changes the reported DPI. With that, it looks like scaling in GodSVG now roughly matches the OS scaling so it should be good now.

elif dpi > 480:
Kiisu-Master marked this conversation as resolved.
Show resolved Hide resolved
return 2.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
2 changes: 1 addition & 1 deletion src/ui_parts/settings_menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func setup_setting_labels() -> void:

var auto_ui_scale := %Misc/AutoUIScale
auto_ui_scale.label.text = tr("Auto UI scale")
auto_ui_scale.tooltip_text = tr("Scale the user interface based on the window size.")
auto_ui_scale.tooltip_text = tr("Scales the user interface based on the screen size.")

%GeneralVBox/NumberPrecision.label.text = tr("Number precision digits")
%GeneralVBox/AnglePrecision.label.text = tr("Angle precision digits")
Expand Down
7 changes: 7 additions & 0 deletions translations/GodSVG.pot
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@ msgstr ""
msgid ""
"If turned on, scrolling will pan the view. To zoom, hold CTRL while "
"scrolling."
msgstr ""

msgid "UI scale"
msgstr ""

msgid "Changes the scale of the visual user interface."
msgstr ""

msgid "Auto UI scale"
msgstr ""

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr ""

Expand Down
7 changes: 7 additions & 0 deletions translations/bg.po
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ msgstr "Мащаб"
msgid "Changes the scale of the visual user interface."
msgstr ""

#, fuzzy
msgid "Auto UI scale"
msgstr "Мащаб"

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr "Входни сигнали"

Expand Down
7 changes: 7 additions & 0 deletions translations/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ msgstr "Skalieren"
msgid "Changes the scale of the visual user interface."
msgstr ""

#, fuzzy
msgid "Auto UI scale"
msgstr "Skalieren"

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr "Eingabe"

Expand Down
6 changes: 6 additions & 0 deletions translations/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ msgstr ""
msgid "Changes the scale of the visual user interface."
msgstr ""

msgid "Auto UI scale"
msgstr ""

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr ""

Expand Down
7 changes: 7 additions & 0 deletions translations/ru.po
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ msgstr "Масштаб интерфейса"
msgid "Changes the scale of the visual user interface."
msgstr "Изменить масштаб пользовательского интерфейса."

#, fuzzy
msgid "Auto UI scale"
msgstr "Масштаб"

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr "Устройства ввода"

Expand Down
7 changes: 7 additions & 0 deletions translations/uk.po
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ msgstr "Масштаб інтерфейсу"
msgid "Changes the scale of the visual user interface."
msgstr "Змінити масштаб інтерфейсу користувача."

#, fuzzy
msgid "Auto UI scale"
msgstr "Масштаб"

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr "Пристрої вводу"

Expand Down