-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMenuEntry.cs
157 lines (138 loc) · 5.09 KB
/
MenuEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Linq;
namespace Konamiman.NestorMSX.Menus
{
/// <summary>
/// Represents a menu entry to be displayed under the "Plugins" menu in the NestorMSX main window.
/// </summary>
/// <remarks>Use <see cref="PluginContext.SetMenuEntry"/> in the plugin constructor
/// to register a menu entry.</remarks>
public class MenuEntry
{
public event EventHandler EnableChanged;
public event EventHandler CheckedChanged;
public event EventHandler VisibleChanged;
public event EventHandler TitleChanged;
/// <summary>
/// Creates a menu entry that will be rendered as a separator.
/// </summary>
/// <param name="isVisible">Initial value for the IsVisible property</param>
/// <param name="isEnabled">Initial value for the IsEnabled property</param>
/// <returns></returns>
public static MenuEntry CreateSeparator(bool isVisible = true, bool isEnabled = true)
{
return new MenuEntry("-", () => { }) { IsVisible = isVisible, IsEnabled = isEnabled };
}
/// <summary>
/// Creates a new menu entry that has an associated action
/// </summary>
/// <param name="title">Title to display in the menu entry</param>
/// <param name="callback">Callback to execute when the menu entry is selected</param>
public MenuEntry(string title, Action callback) : this(title, callback, null)
{
}
/// <summary>
/// Creates a new menu entry that has set of child entries
/// </summary>
/// <param name="title">Title to display in the menu entry</param>
/// <param name="childEntries">Menu entries to show when the menu entry is selected</param>
public MenuEntry(string title, IEnumerable<MenuEntry> childEntries) : this(title, null, childEntries)
{
}
private MenuEntry(string title, Action callback, IEnumerable<MenuEntry> childEntries)
{
this.Title = title;
if (callback == null && childEntries == null)
throw new ArgumentException("Menu entries must have either an associated action or a collection of child entries");
this.Callback = callback;
this.ChildEntries = childEntries?.ToArray();
this.IsEnabled = true;
this.IsVisible = true;
}
/// <summary>
/// Gets the callback to execute when the menu entry is selected,
/// or null if the menu entry has child entries
/// </summary>
public Action Callback { get; }
/// <summary>
/// Gets the child entries to show when the menu entry is selected,
/// or null if there is a registered callback
/// </summary>
public MenuEntry[] ChildEntries { get; }
private string _Title;
/// <summary>
/// Gets or sets the title to display in the menu entry
/// </summary>
public string Title
{
get
{
return _Title;
}
set
{
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("Menu entry titles can't be null or empty");
if (value == _Title)
return;
_Title = value;
TitleChanged?.Invoke(this, EventArgs.Empty);
}
}
private bool _IsEnabled;
/// <summary>
/// Gets or sets a value indicating whether the menu entry can be selected
/// </summary>
public bool IsEnabled
{
get
{
return _IsEnabled;
}
set
{
if(value == _IsEnabled)
return;
_IsEnabled = value;
EnableChanged?.Invoke(this, EventArgs.Empty);
}
}
private bool _IsChecked;
/// <summary>
/// Gets or sets a value indicating whether a check mark is displayed next to the menu entry
/// </summary>
public bool IsChecked
{
get
{
return _IsChecked;
}
set
{
if (value == _IsChecked)
return;
_IsChecked = value;
CheckedChanged?.Invoke(this, EventArgs.Empty);
}
}
private bool _IsVisible;
/// <summary>
/// Gets or sets a value indicating whether the menu entry is currently visible
/// </summary>
public bool IsVisible
{
get
{
return _IsVisible;
}
set
{
if (value == _IsVisible)
return;
_IsVisible = value;
VisibleChanged?.Invoke(this, EventArgs.Empty);
}
}
}
}