feat: respect presenter size change.

This commit is contained in:
rabbitism
2024-12-17 20:07:19 +08:00
parent cde9d67251
commit 31685cda0f
2 changed files with 24 additions and 1 deletions

View File

@@ -10,6 +10,7 @@
mc:Ignorable="d">
<StackPanel>
<u:Form>
<TextBox Width="300" Name="content" u:FormItem.Label="Content" Text="Hello Avalonia"></TextBox>
<u:EnumSelector
u:FormItem.Label="Direction"
Name="direction"
@@ -45,8 +46,9 @@
Speed="{Binding #speed.Value}"
Background="{DynamicResource SemiBlue1}"
Direction="{Binding #direction.Value}"
Content="{Binding #content.Text}"
IsRunning="{Binding #running.IsChecked}">
<TextBlock VerticalAlignment="Center" Text="Hello World" />
</u:Marquee>
</StackPanel>
</UserControl>

View File

@@ -1,6 +1,7 @@
using System.Runtime.CompilerServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Layout;
using Avalonia.Threading;
using Irihi.Avalonia.Shared.Helpers;
@@ -75,6 +76,10 @@ public class Marquee : ContentControl
_timer.Elapsed -= TimerOnTick;
_timer.Stop();
_timer.Dispose();
if (Presenter is not null)
{
Presenter.SizeChanged -= OnPresenterSizeChanged;
}
}
/// <summary>
@@ -104,6 +109,22 @@ public class Marquee : ContentControl
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)
{
InvalidatePresenterPosition();
}
private void TimerOnTick(object sender, System.EventArgs e)
{
var layoutValues = Dispatcher.UIThread.Invoke(GetLayoutValues);