@@ -20,6 +20,7 @@ public static class MenuKeys
|
||||
public const string MenuKeyMessageBox = "MessageBox";
|
||||
public const string MenuKeyNavigation = "Navigation";
|
||||
public const string MenuKeyNavMenu = "NavMenu";
|
||||
public const string MenuKeyNumberDisplayer = "NumberDisplayer";
|
||||
public const string MenuKeyNumericUpDown = "NumericUpDown";
|
||||
public const string MenuKeyPagination = "Pagination";
|
||||
public const string MenuKeyRangeSlider = "RangeSlider";
|
||||
|
||||
31
demo/Ursa.Demo/Pages/NumberDisplayerDemo.axaml
Normal file
31
demo/Ursa.Demo/Pages/NumberDisplayerDemo.axaml
Normal file
@@ -0,0 +1,31 @@
|
||||
<UserControl
|
||||
x:Class="Ursa.Demo.Pages.NumberDisplayerDemo"
|
||||
xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:u="https://irihi.tech/ursa"
|
||||
xmlns:vm="using:Ursa.Demo.ViewModels"
|
||||
d:DesignHeight="450"
|
||||
d:DesignWidth="800"
|
||||
x:CompileBindings="True"
|
||||
x:DataType="vm:NumberDisplayerDemoViewModel"
|
||||
mc:Ignorable="d">
|
||||
<StackPanel HorizontalAlignment="Left">
|
||||
<Button Command="{Binding IncreaseCommand}">Change</Button>
|
||||
<u:Int32Displayer Value="{Binding Value}" />
|
||||
<u:DoubleDisplayer StringFormat="N2" Value="{Binding DoubleValue}" />
|
||||
<u:DateDisplay
|
||||
FontSize="30"
|
||||
StringFormat="yyyy-MM-dd"
|
||||
Value="{Binding DateValue}" />
|
||||
<TextBlock Text="Selectable: "></TextBlock>
|
||||
<u:Int32Displayer Value="{Binding Value}" IsSelectable="True" />
|
||||
<u:DoubleDisplayer StringFormat="N2" Value="{Binding DoubleValue}" IsSelectable="True" />
|
||||
<u:DateDisplay
|
||||
FontSize="30"
|
||||
StringFormat="yyyy-MM-dd"
|
||||
IsSelectable="True"
|
||||
Value="{Binding DateValue}" />
|
||||
</StackPanel>
|
||||
</UserControl>
|
||||
13
demo/Ursa.Demo/Pages/NumberDisplayerDemo.axaml.cs
Normal file
13
demo/Ursa.Demo/Pages/NumberDisplayerDemo.axaml.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
|
||||
namespace Ursa.Demo.Pages;
|
||||
|
||||
public partial class NumberDisplayerDemo : UserControl
|
||||
{
|
||||
public NumberDisplayerDemo()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,7 @@ public class MainViewViewModel : ViewModelBase
|
||||
MenuKeys.MenuKeyMessageBox => new MessageBoxDemoViewModel(),
|
||||
MenuKeys.MenuKeyNavigation => new NavigationMenuDemoViewModel(),
|
||||
MenuKeys.MenuKeyNavMenu => new NavMenuDemoViewModel(),
|
||||
MenuKeys.MenuKeyNumberDisplayer => new NumberDisplayerDemoViewModel(),
|
||||
MenuKeys.MenuKeyNumericUpDown => new NumericUpDownDemoViewModel(),
|
||||
MenuKeys.MenuKeyPagination => new PaginationDemoViewModel(),
|
||||
MenuKeys.MenuKeyRangeSlider => new RangeSliderDemoViewModel(),
|
||||
|
||||
@@ -29,7 +29,8 @@ public class MenuViewModel: ViewModelBase
|
||||
new() { MenuHeader = "Message Box", Key = MenuKeys.MenuKeyMessageBox, Status = "New" },
|
||||
new() { MenuHeader = "Navigation", Key = MenuKeys.MenuKeyNavigation, Status = "WIP" },
|
||||
new() { MenuHeader = "Nav Menu", Key = MenuKeys.MenuKeyNavMenu, Status = "WIP"},
|
||||
new() { MenuHeader = "NumericUpDown", Key = MenuKeys.MenuKeyNumericUpDown, Status = "New" },
|
||||
new() { MenuHeader = "Number Displayer", Key = MenuKeys.MenuKeyNumberDisplayer, Status = "New" },
|
||||
new() { MenuHeader = "Numeric UpDown", Key = MenuKeys.MenuKeyNumericUpDown, Status = "New" },
|
||||
new() { MenuHeader = "Pagination", Key = MenuKeys.MenuKeyPagination },
|
||||
new() { MenuHeader = "RangeSlider", Key = MenuKeys.MenuKeyRangeSlider, Status = "New"},
|
||||
new() { MenuHeader = "Selection List", Key = MenuKeys.MenuKeySelectionList, Status = "New" },
|
||||
|
||||
29
demo/Ursa.Demo/ViewModels/NumberDisplayerDemoViewModel.cs
Normal file
29
demo/Ursa.Demo/ViewModels/NumberDisplayerDemoViewModel.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Windows.Input;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
namespace Ursa.Demo.ViewModels;
|
||||
|
||||
public partial class NumberDisplayerDemoViewModel: ObservableObject
|
||||
{
|
||||
[ObservableProperty] private int _value;
|
||||
[ObservableProperty] private double _doubleValue;
|
||||
[ObservableProperty] private DateTime _dateValue;
|
||||
public ICommand IncreaseCommand { get; }
|
||||
public NumberDisplayerDemoViewModel()
|
||||
{
|
||||
IncreaseCommand = new RelayCommand(OnChange);
|
||||
Value = 0;
|
||||
DoubleValue = 0d;
|
||||
DateValue = DateTime.Now;
|
||||
}
|
||||
|
||||
private void OnChange()
|
||||
{
|
||||
Random r = new Random();
|
||||
Value = r.Next(int.MaxValue);
|
||||
DoubleValue = r.NextDouble() * 100000;
|
||||
DateValue = DateTime.Today.AddDays(r.Next(1000));
|
||||
}
|
||||
}
|
||||
38
src/Ursa.Themes.Semi/Controls/NumberDisplayer.axaml
Normal file
38
src/Ursa.Themes.Semi/Controls/NumberDisplayer.axaml
Normal file
@@ -0,0 +1,38 @@
|
||||
<ResourceDictionary
|
||||
xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:converters="clr-namespace:Ursa.Converters;assembly=Ursa"
|
||||
xmlns:u="https://irihi.tech/ursa">
|
||||
<!-- Add Resources Here -->
|
||||
<ControlTheme x:Key="{x:Type u:NumberDisplayerBase}" TargetType="u:NumberDisplayerBase">
|
||||
<Setter Property="Duration" Value="0:0:0.2" />
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate TargetType="u:NumberDisplayerBase">
|
||||
<TextBlock
|
||||
FontSize="{TemplateBinding FontSize}"
|
||||
Foreground="{TemplateBinding Foreground}"
|
||||
FontFamily="{TemplateBinding FontFamily}"
|
||||
Background="{TemplateBinding Background}"
|
||||
FontWeight="{TemplateBinding FontWeight}"
|
||||
FontStyle="{TemplateBinding FontSize}"
|
||||
FontStretch="{TemplateBinding FontStretch}"
|
||||
Text="{TemplateBinding InternalText, Mode=OneWay}" />
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
<Style Selector="^[IsSelectable=True]">
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate TargetType="u:NumberDisplayerBase">
|
||||
<SelectableTextBlock
|
||||
FontSize="{TemplateBinding FontSize}"
|
||||
Foreground="{TemplateBinding Foreground}"
|
||||
FontFamily="{TemplateBinding FontFamily}"
|
||||
Background="{TemplateBinding Background}"
|
||||
FontWeight="{TemplateBinding FontWeight}"
|
||||
FontStyle="{TemplateBinding FontSize}"
|
||||
FontStretch="{TemplateBinding FontStretch}"
|
||||
Text="{TemplateBinding InternalText, Mode=OneWay}" />
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
</Style>
|
||||
</ControlTheme>
|
||||
</ResourceDictionary>
|
||||
@@ -20,6 +20,7 @@
|
||||
<ResourceInclude Source="Navigation.axaml" />
|
||||
<ResourceInclude Source="NavMenu.axaml" />
|
||||
<ResourceInclude Source="NumericUpDown.axaml" />
|
||||
<ResourceInclude Source="NumberDisplayer.axaml" />
|
||||
<ResourceInclude Source="Pagination.axaml" />
|
||||
<ResourceInclude Source="RangeSlider.axaml" />
|
||||
<ResourceInclude Source="SelectionList.axaml" />
|
||||
|
||||
73
src/Ursa/Controls/NumberDisplayer/Implementations.cs
Normal file
73
src/Ursa/Controls/NumberDisplayer/Implementations.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using Avalonia.Animation;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class Int32Displayer : NumberDisplayer<int>
|
||||
{
|
||||
protected override Type StyleKeyOverride { get; } = typeof(NumberDisplayerBase);
|
||||
|
||||
protected override InterpolatingAnimator<int> GetAnimator()
|
||||
{
|
||||
return new IntAnimator();
|
||||
}
|
||||
|
||||
private class IntAnimator : InterpolatingAnimator<int>
|
||||
{
|
||||
public override int Interpolate(double progress, int oldValue, int newValue)
|
||||
{
|
||||
return oldValue + (int)((newValue - oldValue) * progress);
|
||||
}
|
||||
}
|
||||
|
||||
protected override string GetString(int value)
|
||||
{
|
||||
return value.ToString(StringFormat);
|
||||
}
|
||||
}
|
||||
|
||||
public class DoubleDisplayer : NumberDisplayer<double>
|
||||
{
|
||||
protected override Type StyleKeyOverride { get; } = typeof(NumberDisplayerBase);
|
||||
|
||||
protected override InterpolatingAnimator<double> GetAnimator()
|
||||
{
|
||||
return new DoubleAnimator();
|
||||
}
|
||||
|
||||
private class DoubleAnimator : InterpolatingAnimator<double>
|
||||
{
|
||||
public override double Interpolate(double progress, double oldValue, double newValue)
|
||||
{
|
||||
return oldValue + (newValue - oldValue) * progress;
|
||||
}
|
||||
}
|
||||
|
||||
protected override string GetString(double value)
|
||||
{
|
||||
return value.ToString(StringFormat);
|
||||
}
|
||||
}
|
||||
|
||||
public class DateDisplay : NumberDisplayer<DateTime>
|
||||
{
|
||||
protected override Type StyleKeyOverride { get; } = typeof(NumberDisplayerBase);
|
||||
|
||||
protected override InterpolatingAnimator<DateTime> GetAnimator()
|
||||
{
|
||||
return new DateAnimator();
|
||||
}
|
||||
|
||||
private class DateAnimator : InterpolatingAnimator<DateTime>
|
||||
{
|
||||
public override DateTime Interpolate(double progress, DateTime oldValue, DateTime newValue)
|
||||
{
|
||||
var diff = (newValue - oldValue).TotalSeconds;
|
||||
return oldValue + TimeSpan.FromSeconds(diff * progress);
|
||||
}
|
||||
}
|
||||
|
||||
protected override string GetString(DateTime value)
|
||||
{
|
||||
return value.ToString(StringFormat);
|
||||
}
|
||||
}
|
||||
133
src/Ursa/Controls/NumberDisplayer/NumberDisplayerBase.cs
Normal file
133
src/Ursa/Controls/NumberDisplayer/NumberDisplayerBase.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Animation;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Data;
|
||||
using Avalonia.Styling;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public abstract class NumberDisplayerBase : TemplatedControl
|
||||
{
|
||||
public static readonly DirectProperty<NumberDisplayerBase, string> InternalTextProperty = AvaloniaProperty.RegisterDirect<NumberDisplayerBase, string>(
|
||||
nameof(InternalText), o => o.InternalText, (o, v) => o.InternalText = v);
|
||||
private string _internalText;
|
||||
|
||||
internal string InternalText
|
||||
{
|
||||
get => _internalText;
|
||||
set => SetAndRaise(InternalTextProperty, ref _internalText, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<TimeSpan> DurationProperty = AvaloniaProperty.Register<NumberDisplayerBase, TimeSpan>(
|
||||
nameof(Duration));
|
||||
|
||||
public TimeSpan Duration
|
||||
{
|
||||
get => GetValue(DurationProperty);
|
||||
set => SetValue(DurationProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<string> StringFormatProperty = AvaloniaProperty.Register<NumberDisplayerBase, string>(
|
||||
nameof(StringFormat));
|
||||
|
||||
public string StringFormat
|
||||
{
|
||||
get => GetValue(StringFormatProperty);
|
||||
set => SetValue(StringFormatProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<bool> IsSelectableProperty = AvaloniaProperty.Register<NumberDisplayerBase, bool>(
|
||||
nameof(IsSelectable));
|
||||
|
||||
public bool IsSelectable
|
||||
{
|
||||
get => GetValue(IsSelectableProperty);
|
||||
set => SetValue(IsSelectableProperty, value);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class NumberDisplayer<T>: NumberDisplayerBase
|
||||
{
|
||||
private Animation? _animation;
|
||||
private CancellationTokenSource _cts = new ();
|
||||
|
||||
public static readonly StyledProperty<T?> ValueProperty = AvaloniaProperty.Register<NumberDisplayer<T>, T?>(
|
||||
nameof(Value), defaultBindingMode:BindingMode.TwoWay);
|
||||
|
||||
public T? Value
|
||||
{
|
||||
get => GetValue(ValueProperty);
|
||||
set => SetValue(ValueProperty, value);
|
||||
}
|
||||
|
||||
private static readonly StyledProperty<T?> InternalValueProperty = AvaloniaProperty.Register<NumberDisplayer<T>, T?>(
|
||||
nameof(InternalValue), defaultBindingMode:BindingMode.TwoWay);
|
||||
|
||||
private T? InternalValue
|
||||
{
|
||||
get => GetValue(InternalValueProperty);
|
||||
set => SetValue(InternalValueProperty, value);
|
||||
}
|
||||
|
||||
static NumberDisplayer()
|
||||
{
|
||||
ValueProperty.Changed.AddClassHandler<NumberDisplayer<T>, T?>((item, args) =>
|
||||
{
|
||||
item.OnValueChanged(args.OldValue.Value, args.NewValue.Value);
|
||||
});
|
||||
InternalValueProperty.Changed.AddClassHandler<NumberDisplayer<T>, T?>((item, args) =>
|
||||
{
|
||||
item.InternalText = args.NewValue.Value is null ? string.Empty : item.GetString(args.NewValue.Value);
|
||||
});
|
||||
DurationProperty.Changed.AddClassHandler<NumberDisplayer<T>, TimeSpan>((item, args) =>item.OnDurationChanged(args));
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
_animation = new Animation
|
||||
{
|
||||
Duration = Duration,
|
||||
FillMode = FillMode.Forward
|
||||
};
|
||||
_animation.Children.Add(new KeyFrame()
|
||||
{
|
||||
Cue = new Cue(0.0),
|
||||
Setters = { new Setter{Property = InternalValueProperty } }
|
||||
});
|
||||
_animation.Children.Add(new KeyFrame()
|
||||
{
|
||||
Cue = new Cue(1.0),
|
||||
Setters = { new Setter{Property = InternalValueProperty } }
|
||||
});
|
||||
Animation.SetAnimator(_animation.Children[0].Setters[0], GetAnimator());
|
||||
Animation.SetAnimator(_animation.Children[1].Setters[0], GetAnimator());
|
||||
|
||||
// Display value directly to text on initialization in case value equals to default.
|
||||
SetCurrentValue(InternalTextProperty, this.GetString(Value));
|
||||
}
|
||||
|
||||
private void OnDurationChanged(AvaloniaPropertyChangedEventArgs<TimeSpan> args)
|
||||
{
|
||||
if (_animation is null) return;
|
||||
_animation.Duration = args.NewValue.Value;
|
||||
}
|
||||
|
||||
private void OnValueChanged(T? oldValue, T? newValue)
|
||||
{
|
||||
if (_animation is null)
|
||||
{
|
||||
SetCurrentValue(InternalValueProperty, newValue);
|
||||
return;
|
||||
}
|
||||
_cts.Cancel();
|
||||
_cts = new CancellationTokenSource();
|
||||
(_animation.Children[0].Setters[0] as Setter)!.Value = oldValue;
|
||||
(_animation.Children[1].Setters[0] as Setter)!.Value = newValue;
|
||||
_animation.RunAsync(this, _cts.Token);
|
||||
}
|
||||
|
||||
protected abstract InterpolatingAnimator<T> GetAnimator();
|
||||
|
||||
protected abstract string GetString(T? value);
|
||||
}
|
||||
Reference in New Issue
Block a user