1- using CsDebugScript . UI . CodeWindow ;
1+ using CsDebugScript . Engine . Utility ;
2+ using CsDebugScript . UI . CodeWindow ;
23using System ;
34using System . Collections . Generic ;
45using System . Linq ;
@@ -54,24 +55,26 @@ private interface IResultTreeItem
5455 ImageSource Image { get ; }
5556
5657 IEnumerable < IResultTreeItem > Children { get ; }
58+
59+ string ValueString { get ; }
5760 }
5861
5962 private class ResultTreeItem
6063 {
61- public static IResultTreeItem Create ( object obj , Type objType , string name , ImageSource image )
64+ public static IResultTreeItem Create ( object obj , Type objType , string name , ImageSource image , InteractiveResultVisualizer interactiveResultVisualizer )
6265 {
6366 if ( obj != null && objType . IsArray )
64- return new ArrayResultTreeItem ( ( Array ) obj , objType , name , image ) ;
65- return new ObjectResultTreeItem ( obj , objType , name , image ) ;
67+ return new ArrayResultTreeItem ( ( Array ) obj , objType , name , image , interactiveResultVisualizer ) ;
68+ return new ObjectResultTreeItem ( obj , objType , name , image , interactiveResultVisualizer ) ;
6669 }
6770 }
6871
6972 private class ArrayResultTreeItem : ObjectResultTreeItem
7073 {
7174 private Array array ;
7275
73- public ArrayResultTreeItem ( Array array , Type objType , string name , ImageSource image )
74- : base ( array , objType , name , image )
76+ public ArrayResultTreeItem ( Array array , Type objType , string name , ImageSource image , InteractiveResultVisualizer interactiveResultVisualizer )
77+ : base ( array , objType , name , image , interactiveResultVisualizer )
7578 {
7679 this . array = array ;
7780 }
@@ -83,7 +86,7 @@ public override IEnumerable<IResultTreeItem> Children
8386 foreach ( var child in base . Children )
8487 yield return child ;
8588 for ( int i = 0 ; i < array . Length ; i ++ )
86- yield return ResultTreeItem . Create ( GetValue ( ( ) => array . GetValue ( i ) ) , objType . GetElementType ( ) , string . Format ( "[{0}]" , i ) , CompletionData . GetImage ( CompletionDataType . Variable ) ) ;
89+ yield return ResultTreeItem . Create ( GetValue ( ( ) => array . GetValue ( i ) ) , objType . GetElementType ( ) , string . Format ( "[{0}]" , i ) , CompletionData . GetImage ( CompletionDataType . Variable ) , interactiveResultVisualizer ) ;
8790 }
8891 }
8992
@@ -99,14 +102,18 @@ public override object Value
99102 private class ObjectResultTreeItem : IResultTreeItem
100103 {
101104 private object obj ;
105+ private SimpleCache < string > valueString ;
102106 protected Type objType ;
107+ protected InteractiveResultVisualizer interactiveResultVisualizer ;
103108
104- public ObjectResultTreeItem ( object obj , Type objType , string name , ImageSource image )
109+ public ObjectResultTreeItem ( object obj , Type objType , string name , ImageSource image , InteractiveResultVisualizer interactiveResultVisualizer )
105110 {
106111 this . obj = obj ;
107112 this . objType = objType ;
113+ this . interactiveResultVisualizer = interactiveResultVisualizer ;
108114 Name = name ;
109115 Image = image ;
116+ valueString = SimpleCache . Create ( ( ) => Value . ToString ( ) ) ;
110117 }
111118
112119 public virtual IEnumerable < IResultTreeItem > Children
@@ -124,44 +131,44 @@ public virtual IEnumerable<IResultTreeItem> Children
124131
125132 foreach ( var property in properties )
126133 if ( property . CanRead )
127- yield return ResultTreeItem . Create ( GetValue ( ( ) => property . GetValue ( obj ) ) , property . PropertyType , property . Name , CompletionData . GetImage ( CompletionDataType . Property ) ) ;
134+ yield return ResultTreeItem . Create ( GetValue ( ( ) => property . GetValue ( obj ) ) , property . PropertyType , property . Name , CompletionData . GetImage ( CompletionDataType . Property ) , interactiveResultVisualizer ) ;
128135
129136 // Static properties
130137 var staticProperties = type . GetProperties ( System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Public | System . Reflection . BindingFlags . Static ) ;
131138
132139 foreach ( var property in staticProperties )
133140 if ( property . CanRead )
134- yield return ResultTreeItem . Create ( GetValue ( ( ) => property . GetValue ( obj ) ) , property . PropertyType , property . Name , CompletionData . GetImage ( CompletionDataType . StaticProperty ) ) ;
141+ yield return ResultTreeItem . Create ( GetValue ( ( ) => property . GetValue ( obj ) ) , property . PropertyType , property . Name , CompletionData . GetImage ( CompletionDataType . StaticProperty ) , interactiveResultVisualizer ) ;
135142
136143 // Non-static fields
137144 var fields = type . GetFields ( System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Public | System . Reflection . BindingFlags . Instance ) ;
138145
139146 foreach ( var field in fields )
140- if ( ! field . IsStatic )
141- yield return ResultTreeItem . Create ( GetValue ( ( ) => field . GetValue ( obj ) ) , field . FieldType , field . Name , CompletionData . GetImage ( CompletionDataType . Variable ) ) ;
147+ if ( ! field . IsStatic && ! field . Name . EndsWith ( ">k__BackingField" ) )
148+ yield return ResultTreeItem . Create ( GetValue ( ( ) => field . GetValue ( obj ) ) , field . FieldType , field . Name , CompletionData . GetImage ( CompletionDataType . Variable ) , interactiveResultVisualizer ) ;
142149
143150 // Static fields
144151 var staticFields = type . GetFields ( System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Public | System . Reflection . BindingFlags . Static ) ;
145152
146153 foreach ( var field in staticFields )
147- if ( field . IsStatic )
148- yield return ResultTreeItem . Create ( GetValue ( ( ) => field . GetValue ( obj ) ) , field . FieldType , field . Name , CompletionData . GetImage ( CompletionDataType . StaticVariable ) ) ;
154+ if ( field . IsStatic && ! field . Name . EndsWith ( ">k__BackingField" ) )
155+ yield return ResultTreeItem . Create ( GetValue ( ( ) => field . GetValue ( obj ) ) , field . FieldType , field . Name , CompletionData . GetImage ( CompletionDataType . StaticVariable ) , interactiveResultVisualizer ) ;
149156 }
150157 }
151158 }
152159 }
153160
154161 private static ImageSource ExceptionImage = CompletionData . CreateTextImage ( "" , Brushes . Red ) ;
155162
156- protected static object GetValue ( Func < object > getValueFunction )
163+ protected object GetValue ( Func < object > getValueFunction )
157164 {
158165 try
159166 {
160167 return getValueFunction ( ) ;
161168 }
162169 catch ( Exception ex )
163170 {
164- return CreateTextWithIcon ( "Exception" , ExceptionImage , ex . ToString ( ) ) ;
171+ return interactiveResultVisualizer . CreateTextWithIcon ( "Exception" , ExceptionImage , ex . ToString ( ) ) ;
165172 }
166173 }
167174
@@ -184,6 +191,14 @@ public virtual object Value
184191 return obj != null ? obj : "null" ;
185192 }
186193 }
194+
195+ public string ValueString
196+ {
197+ get
198+ {
199+ return valueString . Value ;
200+ }
201+ }
187202 }
188203
189204 public object Output ( object obj )
@@ -202,11 +217,13 @@ public object Output(object obj)
202217 }
203218
204219 TreeViewItem emptyListItem ;
220+ System . Windows . Threading . Dispatcher dispatcher ;
205221
206222 private UIElement Visualize ( object obj )
207223 {
208224 // Create top level table grid
209225 Grid tableGrid = new Grid ( ) ;
226+ dispatcher = tableGrid . Dispatcher ;
210227
211228 Grid . SetIsSharedSizeScope ( tableGrid , true ) ;
212229 tableGrid . RowDefinitions . Add ( new RowDefinition ( )
@@ -243,7 +260,7 @@ private UIElement Visualize(object obj)
243260
244261 // Create table tree
245262 TreeView tree = new TreeView ( ) ;
246- IResultTreeItem resultTreeItem = ResultTreeItem . Create ( obj , obj . GetType ( ) , "result" , null ) ;
263+ IResultTreeItem resultTreeItem = ResultTreeItem . Create ( obj , obj . GetType ( ) , "result" , null , this ) ;
247264
248265 tree . PreviewKeyDown += Tree_PreviewKeyDown ;
249266 tree . Items . Add ( header ) ;
@@ -350,20 +367,23 @@ private class TreeViewItemTag
350367 public int Level { get ; set ; }
351368 }
352369
353- private static UIElement CreateTextWithIcon ( string text , ImageSource icon , object tooltip = null )
370+ private UIElement CreateTextWithIcon ( string text , ImageSource icon , object tooltip = null )
354371 {
355- StackPanel stackPanel = new StackPanel ( ) ;
356- stackPanel . Orientation = Orientation . Horizontal ;
357- Grid . SetColumn ( stackPanel , NameColumnIndex ) ;
358- TextBlock textBlock = new TextBlock ( ) ;
359- textBlock . Text = text ;
360- Image image = new Image ( ) ;
361- image . Width = image . Height = 16 ;
362- image . Source = icon ;
363- image . ToolTip = tooltip ;
364- stackPanel . Children . Add ( image ) ;
365- stackPanel . Children . Add ( textBlock ) ;
366- return stackPanel ;
372+ return dispatcher . Invoke ( ( ) =>
373+ {
374+ StackPanel stackPanel = new StackPanel ( ) ;
375+ stackPanel . Orientation = Orientation . Horizontal ;
376+ Grid . SetColumn ( stackPanel , NameColumnIndex ) ;
377+ TextBlock textBlock = new TextBlock ( ) ;
378+ textBlock . Text = text ;
379+ Image image = new Image ( ) ;
380+ image . Width = image . Height = 16 ;
381+ image . Source = icon ;
382+ image . ToolTip = tooltip ;
383+ stackPanel . Children . Add ( image ) ;
384+ stackPanel . Children . Add ( textBlock ) ;
385+ return stackPanel ;
386+ } ) ;
367387 }
368388
369389 private TreeViewItem CreateTreeItem ( IResultTreeItem resultTreeItem , int level )
@@ -382,7 +402,7 @@ private TreeViewItem CreateTreeItem(IResultTreeItem resultTreeItem, int level)
382402 else
383403 {
384404 TextBlock value = new TextBlock ( ) ;
385- value . Text = itemValue . ToString ( ) ;
405+ value . Text = resultTreeItem . ValueString ;
386406 Grid . SetColumn ( value , ValueColumnIndex ) ;
387407 grid . Children . Add ( value ) ;
388408 }
@@ -412,22 +432,37 @@ private void TreeViewItem_Expanded(object sender, RoutedEventArgs e)
412432 if ( ( item . Items . Count == 1 ) && ( item . Items [ 0 ] is int ) )
413433 {
414434 TreeViewItemTag tag = ( TreeViewItemTag ) item . Tag ;
415- IResultTreeItem resultTreeItem = tag . ResultTreeItem ;
416- int level = tag . Level ;
417- TreeViewItem lastItem = null ;
418-
419- item . Items . Clear ( ) ;
420- foreach ( var child in resultTreeItem . Children . OrderBy ( s => s . Name . StartsWith ( "[" ) ) . ThenBy ( s => s . Name ) )
421- item . Items . Add ( lastItem = CreateTreeItem ( child , level + 1 ) ) ;
422435
423- // Check if we need to fix empty list item width
424- if ( lastItem != null && double . IsNaN ( emptyListItem . Width ) )
436+ System . Threading . Tasks . Task . Run ( ( ) =>
425437 {
426- item . Dispatcher . BeginInvoke ( new Action ( ( ) =>
438+ IResultTreeItem resultTreeItem = tag . ResultTreeItem ;
439+ var children = resultTreeItem . Children . ToList ( ) ;
440+
441+ foreach ( var child in children )
442+ if ( ! ( child . Value is UIElement ) )
443+ {
444+ string ss = child . ValueString ;
445+ }
446+
447+ item . Dispatcher . InvokeAsync ( ( ) =>
427448 {
428- emptyListItem . Width = item . ActualWidth - lastItem . ActualWidth ;
429- } ) , System . Windows . Threading . DispatcherPriority . Background ) ;
430- }
449+ int level = tag . Level ;
450+ TreeViewItem lastItem = null ;
451+
452+ item . Items . Clear ( ) ;
453+ foreach ( var child in children . OrderBy ( s => s . Name . StartsWith ( "[" ) ) . ThenBy ( s => s . Name ) )
454+ item . Items . Add ( lastItem = CreateTreeItem ( child , level + 1 ) ) ;
455+
456+ // Check if we need to fix empty list item width
457+ if ( lastItem != null && double . IsNaN ( emptyListItem . Width ) )
458+ {
459+ item . Dispatcher . BeginInvoke ( new Action ( ( ) =>
460+ {
461+ emptyListItem . Width = item . ActualWidth - lastItem . ActualWidth ;
462+ } ) , System . Windows . Threading . DispatcherPriority . Background ) ;
463+ }
464+ } ) ;
465+ } ) ;
431466 }
432467 }
433468 catch ( Exception ex )
0 commit comments