Initial work for AbstractUI options

This commit is contained in:
Yoshi Askharoun
2021-08-05 20:45:26 -05:00
parent ba28eb7ef7
commit 3e3228245d
63 changed files with 2392 additions and 68 deletions
@@ -0,0 +1,41 @@
using System;
using System.Collections;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Windows.Data;
using System.Globalization;
namespace OwlCore.Wpf.Converters.Bools
{
/// <summary>
/// A converter that converts a given <see cref="ICollection"/> to an bool based on the presence of any items in the <see cref="ICollection"/>.
/// </summary>
public sealed class CollectionAnyToBoolConverter : IValueConverter
{
/// <summary>
/// Gets whether or not a collection is not empty.
/// </summary>
/// <param name="collection">The collection to check.</param>
/// <returns>Whether or not the colleciton contains any elements</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Convert(ICollection collection) => collection.Count > 0;
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is ICollection collection)
{
return Convert(collection);
}
return false;
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,41 @@
using System;
using System.Collections;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Windows.Data;
using System.Globalization;
namespace OwlCore.Wpf.Converters.Bools
{
/// <summary>
/// A converter that converts a given <see cref="ICollection"/> to an bool based on the lack of presence of any items in the <see cref="ICollection"/>.
/// </summary>
public sealed class CollectionNotAnyToBoolConverter : IValueConverter
{
/// <summary>
/// Gets whether or not a collection is empty.
/// </summary>
/// <param name="collection">The collection to check.</param>
/// <returns>Whether or not the colleciton contains no elements</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Convert(ICollection collection) => collection.Count <= 0;
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is ICollection collection)
{
return Convert(collection);
}
return false;
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,40 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Bools
{
/// <summary>
/// A converter that converts a given <see cref="bool"/> to its inverse.
/// </summary>
public sealed class InverseBoolConverter : IValueConverter
{
/// <summary>
/// Gets the inverse of a bool.
/// </summary>
/// <param name="data">The bool to inverse.</param>
/// <returns>An inversed bool.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Convert(bool data) => !data;
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool bValue)
{
return Convert(bValue);
}
return false;
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,41 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Bools.Visible
{
/// <summary>
/// A converter that converts checks if a string is null or empty and returns a <see cref="Visibility"/>.
/// </summary>
public sealed class NotNullOrEmptyToBoolConverter : IValueConverter
{
/// <summary>
/// Checks if a string is null or empty, and returns a <see cref="Visibility"/>.
/// </summary>
/// <param name="str">The string to null or empty check.</param>
/// <returns><see cref="Visibility.Visible"/> if not null or empty, <see cref="Visibility.Collapsed"/> if null or empty.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Convert(string? str) => !string.IsNullOrEmpty(str);
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string str)
{
return Convert(str);
}
return false;
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,35 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Bools
{
/// <summary>
/// A converter that converts checks null checks an object.
/// </summary>
public sealed class NotNullToBoolConverter : IValueConverter
{
/// <summary>
/// Checks if an object is null.
/// </summary>
/// <param name="obj">The object to check.</param>
/// <returns>True if not null, false if null</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Convert(object? obj) => obj != null;
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value);
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,36 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Bools.Visible
{
/// <summary>
/// A converter that converts a given <see cref="bool"/> a <see cref="Visibility"/>.
/// </summary>
public sealed class BoolToVisibilityConverter : IValueConverter
{
/// <summary>
/// Returns visible if the boolean is true.
/// </summary>
/// <param name="data">boolean to check.</param>
/// <returns>Collapsed if false, otherwise Visible.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Visibility Convert(bool data) => data ? Visibility.Visible : Visibility.Collapsed;
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value is bool bValue && bValue);
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,42 @@
using System;
using System.Collections;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Bools.Visible
{
/// <summary>
/// A simple converter that converts a given <see cref="ICollection"/> to an <see cref="Visibility"/> based on the presence of any items in the <see cref="ICollection"/>.
/// </summary>
public sealed class CollectionAnyToVisibilityConverter : IValueConverter
{
/// <summary>
/// Converts an <see cref="ICollection"/> an <see cref="Visibility"/> based on the presence of any items in the <see cref="ICollection"/>.
/// </summary>
/// <param name="value">The <see cref="ICollection"/>.</param>
/// <returns>A <see cref="Visibility"/>.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Visibility Convert(ICollection value) => BoolToVisibilityConverter.Convert(CollectionAnyToBoolConverter.Convert(value));
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is ICollection collection)
{
return Convert(collection);
}
return Visibility.Collapsed;
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,42 @@
using System;
using System.Collections;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Bools.Visible
{
/// <summary>
/// A converter that converts a given <see cref="ICollection"/> to an <see cref="Visibility"/> based on the presence of any items in the <see cref="ICollection"/>.
/// </summary>
public sealed class CollectionNotAnyToVisibilityConverter : IValueConverter
{
/// <summary>
/// Converts an <see cref="ICollection"/> an <see cref="Visibility"/> based on the presence of any items in the <see cref="ICollection"/>.
/// </summary>
/// <param name="value">The <see cref="ICollection"/>.</param>
/// <returns>A <see cref="Visibility"/>.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Visibility Convert(ICollection value) => BoolToVisibilityConverter.Convert(CollectionNotAnyToBoolConverter.Convert(value));
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is ICollection collection)
{
return Convert(collection);
}
return Visibility.Collapsed;
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,41 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Bools.Visible
{
/// <summary>
/// A converter that converts a the inverse of a given <see cref="bool"/> a <see cref="Visibility"/>.
/// </summary>
public sealed class InverseBoolToVisibilityConverter : IValueConverter
{
/// <summary>
/// Gets a <see cref="Visibility"/> based on the opposite of a bool.
/// </summary>
/// <param name="data">The bool to represented.</param>
/// <returns><see cref="Visibility.Collapsed"/> if <see cref="true"/>, <see cref="Visibility.Visible"/> if <see cref="false"/></returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Visibility Convert(bool data) => BoolToVisibilityConverter.Convert(!data);
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool bValue)
{
return Convert(bValue);
}
return Visibility.Visible;
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,41 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Bools.Visible
{
/// <summary>
/// A converter that converts checks if a string is null or empty and returns a <see cref="Visibility"/>.
/// </summary>
public sealed class NotNullOrEmptyToVisibilityConverter : IValueConverter
{
/// <summary>
/// Checks if a string is null or empty, and returns a <see cref="Visibility"/>.
/// </summary>
/// <param name="str">The string to null or empty check.</param>
/// <returns><see cref="Visibility.Visible"/> if not null or empty, <see cref="Visibility.Collapsed"/> if null or empty.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Visibility Convert(string? str) => BoolToVisibilityConverter.Convert(NotNullOrEmptyToBoolConverter.Convert(str));
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string str)
{
return Convert(str);
}
return false;
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,36 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Bools.Visible
{
/// <summary>
/// A converter that checks if an object is not null and returns a <see cref="Visibility"/>.
/// </summary>
public sealed class NotNullToVisibilityConverter : IValueConverter
{
/// <summary>
/// Checks if an object is null, and returns a <see cref="Visibility"/>.
/// </summary>
/// <param name="obj">The object to null check.</param>
/// <returns><see cref="Visibility.Visible"/> if not null, <see cref="Visibility.Collapsed"/> if null.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Visibility Convert(object? obj) => BoolToVisibilityConverter.Convert(obj != null);
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value);
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,36 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Bools.Visible
{
/// <summary>
/// A converter that checks if an object is null and returns a <see cref="Visibility"/>.
/// </summary>
public sealed class NullToVisibilityConverter : IValueConverter
{
/// <summary>
/// Checks if an object is null, and returns a <see cref="Visibility"/>.
/// </summary>
/// <param name="obj">The object to null check.</param>
/// <returns><see cref="Visibility.Visible"/> if not null, <see cref="Visibility.Collapsed"/> if null.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Visibility Convert(object? obj) => BoolToVisibilityConverter.Convert(obj == null);
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value);
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,25 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters
{
/// <summary>
/// A converter used for debugging the data passed by a binding.
/// </summary>
public sealed class DebugPassThroughConverter : IValueConverter
{
/// <inheritdoc/>
public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
System.Diagnostics.Debug.Write($"Debug passthrough: Type is {value?.GetType().ToString() ?? "null"}");
return value;
}
/// <inheritdoc/>
public object? ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
}
@@ -0,0 +1,33 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Numerical
{
/// <summary>
/// A converter that converts a given <see cref="long"/> to a <see cref="double"/>.
/// </summary>
public class LongToDoubleConverter : IValueConverter
{
/// <summary>
/// Converts a <see cref="long"/> to a <see cref="double"/>.
/// </summary>
/// <param name="value">The <see cref="long"/> to convert</param>
/// <returns>The converted value.</returns>
[Pure]
public static double Convert(long value) => value;
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert((long)value);
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (double)value;
}
}
}
@@ -0,0 +1,40 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters
{
/// <summary>
/// A converter that returns the first element of <see cref="SelectionChangedEventArgs.AddedItems"/>
/// from a <see cref="SelectionChangedEventArgs"/>.
/// </summary>
public sealed class SelectionChangedEventArgsToClickedItemConverter : IValueConverter
{
/// <summary>
/// Gets the <see cref="ItemClickEventArgs.ClickedItem"/> from a <see cref="ItemClickEventArgs"/> .
/// </summary>
/// <param name="args">The event args to check.</param>
/// <returns>The clicked item, cast to <see cref="object"/>.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object Convert(SelectionChangedEventArgs args) => args.AddedItems[0];
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is SelectionChangedEventArgs args)
return Convert(args);
return false;
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,71 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Text
{
/// <summary>
/// A converter that changes the casing of a string.
/// </summary>
public class CaseConverter : IValueConverter
{
/// <summary>
/// Gets or sets the character casing to convert an input string to.
/// </summary>
public CharacterCasing Case { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="CaseConverter"/> class.
/// </summary>
public CaseConverter()
{
Case = CharacterCasing.Upper;
}
/// <summary>
/// Converts a string to a certain character casing.
/// </summary>
/// <param name="value">The string to convert.</param>
/// <param name="characterCasing">The result's character casing.</param>
/// <returns>String <paramref name="value"/> in the specified character casing.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Convert(string? value, CharacterCasing characterCasing)
{
if (value != null)
{
switch (characterCasing)
{
case CharacterCasing.Upper:
return value.ToUpper();
case CharacterCasing.Lower:
return value.ToLower();
case CharacterCasing.Normal:
default:
return value;
}
}
return string.Empty;
}
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var str = value as string;
return Convert(str, Case);
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,40 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Time
{
/// <summary>
/// A converter that converts a given <see cref="DateTime"/> to a formatted year string.
/// </summary>
public class DateTimeToYearTextConverter : IValueConverter
{
/// <summary>
/// Converts a <see cref="DateTime"/> to a formatted year string.
/// </summary>
/// <param name="value">The <see cref="DateTime"/> to convert.</param>
/// <returns>A formatted year string of the <see cref="DateTime"/>.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Convert(DateTime value) => value.ToString("yyyy");
/// <inheritdoc cref="Convert(DateTime)"/>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Convert(DateTime? value) => Convert(value.HasValue ? value.Value : DateTime.MinValue);
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value as DateTime?);
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,40 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Time.Numerical
{
/// <summary>
/// A converter that converts a given <see cref="long"/> to a <see cref="TimeSpan"/>.
/// </summary>
public sealed class DoubleToTimeSpanConverter : IValueConverter
{
/// <summary>
/// Converts a <see cref="long"/> to a <see cref="TimeSpan"/>.
/// </summary>
/// <param name="value">The <see cref="TimeSpan"/> to convert.</param>
/// <returns>A formatted string of the <see cref="TimeSpan"/>.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TimeSpan Convert(double value) => TimeSpan.FromMilliseconds(value);
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double timeSpan)
{
return Convert(timeSpan);
}
return Convert(0);
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,40 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Time.Numerical
{
/// <summary>
/// A converter that converts a given <see cref="long"/> to a <see cref="TimeSpan"/>.
/// </summary>
public sealed class LongToTimeSpanConverter : IValueConverter
{
/// <summary>
/// Converts a <see cref="long"/> to a <see cref="TimeSpan"/>.
/// </summary>
/// <param name="value">The <see cref="TimeSpan"/> to convert.</param>
/// <returns>A formatted string of the <see cref="TimeSpan"/>.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TimeSpan Convert(long value) => TimeSpan.FromMilliseconds(value);
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is long timeSpan)
{
return Convert(timeSpan);
}
return Convert(0);
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,51 @@
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Data;
namespace OwlCore.Wpf.Converters.Time.Numerical
{
/// <summary>
/// A converter that converts a given <see cref="long"/> to a <see cref="TimeSpan"/> then to a natural time format string.
/// </summary>
public sealed class LongToTimeSpanTextConverter : IValueConverter
{
/// <summary>
/// Converts a <see cref="long"/> to a <see cref="TimeSpan"/> formatted string.
/// </summary>
/// <param name="value">The <see cref="long"/> to convert.</param>
/// <returns>A formatted string of the <see cref="long"/> as a <see cref="TimeSpan"/>.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Convert(long value) => TimeSpanToTextConverter.Convert(LongToTimeSpanConverter.Convert(value));
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
long lValue = 0;
switch (value)
{
case long l:
lValue = l;
break;
case double d:
lValue = (long)d;
break;
case int i:
lValue = i;
break;
case float f:
lValue = (long)f;
break;
}
return Convert(lValue);
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

Some files were not shown because too many files have changed in this diff Show More