]> O.S.I.I.S - jp/crow.git/commitdiff
make treeView subdata fetching working with IEnumerable
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 2 Nov 2021 11:47:48 +0000 (12:47 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 2 Nov 2021 11:47:48 +0000 (12:47 +0100)
Crow/src/ItemTemplate.cs
Samples/ShowCase/ShowCase.cs
Samples/common/ui/Interfaces/Experimental/testLoadCtxTree.crow [new file with mode: 0644]

index c263b65622a5292f344a35ad51c97f1af4762a91..9c67d57e9f4dfdd8123895c11c747d65212d9d04 100644 (file)
@@ -226,7 +226,6 @@ namespace Crow
                        il = dm.GetILGenerator (256);
                        System.Reflection.Emit.Label end = il.DefineLabel ();
                        System.Reflection.Emit.Label test = il.DefineLabel ();
-
                        //get the dataSource of the arg0
                        il.Emit (OpCodes.Ldarg_0);
                        il.Emit (OpCodes.Callvirt, CompilerServices.miGetDataSource);
@@ -241,15 +240,20 @@ namespace Crow
                                }else
                                        emitGetSubData(il, dataType);
                        }
-                       il.Emit (OpCodes.Isinst, typeof(System.Collections.ICollection));
                        il.Emit (OpCodes.Dup);//duplicate children for testing if it's a collection for childs counting
+                       il.Emit (OpCodes.Isinst, typeof(System.Collections.ICollection));
                        il.Emit (OpCodes.Brtrue, test);//if true, jump to perform count
-                       il.Emit (OpCodes.Pop);//pop null
-                       il.Emit (OpCodes.Ldc_I4_0);//push false
+                       il.Emit (OpCodes.Isinst, typeof(System.Collections.IEnumerable));//test if enumerable
+                       System.Reflection.Emit.Label pushTrue = il.DefineLabel ();
+                       il.Emit (OpCodes.Brtrue, pushTrue);
+                       il.Emit (OpCodes.Ldc_I4_0);//is not an enumerable, so push false for return
+                       il.Emit (OpCodes.Br, end);
+                       il.MarkLabel (pushTrue);
+                       il.Emit (OpCodes.Ldc_I4_1);//is an enumerable, so push true for return
                        il.Emit (OpCodes.Br, end);
 
                        il.MarkLabel (test);
-
+                       il.Emit (OpCodes.Isinst, typeof(System.Collections.ICollection));
                        il.Emit (OpCodes.Callvirt, CompilerServices.miGetColCount);
                        il.Emit (OpCodes.Ldc_I4_0);
                        il.Emit (OpCodes.Cgt);
@@ -260,31 +264,41 @@ namespace Crow
                        HasSubItems = (BooleanTestOnInstance)dm.CreateDelegate (typeof(BooleanTestOnInstance));
                        #endregion
                }
+               static void emitcallvirtornot (ILGenerator il, MethodInfo mi) {
+                       if (mi.IsStatic)
+                               il.Emit (OpCodes.Call, mi);
+                       else
+                               il.Emit (OpCodes.Callvirt, mi);
+               }
                //data is on the stack
                void emitGetSubData(ILGenerator il, Type dataType){
                        if (dataType.IsValueType)
                                il.Emit (OpCodes.Unbox_Any, dataType);
-                       MethodInfo miGetDatas = dataType.GetMethod (fetchMethodName, new Type[] {});
-                       if (miGetDatas == null)
-                               miGetDatas = iface.SearchExtMethod (dataType, fetchMethodName);
-
-                       if (miGetDatas == null) {//in last resort, search among properties
-                               PropertyInfo piDatas = dataType.GetProperty (fetchMethodName);
-                               if (piDatas == null) {
-                                       FieldInfo fiDatas = dataType.GetField (fetchMethodName);
-                                       if (fiDatas == null)//and among fields
-                                               throw new Exception ("Fetch data member not found in ItemTemplate: " + fetchMethodName);
-                                       il.Emit (OpCodes.Ldfld, fiDatas);
-                                       return;
-                               }
-                               miGetDatas = piDatas.GetGetMethod ();
-                               if (miGetDatas == null)
+                       MemberInfo miGetData = dataType.GetMember (fetchMethodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
+                               .FirstOrDefault(mmi => (mmi is MethodInfo mmmi && mmmi.GetParameters().Length == 0) || mmi.MemberType != MemberTypes.Method);
+
+                       if (miGetData == null) {
+                               MethodInfo extMethd = iface.SearchExtMethod (dataType, fetchMethodName);
+                               if (extMethd == null)
+                                       throw new Exception ("Fetch data member not found in ItemTemplate: " + fetchMethodName);
+                               emitcallvirtornot (il, extMethd);
+                               return;
+                       }
+                       if (miGetData is MethodInfo mi) {
+                               emitcallvirtornot (il, mi);
+                               return;
+                       }
+                       if (miGetData is PropertyInfo pi) {
+                               if (!pi.CanRead)
                                        throw new Exception ("Write only property for fetching data in ItemTemplate: " + fetchMethodName);
+                               emitcallvirtornot (il, pi.GetGetMethod ());
+                               //il.Emit (OpCodes.Call, pi.GetGetMethod ());
+                               return;
+                       }
+                       if (miGetData is FieldInfo fi) {
+                               il.Emit (OpCodes.Ldfld, fi);
+                               return;
                        }
-            if (miGetDatas.IsStatic)
-                           il.Emit (OpCodes.Call, miGetDatas);
-            else
-                il.Emit (OpCodes.Callvirt, miGetDatas);
         }
        }
 
index 7b25d718cd663d992113a9db587894a15f5a2a0c..49663438d51dc56baf441c80d81c4344182c5e9e 100644 (file)
@@ -188,5 +188,7 @@ namespace ShowCase
                                NotifyValueChanged (Crow.Interface.POLLING_INTERVAL);
                        }
                }
