11using System ;
2+ using System . Collections ;
23using System . Collections . Generic ;
34using System . IO ;
45using System . Linq ;
@@ -25,6 +26,8 @@ record Metadata(UIDocument document, UxmlAttribute uxml, string path);
2526 static Dictionary < VisualElement , Metadata > rootMetadata = new ( ) ;
2627 static List < VisualElement > search = new ( 256 ) ;
2728 static Dictionary < VisualElement , string > cloneMap = new ( 256 ) ;
29+
30+ static Dictionary < string , ( float width , float height ) > cachedDimensions = new ( ) ;
2831 #endregion
2932
3033 #region Properties
@@ -43,6 +46,8 @@ public static void Initialize(UIDocument document, IEnumerable<IRootElement> tar
4346 {
4447 foreach ( IRootElement target in targets . Where ( x => x is not null ) )
4548 Initialize ( document , target ) ;
49+
50+ CacheDimensions ( document ) ;
4651 }
4752 public static void Initialize ( UIDocument document , IRootElement target )
4853 {
@@ -505,8 +510,23 @@ public static async void MarginMe(this VisualElement value)
505510 {
506511 static int GetLines ( VisualElement value , VisualElement parent , float spacing , bool horizontalDirection )
507512 {
508- float valueSize = horizontalDirection ? value . resolvedStyle . width : value . resolvedStyle . height ;
509- float parentSize = horizontalDirection ? parent . resolvedStyle . width : parent . resolvedStyle . height ;
513+ string className = value . GetClasses ( ) . Last ( ) ;
514+ float valueSize ;
515+ if ( cachedDimensions . ContainsKey ( className ) )
516+ valueSize = horizontalDirection
517+ ? cachedDimensions [ className ] . width
518+ : cachedDimensions [ className ] . height ;
519+ else
520+ valueSize = horizontalDirection ? value . resolvedStyle . width : value . resolvedStyle . height ;
521+
522+ string parentClassName = parent . GetClasses ( ) . Last ( ) ;
523+ float parentSize ;
524+ if ( cachedDimensions . ContainsKey ( parentClassName ) )
525+ parentSize = horizontalDirection
526+ ? cachedDimensions [ parentClassName ] . width
527+ : cachedDimensions [ parentClassName ] . height ;
528+ else
529+ parentSize = horizontalDirection ? parent . resolvedStyle . width : parent . resolvedStyle . height ;
510530
511531 return ( valueSize . Invalid ( ) || valueSize == 0 ) ? parent . childCount : ( int ) ( parentSize / ( ( 2 * valueSize + ( spacing . Invalid ( ) ? 0 : spacing ) ) / 2 ) ) ;
512532 }
@@ -764,6 +784,54 @@ VisualElement FindIn(VisualElement root)
764784 }
765785 }
766786 }
787+
788+ static void CacheDimensions ( UIDocument document )
789+ {
790+ static object GetValue ( object target , string name ) => target . GetType ( ) . GetField ( name , BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) . GetValue ( target ) ;
791+
792+ StyleSheet uss = document . visualTreeAsset . stylesheets . First ( ) ;
793+
794+ object [ ] complexSelectors = ( object [ ] ) GetValue ( uss , "m_ComplexSelectors" ) ;
795+ object [ ] rules = ( object [ ] ) GetValue ( uss , "m_Rules" ) ;
796+ object [ ] dimensions = ( ( IEnumerable ) GetValue ( uss , "dimensions" ) ) . Cast < object > ( ) . ToArray ( ) ;
797+
798+ float width = 0 , height = 0 ;
799+ for ( int i = 0 ; i < complexSelectors . Length ; ++ i )
800+ {
801+ object [ ] properties = ( object [ ] ) GetValue ( rules [ i ] , "m_Properties" ) ;
802+ foreach ( object property in properties )
803+ {
804+ string propertyName = GetValue ( property , "m_Name" ) . ToString ( ) ;
805+ if ( propertyName is not "width" and not "height" ) continue ;
806+
807+ object propertyValue = ( ( IEnumerable ) GetValue ( property , "m_Values" ) ) . Cast < object > ( ) . First ( ) ;
808+ string valueType = GetValue ( propertyValue , "m_ValueType" ) . ToString ( ) ;
809+ if ( valueType is not "Dimension" ) continue ;
810+
811+ int valueIndex = ( int ) GetValue ( propertyValue , "valueIndex" ) ;
812+
813+ string unit = GetValue ( dimensions [ ( int ) valueIndex ] , "unit" ) . ToString ( ) ;
814+ if ( unit is not "Pixel" ) continue ;
815+
816+ float value = ( float ) GetValue ( dimensions [ valueIndex ] , "value" ) ;
817+
818+ width = propertyName is "width" ? value : width ;
819+ height = propertyName is "height" ? value : height ;
820+ }
821+
822+ if ( width != 0 || height != 0 )
823+ {
824+ object selector = ( ( object [ ] ) GetValue ( complexSelectors [ i ] , "m_Selectors" ) ) . First ( ) ;
825+ object part = ( ( IEnumerable ) GetValue ( selector , "m_Parts" ) ) . Cast < object > ( ) . First ( ) ;
826+ string className = GetValue ( part , "m_Value" ) . ToString ( ) ;
827+
828+ cachedDimensions . Add ( className , new ( width , height ) ) ;
829+
830+ width = 0 ;
831+ height = 0 ;
832+ }
833+ }
834+ }
767835 #endregion
768836 }
769837}
0 commit comments