Files
sbox-public/engine/Sandbox.Engine/Systems/UI/Styles/StyleBlock.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

152 lines
3.9 KiB
C#

namespace Sandbox.UI;
/// <summary>
/// A CSS rule - ie ".chin { width: 100%; height: 100%; }"
/// </summary>
[SkipHotload]
public sealed class StyleBlock : IStyleBlock
{
internal int LoadOrder = 0;
// True if any of this block's selectors target ::before / ::after, so the style build can skip the
// per-block pseudo-element probe for the (vast majority of) blocks that don't use them.
internal bool HasBefore;
internal bool HasAfter;
/// <summary>
/// A list of appropriate selectors for this block (ie ".button")
/// </summary>
public StyleSelector[] Selectors { get; private set; }
/// <summary>
/// A list of selectors for this block
/// </summary>
public IEnumerable<string> SelectorStrings => Selectors.Select( x => x.AsString );
/// <summary>
/// Get the list of raw style values
/// </summary>
public List<IStyleBlock.StyleProperty> GetRawValues()
{
return Styles.RawValues.Values.ToList();
}
internal bool IsEmpty => Styles?.RawValues?.Count == 0;
/// <summary>
/// Update a raw style value
/// </summary>
public bool SetRawValue( string key, string value, string originalValue = null )
{
if ( !Styles.RawValues.TryGetValue( key, out var prop ) )
{
prop.Name = key;
prop.OriginalValue = value;
}
prop.Value = value;
if ( originalValue != null )
prop.OriginalValue = originalValue;
var success = Styles.Set( key, value );
prop.IsValid = success;
Styles.RawValues[key] = prop;
Styles.MarkPanelsDirty();
return success;
}
/// <summary>
/// The filename of the file containing this style block (or null if none)
/// </summary>
public string FileName { get; internal set; }
/// <summary>
/// The absolute on disk filename for this style block (or null if not on disk)
/// </summary>
public string AbsolutePath { get; internal set; }
/// <summary>
/// The line in the file containing this style block
/// </summary>
public int FileLine { get; internal set; }
/// <summary>
/// The styles that are defined in this block
/// </summary>
public Styles Styles;
/// <summary>
/// Test whether target passes our selector tests. We use forceFlag to do alternate tests for flags like ::before and ::after.
/// It's basically added to the target's pseudo class list for the test.
/// </summary>
public StyleSelector Test( IStyleTarget target, PseudoClass forceFlag = PseudoClass.None )
{
if ( Selectors == null ) return null;
// If this is a before or after then use the parent class with the added pseudo class
if ( target.IsBeforeOrAfter )
{
if ( target.PseudoClass.Contains( PseudoClass.Before ) ) forceFlag = PseudoClass.Before;
if ( target.PseudoClass.Contains( PseudoClass.After ) ) forceFlag = PseudoClass.After;
target = target.Parent;
}
for ( int i = 0; i < Selectors.Length; i++ )
{
var item = Selectors[i];
if ( item.Test( target, forceFlag ) )
return item;
}
return null;
}
/// <summary>
/// Tests a few broadphase conditions to build a list of feasible
/// styleblocks tailored for a panel.
/// </summary>
public bool TestBroadphase( IStyleTarget target )
{
if ( Selectors == null ) return false;
// We need to check the parent for pseudo classes like :before and :after
if ( target.IsBeforeOrAfter ) target = target.Parent;
// target might have become null if target didn't have a parent above
if ( target == null ) return false;
for ( int i = 0; i < Selectors.Length; i++ )
{
var item = Selectors[i];
if ( item.TestBroadphase( target ) )
return true;
}
return false;
}
public bool SetSelector( string selector, StyleBlock parent = null )
{
var selectors = StyleParser.Selector( selector, parent );
if ( selectors == null ) return false;
Selectors = selectors.ToArray();
foreach ( var s in Selectors )
{
s.Finalize( this );
if ( (s.Flags & PseudoClass.Before) != 0 ) HasBefore = true;
if ( (s.Flags & PseudoClass.After) != 0 ) HasAfter = true;
}
return true;
}
}