Skip to content

Commit 49b94e2

Browse files
committed
PowerControl: Provide ValueChanged event
1 parent f6dbf3c commit 49b94e2

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

PowerControl/Controller.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public Controller()
5252

5353
var contextMenu = new System.Windows.Forms.ContextMenuStrip(components);
5454

55+
rootMenu.Init();
5556
rootMenu.Visible = false;
5657
rootMenu.Update();
5758
rootMenu.CreateMenu(contextMenu);

PowerControl/Menu/MenuItemWithOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ public class MenuItemWithOptions : MenuItem
1313
public Func<string, string?>? ApplyValue { get; set; }
1414
public Func<string?>? ResetValue { get; set; }
1515

16+
public event Action<MenuItemWithOptions, String?, String> ValueChanged;
17+
1618
private System.Windows.Forms.Timer delayTimer = new System.Windows.Forms.Timer();
1719
private ToolStripMenuItem toolStripItem = new ToolStripMenuItem();
1820

1921
public MenuItemWithOptions()
2022
{
2123
this.Selectable = true;
2224

25+
ValueChanged += delegate { };
26+
2327
delayTimer.Tick += delegate (object? sender, EventArgs e)
2428
{
2529
if (delayTimer != null)
@@ -89,12 +93,17 @@ public void Set(String value, bool immediately = false)
8993

9094
private void FinalizeSet()
9195
{
96+
var wasOption = ActiveOption;
97+
9298
if (ApplyValue != null && SelectedOption != null)
9399
ActiveOption = ApplyValue(SelectedOption);
94100
else
95101
ActiveOption = SelectedOption;
96102

97103
SelectedOption = null;
104+
105+
if (wasOption != ActiveOption && ActiveOption != null)
106+
ValueChanged(this, wasOption, ActiveOption);
98107
}
99108

100109
public override void CreateMenu(System.Windows.Forms.ContextMenuStrip contextMenu)

PowerControl/Menu/MenuRoot.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ public class MenuRoot : MenuItem
66
{
77
public IList<MenuItem> Items { get; } = new List<MenuItem>();
88
public MenuItem? Selected;
9+
910
public event Action VisibleChanged;
11+
public event Action<MenuItemWithOptions, String?, String> ValueChanged;
1012

1113
public MenuRoot()
1214
{
1315
VisibleChanged += delegate { };
16+
ValueChanged += delegate { };
1417
}
1518

1619
public MenuItem? this[String name]
@@ -26,12 +29,34 @@ public MenuItem? this[String name]
2629
}
2730
}
2831

32+
public IEnumerable<MenuItemWithOptions> AllMenuItemOptions()
33+
{
34+
foreach (var item in Items)
35+
{
36+
if (item is MenuItemWithOptions)
37+
yield return (MenuItemWithOptions)item;
38+
}
39+
}
40+
2941
public override void CreateMenu(System.Windows.Forms.ContextMenuStrip contextMenu)
3042
{
3143
foreach (var item in Items)
3244
item.CreateMenu(contextMenu);
3345
}
3446

47+
public void Init()
48+
{
49+
foreach (var item in AllMenuItemOptions())
50+
{
51+
item.ValueChanged += MenuItem_ValueChanged;
52+
}
53+
}
54+
55+
private void MenuItem_ValueChanged(MenuItemWithOptions item, String? was, String isNow)
56+
{
57+
ValueChanged(item, was, isNow);
58+
}
59+
3560
public override void Update()
3661
{
3762
foreach (var item in Items)

0 commit comments

Comments
 (0)