feat: WIP.

This commit is contained in:
Zhang Dian
2024-06-05 10:31:56 +08:00
parent 0a30d01b7d
commit fd7010967e
7 changed files with 67 additions and 51 deletions

View File

@@ -14,7 +14,6 @@
<Grid ColumnDefinitions="*, 300">
<Grid Grid.Column="0">
<StackPanel>
<u:RatingCharacter />
<u:Rating
AllowClear="{Binding AllowClear }"
AllowHalf="{Binding AllowHalf }"

View File

@@ -13,8 +13,8 @@ public partial class RatingDemoViewModel : ViewModelBase
[ObservableProperty] private double _value;
// [ObservableProperty] private object _character;
[ObservableProperty] private int _count = 10;
[ObservableProperty] private double _defaultValue = 5.5;
[ObservableProperty] private int _count = 5;
[ObservableProperty] private double _defaultValue = 2.3;
public ObservableCollection<string> Tooltips { get; set; } = ["1", "2", "3", "4", "5"];
}

View File

@@ -3,7 +3,7 @@
xmlns:u="https://irihi.tech/ursa">
<ControlTheme x:Key="{x:Type u:RatingCharacter}" TargetType="u:RatingCharacter">
<Setter Property="Foreground" Value="{DynamicResource RatingCharacterUnSelectedForeground}" />
<Setter Property="Background" Value="{DynamicResource RatingCharacterUnSelectedForeground}" />
<Setter Property="Background" Value="{DynamicResource RatingCharacterBackground}" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="Template">
@@ -15,7 +15,6 @@
Data="{DynamicResource RatingStarIconGlyph}"
Fill="{TemplateBinding Background}" />
<Border Name="{x:Static u:RatingCharacter.PART_IconGlyph}"
IsVisible="True"
ClipToBounds="True">
<Path Width="24"
Height="24"
@@ -32,15 +31,7 @@
<Setter Property="RenderTransform" Value="scale(1.1)" />
</Style>
<Style Selector="^:selected">
<Setter Property="Foreground" Value="{DynamicResource RatingCharacterSelectedForeground}" />
</Style>
<Style Selector="^:half">
<!-- <Setter Property="Foreground"> -->
<!-- <LinearGradientBrush StartPoint="0%,0%" EndPoint="0%,100%"> -->
<!-- <GradientStop Color="#6b4c1b" Offset="0" /> -->
<!-- <GradientStop Color="#291e10" Offset="1" /> -->
<!-- </LinearGradientBrush> -->
<!-- </Setter> -->
<Setter Property="Foreground" Value="{DynamicResource RatingCharacterForeground}" />
</Style>
</ControlTheme>

View File

@@ -1,4 +1,5 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="RatingCharacterUnSelectedForeground" Opacity="0.12" Color="White" />
<SolidColorBrush x:Key="RatingCharacterSelectedForeground" Color="#FDDE43" />
<SolidColorBrush x:Key="RatingCharacterUnSelectedForeground" Color="Transparent" />
<SolidColorBrush x:Key="RatingCharacterForeground" Color="#FDDE43" />
<SolidColorBrush x:Key="RatingCharacterBackground" Opacity="0.12" Color="White" />
</ResourceDictionary>

View File

@@ -1,4 +1,5 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="RatingCharacterUnSelectedForeground" Opacity="0.05" Color="#2E3238" />
<SolidColorBrush x:Key="RatingCharacterSelectedForeground" Color="#FAC800" />
<SolidColorBrush x:Key="RatingCharacterUnSelectedForeground" Color="Transparent" />
<SolidColorBrush x:Key="RatingCharacterForeground" Color="#FAC800" />
<SolidColorBrush x:Key="RatingCharacterBackground" Opacity="0.05" Color="#2E3238" />
</ResourceDictionary>

View File

