You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.9 KiB

4 weeks ago
using Mindmagma.Curses;
namespace SCI.CursesWrapper.UiElements;
4 weeks ago
public class MenuItem {
private List<MenuItem> _childItems;
public List<MenuItem> ChildItems { get { return _childItems; }}
private string _label;
public string Label { get { return _label; }}
private string? _key;
public string? Key { get { return _key; }}
4 weeks ago
public delegate void ItemActivated (object sender, MenuItemActivatedEventArgs e);
4 weeks ago
// Event fired when menu item is activated
public event ItemActivated? OnItemActivated;
4 weeks ago
private TopMenu? _parentTopMenuWindow;
public TopMenu? ParentTopMenuWindow { get { return _parentTopMenuWindow; }}
4 weeks ago
/// <summary>
/// Class constructor
/// </summary>
/// <param name="label"></param>
/// <param name="key"></param>
/// <param name="childItems"></param>
public MenuItem (string label, string? key = null, List<MenuItem>? childItems = null) {
4 weeks ago
_childItems = childItems ?? new List<MenuItem> ();
_key = key;
_label = label;
}
/// <summary>
/// Sets this menu items parent TopMenu window
/// </summary>
/// <param name="parent">TopMenu window this menu item belongs to</param>
public void AssignParentTopMenuWindow (TopMenu parent) {
if (_parentTopMenuWindow is not null) return;
_parentTopMenuWindow = parent;
}
/// <summary>
/// Called when key on parent TopMenu window is pressed
/// </summary>
/// <param name="sender">Event sender, should be a Window object</param>
/// <param name="e">Key Press Event arguments</param>
public void KeypressHandler (object sender, NCursesKeyPressEventArgs e) {
// Do not listen to key presses if not hotkey is assigned
if (Key is null) return;
// Do not react to key presses unless this menu items belongs to a window
// and has event listeners upon menu item activation
4 weeks ago
if (OnItemActivated is null) return;
if (ParentTopMenuWindow is null) return;
4 weeks ago
if (Key.Length > 1 && Key.StartsWith ("F")) { // Handle F keys
if (e.KeyCode == CursesKey.KEY_F (int.Parse (Key.Substring (1)))) {
ActivateItem ();
4 weeks ago
}
} else if (Key.Length == 1) { // Handle letters and numbers
if (e.KeyCode == Key [0]) {
ActivateItem ();
4 weeks ago
}
} else throw new NotImplementedException ("Currently only F-keys and letters work for Top Menu actions");
}
/// <summary>
/// Call event handler assigned to this item being activated
/// </summary>
public void ActivateItem () {
if (OnItemActivated is null) return;
if (ParentTopMenuWindow is null) return;
var eventArgs = new MenuItemActivatedEventArgs (ParentTopMenuWindow, this);
OnItemActivated (this, eventArgs);
}
}
4 weeks ago
public class MenuItemActivatedEventArgs : EventArgs {
private TopMenu _topMenuWindow;
public TopMenu TopMenuWindow { get { return _topMenuWindow; }}
private MenuItem _relatedMenuItem;
public MenuItem RelatedMenuItem { get { return _relatedMenuItem; }}
4 weeks ago
public MenuItemActivatedEventArgs (TopMenu sourceTopMenuWindow, MenuItem relatedMenuItem) {
_topMenuWindow = sourceTopMenuWindow;
_relatedMenuItem = relatedMenuItem;
4 weeks ago
}
}