|
|
|
using Mindmagma.Curses;
|
|
|
|
|
|
|
|
namespace SCI.CursesWrapper.UiElements;
|
|
|
|
|
|
|
|
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; }}
|
|
|
|
|
|
|
|
public delegate void ItemActivated (object sender, MenuItemActivatedEventArgs e);
|
|
|
|
|
|
|
|
// Event fired when menu item is activated
|
|
|
|
public event ItemActivated? OnItemActivated;
|
|
|
|
|
|
|
|
private TopMenu? _parentTopMenuWindow;
|
|
|
|
public TopMenu? ParentTopMenuWindow { get { return _parentTopMenuWindow; }}
|
|
|
|
|
|
|
|
/// <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) {
|
|
|
|
_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
|
|
|
|
if (OnItemActivated is null) return;
|
|
|
|
if (ParentTopMenuWindow is null) return;
|
|
|
|
|
|
|
|
if (Key.Length > 1 && Key.StartsWith ("F")) { // Handle F keys
|
|
|
|
if (e.KeyCode == CursesKey.KEY_F (int.Parse (Key.Substring (1)))) {
|
|
|
|
ActivateItem ();
|
|
|
|
}
|
|
|
|
} else if (Key.Length == 1) { // Handle letters and numbers
|
|
|
|
if (e.KeyCode == Key [0]) {
|
|
|
|
ActivateItem ();
|
|
|
|
}
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class MenuItemActivatedEventArgs : EventArgs {
|
|
|
|
private TopMenu _topMenuWindow;
|
|
|
|
public TopMenu TopMenuWindow { get { return _topMenuWindow; }}
|
|
|
|
|
|
|
|
private MenuItem _relatedMenuItem;
|
|
|
|
public MenuItem RelatedMenuItem { get { return _relatedMenuItem; }}
|
|
|
|
|
|
|
|
public MenuItemActivatedEventArgs (TopMenu sourceTopMenuWindow, MenuItem relatedMenuItem) {
|
|
|
|
_topMenuWindow = sourceTopMenuWindow;
|
|
|
|
_relatedMenuItem = relatedMenuItem;
|
|
|
|
}
|
|
|
|
}
|