feat: coerce speed to be larger than 0. only initialize timer after attached to visual tree.

This commit is contained in:
rabbitism
2024-12-18 14:10:07 +08:00
parent 7a364ff6b3
commit 36f0cf78c0
2 changed files with 30 additions and 24 deletions

View File

@@ -30,7 +30,7 @@
Name="speed" Name="speed"
AllowDrag="True" AllowDrag="True"
u:FormItem.Label="Speed" u:FormItem.Label="Speed"
Minimum="1" Minimum="-100"
Maximum="300" Maximum="300"
Value="60"/> Value="60"/>
<ToggleSwitch <ToggleSwitch

View File

@@ -1,7 +1,6 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Layout; using Avalonia.Layout;
using Avalonia.Threading; using Avalonia.Threading;
using Irihi.Avalonia.Shared.Helpers; using Irihi.Avalonia.Shared.Helpers;
@@ -27,17 +26,23 @@ public class Marquee : ContentControl
/// Defines the <see cref="Speed" /> property. /// Defines the <see cref="Speed" /> property.
/// </summary> /// </summary>
public static readonly StyledProperty<double> SpeedProperty = AvaloniaProperty.Register<Marquee, double>( public static readonly StyledProperty<double> SpeedProperty = AvaloniaProperty.Register<Marquee, double>(
nameof(Speed), 60.0); nameof(Speed), 60.0, coerce: OnCoerceSpeed);
private Timer _timer; private static double OnCoerceSpeed(AvaloniaObject arg1, double arg2)
{
if (arg2 < 0) return 0;
return arg2;
}
private Timer? _timer;
static Marquee() static Marquee()
{ {
ClipToBoundsProperty.OverrideDefaultValue<Marquee>(true); ClipToBoundsProperty.OverrideDefaultValue<Marquee>(true);
HorizontalContentAlignmentProperty.OverrideDefaultValue<Marquee>(HorizontalAlignment.Center); HorizontalContentAlignmentProperty.OverrideDefaultValue<Marquee>(HorizontalAlignment.Center);
VerticalContentAlignmentProperty.OverrideDefaultValue<Marquee>(VerticalAlignment.Center); VerticalContentAlignmentProperty.OverrideDefaultValue<Marquee>(VerticalAlignment.Center);
HorizontalContentAlignmentProperty.Changed.AddClassHandler<Marquee>((o,args)=>o.InvalidatePresenterPosition()); HorizontalContentAlignmentProperty.Changed.AddClassHandler<Marquee>((o,_)=>o.InvalidatePresenterPosition());
VerticalContentAlignmentProperty.Changed.AddClassHandler<Marquee>((o,args)=>o.InvalidatePresenterPosition()); VerticalContentAlignmentProperty.Changed.AddClassHandler<Marquee>((o,_)=>o.InvalidatePresenterPosition());
IsRunningProperty.Changed.AddClassHandler<Marquee, bool>((o, args) => o.OnIsRunningChanged(args)); IsRunningProperty.Changed.AddClassHandler<Marquee, bool>((o, args) => o.OnIsRunningChanged(args));
} }
@@ -45,11 +50,11 @@ public class Marquee : ContentControl
{ {
if (args.NewValue.Value) if (args.NewValue.Value)
{ {
_timer.Start(); _timer?.Start();
} }
else else
{ {
_timer.Stop(); _timer?.Stop();
} }
} }
@@ -62,20 +67,30 @@ public class Marquee : ContentControl
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{ {
base.OnAttachedToVisualTree(e); base.OnAttachedToVisualTree(e);
_timer.Stop(); if (Presenter is not null)
_timer.Dispose(); {
Presenter.SizeChanged+= OnPresenterSizeChanged;
}
_timer?.Stop();
_timer?.Dispose();
_timer = new Timer(); _timer = new Timer();
_timer.Interval = 1000 / 60.0; _timer.Interval = 1000 / 60.0;
_timer.Elapsed += TimerOnTick; _timer.Elapsed += TimerOnTick;
_timer.Start(); if (IsRunning)
{
_timer.Start();
}
} }
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{ {
base.OnDetachedFromVisualTree(e); base.OnDetachedFromVisualTree(e);
_timer.Elapsed -= TimerOnTick; if (_timer is not null)
_timer.Stop(); {
_timer.Dispose(); _timer.Elapsed -= TimerOnTick;
_timer.Stop();
_timer.Dispose();
}
if (Presenter is not null) if (Presenter is not null)
{ {
Presenter.SizeChanged -= OnPresenterSizeChanged; Presenter.SizeChanged -= OnPresenterSizeChanged;
@@ -101,7 +116,7 @@ public class Marquee : ContentControl
} }
/// <summary> /// <summary>
/// Gets or sets the speed of the marquee. /// Gets or sets the speed of the marquee. Point per second.
/// </summary> /// </summary>
public double Speed public double Speed
{ {
@@ -109,15 +124,6 @@ public class Marquee : ContentControl
set => SetValue(SpeedProperty, value); set => SetValue(SpeedProperty, value);
} }
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
if (Presenter is not null)
{
Presenter.SizeChanged+= OnPresenterSizeChanged;
}
}
private void OnPresenterSizeChanged(object sender, SizeChangedEventArgs e) private void OnPresenterSizeChanged(object sender, SizeChangedEventArgs e)
{ {
InvalidatePresenterPosition(); InvalidatePresenterPosition();