Skip to content

Commit 9a80397

Browse files
author
tznind
committed
Fix menu item to code
1 parent a551188 commit 9a80397

10 files changed

Lines changed: 106 additions & 66 deletions

Showcase/Menus.Designer.cs

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MenuBarExtensions.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,29 +79,39 @@ public static class MenuBarExtensions
7979
}
8080

8181
/// <summary>
82-
/// Gets the Menu that contains the child items for this MenuItem.
83-
/// For MenuBarItem, this is PopoverMenu.Root. For MenuItem, this is SubMenu.
84-
/// </summary>
82+
/// <para>
83+
/// A MenuItem can have 2 kinds of submenu. If the MenuItem is an element on a
84+
/// is on a MenuBar (i.e. a top level menu item like File, Edit, View etc) then it will
85+
/// have a PopoverMenu.
86+
/// </para>
87+
/// <para>
88+
/// Otherwise if it is a regular MenuItem entry e.g. File->New then it may have an SubMenu
89+
/// ordinary SubMenu i.e. not a popover.
90+
/// </para>
8591
/// <param name="menuItem">The MenuItem to get the menu from.</param>
92+
///
8693
/// <returns>The Menu containing child items, or null if none exists.</returns>
87-
public static Menu? GetChildMenu(this MenuItem menuItem)
94+
public static Menu? GetChildMenu(this MenuItem menuItem, out bool wasPopover)
8895
{
8996
if (menuItem is MenuBarItem mbi)
9097
{
98+
wasPopover = true;
9199
return mbi.PopoverMenu?.Root;
92100
}
93101

102+
wasPopover = false;
94103
return menuItem.SubMenu;
95104
}
96105

97106
/// <summary>
98107
/// Gets all MenuItem children from this MenuItem's menu.
99108
/// </summary>
100109
/// <param name="menuItem">The MenuItem to get children from.</param>
110+
/// <param name="wasPopover"></param>
101111
/// <returns>List of MenuItem children, or empty list if no menu exists.</returns>
102-
public static List<MenuItem> GetMenuItems(this MenuItem menuItem)
112+
public static List<MenuItem> GetMenuItems(this MenuItem menuItem, out bool wasPopover)
103113
{
104-
var menu = menuItem.GetChildMenu();
114+
var menu = menuItem.GetChildMenu(out wasPopover);
105115
return menu?.SubViews.OfType<MenuItem>().ToList() ?? new List<MenuItem>();
106116
}
107117

