diff --git a/demo/Ursa.Demo/Pages/RatingDemo.axaml b/demo/Ursa.Demo/Pages/RatingDemo.axaml new file mode 100644 index 0000000..89ab51f --- /dev/null +++ b/demo/Ursa.Demo/Pages/RatingDemo.axaml @@ -0,0 +1,123 @@ + + + + M12 5.99999C10.5 2.12432 5.75193 2.0557 3.40383 4.4038C0.865423 6.94221 0.999999 10.5 3.50001 14C5.52139 16.8299 9.83088 20.3136 11.4069 21.5438C11.7573 21.8172 12.2427 21.8172 12.5931 21.5438C14.1691 20.3136 18.4786 16.8299 20.5 14C23 10.5 23.1346 6.94221 20.5962 4.4038C18.2481 2.0557 14.5 2.12432 12 5.99999Z + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/demo/Ursa.Demo/Pages/RatingDemo.axaml.cs b/demo/Ursa.Demo/Pages/RatingDemo.axaml.cs new file mode 100644 index 0000000..8168903 --- /dev/null +++ b/demo/Ursa.Demo/Pages/RatingDemo.axaml.cs @@ -0,0 +1,13 @@ +using Avalonia.Controls; +using Ursa.Demo.ViewModels; + +namespace Ursa.Demo.Pages; + +public partial class RatingDemo : UserControl +{ + public RatingDemo() + { + InitializeComponent(); + this.DataContext = new RatingDemoViewModel(); + } +} \ No newline at end of file diff --git a/demo/Ursa.Demo/ViewModels/MainViewViewModel.cs b/demo/Ursa.Demo/ViewModels/MainViewViewModel.cs index f981fa6..89a8532 100644 --- a/demo/Ursa.Demo/ViewModels/MainViewViewModel.cs +++ b/demo/Ursa.Demo/ViewModels/MainViewViewModel.cs @@ -51,6 +51,7 @@ public class MainViewViewModel : ViewModelBase MenuKeys.MenuKeyNumPad => new NumPadDemoViewModel(), MenuKeys.MenuKeyPagination => new PaginationDemoViewModel(), MenuKeys.MenuKeyRangeSlider => new RangeSliderDemoViewModel(), + MenuKeys.MenuKeyRating => new RatingDemoViewModel(), MenuKeys.MenuKeyScrollToButton => new ScrollToButtonDemoViewModel(), MenuKeys.MenuKeySelectionList => new SelectionListDemoViewModel(), MenuKeys.MenuKeySkeleton => new SkeletonDemoViewModel(), diff --git a/demo/Ursa.Demo/ViewModels/MenuViewModel.cs b/demo/Ursa.Demo/ViewModels/MenuViewModel.cs index ead5ab8..a6c1b79 100644 --- a/demo/Ursa.Demo/ViewModels/MenuViewModel.cs +++ b/demo/Ursa.Demo/ViewModels/MenuViewModel.cs @@ -38,6 +38,7 @@ public class MenuViewModel: ViewModelBase new() { MenuHeader = "NumPad", Key = MenuKeys.MenuKeyNumPad }, new() { MenuHeader = "Pagination", Key = MenuKeys.MenuKeyPagination }, new() { MenuHeader = "RangeSlider", Key = MenuKeys.MenuKeyRangeSlider }, + new() { MenuHeader = "Rating", Key = MenuKeys.MenuKeyRating, Status = "New"}, new() { MenuHeader = "Scroll To", Key = MenuKeys.MenuKeyScrollToButton }, new() { MenuHeader = "Selection List", Key = MenuKeys.MenuKeySelectionList }, new() { MenuHeader = "Skeleton", Key = MenuKeys.MenuKeySkeleton }, @@ -83,6 +84,7 @@ public static class MenuKeys public const string MenuKeyNumPad = "NumPad"; public const string MenuKeyPagination = "Pagination"; public const string MenuKeyRangeSlider = "RangeSlider"; + public const string MenuKeyRating = "Rating"; public const string MenuKeyScrollToButton = "ScrollToButton"; public const string MenuKeySelectionList = "SelectionList"; public const string MenuKeyTagInput = "TagInput"; diff --git a/demo/Ursa.Demo/ViewModels/RatingDemoViewModel.cs b/demo/Ursa.Demo/ViewModels/RatingDemoViewModel.cs new file mode 100644 index 0000000..6028660 --- /dev/null +++ b/demo/Ursa.Demo/ViewModels/RatingDemoViewModel.cs @@ -0,0 +1,13 @@ +using CommunityToolkit.Mvvm.ComponentModel; + +namespace Ursa.Demo.ViewModels; + +public partial class RatingDemoViewModel : ViewModelBase +{ + [ObservableProperty] private bool _allowClear = true; + [ObservableProperty] private bool _allowHalf = true; + [ObservableProperty] private bool _isEnabled = true; + [ObservableProperty] private double _value; + [ObservableProperty] private double _defaultValue = 2.3; + [ObservableProperty] private int _count = 5; +} \ No newline at end of file diff --git a/src/Ursa.Themes.Semi/Controls/Rating.axaml b/src/Ursa.Themes.Semi/Controls/Rating.axaml new file mode 100644 index 0000000..6a91411 --- /dev/null +++ b/src/Ursa.Themes.Semi/Controls/Rating.axaml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Ursa.Themes.Semi/Controls/_index.axaml b/src/Ursa.Themes.Semi/Controls/_index.axaml index bf6acad..23ba36e 100644 --- a/src/Ursa.Themes.Semi/Controls/_index.axaml +++ b/src/Ursa.Themes.Semi/Controls/_index.axaml @@ -28,6 +28,7 @@ + diff --git a/src/Ursa.Themes.Semi/Themes/Dark/Rating.axaml b/src/Ursa.Themes.Semi/Themes/Dark/Rating.axaml new file mode 100644 index 0000000..f0cc810 --- /dev/null +++ b/src/Ursa.Themes.Semi/Themes/Dark/Rating.axaml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/Ursa.Themes.Semi/Themes/Dark/_index.axaml b/src/Ursa.Themes.Semi/Themes/Dark/_index.axaml index 7f6bc9c..86727f2 100644 --- a/src/Ursa.Themes.Semi/Themes/Dark/_index.axaml +++ b/src/Ursa.Themes.Semi/Themes/Dark/_index.axaml @@ -12,6 +12,7 @@ + diff --git a/src/Ursa.Themes.Semi/Themes/Light/Rating.axaml b/src/Ursa.Themes.Semi/Themes/Light/Rating.axaml new file mode 100644 index 0000000..ddb703a --- /dev/null +++ b/src/Ursa.Themes.Semi/Themes/Light/Rating.axaml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/Ursa.Themes.Semi/Themes/Light/_index.axaml b/src/Ursa.Themes.Semi/Themes/Light/_index.axaml index 7f6bc9c..86727f2 100644 --- a/src/Ursa.Themes.Semi/Themes/Light/_index.axaml +++ b/src/Ursa.Themes.Semi/Themes/Light/_index.axaml @@ -12,6 +12,7 @@ + diff --git a/src/Ursa.Themes.Semi/Themes/Shared/Rating.axaml b/src/Ursa.Themes.Semi/Themes/Shared/Rating.axaml new file mode 100644 index 0000000..5065bb6 --- /dev/null +++ b/src/Ursa.Themes.Semi/Themes/Shared/Rating.axaml @@ -0,0 +1,6 @@ + + 24 + 16 + 3 4 + M10.7525 1.90411C11.1451 0.698628 12.8549 0.698631 13.2475 1.90411L15.2395 8.01946H21.6858C22.9565 8.01946 23.4848 9.64143 22.4568 10.3865L17.2417 14.1659L19.2337 20.2813C19.6263 21.4868 18.2431 22.4892 17.2151 21.7442L12 17.9647L6.78489 21.7442C5.75687 22.4892 4.37368 21.4868 4.76635 20.2813L6.75834 14.1659L1.54323 10.3865C0.515206 9.64142 1.04354 8.01946 2.31425 8.01946H8.76048L10.7525 1.90411Z + \ No newline at end of file diff --git a/src/Ursa.Themes.Semi/Themes/Shared/_index.axaml b/src/Ursa.Themes.Semi/Themes/Shared/_index.axaml index 658f6ae..5350d09 100644 --- a/src/Ursa.Themes.Semi/Themes/Shared/_index.axaml +++ b/src/Ursa.Themes.Semi/Themes/Shared/_index.axaml @@ -13,6 +13,7 @@ + diff --git a/src/Ursa/Controls/Rating/Rating.cs b/src/Ursa/Controls/Rating/Rating.cs new file mode 100644 index 0000000..e2f277e --- /dev/null +++ b/src/Ursa/Controls/Rating/Rating.cs @@ -0,0 +1,257 @@ +using Avalonia; +using Avalonia.Collections; +using Avalonia.Controls; +using Avalonia.Controls.Metadata; +using Avalonia.Controls.Primitives; +using Avalonia.Controls.Templates; +using Avalonia.Data; +using Avalonia.Interactivity; + +namespace Ursa.Controls; + +[PseudoClasses(PC_Selected)] +[TemplatePart(PART_ItemsControl, typeof(ItemsControl))] +public class Rating : TemplatedControl +{ + public const string PART_ItemsControl = "PART_ItemsControl"; + protected const string PC_Selected = ":selected"; + + public static readonly StyledProperty ValueProperty = + AvaloniaProperty.Register(nameof(Value), defaultBindingMode: BindingMode.TwoWay); + + public static readonly StyledProperty AllowClearProperty = + AvaloniaProperty.Register(nameof(AllowClear), true); + + public static readonly StyledProperty AllowHalfProperty = + AvaloniaProperty.Register(nameof(AllowHalf)); + + public static readonly StyledProperty CharacterProperty = + AvaloniaProperty.Register(nameof(Character)); + + public static readonly StyledProperty CountProperty = + AvaloniaProperty.Register(nameof(Count), 5); + + public static readonly StyledProperty DefaultValueProperty = + AvaloniaProperty.Register(nameof(DefaultValue)); + + public static readonly StyledProperty SizeProperty = + AvaloniaProperty.Register(nameof(Size)); + + public static readonly StyledProperty ItemTemplateProperty = + AvaloniaProperty.Register(nameof(ItemTemplate)); + + public double Value + { + get => GetValue(ValueProperty); + set => SetValue(ValueProperty, value); + } + + public bool AllowClear + { + get => GetValue(AllowClearProperty); + set => SetValue(AllowClearProperty, value); + } + + public bool AllowHalf + { + get => GetValue(AllowHalfProperty); + set => SetValue(AllowHalfProperty, value); + } + + public object Character + { + get => GetValue(CharacterProperty); + set => SetValue(CharacterProperty, value); + } + + public int Count + { + get => GetValue(CountProperty); + set => SetValue(CountProperty, value); + } + + public double DefaultValue + { + get => GetValue(DefaultValueProperty); + set => SetValue(DefaultValueProperty, value); + } + + public double Size + { + get => GetValue(SizeProperty); + set => SetValue(SizeProperty, value); + } + + public IDataTemplate? ItemTemplate + { + get => GetValue(ItemTemplateProperty); + set => SetValue(ItemTemplateProperty, value); + } + + public static readonly DirectProperty> ItemsProperty = + AvaloniaProperty.RegisterDirect>( + nameof(Items), o => o.Items); + + private AvaloniaList _items = null!; + + public AvaloniaList Items + { + get => _items; + private set => SetAndRaise(ItemsProperty, ref _items, value); + } + + public Rating() + { + Items = []; + } + + static Rating() + { + ValueProperty.Changed.AddClassHandler((s, e) => s.OnValueChanged(e)); + CountProperty.Changed.AddClassHandler((s, e) => s.OnCountChanged(e)); + AllowHalfProperty.Changed.AddClassHandler((s, e) => s.OnAllowHalfChanged(e)); + } + + private void OnValueChanged(AvaloniaPropertyChangedEventArgs e) + { + if (!IsLoaded) return; + if (e.NewValue is double newValue) + { + UpdateItemsByValue(newValue); + } + } + + private void OnCountChanged(AvaloniaPropertyChangedEventArgs e) + { + if (!IsLoaded) return; + var currentCount = Items.Count; + var newCount = e.GetNewValue(); + + if (currentCount < newCount) + { + var itemsToAdd = newCount - currentCount; + for (var i = 0; i < itemsToAdd; i++) + { + Items.Add(new RatingCharacter + { + Character = Character, + AllowHalf = AllowHalf, + Size = Size, + }); + } + } + else if (currentCount > newCount) + { + var itemsToRemove = currentCount - newCount; + for (var i = 0; i < itemsToRemove && currentCount > i; i++) + { + Items.RemoveAt(currentCount - i - 1); + } + } + + UpdateItemsByValue(Value); + } + + private void OnAllowHalfChanged(AvaloniaPropertyChangedEventArgs e) + { + if (!IsLoaded) return; + if (e.NewValue is not bool newValue) return; + foreach (var item in Items) + { + item.AllowHalf = newValue; + } + + UpdateItemsByValue(Value); + } + + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) + { + base.OnApplyTemplate(e); + for (var i = 0; i < Count; i++) + { + Items.Add(new RatingCharacter + { + Character = Character, + AllowHalf = AllowHalf, + Size = Size, + } + ); + } + + SetCurrentValue(ValueProperty, DefaultValue); + } + + protected override void OnLoaded(RoutedEventArgs e) + { + base.OnLoaded(e); + UpdateItemsByValue(DefaultValue); + } + + internal void PointerEnteredHandler(RatingCharacter o) + { + var item = Items.FirstOrDefault(item => item.IsLast); + if (item is not null) + { + item.IsHalf = false; + } + + var index = Items.IndexOf(o); + UpdateItemsByIndex(index); + } + + internal void PointerReleasedHandler(RatingCharacter o) + { + var index = Items.IndexOf(o); + double newValue = index + 1; + if (AllowHalf && o.IsHalf) + { + newValue = index + 0.5; + } + + if (AllowClear && Math.Abs(Value - newValue) < double.Epsilon) + { + SetCurrentValue(ValueProperty, 0); + } + else + { + SetCurrentValue(ValueProperty, newValue); + } + } + + internal void UpdateItemsByValue(double newValue) + { + RestorePreviousLastItem(); + var index = (int)Math.Ceiling(newValue - 1); + UpdateItemsByIndex(index); + UpdateChosenItem(newValue); + } + + private void RestorePreviousLastItem() + { + if (!AllowHalf) return; + var item = Items.FirstOrDefault(item => item.IsLast); + if (item is null) return; + item.Ratio = 1; + item.ApplyRatio(); + } + + private void UpdateItemsByIndex(int index) + { + for (var i = 0; i < Items.Count; i++) + { + Items[i].SetSelectedState(i <= index); + Items[i].IsLast = i == index; + } + } + + private void UpdateChosenItem(double newValue) + { + var item = Items.FirstOrDefault(item => item.IsLast); + if (item is null) return; + var ratio = newValue - Math.Floor(newValue); + var isFraction = ratio >= double.Epsilon; + item.SetSelectedState(AllowHalf || !isFraction); + item.Ratio = AllowHalf && isFraction ? ratio : 1; + item.ApplyRatio(); + } +} \ No newline at end of file diff --git a/src/Ursa/Controls/Rating/RatingCharacter.cs b/src/Ursa/Controls/Rating/RatingCharacter.cs new file mode 100644 index 0000000..c3623cf --- /dev/null +++ b/src/Ursa/Controls/Rating/RatingCharacter.cs @@ -0,0 +1,114 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Controls.Metadata; +using Avalonia.Controls.Primitives; +using Avalonia.Input; +using Avalonia.Interactivity; +using Avalonia.LogicalTree; + +namespace Ursa.Controls; + +[PseudoClasses(PC_Selected)] +[TemplatePart(PART_IconBorder, typeof(Border))] +public class RatingCharacter : TemplatedControl +{ + public const string PART_IconBorder = "PART_IconBorder"; + protected const string PC_Selected = ":selected"; + + private Border? _icon; + + public static readonly StyledProperty AllowHalfProperty = + Rating.AllowHalfProperty.AddOwner(); + + public static readonly StyledProperty CharacterProperty = + Rating.CharacterProperty.AddOwner(); + + public static readonly StyledProperty SizeProperty = + Rating.SizeProperty.AddOwner(); + + public bool AllowHalf + { + get => GetValue(AllowHalfProperty); + set => SetValue(AllowHalfProperty, value); + } + + public object Character + { + get => GetValue(CharacterProperty); + set => SetValue(CharacterProperty, value); + } + + public double Size + { + get => GetValue(SizeProperty); + set => SetValue(SizeProperty, value); + } + + internal bool IsLast { get; set; } + + private bool _isHalf; + + internal bool IsHalf + { + get => _isHalf; + set + { + if (!AllowHalf) return; + _isHalf = value; + if (_icon is null) return; + _icon.Width = value ? Bounds.Width * 0.5 : Bounds.Width; + } + } + + internal double Ratio { get; set; } = 1; + + protected override void OnLoaded(RoutedEventArgs e) + { + base.OnLoaded(e); + ApplyRatio(); + } + + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) + { + base.OnApplyTemplate(e); + _icon = e.NameScope.Find(PART_IconBorder); + } + + protected override void OnPointerEntered(PointerEventArgs e) + { + var parent = this.GetLogicalAncestors().OfType().FirstOrDefault(); + parent?.PointerEnteredHandler(this); + } + + protected override void OnPointerMoved(PointerEventArgs e) + { + if (!AllowHalf) return; + var p = e.GetPosition(this); + IsHalf = p.X < Bounds.Width * 0.5; + } + + protected override void OnPointerExited(PointerEventArgs e) + { + var parent = this.GetLogicalAncestors().OfType().FirstOrDefault(); + parent?.UpdateItemsByValue(parent.Value); + } + + protected override void OnPointerReleased(PointerReleasedEventArgs e) + { + var parent = this.GetLogicalAncestors().OfType().FirstOrDefault(); + parent?.PointerReleasedHandler(this); + } + + internal void SetSelectedState(bool value) + { + PseudoClasses.Set(PC_Selected, value); + } + + internal void ApplyRatio() + { + if (_icon is not null) + { + _icon.Width = Bounds.Width * Ratio; + } + } +} \ No newline at end of file