From ebe1cdba874c91ee2b624edf1178007b27fcad50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Mon, 19 Dec 2022 22:21:12 +0100 Subject: [PATCH] PowerControl: Improve `nulls` in `MenuItems` --- PowerControl/Menu/MenuItemWithOptions.cs | 21 ++++++++------------- PowerControl/Menu/MenuRoot.cs | 4 ++-- PowerControl/Menu/MenuSeparator.cs | 6 +----- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/PowerControl/Menu/MenuItemWithOptions.cs b/PowerControl/Menu/MenuItemWithOptions.cs index efbdc0e..9182214 100644 --- a/PowerControl/Menu/MenuItemWithOptions.cs +++ b/PowerControl/Menu/MenuItemWithOptions.cs @@ -7,18 +7,18 @@ namespace PowerControl.Menu public delegate object ApplyValueDelegate(object selected); public IList Options { get; set; } = new List(); - public Object SelectedOption { get; set; } - public Object ActiveOption { get; set; } + public Object? SelectedOption { get; set; } + public Object? ActiveOption { get; set; } public int ApplyDelay { get; set; } public bool CycleOptions { get; set; } = true; - public CurrentValueDelegate CurrentValue { get; set; } - public OptionsValueDelegate OptionsValues { get; set; } - public ApplyValueDelegate ApplyValue { get; set; } - public CurrentValueDelegate ResetValue { get; set; } + public Func? CurrentValue { get; set; } + public Func? OptionsValues { get; set; } + public Func? ApplyValue { get; set; } + public Func? ResetValue { get; set; } - private System.Windows.Forms.Timer delayTimer; - private ToolStripMenuItem toolStripItem; + private System.Windows.Forms.Timer delayTimer = new System.Windows.Forms.Timer(); + private ToolStripMenuItem toolStripItem = new ToolStripMenuItem(); public MenuItemWithOptions() { @@ -85,7 +85,6 @@ namespace PowerControl.Menu return; } - delayTimer = new System.Windows.Forms.Timer(); delayTimer.Interval = ApplyDelay > 0 ? ApplyDelay : 1; delayTimer.Tick += delegate (object? sender, EventArgs e) { @@ -142,10 +141,6 @@ namespace PowerControl.Menu public override void CreateMenu(ToolStripItemCollection collection) { - if (toolStripItem != null) - return; - - toolStripItem = new ToolStripMenuItem(); toolStripItem.Text = Name; updateOptions(); collection.Add(toolStripItem); diff --git a/PowerControl/Menu/MenuRoot.cs b/PowerControl/Menu/MenuRoot.cs index 2fd80ec..9956e2d 100644 --- a/PowerControl/Menu/MenuRoot.cs +++ b/PowerControl/Menu/MenuRoot.cs @@ -4,7 +4,7 @@ namespace PowerControl.Menu { public class MenuRoot : MenuItem { - public IList Items { get; set; } = new List(); + public IList Items { get; } = new List(); public MenuItem? Selected; public event Action VisibleChanged; @@ -123,4 +123,4 @@ namespace PowerControl.Menu return Selected; } } -} \ No newline at end of file +} diff --git a/PowerControl/Menu/MenuSeparator.cs b/PowerControl/Menu/MenuSeparator.cs index 1702f98..9fe82b1 100644 --- a/PowerControl/Menu/MenuSeparator.cs +++ b/PowerControl/Menu/MenuSeparator.cs @@ -2,7 +2,7 @@ namespace PowerControl.Menu { public class MenuItemSeparator : MenuItem { - private ToolStripItem toolStripItem; + private ToolStripItem toolStripItem = new ToolStripSeparator(); public MenuItemSeparator() { @@ -11,10 +11,6 @@ namespace PowerControl.Menu public override void CreateMenu(ToolStripItemCollection collection) { - if (toolStripItem != null) - return; - - toolStripItem = new ToolStripSeparator(); collection.Add(toolStripItem); }