@@ -113,7 +123,7 @@ public static List<MenuItem> GetMenuItems(this MenuItem menuItem)
113123
/// <param name="newItems">The new list of MenuItems in the desired order.</param>
114124
public static void SetMenuItems(this MenuItem menuItem, List<MenuItem> newItems)
115125
{
116-
var menu = menuItem.GetChildMenu();
126+
var menu = menuItem.GetChildMenu(out _);
117127
if (menu == null)
118128
{
119129
return;
@@ -151,7 +161,7 @@ public static void SetMenuItems(this MenuItem menuItem, List<MenuItem> newItems)
151161
/// <param name="itemToInsert">The MenuItem to insert.</param>
152162
public static void InsertMenuItem(this MenuItem menuItem, int index, MenuItem itemToInsert)
153163
{
154-
var items = menuItem.GetMenuItems();
164+
var items = menuItem.GetMenuItems(out _);
155165
items.Insert(Math.Min(index, items.Count), itemToInsert);
156166
menuItem.SetMenuItems(items);
157167

@@ -166,7 +176,7 @@ public static void InsertMenuItem(this MenuItem menuItem, int index, MenuItem it
166176
/// <returns>True if the item was found and removed.</returns>
167177
public static bool RemoveMenuItem(this MenuItem menuItem, MenuItem itemToRemove)
168178
{
169-
var menu = menuItem.GetChildMenu();
179+
var menu = menuItem.GetChildMenu(out _);
170180
if (menu == null)
171181
{
172182
return false;

src/MenuTracker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ private void ConvertEmptySubMenus(MenuItem menuItem)
270270
private MenuItem? FindParentRecursive(MenuItem item, MenuItem potentialParent)
271271
{
272272
// Check if the item is directly in this MenuItem's children
273-
var children = potentialParent.GetMenuItems();
273+
var children = potentialParent.GetMenuItems(out _);
274274
if (children.Contains(item))
275275
{
276276
return potentialParent;

src/Operations/MenuOperations/AddMenuItemOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private bool Add(MenuItem menuItem)
6767
return false;
6868
}
6969

70-
var children = this.Parent.GetMenuItems();
70+
var children = this.Parent.GetMenuItems(out _);
7171
var currentItemIdx = children.IndexOf(this.OperateOn);
7272

7373
// We are the parent but parents children don't contain us. That's bad. TODO: log this

src/Operations/MenuOperations/ConvertMenuItemToSeperatorOperation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected override void UndoImpl()
3939
return;
4040
}
4141

42-
var menu = this.Parent.GetChildMenu();
42+
var menu = this.Parent.GetChildMenu(out _);
4343
if (menu == null)
4444
{
4545
return;
@@ -81,13 +81,13 @@ protected override bool DoImpl()
8181
return false;
8282
}
8383

84-
var menu = this.Parent.GetChildMenu();
84+
var menu = this.Parent.GetChildMenu(out _);
8585
if (menu == null)
8686
{
8787
return false;
8888
}
8989

90-
var items = this.Parent.GetMenuItems();
90+
var items = this.Parent.GetMenuItems(out _);
9191
this.removedAtIdx = Math.Max(0, items.IndexOf(this.OperateOn));
9292

9393
// Find the actual index in SubViews

src/Operations/MenuOperations/MoveMenuItemLeftOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public MoveMenuItemLeftOperation(IApplication app, MenuItem toMove)
4343

4444
if (this.Parent != null)
4545
{
46-
var items = this.Parent.GetMenuItems();
46+
var items = this.Parent.GetMenuItems(out _);
4747
this.pulledFromIndex = items.IndexOf(this.OperateOn);
4848
}
4949
}
@@ -83,7 +83,7 @@ protected override bool DoImpl()
8383
}
8484

8585
// Figure out where the parent is in the list
86-
var parentsParentItems = parentsParent.GetMenuItems();
86+
var parentsParentItems = parentsParent.GetMenuItems(out _);
8787
var parentsIdx = parentsParentItems.IndexOf(this.Parent);
8888

8989
// remove us from our current location

src/Operations/MenuOperations/MoveMenuItemOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public MoveMenuItemOperation(IApplication app, MenuItem toMove, bool up)
3434
return;
3535
}
3636

37-
this.siblings = this.Parent.GetMenuItems();
37+
this.siblings = this.Parent.GetMenuItems(out _);
3838
this.currentItemIdx = this.siblings.IndexOf(this.OperateOn);
3939

4040
if (this.currentItemIdx < 0)

src/Operations/MenuOperations/MoveMenuItemRightOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public MoveMenuItemRightOperation(IApplication app, MenuItem toMove)
2626
return;
2727
}
2828

29-
var items = this.Parent.GetMenuItems();
29+
var items = this.Parent.GetMenuItems(out _);
3030
int idx = items.IndexOf(toMove);
3131

3232
// Can't move right if we're the first item (no item above to become parent)
@@ -74,7 +74,7 @@ protected override bool DoImpl()
7474
}
7575

7676
// When user hits shift right
77-
var children = this.Parent.GetMenuItems();
77+
var children = this.Parent.GetMenuItems(out _);
7878
var currentItemIdx = children.IndexOf(this.OperateOn);
7979
var aboveIdx = currentItemIdx - 1;
8080

src/Operations/MenuOperations/RemoveMenuItemOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected override void UndoImpl()
8383
{
8484
if (grandparent != null)
8585
{
86-
var grandparentItems = grandparent.GetMenuItems();
86+
var grandparentItems = grandparent.GetMenuItems(out _);
8787
int replacementIndex = grandparentItems.IndexOf(converted.Value);
8888
if(replacementIndex >= 0 && replacementIndex < grandparentItems.Count)
8989
{
@@ -139,7 +139,7 @@ protected override bool DoImpl()
139139
return false;
140140
}
141141

142-
var items = this.Parent.GetMenuItems();
142+
var items = this.Parent.GetMenuItems(out _);
143143
this.removedAtIdx = Math.Max(0, items.IndexOf(this.OperateOn));
144144

145145
this.Parent.RemoveMenuItem(this.OperateOn);

src/ToCode/MenuBarItemsToCode.cs

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)