misc: optimize.
This commit is contained in:
@@ -5,6 +5,7 @@ using Avalonia.Controls.Metadata;
|
|||||||
using Avalonia.Controls.Primitives;
|
using Avalonia.Controls.Primitives;
|
||||||
using Avalonia.Controls.Templates;
|
using Avalonia.Controls.Templates;
|
||||||
using Avalonia.Data;
|
using Avalonia.Data;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
|
||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
@@ -123,7 +124,6 @@ public class Rating : TemplatedControl
|
|||||||
private void OnCountChanged(AvaloniaPropertyChangedEventArgs e)
|
private void OnCountChanged(AvaloniaPropertyChangedEventArgs e)
|
||||||
{
|
{
|
||||||
if (!IsLoaded) return;
|
if (!IsLoaded) return;
|
||||||
|
|
||||||
var currentCount = Items.Count;
|
var currentCount = Items.Count;
|
||||||
var newCount = e.GetNewValue<int>();
|
var newCount = e.GetNewValue<int>();
|
||||||
|
|
||||||
@@ -154,6 +154,7 @@ public class Rating : TemplatedControl
|
|||||||
|
|
||||||
private void OnAllowHalfChanged(AvaloniaPropertyChangedEventArgs e)
|
private void OnAllowHalfChanged(AvaloniaPropertyChangedEventArgs e)
|
||||||
{
|
{
|
||||||
|
if (!IsLoaded) return;
|
||||||
if (e.NewValue is not bool newValue) return;
|
if (e.NewValue is not bool newValue) return;
|
||||||
foreach (var item in Items)
|
foreach (var item in Items)
|
||||||
{
|
{
|
||||||
@@ -178,6 +179,11 @@ public class Rating : TemplatedControl
|
|||||||
}
|
}
|
||||||
|
|
||||||
SetCurrentValue(ValueProperty, DefaultValue);
|
SetCurrentValue(ValueProperty, DefaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnLoaded(RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnLoaded(e);
|
||||||
UpdateItemsByValue(DefaultValue);
|
UpdateItemsByValue(DefaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,17 +246,12 @@ public class Rating : TemplatedControl
|
|||||||
|
|
||||||
private void UpdateChosenItem(double newValue)
|
private void UpdateChosenItem(double newValue)
|
||||||
{
|
{
|
||||||
var ratio = newValue - Math.Floor(newValue);
|
|
||||||
var isFraction = ratio >= double.Epsilon;
|
|
||||||
ratio = AllowHalf && isFraction ? ratio : 1;
|
|
||||||
var item = Items.FirstOrDefault(item => item.IsLast);
|
var item = Items.FirstOrDefault(item => item.IsLast);
|
||||||
if (item is null) return;
|
if (item is null) return;
|
||||||
if (!AllowHalf && isFraction)
|
var ratio = newValue - Math.Floor(newValue);
|
||||||
{
|
var isFraction = ratio >= double.Epsilon;
|
||||||
item.SetSelectedState(false);
|
item.SetSelectedState(AllowHalf || !isFraction);
|
||||||
}
|
item.Ratio = AllowHalf && isFraction ? ratio : 1;
|
||||||
|
|
||||||
item.Ratio = ratio;
|
|
||||||
item.ApplyRatio();
|
item.ApplyRatio();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user