@@ -137,7 +137,11 @@ public class Rating : TemplatedControl
private void OnValueChanged(AvaloniaPropertyChangedEventArgs e)
{
UpdateItems((int)Value - 1);
if (e.NewValue is double newValue)
{
UpdateItemsByValue(newValue);
AdjustWidth(newValue);
}
}
private void OnCountChanged(AvaloniaPropertyChangedEventArgs e)
@@ -164,10 +168,8 @@ public class Rating : TemplatedControl
}
}
if (Value > newCount)
{
SetCurrentValue(ValueProperty, Math.Max(newCount, 0));
}
UpdateItemsByValue(Value);
AdjustWidth(Value);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
@@ -180,7 +182,6 @@ public class Rating : TemplatedControl
Items.Add(new RatingCharacter());
}
UpdateItems((int)DefaultValue - 1);
if (DefaultValue > Count)
{
SetCurrentValue(ValueProperty, Math.Max(Count, 0));
@@ -207,15 +208,15 @@ public class Rating : TemplatedControl
}
}
UpdateItems(index);
UpdateItemsByIndex(index);
}
protected override void OnPointerExited(PointerEventArgs e)
{
UpdateItems((int)Value - 1);
UpdateItemsByValue(Value);
}
public void Select(RatingCharacter o)
public void PointerReleasedHandler(RatingCharacter o)
{
var index = Items.IndexOf(o);
double newValue = index + 1;
@@ -226,23 +227,25 @@ public class Rating : TemplatedControl
if (AllowClear && Math.Abs(Value - newValue) < Tolerance)
{
UpdateItems(-1);
UpdateItemsByValue(-1);
SetCurrentValue(ValueProperty, 0);
}
else
{
UpdateItems(index);
UpdateItemsByValue(newValue);
SetCurrentValue(ValueProperty, newValue);
}
}
private void UpdateItems(int index)
private void UpdateItemsByIndex(int index)
{
var isInt = Math.Abs(Value - Math.Floor(Value)) < Tolerance;
for (var i = 0; i <= index && i < Items.Count; i++)
{
if (Items[i] is RatingCharacter item)
{
item.Select(true);
item.IsHalf = !isInt && i == index;
}
}
@@ -251,6 +254,34 @@ public class Rating : TemplatedControl
if (Items[i] is RatingCharacter item)
{
item.Select(false);
item.IsHalf = false;
}
}
}
private void UpdateItemsByValue(double newValue)
{
var index = (int)Math.Ceiling(newValue - 1);
UpdateItemsByIndex(index);
}
private void AdjustWidth(double newValue)
{
var ratio = Math.Abs(newValue - Math.Floor(newValue));
foreach (var character in Items)
{
if (character is RatingCharacter item)
{
if (item.IsHalf)
{
item.Ratio = ratio;
}
else
{
item.Ratio = 1;
}
item.AdjustWidth();
}
}
}

View File

@@ -6,13 +6,12 @@ using Avalonia.LogicalTree;
namespace Ursa.Controls;
[PseudoClasses(PC_Selected, PC_Half)]
[PseudoClasses(PC_Selected)]
[TemplatePart(PART_IconGlyph, typeof(Control))]
public class RatingCharacter : TemplatedControl
{
public const string PART_IconGlyph = "PART_IconGlyph";
protected const string PC_Selected = ":selected";
protected const string PC_Half = ":half";
private Control? _icon;
@@ -23,13 +22,14 @@ public class RatingCharacter : TemplatedControl
get => _isHalf;
set
{
if (_isHalf == value) return;
_isHalf = value;
if (_icon is null) return;
_icon.Width = value ? Bounds.Width / 2 : Bounds.Width;
_icon.Width = value ? Bounds.Width * 0.5 : Bounds.Width;
}
}
internal double Ratio { get; set; }
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
@@ -45,32 +45,25 @@ public class RatingCharacter : TemplatedControl
protected override void OnPointerMoved(PointerEventArgs e)
{
var p = e.GetPosition(this);
var flag = p.X < Bounds.Width / 2;
PseudoClasses.Set(PC_Half, flag);
IsHalf = flag;
// if (flag)
// {
// _icon.Width = Bounds.Width / 2;
// }
// else
// {
// _icon.Width = Bounds.Width;
// }
IsHalf = p.X < Bounds.Width * 0.5;
}
// protected override void OnPointerExited(PointerEventArgs e)
// {
// // _icon.Width = Bounds.Width;
// }
protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
var parent = this.GetLogicalAncestors().OfType<Rating>().FirstOrDefault();
parent?.Select(this);
parent?.PointerReleasedHandler(this);
}
public void Select(bool value)
{
PseudoClasses.Set(PC_Selected, value);
}
public void AdjustWidth()
{
if (_icon is not null)
{
_icon.Width = Bounds.Width * Ratio;
}
}
}