From 96ab06353da82c6fc5f6937c124afcf362f98a93 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Sun, 26 Mar 2023 15:29:34 +0200 Subject: [PATCH] resolved warnings and messages, beautified code --- MultiTerm.Wpf/Controls/SingleSelectSubMenu.cs | 23 ++++++------------- .../EnumDescriptionToMenuItemConverter.cs | 11 ++++----- MultiTerm.Wpf/View/ShellView.xaml.cs | 20 +--------------- 3 files changed, 13 insertions(+), 41 deletions(-) diff --git a/MultiTerm.Wpf/Controls/SingleSelectSubMenu.cs b/MultiTerm.Wpf/Controls/SingleSelectSubMenu.cs index aad2f3a..67a737e 100644 --- a/MultiTerm.Wpf/Controls/SingleSelectSubMenu.cs +++ b/MultiTerm.Wpf/Controls/SingleSelectSubMenu.cs @@ -13,7 +13,7 @@ namespace MultiTerm.Wpf.Controls; public class SingleSelectSubMenu : MenuItem { #region Static Properties - public static Dictionary RegisteredSubItemsAndParent = new Dictionary(); + private static readonly Dictionary RegisteredSubItemsAndParent = new(); #endregion #region Dependency Properties @@ -72,11 +72,9 @@ public class SingleSelectSubMenu : MenuItem private static void OnSelectedMenuItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // extract instance and guard null - var sssm = d as SingleSelectSubMenu; - if (sssm == null) { return; } + if (d is not SingleSelectSubMenu sssm) { return; } // extract instance of new Value and guard null - var menuItem = e.NewValue as MenuItem; - if (menuItem == null) { return; } + if (e.NewValue is not MenuItem menuItem) { return; } // get associated menu items (same group) var associatedMenuitems = GetAssociatedMenuItems(sssm); @@ -100,11 +98,7 @@ public class SingleSelectSubMenu : MenuItem private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // extract instance and guard null - var sssm = d as SingleSelectSubMenu; - if (sssm == null) - { - return; - } + if (d is not SingleSelectSubMenu sssm) { return; } sssm.Visibility = Visibility.Collapsed; sssm.Header = sssm.Title; @@ -119,11 +113,9 @@ public class SingleSelectSubMenu : MenuItem private static void OnOptionsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // extract instance and guard null - var sssm = d as SingleSelectSubMenu; - if (sssm == null) { return; } + if (d is not SingleSelectSubMenu sssm) { return; } // extract parent instance of SSSM and guard null - var parent = sssm.Parent as MenuItem; - if (parent == null) { return; } + if (sssm.Parent is not MenuItem parent) { return; } // IMRPOVEMENT Delete currently associated values (using e.OldValue) from RegisteredSubItemsAndParent // create and add title menu item @@ -159,8 +151,7 @@ public class SingleSelectSubMenu : MenuItem private static void OnAnyItemChecked(object sender, RoutedEventArgs e) { // extract sender menuItem and guard null - var menuItem = sender as MenuItem; - if (menuItem == null) { return; } + if (sender is not MenuItem menuItem) { return; } // get associated menu items var associatedMenuitems = GetAssociatedMenuItems(menuItem); diff --git a/MultiTerm.Wpf/ValueConverters/EnumDescriptionToMenuItemConverter.cs b/MultiTerm.Wpf/ValueConverters/EnumDescriptionToMenuItemConverter.cs index 18048d7..9bced4b 100644 --- a/MultiTerm.Wpf/ValueConverters/EnumDescriptionToMenuItemConverter.cs +++ b/MultiTerm.Wpf/ValueConverters/EnumDescriptionToMenuItemConverter.cs @@ -22,7 +22,7 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter /// /// Enum Object to get Description of. /// String with Content of DescriptionAttribute of Enum object. - private string GetEnumDescription(Enum enumObject) + private static string GetEnumDescription(Enum enumObject) { // guard argument null if(enumObject == null) { throw new ArgumentNullException(nameof(enumObject)); } @@ -52,7 +52,7 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter /// /// An object of the Enum. Will be searched for other Descriptions /// Key Value Pair of Description and respective Enum Value - private Dictionary GetAllEnumDescriptions(Enum enumObject) + private static Dictionary GetAllEnumDescriptions(Enum enumObject) { // guard argument null if (enumObject == null) { throw new ArgumentNullException(nameof(enumObject)); } @@ -87,7 +87,7 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Enum enumValue = (Enum)value; - string description = this.GetEnumDescription(enumValue); + string description = GetEnumDescription(enumValue); MenuItem newMenuItem = new() { Header = description @@ -113,8 +113,7 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter if (value == null) { throw new ArgumentNullException(nameof(value)); } // extract menu item and guard null - MenuItem? menuObject = value as MenuItem; - if (menuObject == null) + if (value is not MenuItem menuObject) { throw new Exception($"Cannot convert value that is not of type {nameof(MenuItem)} with {nameof(EnumDescriptionToMenuItemConverter)}"); } @@ -127,7 +126,7 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter } // get all enum descriptions and iterate - var descriptionToEnumValue = this.GetAllEnumDescriptions(enumObj); + var descriptionToEnumValue = GetAllEnumDescriptions(enumObj); foreach (var kvp in descriptionToEnumValue) { // compare key (enum description) to menu header diff --git a/MultiTerm.Wpf/View/ShellView.xaml.cs b/MultiTerm.Wpf/View/ShellView.xaml.cs index 91d19a7..84c3eb3 100644 --- a/MultiTerm.Wpf/View/ShellView.xaml.cs +++ b/MultiTerm.Wpf/View/ShellView.xaml.cs @@ -1,7 +1,5 @@ -using MultiTerm.Core.Common; -using MultiTerm.Core.ViewModel; +using MultiTerm.Core.ViewModel; using System; -using System.Linq; using System.Windows.Controls; namespace MultiTerm.Wpf.View; @@ -12,21 +10,5 @@ public partial class ShellView : UserControl { InitializeComponent(); this.DataContext = App.AppHost!.Services.GetService(typeof(ShellViewModel)); - this.PopulateSettingsNewlineSelectors(); - } - - private void PopulateSettingsNewlineSelectors() - { - //var types = Enum.GetValues(typeof(NewlineSeparatorType)).Cast(); - //foreach (var newlineSeparatorType in types) - //{ - // this.newlineReceiveMenuItem.Items.Add(new MenuItem() { Header = newlineSeparatorType }); - // this.newlineSendMenuItem.Items.Add(new MenuItem() { Header = newlineSeparatorType }); - //} - } - - private void MenuItem_IsCheckedChanged(object sender, System.Windows.RoutedEventArgs e) - { - Console.WriteLine("changed"); } }