Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Milkitic committed Dec 31, 2023
1 parent 8db9584 commit d94ab10
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 54 deletions.
32 changes: 32 additions & 0 deletions KeyAsio.Gui/Converters/Enum2ListConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace KeyAsio.Gui.Converters;

public class Enum2ListConverter : IValueConverter
{
private readonly Type _enumBaseType = typeof(Enum);

private static readonly Dictionary<Type, Enum[]> CachedList = new();

public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (parameter is not Type t || !t.IsSubclassOf(_enumBaseType))
return DependencyProperty.UnsetValue;
if (CachedList.TryGetValue(t, out var enums))
return enums;

var array = Enum.GetValues(t).Cast<Enum>().ToArray();
CachedList.Add(t, array);
return array;
}

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
4 changes: 2 additions & 2 deletions KeyAsio.Gui/Converters/IntMillisecond2TimeSpanConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace KeyAsio.Gui.Converters;

internal class IntMillisecond2TimeSpanConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object parameter, CultureInfo culture)
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
var i = (int)value!;
Span<char> c = stackalloc char[10];
Expand All @@ -21,7 +21,7 @@ public object Convert(object? value, Type targetType, object parameter, CultureI
return vsb.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
Expand Down
4 changes: 2 additions & 2 deletions KeyAsio.Gui/Converters/IsNullToVisibilityConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace KeyAsio.Gui.Converters;

internal class IsNullToVisibilityConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object parameter, CultureInfo culture)
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return value is null ? Visibility.Collapsed : Visibility.Visible;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
Expand Down
4 changes: 2 additions & 2 deletions KeyAsio.Gui/Converters/IsNullToVisibilityReConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace KeyAsio.Gui.Converters;

internal class IsNullToVisibilityReConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object parameter, CultureInfo culture)
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return value is null ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class Multi_LatencySampleRate2LatencyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length == 2 && values[0] is int playbackLatency && values[1] is int sampleRate)
if (values is [int playbackLatency, int sampleRate])
{
var latency = 1000d / sampleRate * playbackLatency;
return latency;
Expand Down
31 changes: 2 additions & 29 deletions KeyAsio.Gui/Converters/UpperCaseConverter.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,17 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace KeyAsio.Gui.Converters;

internal class UpperCaseConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object parameter, CultureInfo culture)
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return value?.ToString()?.ToUpper();
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

public class Enum2ListConverter : IValueConverter
{
private readonly Type _enumBaseType = typeof(Enum);

private static readonly Dictionary<Type, Enum[]> CachedList = new();

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter is not Type t || !t.IsSubclassOf(_enumBaseType))
return DependencyProperty.UnsetValue;
if (CachedList.ContainsKey(t))
return CachedList[t];

var array = Enum.GetValues(t).Cast<Enum>().ToArray();
CachedList.Add(t, array);
return array;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
Expand Down
22 changes: 11 additions & 11 deletions KeyAsio.Gui/UserControls/CustomizableButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ControlTemplate IconTemplate

