@@ -69,6 +69,57 @@ public void ToCode(CodeDomArgs args)
6969 }
7070
7171 private void ToCode ( CodeDomArgs args , MenuItem child , out string fieldName )
72+ {
73+ CreateMenuMembersAndPropertyAssignments ( args , child , out fieldName ) ;
74+
75+ List < string ? > children = new ( ) ;
76+
77+ bool wasPopover ;
78+
79+ // TODO: Make recursive for more children
80+ // plus again let user name these
81+ foreach ( var mi in child . GetMenuItems ( out wasPopover ) )
82+ {
83+ string subFieldName ;
84+
85+ // If it has its own children e.g.
86+ // File->New->Project
87+ if ( mi . SubMenu != null )
88+ {
89+ ToCode ( args , mi , out subFieldName ) ;
90+ }
91+ else
92+ {
93+
94+ // It has no children of its own e.g. its just Edit->Paste
95+ CreateMenuMembersAndPropertyAssignments ( args , mi , out subFieldName ) ;
96+ }
97+
98+ children . Add ( subFieldName ) ;
99+ }
100+
101+ if ( wasPopover )
102+ {
103+ // this.fileMenu.PopoverMenu = new PopoverMenu([editMeMenuItem]);
104+ this . AddPropertyAssignment ( args , $ "this.{ fieldName } .{ nameof ( MenuBarItem . PopoverMenu ) } ",
105+ new CodeSnippetExpression ( $ "new PopoverMenu([{ string . Join ( "," , children ) } ])") ) ;
106+ }
107+ else
108+ {
109+ // this.newMenu.SubMenu = new Menu([carMenuItem]);
110+ this . AddPropertyAssignment ( args , $ "this.{ fieldName } .{ nameof ( MenuBarItem . SubMenu ) } ",
111+ new CodeSnippetExpression ( $ "new Menu([{ string . Join ( "," , children ) } ])") ) ;
112+ }
113+ }
114+
115+ /// <summary>
116+ /// Creates all class fields and property assignments for <see cref="MenuItem"/> excluding
117+ /// child menu items (which are handled in <see cref="ToCode(CodeDomArgs, MenuItem, out string)"/>)
118+ /// </summary>
119+ /// <param name="args"></param>
120+ /// <param name="child"></param>
121+ /// <param name="fieldName"></param>
122+ private void CreateMenuMembersAndPropertyAssignments ( CodeDomArgs args , MenuItem child , out string fieldName )
72123 {
73124 // ------------ Class Fields -------------
74125
@@ -83,49 +134,14 @@ private void ToCode(CodeDomArgs args, MenuItem child, out string fieldName)
83134
84135 // this.fileMenu.Title = "_File";
85136 this . AddPropertyAssignment ( args , $ "this.{ fieldName } .{ nameof ( MenuItem . Title ) } ", child . Title ) ;
86-
137+
87138 // TODO: Verify that all ToString exactly match the static property
88139 // this.fileMenu.Key = Key.F9;
89- this . AddPropertyAssignment ( args , $ "this.{ fieldName } .{ nameof ( MenuItem . Key ) } ",
90- new CodeSnippetExpression ( $ "Key.{ child . Key } ") ) ;
91-
92-
93- List < string ? > children = new ( ) ;
94-
95- // TODO: Make recursive for more children
96- // plus again let user name these
97- foreach ( var mi in child . GetMenuItems ( ) )
140+ if ( child . Key != KeyCode . Null )
98141 {
99- string subFieldName = this . GetUniqueFieldName ( args , mi ) ;
100- this . AddFieldToClass ( args , mi . GetType ( ) , subFieldName ) ;
101- this . AddConstructorCall ( args , $ "this.{ subFieldName } ", mi . GetType ( ) ) ;
102- this . AddPropertyAssignment ( args , $ "this.{ subFieldName } .{ nameof ( MenuItem . Title ) } ", mi . Title ) ;
103- this . AddPropertyAssignment ( args , $ "this.{ subFieldName } .{ nameof ( MenuItem . Data ) } ", subFieldName ) ;
104-
105- if ( mi . Key != KeyCode . Null )
106- {
107- this . AddPropertyAssignment (
108- args ,
109- $ "this.{ subFieldName } .{ nameof ( MenuItem . Key ) } ",
110- new CodeCastExpression (
111- new CodeTypeReference ( typeof ( KeyCode ) ) ,
112- new CodePrimitiveExpression ( ( uint ) mi . Key ) ) ) ;
113- }
114-
115- // If it has its own children e.g.
116- // File->New->Project
117- if ( mi . SubMenu != null )
118- {
119- //ToCode(args, mi, out string subFieldName2);
120- }
121-
122- children . Add ( subFieldName ) ;
123-
142+ this . AddPropertyAssignment ( args , $ "this.{ fieldName } .{ nameof ( MenuItem . Key ) } ",
143+ new CodeSnippetExpression ( $ "Key.{ child . Key } ") ) ;
124144 }
125-
126- // this.fileMenu.PopoverMenu = new PopoverMenu([editMeMenuItem]);
127- this . AddPropertyAssignment ( args , $ "this.{ fieldName } .{ nameof ( MenuBarItem . PopoverMenu ) } ",
128- new CodeSnippetExpression ( $ "new PopoverMenu([{ string . Join ( "," , children ) } ])") ) ;
129145 }
130146
131147 private string GetUniqueFieldName ( CodeDomArgs args , MenuItem item )
0 commit comments