完成 slider 颜色

This commit is contained in:
polarboy 2024-07-28 15:36:46 +08:00
parent d2aeb89d07
commit 55ca06f9e0
2 changed files with 33 additions and 23 deletions

View File

@ -1,4 +1,8 @@
using Avalonia;
using AtomUI.Media;
using AtomUI.Styling;
using AtomUI.Utils;
using Avalonia;
using Avalonia.Animation;
using Avalonia.Automation.Peers;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
@ -24,10 +28,10 @@ public class SliderThumb : TemplatedControl
AvaloniaProperty.Register<SliderThumb, IBrush?>(nameof(OutlineBrush));
public static readonly StyledProperty<Thickness> OutlineThicknessProperty =
AvaloniaProperty.Register<TemplatedControl, Thickness>(nameof(OutlineThickness));
AvaloniaProperty.Register<SliderThumb, Thickness>(nameof(OutlineThickness));
public static readonly StyledProperty<double> ThumbCircleSizeProperty =
AvaloniaProperty.Register<TemplatedControl, double>(nameof(ThumbCircleSize));
AvaloniaProperty.Register<SliderThumb, double>(nameof(ThumbCircleSize));
private Point? _lastPoint;
@ -43,6 +47,14 @@ public class SliderThumb : TemplatedControl
BorderBrushProperty);
}
public SliderThumb()
{
var transitions = new Transitions();
transitions.Add(AnimationUtils.CreateTransition<SolidColorBrushTransition>(OutlineBrushProperty, GlobalResourceKey.MotionDurationFast));
transitions.Add(AnimationUtils.CreateTransition<ThicknessTransition>(OutlineThicknessProperty, GlobalResourceKey.MotionDurationFast));
Transitions = transitions;
}
public event EventHandler<VectorEventArgs>? DragStarted
{
add => AddHandler(DragStartedEvent, value);
@ -83,7 +95,14 @@ public class SliderThumb : TemplatedControl
{
if (_lastPoint.HasValue) _lastPoint = _lastPoint.Value + v;
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
Console.WriteLine(change.Property.Name);
Console.WriteLine(Transitions?.Count);
}
protected override AutomationPeer OnCreateAutomationPeer() => new SliderThumbAutomationPeer(this);
protected virtual void OnDragStarted(VectorEventArgs e) { }
@ -162,6 +181,7 @@ public class SliderThumb : TemplatedControl
public override void Render(DrawingContext context)
{
Console.WriteLine("xxxx");
// 绘制圆
var centerPos = new Point(Bounds.Width / 2, Bounds.Height / 2);
var thumbCircleRadius = ThumbCircleSize / 2 + BorderThickness.Left / 2;

View File

@ -2,6 +2,7 @@
using AtomUI.Styling;
using AtomUI.Utils;
using Avalonia;
using Avalonia.Animation;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
@ -274,6 +275,12 @@ public class SliderTrack : Control, IControlCustomStyle
BindUtils.CreateTokenBinding(this, SliderTrack.SliderRailSizeProperty, SliderResourceKey.RailSize);
HandleRangeModeChanged();
if (Transitions is null) {
var transitions = new Transitions();
transitions.Add(AnimationUtils.CreateTransition<SolidColorBrushTransition>(TrackGrooveBrushProperty));
transitions.Add(AnimationUtils.CreateTransition<SolidColorBrushTransition>(TrackBarBrushProperty));
Transitions = transitions;
}
}
private void HandleRangeModeChanged()
@ -329,20 +336,6 @@ public class SliderTrack : Control, IControlCustomStyle
}
}
private bool IsNeedHandlePressedForValue(Point point)
{
if ((StartSliderThumb is not null &&
StartSliderThumb.IsVisible &&
StartSliderThumb.Bounds.Contains(point)) ||
EndSliderThumb is not null &&
EndSliderThumb.IsVisible &&
EndSliderThumb.Bounds.Contains(point)) {
return false;
}
return true;
}
private Point GetGlobalOffset()
{
var topLevel = TopLevel.GetTopLevel(this);
@ -667,12 +660,9 @@ public class SliderTrack : Control, IControlCustomStyle
void IControlCustomStyle.PrepareRenderInfo()
{
_renderContextData = new RenderContextData();
var startThumbPivotOffset = 0d;
var endThumbPivotOffset = 0d;
CalculateThumbValuePivotOffset(Bounds.Size, Orientation == Orientation.Vertical,
out startThumbPivotOffset,
out endThumbPivotOffset);
out var startThumbPivotOffset,
out var endThumbPivotOffset);
var thumbSize = StartSliderThumb!.DesiredSize.Width;
if (Orientation == Orientation.Horizontal) {
{