|
|
|
using SCI.CursesWrapper;
|
|
|
|
using Mindmagma.Curses;
|
|
|
|
|
|
|
|
namespace ANSI_Fahrplan.UiElements;
|
|
|
|
|
|
|
|
public class TopMenu : UiElement {
|
|
|
|
private List<MenuItem> _menuItems = new List<MenuItem> ();
|
|
|
|
public List<MenuItem> MenuItems {
|
|
|
|
get {
|
|
|
|
return _menuItems;
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
UnregisterItemEventHandlers ();
|
|
|
|
_menuItems = value;
|
|
|
|
RegisterItemEventHandlers ();
|
|
|
|
DrawMenu ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private MenuItem? activeMenuItem;
|
|
|
|
|
|
|
|
public TopMenu (nint screen, List<MenuItem> menuItems) : base (screen) {
|
|
|
|
NCurses.GetMaxYX (screen, out _, out int width);
|
|
|
|
innerWindow = NCurses.NewWindow (1, width, 0, 0);
|
|
|
|
|
|
|
|
var colorSchemeNormal = ColorSchemes.TopMenuNormal ();
|
|
|
|
NCurses.WindowBackground (innerWindow, colorSchemeNormal);
|
|
|
|
NCurses.WindowAttributeOn (innerWindow, colorSchemeNormal);
|
|
|
|
|
|
|
|
MenuItems = menuItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Register key event handlers for each menu item
|
|
|
|
/// </summary>
|
|
|
|
private void RegisterItemEventHandlers () {
|
|
|
|
foreach (var item in MenuItems) {
|
|
|
|
item.OnItemActivated += ItemActivatedHandler;
|
|
|
|
item.RegisterKeyHandler ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Unregister key event handlers for each menu item
|
|
|
|
/// </summary>
|
|
|
|
private void UnregisterItemEventHandlers () {
|
|
|
|
foreach (var item in MenuItems) {
|
|
|
|
item.OnItemActivated -= ItemActivatedHandler;
|
|
|
|
item.UnregisterKeyHandler ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Event handler to update menu interface when item reports itself as activated
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">Sender Menu Item firing the event</param>
|
|
|
|
/// <param name="e">Event arguments</param>
|
|
|
|
private void ItemActivatedHandler (object? sender, EventArgs e) {
|
|
|
|
if (sender is null) return;
|
|
|
|
|
|
|
|
activeMenuItem = (MenuItem) sender;
|
|
|
|
DrawMenu ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Draw the top menu with currently configured menu items
|
|
|
|
/// </summary>
|
|
|
|
private void DrawMenu () {
|
|
|
|
NCurses.ClearWindow (innerWindow);
|
|
|
|
|
|
|
|
var normalColor = ColorSchemes.TopMenuNormal ();
|
|
|
|
var activeColor = ColorSchemes.TopMenuActive ();
|
|
|
|
|
|
|
|
foreach (var item in MenuItems) {
|
|
|
|
var itemIsActive = item == activeMenuItem;
|
|
|
|
var itemString = " ";
|
|
|
|
|
|
|
|
if (itemIsActive) {
|
|
|
|
itemString = "[";
|
|
|
|
NCurses.WindowAttributeOff (innerWindow, normalColor);
|
|
|
|
NCurses.WindowAttributeOn (innerWindow, activeColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
itemString += $"{item.Label}";
|
|
|
|
|
|
|
|
if (item.Key is not null) {
|
|
|
|
itemString += $" ({item.Key})";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemIsActive) {
|
|
|
|
NCurses.WindowAttributeOff (innerWindow, activeColor);
|
|
|
|
NCurses.WindowAttributeOn (innerWindow, normalColor);
|
|
|
|
itemString += "] ";
|
|
|
|
} else {
|
|
|
|
itemString += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
NCurses.WindowAddString (innerWindow, itemString);
|
|
|
|
}
|
|
|
|
|
|
|
|
NCurses.Refresh ();
|
|
|
|
NCurses.WindowRefresh (innerWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Refresh () {
|
|
|
|
DrawMenu ();
|
|
|
|
}
|
|
|
|
}
|