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 public class SingleSelectSubMenu : MenuItem
{ {
#region Static Properties #region Static Properties
public static Dictionary<MenuItem, SingleSelectSubMenu> RegisteredSubItemsAndParent = new Dictionary<MenuItem, SingleSelectSubMenu>(); private static readonly Dictionary<MenuItem, SingleSelectSubMenu> RegisteredSubItemsAndParent = new();
#endregion #endregion
#region Dependency Properties #region Dependency Properties
@ -72,11 +72,9 @@ public class SingleSelectSubMenu : MenuItem
private static void OnSelectedMenuItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) private static void OnSelectedMenuItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ {
// extract instance and guard null // extract instance and guard null
var sssm = d as SingleSelectSubMenu; if (d is not SingleSelectSubMenu sssm) { return; }
if (sssm == null) { return; }
// extract instance of new Value and guard null // extract instance of new Value and guard null
var menuItem = e.NewValue as MenuItem; if (e.NewValue is not MenuItem menuItem) { return; }
if (menuItem == null) { return; }
// get associated menu items (same group) // get associated menu items (same group)
var associatedMenuitems = GetAssociatedMenuItems(sssm); var associatedMenuitems = GetAssociatedMenuItems(sssm);
@ -100,11 +98,7 @@ public class SingleSelectSubMenu : MenuItem
private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ {
// extract instance and guard null // extract instance and guard null
var sssm = d as SingleSelectSubMenu; if (d is not SingleSelectSubMenu sssm) { return; }
if (sssm == null)
{
return;
}
sssm.Visibility = Visibility.Collapsed; sssm.Visibility = Visibility.Collapsed;
sssm.Header = sssm.Title; sssm.Header = sssm.Title;
@ -119,11 +113,9 @@ public class SingleSelectSubMenu : MenuItem
private static void OnOptionsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) private static void OnOptionsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ {
// extract instance and guard null // extract instance and guard null
var sssm = d as SingleSelectSubMenu; if (d is not SingleSelectSubMenu sssm) { return; }
if (sssm == null) { return; }
// extract parent instance of SSSM and guard null // extract parent instance of SSSM and guard null
var parent = sssm.Parent as MenuItem; if (sssm.Parent is not MenuItem parent) { return; }
if (parent == null) { return; }
// IMRPOVEMENT Delete currently associated values (using e.OldValue) from RegisteredSubItemsAndParent // IMRPOVEMENT Delete currently associated values (using e.OldValue) from RegisteredSubItemsAndParent
// create and add title menu item // create and add title menu item
@ -159,8 +151,7 @@ public class SingleSelectSubMenu : MenuItem
private static void OnAnyItemChecked(object sender, RoutedEventArgs e) private static void OnAnyItemChecked(object sender, RoutedEventArgs e)
{ {
// extract sender menuItem and guard null // extract sender menuItem and guard null
var menuItem = sender as MenuItem; if (sender is not MenuItem menuItem) { return; }
if (menuItem == null) { return; }
// get associated menu items // get associated menu items
var associatedMenuitems = GetAssociatedMenuItems(menuItem); var associatedMenuitems = GetAssociatedMenuItems(menuItem);

@ -22,7 +22,7 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter
/// </summary> /// </summary>
/// <param name="enumObject">Enum Object to get Description of.</param> /// <param name="enumObject">Enum Object to get Description of.</param>
/// <returns>String with Content of DescriptionAttribute of Enum object.</returns> /// <returns>String with Content of DescriptionAttribute of Enum object.</returns>
private string GetEnumDescription(Enum enumObject) private static string GetEnumDescription(Enum enumObject)
{ {
// guard argument null // guard argument null
if(enumObject == null) { throw new ArgumentNullException(nameof(enumObject)); } if(enumObject == null) { throw new ArgumentNullException(nameof(enumObject)); }
@ -52,7 +52,7 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter
/// </summary> /// </summary>
/// <param name="enumObject">An object of the Enum. Will be searched for other Descriptions</param> /// <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> /// <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 // guard argument null
if (enumObject == null) { throw new ArgumentNullException(nameof(enumObject)); } 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) public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ {
Enum enumValue = (Enum)value; Enum enumValue = (Enum)value;
string description = this.GetEnumDescription(enumValue); string description = GetEnumDescription(enumValue);
MenuItem newMenuItem = new() MenuItem newMenuItem = new()
{ {
Header = description Header = description
@ -113,8 +113,7 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter
if (value == null) { throw new ArgumentNullException(nameof(value)); } if (value == null) { throw new ArgumentNullException(nameof(value)); }
// extract menu item and guard null // extract menu item and guard null
MenuItem? menuObject = value as MenuItem; if (value is not MenuItem menuObject)
if (menuObject == null)
{ {
throw new Exception($"Cannot convert value that is not of type {nameof(MenuItem)} with {nameof(EnumDescriptionToMenuItemConverter)}"); 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 // get all enum descriptions and iterate
var descriptionToEnumValue = this.GetAllEnumDescriptions(enumObj); var descriptionToEnumValue = GetAllEnumDescriptions(enumObj);
foreach (var kvp in descriptionToEnumValue) foreach (var kvp in descriptionToEnumValue)
{ {
// compare key (enum description) to menu header // 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;
using System.Linq;
using System.Windows.Controls; using System.Windows.Controls;
namespace MultiTerm.Wpf.View; namespace MultiTerm.Wpf.View;
@ -12,21 +10,5 @@ public partial class ShellView : UserControl
{ {
InitializeComponent(); InitializeComponent();
this.DataContext = App.AppHost!.Services.GetService(typeof(ShellViewModel)); 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