6
6
7
7
static class MenuBuilder
8
8
{
9
- static List < ToolStripItem > ? itemsToCleanup ;
10
-
11
9
public static ContextMenuStrip Build ( Action exit , Action launchOptions , Tracker tracker )
12
10
{
13
11
var menu = new ContextMenuStrip ( ) ;
14
12
var items = menu . Items ;
15
13
menu . Opening += delegate
16
14
{
17
- DisposePreviousItems ( ) ;
15
+ DisposePreviousItems ( items ) ;
18
16
19
17
foreach ( var item in BuildTrackingMenuItems ( tracker ) )
20
18
{
21
19
items . Add ( item ) ;
22
20
}
23
21
} ;
24
22
menu . Font = new Font ( menu . Font . FontFamily , 10 ) ;
25
- menu . Closed += delegate { CleanTransientMenus ( items ) ; } ;
26
23
items . Add ( new MenuButton ( "Exit" , exit , Images . Exit ) ) ;
27
24
items . Add ( new MenuButton ( "Options" , launchOptions , Images . Options ) ) ;
28
25
items . Add ( new MenuButton ( "Open logs" , Logging . OpenDirectory , Images . Folder ) ) ;
@@ -40,25 +37,15 @@ static List<ToolStripItem> NonDefaultMenus(ToolStripItemCollection items)
40
37
. ToList ( ) ;
41
38
}
42
39
43
- static void DisposePreviousItems ( )
40
+ static void DisposePreviousItems ( ToolStripItemCollection items )
44
41
{
45
- if ( itemsToCleanup == null )
46
- {
47
- return ;
48
- }
49
-
50
- foreach ( var item in itemsToCleanup )
42
+ foreach ( var item in NonDefaultMenus ( items ) )
51
43
{
44
+ items . Remove ( item ) ;
52
45
item . Dispose ( ) ;
53
46
}
54
47
}
55
48
56
- static void CleanTransientMenus ( ToolStripItemCollection items )
57
- {
58
- itemsToCleanup = NonDefaultMenus ( items ) ;
59
- items . RemoveRange ( itemsToCleanup ) ;
60
- }
61
-
62
49
static IEnumerable < ToolStripItem > BuildTrackingMenuItems ( Tracker tracker )
63
50
{
64
51
if ( ! tracker . TrackingAny )
0 commit comments