Browse Source

Subscribe to DashStyle too

pull/7303/head
Nikita Tsukanov 5 years ago
parent
commit
7e45ee1e53
  1. 30
      src/Avalonia.Visuals/Media/Pen.cs

30
src/Avalonia.Visuals/Media/Pen.cs

@ -46,7 +46,8 @@ namespace Avalonia.Media
AvaloniaProperty.Register<Pen, double>(nameof(MiterLimit), 10.0); AvaloniaProperty.Register<Pen, double>(nameof(MiterLimit), 10.0);
private EventHandler? _invalidated; private EventHandler? _invalidated;
private IAffectsRender? _subscribedTo; private IAffectsRender? _subscribedToBrush;
private IAffectsRender? _subscribedToDashes;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Pen"/> class. /// Initializes a new instance of the <see cref="Pen"/> class.
@ -167,12 +168,12 @@ namespace Avalonia.Media
add add
{ {
_invalidated += value; _invalidated += value;
UpdateBrushSubscription(); UpdateSubscriptions();
} }
remove remove
{ {
_invalidated -= value; _invalidated -= value;
UpdateBrushSubscription(); UpdateSubscriptions();
} }
} }
@ -195,24 +196,33 @@ namespace Avalonia.Media
{ {
_invalidated?.Invoke(this, EventArgs.Empty); _invalidated?.Invoke(this, EventArgs.Empty);
if(change.Property == BrushProperty) if(change.Property == BrushProperty)
UpdateBrushSubscription(); UpdateSubscription(ref _subscribedToBrush, Brush);
if(change.Property == DashStyleProperty)
UpdateSubscription(ref _subscribedToDashes, DashStyle);
base.OnPropertyChanged(change); base.OnPropertyChanged(change);
} }
void UpdateBrushSubscription()
void UpdateSubscription(ref IAffectsRender? field, object? value)
{ {
if ((_invalidated == null || _subscribedTo != Brush) && _subscribedTo != null) if ((_invalidated == null || field != value) && field != null)
{ {
InvalidatedWeakEvent.Unsubscribe(_subscribedTo, this); InvalidatedWeakEvent.Unsubscribe(field, this);
_subscribedTo = null; field = null;
} }
if (_invalidated != null && _subscribedTo != Brush && Brush is IAffectsRender affectsRender) if (_invalidated != null && field != value && value is IAffectsRender affectsRender)
{ {
InvalidatedWeakEvent.Subscribe(affectsRender, this); InvalidatedWeakEvent.Subscribe(affectsRender, this);
_subscribedTo = affectsRender; field = affectsRender;
} }
} }
void UpdateSubscriptions()
{
UpdateSubscription(ref _subscribedToBrush, Brush);
UpdateSubscription(ref _subscribedToDashes, DashStyle);
}
void IWeakEventSubscriber<EventArgs>.OnEvent(object? sender, WeakEvent ev, EventArgs e) void IWeakEventSubscriber<EventArgs>.OnEvent(object? sender, WeakEvent ev, EventArgs e)
{ {

Loading…
Cancel
Save