Files
sbox-public/engine/Sandbox.Engine/Systems/UI/Panel/Panel.Classes.cs
Garry Newman d27c236689 UI additions and optimizations (#4968)
ADDED
css min(), max() and clamp() for lengths
css-wide keywords inherit, initial, unset, revert (incl. shorthands)
added currentColor keyword to css
colors can parse oklch(), lab() and hwb()
added css text-align: justify
css white-space: pre-wrap and break-spaces
added css word-break: break-word
parses css image-rendering: crisp-edges (point sampling)
added css inset shorthand
now parses 1–4 values in the css border-width shorthand
added css transition-duration, -delay, -property and -timing-function longhands
css :has() now supports descendant selectors
added css opacity percentages
added css overflow: auto (maps to scroll)
added css gap: normal
added css filter: none
css transform: scale() now takes comma-separated args
css animations now accept ms units
added more css justify-content, align-* and text-align keywords (start, end, …)
parses css dvh/svh/lvh/dvw viewport units (treated as vh/vw)
parses css object-fit: scale-down (treated as contain)
added css margin-block / margin-inline (+ the logical margin sides)
added css padding-block / padding-inline (+ the logical padding sides)
added css inset-block / inset-inline (+ the logical inset sides)
added the css flex-flow shorthand
added the css font shorthand
added css font-size keywords (xx-small … xxx-large)
css letter-spacing and word-spacing now accept normal
added css font-smooth: none
css aspect-ratio now accepts the auto form
css font-family now maps generic families (serif, sans-serif, monospace)
added css flex-flow
added css font

FIXED
fixed css !important dropping the whole declaration
css line-height without units is now a multiplier
fixed css calc() division and full-expression parsing
fixed the css flex shorthand for single-number and three-value forms
fixed the css transition shorthand when the property is omitted
css font-family now uses the first family in a comma stack
colors now parse fully in the css background shorthand
an unknown css @-rule no longer drops the whole stylesheet
css variables no longer match on partial tokens
injected css variables are kept across stylesheet hotloads
fixed css selector specificity overflowing when comparing rules
css background: none now resets the background (image and colour)
css filter: none now overrides a filter set by a base class
css transform: none now overrides a transform set by a base class

IMPROVED
css rule matching is ~3–5× faster on large stylesheets (rules indexed by class)
~2× fewer allocations in css transitions (snapshot styles once, not per property)
finished css animations no longer re-layout every frame
less GC in css sibling/child selector queries (no list copies or wrappers)
less GC diffing active css rules (no LINQ or hashsets)
faster, lower-GC Yoga layout wrapper
css stylesheet hotload now watches newly imported files
2026-06-02 19:19:23 +01:00

224 lines
4.9 KiB
C#

namespace Sandbox.UI;
public partial class Panel
{
/// <summary>
/// A list of CSS classes applied to this panel.
/// </summary>
[Hide]
public IEnumerable<string> Class => _class ?? Enumerable.Empty<string>();
/// <inheritdoc cref="Class"/>
internal HashSet<string> _class;
internal string _classes;
/// <summary>
/// All CSS classes applied to this panel, separated with spaces.
/// </summary>
[Property]
public string Classes
{
get
{
if ( _classes == null )
_classes = string.Join( " ", Class );
return _classes;
}
set
{
bool had = _class != null && _class.Count > 0;
_class?.Clear();
_classes = null;
if ( had )
StyleSelectorsChanged( true, true );
AddClass( value );
}
}
/// <summary>
/// Adds CSS class(es) separated by spaces to this panel.
/// </summary>
public void AddClass( string classname )
{
if ( string.IsNullOrWhiteSpace( classname ) )
return;
if ( classname.Contains( ' ' ) )
{
AddClasses( classname );
return;
}
classname = classname.ToLowerInvariant();
_class ??= new HashSet<string>( StringComparer.OrdinalIgnoreCase );
if ( _class.Contains( classname ) ) return;
_class.Add( classname );
_classes = null;
// Adding a class changes the selector so children
// may have rules that now match - need to update those too.
StyleSelectorsChanged( true, true );
}
/// <summary>
/// Sets a specific CSS class active or not.
/// </summary>
public void SetClass( string classname, bool active )
{
if ( string.IsNullOrWhiteSpace( classname ) )
return;
if ( active ) AddClass( classname );
else RemoveClass( classname );
}
/// <summary>
/// Add a class for a set amount of seconds. If called multiple times, we will stomp the earlier call.
/// </summary>
public void FlashClass( string classname, float seconds )
{
if ( string.IsNullOrWhiteSpace( classname ) )
return;
AddClass( classname );
InvokeOnce( $"FlashClass;{classname}", seconds, () => RemoveClass( classname ) );
}
/// <summary>
/// Add a class if we don't have it, remove a class if we do have it
/// </summary>
public void ToggleClass( string classname )
{
if ( string.IsNullOrWhiteSpace( classname ) )
return;
SetClass( classname, !HasClass( classname ) );
}
/// <summary>
/// Add multiple CSS classes separated by spaces to this panel.
/// </summary>
void AddClasses( string classname )
{
if ( string.IsNullOrWhiteSpace( classname ) )
return;
foreach ( var cname in classname.Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries ) )
{
AddClass( cname );
}
}
/// <summary>
/// Removes given CSS class from this panel.
/// </summary>
public void RemoveClass( string classname )
{
if ( _class == null ) return;
if ( string.IsNullOrWhiteSpace( classname ) ) return;
if ( classname.Contains( ' ' ) )
{
foreach ( var cname in classname.Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries ) )
{
RemoveClass( cname );
}
return;
}
classname = classname.ToLowerInvariant();
if ( _class.Remove( classname ) )
{
// Removing a class changes the selector so children
// may have rules that now match - need to update those too.
StyleSelectorsChanged( true, true );
_classes = null;
}
}
/// <summary>
/// Whether we have the given CSS class or not.
/// </summary>
public bool HasClass( string classname )
{
if ( _class == null ) return false;
if ( string.IsNullOrWhiteSpace( classname ) ) return false;
if ( _class.Contains( classname ) ) return true;
return false;
}
/// <summary>
/// Whether if we have <b>all</b> of these CSS classes.
/// </summary>
internal bool HasClasses( string[] classes )
{
if ( _class == null ) return false;
for ( int i = 0; i < classes.Length; i++ )
{
if ( !_class.Contains( classes[i] ) )
return false;
}
return true;
}
/// <summary>
/// Dirty the styles on this panel
/// </summary>
internal void DirtyStylesRecursive()
{
StyleSelectorsChanged( true, true );
Style.InvalidateBroadphase();
}
/// <summary>
/// Dirty the styles of this class and its children recursively.
/// </summary>
internal void DirtyStylesWithStyle( Styles withStyles, bool skipTransitions = false )
{
if ( Style.ContainsStyle( withStyles ) )
{
Style.UnderlyingStyleHasChanged();
StyleSelectorsChanged( false, false );
if ( skipTransitions )
SkipTransitions();
}
foreach ( var child in Children )
{
child.DirtyStylesWithStyle( withStyles, skipTransitions );
}
}
Dictionary<string, Func<bool>> classBinds;
/// <summary>
/// Switch the class on or off depending on the value of the bool.
/// </summary>
public void BindClass( string className, Func<bool> func )
{
classBinds ??= new();
classBinds[className] = func;
}
private void RunClassBinds()
{
if ( classBinds is null ) return;
foreach ( var c in classBinds )
{
SetClass( c.Key, c.Value() );
}
}
}