-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,212 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/Shared/Corathing.UI.WPF/Behaviors/DelayedMultiBindingConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Data; | ||
using System.Windows; | ||
|
||
namespace Corathing.UI.WPF.Behaviors; | ||
|
||
/// <summary> | ||
/// Determines if the Dashboard selector (Pop up) should be open | ||
/// Implements the <see cref="System.Windows.Data.IMultiValueConverter" /> | ||
/// </summary> | ||
/// <seealso cref="System.Windows.Data.IMultiValueConverter" /> | ||
public class DelayedMultiBindingConverter : IMultiValueConverter | ||
{ | ||
#region Public Methods | ||
|
||
/// <summary> | ||
/// Converts source values to a value for the binding target. The data binding engine calls this method when it propagates the values from source bindings to the binding target. | ||
/// </summary> | ||
/// <param name="values">The array of values that the source bindings in the <see cref="T:System.Windows.Data.MultiBinding" /> produces. The value <see cref="F:System.Windows.DependencyProperty.UnsetValue" /> indicates that the source binding has no value to provide for conversion.</param> | ||
/// <param name="targetType">The type of the binding target property.</param> | ||
/// <param name="parameter">The converter parameter to use.</param> | ||
/// <param name="culture">The culture to use in the converter.</param> | ||
/// <returns>A converted value.If the method returns <see langword="null" />, the valid <see langword="null" /> value is used.A return value of <see cref="T:System.Windows.DependencyProperty" />.<see cref="F:System.Windows.DependencyProperty.UnsetValue" /> indicates that the converter did not produce a value, and that the binding will use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue" /> if it is available, or else will use the default value.A return value of <see cref="T:System.Windows.Data.Binding" />.<see cref="F:System.Windows.Data.Binding.DoNothing" /> indicates that the binding does not transfer the value or use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue" /> or the default value.</returns> | ||
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
if (values.Any(value => value is not bool) || values.Length < 1 || !(bool)values[0]) | ||
return false; | ||
|
||
for (var i = 1; i < values.Length; i++) | ||
{ | ||
if ((bool)values[i]) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Converts a binding target value to the source binding values. | ||
/// </summary> | ||
/// <param name="value">The value that the binding target produces.</param> | ||
/// <param name="targetTypes">The array of types to convert to. The array length indicates the number and types of values that are suggested for the method to return.</param> | ||
/// <param name="parameter">The converter parameter to use.</param> | ||
/// <param name="culture">The culture to use in the converter.</param> | ||
/// <returns>An array of values that have been converted from the target value back to the source values.</returns> | ||
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) | ||
{ | ||
return [DependencyProperty.UnsetValue]; | ||
} | ||
|
||
#endregion Public Methods | ||
} |
249 changes: 249 additions & 0 deletions
249
src/Shared/Corathing.UI.WPF/Behaviors/DelayedMultiBindingExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
// Source from : WpfDashboardControl | ||
// https://github.com/DustinBryant/WpfDashboardControl | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Data; | ||
using System.Windows.Markup; | ||
using System.Windows.Threading; | ||
using System.Windows; | ||
|
||
namespace Corathing.UI.WPF.Behaviors; | ||
|
||
/// <summary> | ||
/// Provides a delayed multi-binding. This class cannot be inherited. | ||
/// Implements the <see cref="System.Windows.Markup.MarkupExtension" /> | ||
/// Implements the <see cref="System.Windows.Data.IMultiValueConverter" /> | ||
/// Implements the <see cref="System.ComponentModel.INotifyPropertyChanged" /> | ||
/// </summary> | ||
/// <seealso cref="System.Windows.Markup.MarkupExtension" /> | ||
/// <seealso cref="System.Windows.Data.IMultiValueConverter" /> | ||
/// <seealso cref="System.ComponentModel.INotifyPropertyChanged" /> | ||
[ContentProperty("Bindings")] | ||
public sealed class DelayedMultiBindingExtension : MarkupExtension, IMultiValueConverter, INotifyPropertyChanged | ||
{ | ||
#region Private Fields | ||
|
||
private readonly DispatcherTimer _timer; | ||
|
||
private object? _delayedValue; | ||
|
||
private object? _startingValue; | ||
|
||
private bool _startingValueInitialSet; | ||
|
||
private object? _unDelayedValue; | ||
|
||
#endregion Private Fields | ||
|
||
#region Public Events | ||
|
||
/// <summary> | ||
/// Occurs when a property value changes. | ||
/// </summary> | ||
public event PropertyChangedEventHandler? PropertyChanged; | ||
|
||
#endregion Public Events | ||
|
||
#region Public Properties | ||
|
||
/// <summary> | ||
/// Gets the bindings. | ||
/// </summary> | ||
/// <value>The bindings.</value> | ||
public Collection<BindingBase> Bindings { get; } | ||
|
||
/// <summary> | ||
/// Gets the change count. | ||
/// </summary> | ||
/// <value>The change count.</value> | ||
public int ChangeCount { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the converter. | ||
/// </summary> | ||
/// <value>The converter.</value> | ||
public IMultiValueConverter? Converter { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the converter culture. | ||
/// </summary> | ||
/// <value>The converter culture.</value> | ||
public CultureInfo? ConverterCulture { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the converter parameter. | ||
/// </summary> | ||
/// <value>The converter parameter.</value> | ||
public object? ConverterParameter { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the current value. | ||
/// </summary> | ||
/// <value>The current value.</value> | ||
public object? CurrentValue | ||
{ | ||
get => _delayedValue; | ||
set | ||
{ | ||
_delayedValue = _unDelayedValue = value; | ||
_timer.Stop(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the delay. | ||
/// </summary> | ||
/// <value>The delay.</value> | ||
public TimeSpan Delay | ||
{ | ||
get => _timer.Interval; | ||
set => _timer.Interval = value; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the mode. | ||
/// </summary> | ||
/// <value>The mode.</value> | ||
public BindingMode Mode { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the starting value. | ||
/// </summary> | ||
/// <value>The starting value.</value> | ||
public object? StartingValue | ||
{ | ||
get => _startingValue; | ||
set | ||
{ | ||
if (_startingValueInitialSet) | ||
return; | ||
|
||
_startingValue = value; | ||
CurrentValue = value; | ||
_startingValueInitialSet = true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the update source trigger. | ||
/// </summary> | ||
/// <value>The update source trigger.</value> | ||
public UpdateSourceTrigger UpdateSourceTrigger { get; set; } | ||
|
||
#endregion Public Properties | ||
|
||
#region Public Constructors | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DelayedMultiBindingExtension"/> class. | ||
/// </summary> | ||
public DelayedMultiBindingExtension() | ||
{ | ||
Bindings = new Collection<BindingBase>(); | ||
_timer = new DispatcherTimer(); | ||
_timer.Tick += Timer_Tick; | ||
_timer.Interval = TimeSpan.FromMilliseconds(10); | ||
} | ||
|
||
#endregion Public Constructors | ||
|
||
#region Public Methods | ||
|
||
/// <summary> | ||
/// Converts source values to a value for the binding target. The data binding engine calls this method when it propagates the values from source bindings to the binding target. | ||
/// </summary> | ||
/// <param name="values">The array of values that the source bindings in the <see cref="T:System.Windows.Data.MultiBinding" /> produces. The value <see cref="F:System.Windows.DependencyProperty.UnsetValue" /> indicates that the source binding has no value to provide for conversion.</param> | ||
/// <param name="targetType">The type of the binding target property.</param> | ||
/// <param name="parameter">The converter parameter to use.</param> | ||
/// <param name="culture">The culture to use in the converter.</param> | ||
/// <returns>A converted value.If the method returns <see langword="null" />, the valid <see langword="null" /> value is used.A return value of <see cref="T:System.Windows.DependencyProperty" />.<see cref="F:System.Windows.DependencyProperty.UnsetValue" /> indicates that the converter did not produce a value, and that the binding will use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue" /> if it is available, or else will use the default value.A return value of <see cref="T:System.Windows.Data.Binding" />.<see cref="F:System.Windows.Data.Binding.DoNothing" /> indicates that the binding does not transfer the value or use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue" /> or the default value.</returns> | ||
public object? Convert(object[] values, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
ArgumentNullException.ThrowIfNull(Converter); | ||
|
||
var newValue = Converter.Convert(values.Take(values.Length - 1).ToArray(), | ||
targetType, | ||
ConverterParameter, | ||
ConverterCulture ?? culture); | ||
|
||
if (Equals(newValue, _unDelayedValue)) | ||
return _delayedValue; | ||
|
||
_unDelayedValue = newValue; | ||
_timer.Stop(); | ||
_timer.Start(); | ||
|
||
return _delayedValue; | ||
} | ||
|
||
/// <summary> | ||
/// Converts a binding target value to the source binding values. | ||
/// </summary> | ||
/// <param name="value">The value that the binding target produces.</param> | ||
/// <param name="targetTypes">The array of types to convert to. The array length indicates the number and types of values that are suggested for the method to return.</param> | ||
/// <param name="parameter">The converter parameter to use.</param> | ||
/// <param name="culture">The culture to use in the converter.</param> | ||
/// <returns>An array of values that have been converted from the target value back to the source values.</returns> | ||
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) | ||
{ | ||
ArgumentNullException.ThrowIfNull(Converter); | ||
|
||
return [.. Converter.ConvertBack(value, targetTypes, ConverterParameter, ConverterCulture ?? culture) | ||
, .. new object[] { ChangeCount }]; | ||
} | ||
|
||
/// <summary> | ||
/// When implemented in a derived class, returns an object that is provided as the value of the target property for this markup extension. | ||
/// </summary> | ||
/// <param name="serviceProvider">A service provider helper that can provide services for the markup extension.</param> | ||
/// <returns>The object value to set on the property where the extension is applied.</returns> | ||
public override object? ProvideValue(IServiceProvider serviceProvider) | ||
{ | ||
if (serviceProvider.GetService(typeof(IProvideValueTarget)) is not IProvideValueTarget valueProvider) | ||
return null; | ||
|
||
var bindingTarget = valueProvider.TargetObject as DependencyObject; | ||
var bindingProperty = valueProvider.TargetProperty as DependencyProperty; | ||
|
||
var multi = new MultiBinding | ||
{ | ||
Converter = this, | ||
Mode = Mode, | ||
UpdateSourceTrigger = UpdateSourceTrigger | ||
}; | ||
|
||
foreach (var binding in Bindings) | ||
multi.Bindings.Add(binding); | ||
|
||
multi.Bindings.Add(new Binding("ChangeCount") | ||
{ | ||
Source = this, | ||
Mode = BindingMode.OneWay | ||
}); | ||
|
||
if (bindingTarget != null && bindingProperty != null) | ||
BindingOperations.SetBinding(bindingTarget, bindingProperty, multi); | ||
|
||
return bindingProperty == null ? multi : bindingTarget?.GetValue(bindingProperty); | ||
} | ||
|
||
#endregion Public Methods | ||
|
||
#region Private Methods | ||
|
||
private void Timer_Tick(object? sender, EventArgs e) | ||
{ | ||
_timer.Stop(); | ||
_delayedValue = _unDelayedValue; | ||
ChangeCount++; | ||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ChangeCount))); | ||
} | ||
|
||
#endregion Private Methods | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.