resolved warnings and messages, beautified code

master
Jonas Arnold 3 years ago
parent 1c2e67df8d
commit 96ab06353d
  1. 23
      MultiTerm.Wpf/Controls/SingleSelectSubMenu.cs
  2. 11
      MultiTerm.Wpf/ValueConverters/EnumDescriptionToMenuItemConverter.cs
  3. 20
      MultiTerm.Wpf/View/ShellView.xaml.cs

@ -13,7 +13,7 @@ namespace MultiTerm.Wpf.Controls;
public class SingleSelectSubMenu : MenuItem
{
#region Static Properties
public static Dictionary<MenuItem, SingleSelectSubMenu> RegisteredSubItemsAndParent = new Dictionary<MenuItem, SingleSelectSubMenu>();
private static readonly Dictionary<MenuItem, SingleSelectSubMenu> 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);

@ -22,7 +22,7 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter
/// </summary>
/// <param name="enumObject">Enum Object to get Description of.</param>
/// <returns>String with Content of DescriptionAttribute of Enum object.</returns>
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
/// </summary>
/// <param name="enumObject">An object of the Enum. Will be searched for other Descriptions</param>
/// <returns>Key Value Pair of Description and respective Enum Value</returns>
private Dictionary<string, Enum> GetAllEnumDescriptions(Enum enumObject)
private static Dictionary<string, Enum> 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

@ -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<NewlineSeparatorType>();
//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");
}
}

Loading…
Cancel
Save