+               public IEnumerable<System.Runtime.Loader.AssemblyLoadContext> AllLoadContexts =>
+                       System.Runtime.Loader.AssemblyLoadContext.All;
     }
 }
\ No newline at end of file
diff --git a/Samples/common/ui/Interfaces/Experimental/testLoadCtxTree.crow b/Samples/common/ui/Interfaces/Experimental/testLoadCtxTree.crow
new file mode 100644 (file)
index 0000000..3a1dd0e
--- /dev/null
@@ -0,0 +1,44 @@
+<TreeView IsRoot="true" Name="lbCtxs"  Data="{AllLoadContexts}" UseLoadingThread = 'false' >
+                       <!--<ItemTemplate DataType="System.Reflection.Assembly">
+                               <ListItem CornerRadius="2" Margin="0" Height="Fit" Width="Stretched"
+                                               Selected="{Background=${ControlHighlight}}"
+                                               Unselected="{Background=Transparent}">
+                                       <HorizontalStack>
+                                               <Label Text="{}" Width="Stretched"/>
+                                       </HorizontalStack>
+                               </ListItem>
+                       </ItemTemplate>-->
+                       <ItemTemplate DataType="System.Runtime.Loader.AssemblyLoadContext" Data="Assemblies">
+                               <ListItem
+                                               Selected="{/exp.Background=${ControlHighlight}}"
+                                               Unselected="{/exp.Background=Transparent}">
+                                       <Expandable Name="exp" Caption="{}" MouseDoubleClick="/onClickForExpand" BubbleEvents="All">
+                                               <Template>
+                                                       <VerticalStack>
+                                                               <Border CornerRadius="2" Margin="0" Height="Fit" MouseDoubleClick="./onClickForExpand"
+                                                                               Foreground="Transparent"
+                                                                               MouseEnter="{Foreground=DimGrey}"
+                                                                               MouseLeave="{Foreground=Transparent}">
+                                                                       <HorizontalStack Background="{./Background}" Spacing="1">
+                                                                               <Image Margin="1" Width="9" Height="9" Focusable="true" MouseDown="./onClickForExpand"
+                                                                                       Path="{./Image}"
+                                                                                       Visible="{./IsExpandable}"
+                                                                                       SvgSub="{./IsExpanded}"
+                                                                                       MouseEnter="{Background=LightGrey}"
+                                                                                       MouseLeave="{Background=Transparent}"/>
+                                                                               <Image Margin="1" Width="16" Height="16"
+                                                                                       Path="#Crow.Icons.folder.svg" SvgSub="{./IsExpanded}"/>
+                                                                               <Label Text="{}"/>
+                                                                       </HorizontalStack>
+                                                               </Border>
+                                                               <Container Name="Content" Visible="false"/>
+                                                       </VerticalStack>
+                                               </Template>
+                                               <HorizontalStack Height="Fit">
+                                                       <Widget Width="12" Height="10"/>
+                                                       <VerticalStack Height="Fit" Name="ItemsContainer"/>
+                                               </HorizontalStack>
+                                       </Expandable>
+                               </ListItem>
+                       </ItemTemplate> 
+               </TreeView>
\ No newline at end of file