@@ -47,13 +47,12 @@ public void ToCode(CodeDomArgs args)
4747 MenuBarItem m1 = new MenuBarItem();
4848 m1.Children = new []{m1_1};
4949
50- mb.Menus = new []{m1};
51- */
52-
53- /*
50+ mb.Menus = new []{m1};*/
51+
5452 // TODO: Let user name these
5553 List < string > menus = new ( ) ;
56- foreach (var child in this.menuBar.Menus)
54+
55+ foreach ( var child in this . menuBar . SubViews . OfType < MenuBarItem > ( ) )
5756 {
5857 this . ToCode ( args , child , out string fieldName ) ;
5958 menus . Add ( fieldName ) ;
@@ -67,21 +66,23 @@ public void ToCode(CodeDomArgs args)
6766 menus . Select ( c =>
6867 new CodeFieldReferenceExpression ( new CodeThisReferenceExpression ( ) , c ) )
6968 . ToArray ( ) ) ) ;
70- */
69+
7170 }
72- /*
71+
7372 private void ToCode ( CodeDomArgs args , MenuBarItem child , out string fieldName )
7473 {
7574 fieldName = this . GetUniqueFieldName ( args , child ) ;
7675 this . AddFieldToClass ( args , child . GetType ( ) , fieldName ) ;
7776 this . AddConstructorCall ( args , $ "this.{ fieldName } ", child . GetType ( ) ) ;
7877 this . AddPropertyAssignment ( args , $ "this.{ fieldName } .{ nameof ( MenuItem . Title ) } ", child . Title ) ;
7978
79+
80+
8081 List < string ? > children = new ( ) ;
8182
8283 // TODO: Make recursive for more children
8384 // plus again let user name these
84- foreach (var sub in child.Children )
85+ foreach ( var sub in child . GetMenuItems ( ) )
8586 {
8687 if ( sub is MenuBarItem bar )
8788 {
@@ -94,47 +95,46 @@ private void ToCode(CodeDomArgs args, MenuBarItem child, out string fieldName)
9495 // its a menu separator (in Terminal.Gui separators are indicated by having a null element).
9596 children . Add ( null ) ;
9697 }
97- else
98+ else if ( sub is MenuItem mi )
9899 {
99- string subFieldName = this.GetUniqueFieldName(args, sub );
100+ string subFieldName = this . GetUniqueFieldName ( args , mi ) ;
100101 this . AddFieldToClass ( args , sub . GetType ( ) , subFieldName ) ;
101102 this . AddConstructorCall ( args , $ "this.{ subFieldName } ", sub . GetType ( ) ) ;
102103 this . AddPropertyAssignment ( args , $ "this.{ subFieldName } .{ nameof ( MenuItem . Title ) } ", sub . Title ) ;
103104 this . AddPropertyAssignment ( args , $ "this.{ subFieldName } .{ nameof ( MenuItem . Data ) } ", subFieldName ) ;
104105
105- if (sub .Key != KeyCode.Null)
106+ if ( mi . Key != KeyCode . Null )
106107 {
107108 this . AddPropertyAssignment (
108109 args ,
109110 $ "this.{ subFieldName } .{ nameof ( MenuItem . Key ) } ",
110111 new CodeCastExpression (
111112 new CodeTypeReference ( typeof ( KeyCode ) ) ,
112- new CodePrimitiveExpression((uint)sub .Key)));
113+ new CodePrimitiveExpression ( ( uint ) mi . Key ) ) ) ;
113114 }
114115
115116 children . Add ( subFieldName ) ;
116117 }
117118 }
118119
119- // TODO: This is not the way to do it in v2
120+ /*
121+ Creates code like:
122+ this.fileMenu.SetMenuItems([this.editMeMenuItem, this.editMeToo]);
123+ */
124+
120125
121126 // we have created fields and constructor calls for our menu
122127 // now set the menu to an array of all those fields
123- this.AddPropertyAssignment(
124- args,
125- $"this.{fieldName}.{"Children"}",
126- new CodeArrayCreateExpression(
127- typeof(MenuItem),
128- children.Select(c =>
128+ this . AddMethodCall ( args ,
129129
130- // the array elements have null for separator
131- c is null ? new CodePrimitiveExpression() :
130+ new CodeFieldReferenceExpression (
131+ new CodeThisReferenceExpression ( ) , fieldName ) ,
132+ "SetMenuItems" ,
133+ new CodeSnippetExpression ( $ "[{ string . Join ( "," , children . Select ( c=> "this." + c ) ) } ]")
134+ ) ;
132135
133- // or the name of the field for each menu item
134- (CodeExpression)new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), c))
135- .ToArray()));
136136 }
137- */
137+
138138 private string GetUniqueFieldName ( CodeDomArgs args , MenuItem item )
139139 {
140140 // if user has an explicit name they have set
0 commit comments