Browse Source

Add SizeChanged event

pull/9307/head
robloo 4 years ago
parent
commit
5ea8d3d83a
  1. 34
      src/Avalonia.Controls/Control.cs
  2. 60
      src/Avalonia.Controls/SizeChangedEventArgs.cs

34
src/Avalonia.Controls/Control.cs

@ -84,6 +84,13 @@ namespace Avalonia.Controls
nameof(Unloaded),
RoutingStrategies.Direct);
/// <summary>
/// Defines the <see cref="SizeChanged"/> event.
/// </summary>
public static readonly RoutedEvent<SizeChangedEventArgs> SizeChangedEvent =
RoutedEvent.Register<Control, SizeChangedEventArgs>(
nameof(SizeChanged), RoutingStrategies.Bubble);
/// <summary>
/// Defines the <see cref="FlowDirection"/> property.
/// </summary>
@ -211,6 +218,15 @@ namespace Avalonia.Controls
remove => RemoveHandler(UnloadedEvent, value);
}
/// <summary>
/// Occurs when the bounds (actual size) of the control have changed.
/// </summary>
public event EventHandler<SizeChangedEventArgs>? SizeChanged
{
add => AddHandler(SizeChangedEvent, value);
remove => RemoveHandler(SizeChangedEvent, value);
}
public new IControl? Parent => (IControl?)base.Parent;
/// <summary>
@ -530,14 +546,28 @@ namespace Avalonia.Controls
}
}
/// <inheritdoc/>
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == FlowDirectionProperty)
if (change.Property == BoundsProperty)
{
var oldValue = change.GetOldValue<Rect>();
var newValue = change.GetNewValue<Rect>();
var sizeChangedEventArgs = new SizeChangedEventArgs(
SizeChangedEvent,
source: this,
newSize: new Size(newValue.Width, newValue.Height),
previousSize: new Size(oldValue.Width, oldValue.Height));
RaiseEvent(sizeChangedEventArgs);
}
else if (change.Property == FlowDirectionProperty)
{
InvalidateMirrorTransform();
foreach (var visual in VisualChildren)
{
if (visual is Control child)

60
src/Avalonia.Controls/SizeChangedEventArgs.cs

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Avalonia.Interactivity;
namespace Avalonia.Controls
{
/// <summary>
/// Provides data specific to a SizeChanged event.
/// </summary>
public class SizeChangedEventArgs : RoutedEventArgs
{
public SizeChangedEventArgs(RoutedEvent? routedEvent)
: base (routedEvent)
{
}
public SizeChangedEventArgs(RoutedEvent? routedEvent, IInteractive? source)
: base(routedEvent, source)
{
}
public SizeChangedEventArgs(
RoutedEvent? routedEvent,
IInteractive? source,
Size newSize,
Size previousSize)
: base(routedEvent, source)
{
NewSize = newSize;
PreviousSize = previousSize;
HeightChanged = newSize.Height != previousSize.Height;
WidthChanged = newSize.Width != previousSize.Width;
}
/// <summary>
/// Gets a value indicating whether the height of the new size is different
/// than the previous size height.
/// </summary>
public bool HeightChanged { get; init; }
/// <summary>
/// Gets the new size (or bounds) of the object.
/// </summary>
public Size NewSize { get; init; }
/// <summary>
/// Gets the previous size (or bounds) of the object.
/// </summary>
public Size PreviousSize { get; init; }
/// <summary>
/// Gets a value indicating whether the width of the new size is different
/// than the previous size width.
/// </summary>
public bool WidthChanged { get; init; }
}
}
Loading…
Cancel
Save