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.

52 lines
1.5 KiB

using SCI.CursesWrapper;
4 weeks ago
using Mindmagma.Curses;
namespace ANSI_Fahrplan.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, EventArgs e);
// Event fired when menu item is activated
public event EventHandler? OnItemActivated;
private InputHandler _inputHandler;
public InputHandler InputHandler { get { return _inputHandler; }}
public MenuItem (string label, string key, InputHandler inputHandler, List<MenuItem>? childItems = null) {
_childItems = childItems ?? new List<MenuItem> ();
_key = key;
_inputHandler = inputHandler;
_label = label;
}
public void KeyHandler (object sender, NCursesKeyPressEventArgs e) {
if (OnItemActivated is null) return;
if (Key.Length > 1 && Key.StartsWith ("F")) { // Handle F keys
if (e.KeyCode == CursesKey.KEY_F (int.Parse (Key.Substring (1)))) {
OnItemActivated (this, EventArgs.Empty);
}
} else if (Key.Length == 1) { // Handle letters and numbers
if (e.KeyCode == Key [0]) {
OnItemActivated (this, EventArgs.Empty);
}
} else throw new NotImplementedException ("Currently only F-keys and letters work for Top Menu actions");
}
public void RegisterKeyHandler () {
InputHandler.OnKeyPress += KeyHandler;
}
public void UnregisterKeyHandler () {
InputHandler.OnKeyPress -= KeyHandler;
}
}