Skip to content

Commit

Permalink
[Nodify] Add ResizeCompleted event to GroupingNode
Browse files Browse the repository at this point in the history
  • Loading branch information
miroiu committed Jun 8, 2021
1 parent f5a7dd0 commit 12a8d2f
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Nodify/Events/ConnectorEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System;
using System.Windows;

namespace Nodify
{
Expand Down Expand Up @@ -30,5 +31,8 @@ public ConnectorEventArgs(object connector)
/// Gets the <see cref="FrameworkElement.DataContext"/> of the <see cref="Nodify.Connector"/> associated with this event.
/// </summary>
public object Connector { get; }

protected override void InvokeEventHandler(Delegate genericHandler, object genericTarget)
=> ((ConnectorEventHandler)genericHandler)(genericTarget, this);
}
}
6 changes: 5 additions & 1 deletion Nodify/Events/PendingConnectionEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System;
using System.Windows;

namespace Nodify
{
Expand Down Expand Up @@ -50,5 +51,8 @@ public PendingConnectionEventArgs(object sourceConnector)
/// Gets or sets a value that indicates whether this <see cref="PendingConnection"/> was cancelled.
/// </summary>
public bool Canceled { get; set; }

protected override void InvokeEventHandler(Delegate genericHandler, object genericTarget)
=> ((PendingConnectionEventHandler)genericHandler)(genericTarget, this);
}
}
43 changes: 43 additions & 0 deletions Nodify/Events/ResizeEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Windows;

namespace Nodify
{
/// <summary>
/// Represents the method that will handle resize related routed events.
/// </summary>
/// <param name="sender">The sender of this event.</param>
/// <param name="e">The event data.</param>
public delegate void ResizeEventHandler(object sender, ResizeEventArgs e);

/// <summary>
/// Provides data for resize related routed events.
/// </summary>
public class ResizeEventArgs : RoutedEventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="ResizeEventArgs"/> class with the previous and the new <see cref="Size"/>.
/// </summary>
/// <param name="previousSize">The previous size associated with this event.</param>
/// <param name="newSize">The new size associated with this event.</param>
public ResizeEventArgs(Size previousSize, Size newSize)
{
PreviousSize = previousSize;
NewSize = newSize;
}

/// <summary>
/// Gets the previous size of the object.
/// </summary>
public Size PreviousSize { get; }


/// <summary>
/// Gets the new size of the object.
/// </summary>
public Size NewSize { get; }

protected override void InvokeEventHandler(Delegate genericHandler, object genericTarget)
=> ((ResizeEventHandler)genericHandler)(genericTarget, this);
}
}
46 changes: 45 additions & 1 deletion Nodify/Nodes/GroupingNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,28 @@ public class GroupingNode : HeaderedContentControl
protected const string ElementHeader = "PART_Header";
protected const string ElementContent = "PART_Content";

#region Routed Events

public static readonly RoutedEvent ResizeCompletedEvent = EventManager.RegisterRoutedEvent(nameof(ResizeCompleted), RoutingStrategy.Bubble, typeof(ResizeEventHandler), typeof(GroupingNode));

/// <summary>
/// Occurs when the node is resized and <see cref="ActualSize"/> is set.
/// </summary>
public event ResizeEventHandler ResizeCompleted
{
add => AddHandler(ResizeCompletedEvent, value);
remove => RemoveHandler(ResizeCompletedEvent, value);
}

#endregion

#region Dependency Properties

public static readonly DependencyProperty HeaderBrushProperty = Node.HeaderBrushProperty.AddOwner(typeof(GroupingNode));
public static readonly DependencyProperty CanResizeProperty = DependencyProperty.Register(nameof(CanResize), typeof(bool), typeof(GroupingNode), new FrameworkPropertyMetadata(BoxValue.True));
public static readonly DependencyProperty ActualSizeProperty = DependencyProperty.Register(nameof(ActualSize), typeof(Size), typeof(GroupingNode), new FrameworkPropertyMetadata(BoxValue.Size, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnActualSizeChanged));
public static readonly DependencyProperty MovementModeProperty = DependencyProperty.Register(nameof(MovementMode), typeof(GroupingMovementMode), typeof(GroupingNode), new FrameworkPropertyMetadata(GroupMovementBoxed));
public static readonly DependencyProperty ResizeCompletedCommandProperty = DependencyProperty.Register(nameof(ResizeCompletedCommand), typeof(ICommand), typeof(GroupingNode));

private static void OnActualSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Expand Down Expand Up @@ -88,6 +104,16 @@ public GroupingMovementMode MovementMode
set => SetValue(MovementModeProperty, value);
}

/// <summary>
/// Invoked when the <see cref="ResizeCompleted"/> event is not handled.
/// Parameter is the <see cref="ActualSize"/> of this control.
/// </summary>
public ICommand? ResizeCompletedCommand
{
get => (ICommand?)GetValue(ResizeCompletedCommandProperty);
set => SetValue(ResizeCompletedCommandProperty, value);
}

#endregion

#region Fields
Expand Down Expand Up @@ -244,7 +270,25 @@ private void OnResize(object sender, DragDeltaEventArgs e)
}

private void OnResizeCompleted(object sender, DragCompletedEventArgs e)
=> ActualSize = new Size(ActualWidth, ActualHeight);
{
Size previousSize = ActualSize;
var newSize = new Size(ActualWidth, ActualHeight);
ActualSize = newSize;

var args = new ResizeEventArgs(previousSize, newSize)
{
RoutedEvent = ResizeCompletedEvent,
Source = this
};

RaiseEvent(args);

// Raise ResizeCompletedCommand if ResizeCompletedEvent event is not handled
if (!args.Handled && (ResizeCompletedCommand?.CanExecute(newSize) ?? false))
{
ResizeCompletedCommand.Execute(newSize);
}
}

private void OnHeaderSizeChanged(object sender, SizeChangedEventArgs e)
=> CalculateDesiredHeaderSize();
Expand Down

0 comments on commit 12a8d2f

Please sign in to comment.