2022-12-19 22:03:45 +01:00
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace PowerControl.Menu
|
|
|
|
|
{
|
|
|
|
|
public class MenuRoot : MenuItem
|
|
|
|
|
{
|
2022-12-19 22:21:12 +01:00
|
|
|
public IList<MenuItem> Items { get; } = new List<MenuItem>();
|
2022-12-19 22:08:54 +01:00
|
|
|
public MenuItem? Selected;
|
2022-12-19 23:45:44 +01:00
|
|
|
|
2022-12-19 22:08:54 +01:00
|
|
|
public event Action VisibleChanged;
|
2022-12-19 23:45:44 +01:00
|
|
|
public event Action<MenuItemWithOptions, String?, String> ValueChanged;
|
2022-12-19 22:03:45 +01:00
|
|
|
|
2022-12-19 22:08:54 +01:00
|
|
|
public MenuRoot()
|
|
|
|
|
{
|
|
|
|
|
VisibleChanged += delegate { };
|
2022-12-19 23:45:44 +01:00
|
|
|
ValueChanged += delegate { };
|
2022-12-19 22:08:54 +01:00
|
|
|
}
|
2022-12-19 22:03:45 +01:00
|
|
|
|
2022-12-19 22:08:54 +01:00
|
|
|
public MenuItem? this[String name]
|
2022-12-19 22:03:45 +01:00
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in Items)
|
|
|
|
|
{
|
|
|
|
|
if (item.Name == name)
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 23:45:44 +01:00
|
|
|
public IEnumerable<MenuItemWithOptions> AllMenuItemOptions()
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in Items)
|
|
|
|
|
{
|
|
|
|
|
if (item is MenuItemWithOptions)
|
|
|
|
|
yield return (MenuItemWithOptions)item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 23:30:19 +01:00
|
|
|
public override void CreateMenu(System.Windows.Forms.ContextMenuStrip contextMenu)
|
2022-12-19 22:03:45 +01:00
|
|
|
{
|
|
|
|
|
foreach (var item in Items)
|
2022-12-19 23:30:19 +01:00
|
|
|
item.CreateMenu(contextMenu);
|
2022-12-19 22:03:45 +01:00
|
|
|
}
|
2022-12-19 22:08:54 +01:00
|
|
|
|
2022-12-19 23:45:44 +01:00
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in AllMenuItemOptions())
|
|
|
|
|
{
|
|
|
|
|
item.ValueChanged += MenuItem_ValueChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MenuItem_ValueChanged(MenuItemWithOptions item, String? was, String isNow)
|
|
|
|
|
{
|
|
|
|
|
ValueChanged(item, was, isNow);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 22:03:45 +01:00
|
|
|
public override void Update()
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in Items)
|
|
|
|
|
item.Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Reset()
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in Items)
|
|
|
|
|
item.Reset();
|
2022-12-19 22:08:54 +01:00
|
|
|
VisibleChanged();
|
2022-12-19 22:03:45 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-19 22:08:54 +01:00
|
|
|
public override string Render(MenuItem? parentSelected)
|
2022-12-19 22:03:45 +01:00
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
2022-12-19 22:47:10 +01:00
|
|
|
sb.AppendJoin("", OSDHelpers);
|
2022-12-19 22:03:45 +01:00
|
|
|
if (Name != "")
|
|
|
|
|
sb.AppendLine(Color(Name, Colors.Blue));
|
|
|
|
|
|
|
|
|
|
foreach (var item in Items)
|
|
|
|
|
{
|
|
|
|
|
if (!item.Visible)
|
|
|
|
|
continue;
|
|
|
|
|
var lines = item.Render(Selected).Split("\r\n").Select(line => " " + line);
|
|
|
|
|
foreach (var line in lines)
|
|
|
|
|
sb.AppendLine(line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Show()
|
|
|
|
|
{
|
|
|
|
|
if (Visible)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Visible = true;
|
|
|
|
|
Update();
|
2022-12-19 22:08:54 +01:00
|
|
|
VisibleChanged();
|
2022-12-19 22:03:45 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 22:17:05 +01:00
|
|
|
public void Next(int change)
|
2022-12-19 22:03:45 +01:00
|
|
|
{
|
|
|
|
|
if (Show())
|
|
|
|
|
return;
|
|
|
|
|
|
2022-12-19 22:17:05 +01:00
|
|
|
int index = -1;
|
|
|
|
|
if (Selected is not null)
|
|
|
|
|
index = Items.IndexOf(Selected);
|
|
|
|
|
if (index < 0 && change < 0)
|
|
|
|
|
index = Items.Count; // Select last item if want to iterate down
|
2022-12-19 22:03:45 +01:00
|
|
|
|
|
|
|
|
for (int i = 0; i < Items.Count; i++)
|
|
|
|
|
{
|
2022-12-19 22:17:05 +01:00
|
|
|
index = (index + change + Items.Count) % Items.Count;
|
2022-12-19 22:03:45 +01:00
|
|
|
var item = Items[index];
|
|
|
|
|
if (item.Visible && item.Selectable)
|
|
|
|
|
{
|
|
|
|
|
Selected = item;
|
2022-12-19 22:08:54 +01:00
|
|
|
VisibleChanged();
|
2022-12-19 22:03:45 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 22:17:05 +01:00
|
|
|
public override void SelectNext(int change)
|
2022-12-19 22:03:45 +01:00
|
|
|
{
|
|
|
|
|
if (Show())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (Selected != null)
|
|
|
|
|
{
|
2022-12-19 22:17:05 +01:00
|
|
|
Selected.SelectNext(change);
|
2022-12-19 22:08:54 +01:00
|
|
|
VisibleChanged();
|
2022-12-19 22:03:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 22:17:05 +01:00
|
|
|
public MenuItem? Select(String name)
|
2022-12-19 22:03:45 +01:00
|
|
|
{
|
2022-12-19 22:17:05 +01:00
|
|
|
Selected = this[name];
|
|
|
|
|
if (Selected is null)
|
|
|
|
|
return null;
|
2022-12-19 22:03:45 +01:00
|
|
|
|
|
|
|
|
Show();
|
2022-12-19 22:08:54 +01:00
|
|
|
VisibleChanged();
|
2022-12-19 22:17:05 +01:00
|
|
|
return Selected;
|
2022-12-19 22:03:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-12-19 22:21:12 +01:00
|
|
|
}
|