Add persistent light and dark themes

This commit is contained in:
hz
2026-07-14 13:39:42 -05:00
parent 04694e4932
commit abbe6cf9f8
8 changed files with 315 additions and 26 deletions
+61
View File
@@ -0,0 +1,61 @@
extends Node
signal theme_changed(is_dark: bool)
const Style := preload("res://scripts/ui/ui_style.gd")
const SETTINGS_PATH := "user://settings.cfg"
const SETTINGS_SECTION := "appearance"
const SETTINGS_KEY := "color_scheme"
var _is_dark := true
func _enter_tree() -> void:
_is_dark = _load_preference()
Style.set_dark_mode(_is_dark)
func is_dark_mode() -> bool:
return _is_dark
func set_dark_mode(value: bool) -> void:
if _is_dark == value:
return
_is_dark = value
Style.set_dark_mode(_is_dark)
_save_preference()
theme_changed.emit(_is_dark)
func toggle_theme() -> void:
set_dark_mode(not _is_dark)
func _load_preference() -> bool:
var config := ConfigFile.new()
if config.load(SETTINGS_PATH) == OK:
var saved := str(config.get_value(SETTINGS_SECTION, SETTINGS_KEY, ""))
if saved == "dark" or saved == "light":
return saved == "dark"
return _system_prefers_dark()
func _system_prefers_dark() -> bool:
if OS.has_feature("web"):
var result: Variant = JavaScriptBridge.eval(
"window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches",
true
)
return bool(result)
if DisplayServer.is_dark_mode_supported():
return DisplayServer.is_dark_mode()
return true
func _save_preference() -> void:
var config := ConfigFile.new()
config.load(SETTINGS_PATH)
config.set_value(SETTINGS_SECTION, SETTINGS_KEY, "dark" if _is_dark else "light")
if config.save(SETTINGS_PATH) != OK:
push_warning("Unable to save color scheme preference.")
+1
View File
@@ -0,0 +1 @@
uid://dgfxwfgg7bapl