namespace Sandbox.UI; /// /// A collection of objects applied directly to a panel. /// See . /// public struct StyleSheetCollection { internal List List; internal readonly Panel Owner; internal StyleSheetCollection( Panel owner ) : this() { Owner = owner; } /// /// Add a stylesheet directly /// public void Add( StyleSheet sheet ) { if ( sheet is null ) return; List ??= new List(); if ( List.Contains( sheet ) ) return; List.Insert( 0, sheet ); Owner?.Style?.Dirty(); Owner?.Style?.InvalidateBroadphase(); } /// /// Load the stylesheet from a file. /// public void Load( string filename, bool inheritVariables = true, bool failSilently = false ) { Add( StyleSheet.FromFile( filename, inheritVariables ? CollectVariables() : null, failSilently ) ); } /// /// Load the stylesheet from a string. /// public void Parse( string stylesheet, bool inheritVariables = true ) { Remove( "string" ); Add( StyleSheet.FromString( stylesheet, "string", inheritVariables ? CollectVariables() : null ) ); } /// /// Remove a specific from the collection. /// public void Remove( StyleSheet sheet ) { if ( List is null ) return; if ( List.Remove( sheet ) ) { Owner?.Style?.InvalidateBroadphase(); } } /// /// Remove all stylesheets whose filename matches this wildcard glob. /// public void Remove( string wildcardGlob ) { if ( (List?.RemoveAll( x => x.FileName != null && x.FileName.WildcardMatch( wildcardGlob ) ) ?? 0) > 0 ) { Owner?.Style?.Dirty(); Owner?.Style?.InvalidateBroadphase(); } } /// /// Returns all CSS variables from the owning panel and its ancestors. /// public IEnumerable<(string key, string value)> CollectVariables() { if ( Owner == null ) yield break; foreach ( var sheet in Owner.AllStyleSheets ) { if ( sheet.Variables == null ) continue; foreach ( var v in sheet.Variables ) { yield return (v.Key, v.Value); } } } }