Skip to content

Commit

Permalink
minor update
Browse files Browse the repository at this point in the history
- Implemented a CustomMessageBox window (not used by anything yet)
- Moved WindowPositioningExtensions to VolumeControl.WPF assembly
- Moved JsonConverter attrib from EScreenCorner to relevant config props
- Added log message indicating if the config file existed at startup
- Added log message when loading config (if log is initialized)
- Included Directory.Build.props in solution files
- Simplified StringDataTemplate by removing the Grid control
- Renamed SelectValues to SelectValue in EnumerableExtensions.cs
- Inconsequential changes to ActionSettingsWindow.xaml
  • Loading branch information
radj307 committed Nov 26, 2023
1 parent a8c1721 commit fe5366f
Show file tree
Hide file tree
Showing 18 changed files with 1,737 additions and 42 deletions.
8 changes: 8 additions & 0 deletions VolumeControl.Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using VolumeControl.Core.Structs;
using VolumeControl.Log;
using VolumeControl.WPF.Collections;
using VolumeControl.WPF.Extensions;

namespace VolumeControl.Core
{
Expand Down Expand Up @@ -95,6 +96,12 @@ public void Save(Formatting formatting = Formatting.Indented, [CallerMemberName]
FLog.Trace($"Saved {nameof(Config)} (Caller: \"{callerName}\")");
base.Save(formatting);
}
public override bool Load()
{
if (FLog.IsInitialized && FLog.FilterEventType(EventType.TRACE))
FLog.Trace($"Loaded {nameof(Config)} from {Location}.");
return base.Load();
}
#endregion Method Overrides

#region EventHandlers
Expand Down Expand Up @@ -222,6 +229,7 @@ private void PropertyWithCollectionChangedEvents_CollectionChanged(object? sende
/// <summary>
/// Gets or sets the origin corner to use when restoring the main window's position.
/// </summary>
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public EScreenCorner MainWindowOriginCorner { get; set; } = EScreenCorner.TopLeft;
/// <summary>
/// Gets or sets the position to restore the main window to when the application starts.
Expand Down
2 changes: 2 additions & 0 deletions VolumeControl.Core/NotificationConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Windows;
using System.Windows.Media;
using VolumeControl.Core.Enum;
using VolumeControl.WPF.Extensions;

namespace VolumeControl.Core
{
Expand Down Expand Up @@ -51,6 +52,7 @@ public class NotificationConfigSection : INotifyPropertyChanged
/// <summary>
/// Gets or sets the corner from which ListNotification size transform operations are rooted.
/// </summary>
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public EScreenCorner PositionOriginCorner { get; set; } = EScreenCorner.TopLeft;
/// <summary>
/// Gets or sets whether the notification window slowly fades in instead of appearing instantly.
Expand Down
4 changes: 1 addition & 3 deletions VolumeControl.SDK/DataTemplates/DataTemplateDictionary.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
x:Key="StringDataTemplate"
DataType="{x:Type actionSettings:IActionSettingInstance}"
ValueType="{x:Type sys:String}">
<Grid>
<TextBox Style="{StaticResource RoundedTextBoxStyle}" Text="{Binding Value, UpdateSourceTrigger=LostFocus}" />
</Grid>
<TextBox Style="{StaticResource RoundedTextBoxStyle}" Text="{Binding Value, UpdateSourceTrigger=LostFocus}" />
</core:ActionSettingDataTemplate>

<!-- Bool -->
Expand Down
17 changes: 16 additions & 1 deletion VolumeControl.TypeExtensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,29 @@ public static IEnumerable<TResult> SelectIf<TSource, TResult>(this IEnumerable<T
}
}
/// <summary>
/// Selects non-<see langword="null"/> values from the enumerable.
/// </summary>
/// <typeparam name="TSource">The type of element in the enumerable.</typeparam>
/// <returns>The resulting elements that aren't <see langword="null"/>.</returns>
public static IEnumerable<TSource> SelectValues<TSource>(this IEnumerable<TSource> source)
{
foreach (var item in source)
{
if (item is TSource value)
{
yield return value;
}
}
}
/// <summary>
/// Selects non-<see langword="null"/> values from the enumerable using the specified <paramref name="selector"/>.
/// </summary>
/// <typeparam name="TSource">The type of element in the enumerable.</typeparam>
/// <typeparam name="TResult">The type of element in the resulting enumerable.</typeparam>
/// <param name="source">The enumerable to apply the <paramref name="selector"/> to.</param>
/// <param name="selector">A function that accepts a parameter of type <typeparamref name="TSource"/> and returns a <typeparamref name="TResult"/> instance, or <see langword="null"/>. When this returns <see langword="null"/> the item does not appear in the resulting enumeration.</param>
/// <returns>The resulting elements of type <typeparamref name="TResult"/> that aren't <see langword="null"/>.</returns>
public static IEnumerable<TResult> SelectValue<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult?> selector)
public static IEnumerable<TResult> SelectValues<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult?> selector)
{
foreach (var item in source)
{
Expand Down
58 changes: 58 additions & 0 deletions VolumeControl.WPF/Converters/EnumHasFlagConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Globalization;
using System.Windows.Data;

namespace VolumeControl.WPF.Converters
{
/// <summary>
/// Converts from an enum value to <see cref="bool"/> depending on the flag value specified as a converter parameter.
/// </summary>
[ValueConversion(typeof(Enum), typeof(bool))]
public class EnumHasFlagConverter : IValueConverter
{
#region Properties
/// <summary>
/// Gets or sets the <see cref="bool"/> value returned by Convert when the input value is <see langword="null"/>.
/// </summary>
public bool ConvertResultWhenNull { get; set; } = false;
#endregion Properties

#region IValueConverter
/// <summary>
/// Converts from an enum flag <paramref name="value"/> to <see cref="bool"/>.
/// </summary>
/// <remarks>
/// When <paramref name="value"/> is <see langword="null"/>, the value of the ConvertResultWhenNull property is returned instead.
/// </remarks>
/// <returns><see langword="true"/> when <paramref name="value"/> has the <paramref name="parameter"/> flag set; otherwise, <see langword="false"/>.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Provide the enum flag value(s) to check for as the ConverterParameter
ArgumentNullException.ThrowIfNull(parameter);

if (value == null) return ConvertResultWhenNull;

var e_value = System.Convert.ToInt64(value);
var flag = System.Convert.ToInt64(parameter);

return (e_value & flag) != 0;
}
/// <summary>
/// Converts from <see cref="bool"/> to the specified <paramref name="targetType"/> enum.
/// </summary>
/// <returns>The <paramref name="parameter"/> value as the specified <paramref name="targetType"/>.</returns>
/// <exception cref="NotImplementedException"/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
ArgumentNullException.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(parameter);

long result = 0;
if ((bool)value)
result |= System.Convert.ToInt64(parameter);

return Enum.ToObject(targetType, result);
}
#endregion IValueConverter
}
}
Loading

0 comments on commit fe5366f

Please sign in to comment.