UiTextSizing.cs

Static helper that applies a user text size preference to the UI. It caches per-root-panel state, loads /UiTextSizing.scss once, and toggles text-size-100/115/130 classes on the root panel when GamePreferences.TextSizeRevision changes.

using Sandbox.UI;
using System.Runtime.CompilerServices;

namespace Socer;

/// <summary>Typography only: the ScreenPanel's render scale and the pitch remain unchanged.</summary>
public static class UiTextSizing
{
    sealed class RootState
    {
        public int Revision=-1;
        public bool StylesLoaded;
    }
    static readonly ConditionalWeakTable<RootPanel,RootState> Roots=new();

    public static void Apply(Panel panel)
    {
        var root=panel?.FindRootPanel();
        if(root==null)return;
        if(!Roots.TryGetValue(root,out var state))
        {
            state=new RootState();
            Roots.Add(root,state);
        }
        if(!state.StylesLoaded)
        {
            root.StyleSheet.Load("/UiTextSizing.scss");
            state.StylesLoaded=true;
        }
        if(state.Revision==GamePreferences.TextSizeRevision)return;
        state.Revision=GamePreferences.TextSizeRevision;
        root.SetClass("text-size-100",GamePreferences.TextSizePercent==100);
        root.SetClass("text-size-115",GamePreferences.TextSizePercent==115);
        root.SetClass("text-size-130",GamePreferences.TextSizePercent==130);
    }
}