using Mindmagma.Curses; namespace SCI.CursesWrapper.UiElements; public class MenuItem { private List _childItems; public List 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; }} /// /// Class constructor /// /// /// /// public MenuItem (string label, string? key = null, List? childItems = null) { _childItems = childItems ?? new List (); _key = key; _label = label; } /// /// Sets this menu items parent TopMenu window /// /// TopMenu window this menu item belongs to public void AssignParentTopMenuWindow (TopMenu parent) { if (_parentTopMenuWindow is not null) return; _parentTopMenuWindow = parent; } /// /// Called when key on parent TopMenu window is pressed /// /// Event sender, should be a Window object /// Key Press Event arguments 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"); } /// /// Call event handler assigned to this item being activated /// 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; } }