public static readonly DependencyProperty IconTemplateProperty =
DependencyProperty.Register(
"IconTemplate",
nameof(IconTemplate),
typeof(ControlTemplate),
typeof(CustomizableButton),
null
Expand All @@ -46,7 +46,7 @@ public CornerRadius CornerRadius

public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register(
"CornerRadius",
nameof(CornerRadius),
typeof(CornerRadius),
typeof(CustomizableButton),
new PropertyMetadata(new CornerRadius(0))
Expand All @@ -60,7 +60,7 @@ public Thickness IconMargin

public static readonly DependencyProperty IconMarginProperty =
DependencyProperty.Register(
"IconMargin",
nameof(IconMargin),
typeof(Thickness),
typeof(CustomizableButton),
new PropertyMetadata(new Thickness(0, 0, 0, 0))
Expand All @@ -74,7 +74,7 @@ public Orientation IconOrientation

public static readonly DependencyProperty IconOrientationProperty =
DependencyProperty.Register(
"IconOrientation",
nameof(IconOrientation),
typeof(Orientation),
typeof(CustomizableButton),
new PropertyMetadata(Orientation.Horizontal)
Expand All @@ -88,7 +88,7 @@ public double IconSize

public static readonly DependencyProperty IconSizeProperty =
DependencyProperty.Register(
"IconSize",
nameof(IconSize),
typeof(double),
typeof(CustomizableButton),
new PropertyMetadata(24d)
Expand Down Expand Up @@ -153,12 +153,12 @@ public Brush CheckedForeground
return null;
}

public static readonly DependencyProperty MouseOverBackgroundProperty = DependencyProperty.Register("MouseOverBackground", typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty MouseOverForegroundProperty = DependencyProperty.Register("MouseOverForeground", typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty MouseDownBackgroundProperty = DependencyProperty.Register("MouseDownBackground", typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty MouseDownForegroundProperty = DependencyProperty.Register("MouseDownForeground", typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty CheckedBackgroundProperty = DependencyProperty.Register("CheckedBackground", typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty CheckedForegroundProperty = DependencyProperty.Register("CheckedForeground", typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty MouseOverBackgroundProperty = DependencyProperty.Register(nameof(MouseOverBackground), typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty MouseOverForegroundProperty = DependencyProperty.Register(nameof(MouseOverForeground), typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty MouseDownBackgroundProperty = DependencyProperty.Register(nameof(MouseDownBackground), typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty MouseDownForegroundProperty = DependencyProperty.Register(nameof(MouseDownForeground), typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty CheckedBackgroundProperty = DependencyProperty.Register(nameof(CheckedBackground), typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty CheckedForegroundProperty = DependencyProperty.Register(nameof(CheckedForeground), typeof(Brush), typeof(CustomizableButton), new PropertyMetadata(default(Brush)));

static CustomizableButton()
{
Expand Down
2 changes: 1 addition & 1 deletion KeyAsio.Gui/UserControls/FrameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace KeyAsio.Gui.UserControls;

public class FrameWindow : Window
{
public static readonly DependencyProperty CanCloseProperty = DependencyProperty.Register("CanClose",
public static readonly DependencyProperty CanCloseProperty = DependencyProperty.Register(nameof(CanClose),
typeof(bool),
typeof(FrameWindow),
new PropertyMetadata(true, (d, e) =>
Expand Down
12 changes: 6 additions & 6 deletions KeyAsio.Gui/UserControls/WindowFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ namespace KeyAsio.Gui.UserControls;

public class WindowFrame : UserControl, INotifyPropertyChanged
{
public static readonly DependencyProperty ChildProperty = DependencyProperty.Register("Child",
public static readonly DependencyProperty ChildProperty = DependencyProperty.Register(nameof(Child),
typeof(object),
typeof(WindowFrame),
new PropertyMetadata(default(object)));

public static readonly DependencyProperty IsMaxProperty = DependencyProperty.Register("IsMax",
public static readonly DependencyProperty IsMaxProperty = DependencyProperty.Register(nameof(IsMax),
typeof(bool),
typeof(WindowFrame),
new PropertyMetadata(default(bool)));

public static readonly DependencyProperty HasMinProperty = DependencyProperty.Register("HasMin",
public static readonly DependencyProperty HasMinProperty = DependencyProperty.Register(nameof(HasMin),
typeof(bool),
typeof(WindowFrame),
new PropertyMetadata(true));

public static readonly DependencyProperty HasMaxProperty = DependencyProperty.Register("HasMax",
public static readonly DependencyProperty HasMaxProperty = DependencyProperty.Register(nameof(HasMax),
typeof(bool),
typeof(WindowFrame),
new PropertyMetadata(true));

public static readonly DependencyProperty CanCloseProperty = DependencyProperty.Register("CanClose",
public static readonly DependencyProperty CanCloseProperty = DependencyProperty.Register(nameof(CanClose),
typeof(bool),
typeof(WindowFrame),
new PropertyMetadata(true));

public static readonly DependencyProperty HasIconProperty = DependencyProperty.Register("HasIcon",
public static readonly DependencyProperty HasIconProperty = DependencyProperty.Register(nameof(HasIcon),
typeof(bool),
typeof(WindowFrame),
new PropertyMetadata(true));
Expand Down

0 comments on commit d94ab10

Please sign in to comment.