Skip to content

Commit

Permalink
Add AutoSize option to groups to control whether moving children resi…
Browse files Browse the repository at this point in the history
…zes the group
  • Loading branch information
zHaytam committed Nov 23, 2022
1 parent b3caf1a commit 9198933
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ private void OnPointerDown(Model? model, PointerEventArgs e)
if (sm is not MovableModel movable || movable.Locked)
continue;

// Special case: groups without auto size on
if (sm is NodeModel node && node.Group != null && !node.Group.AutoSize)
continue;

var position = movable.Position;
if (Diagram.Options.GridSnapToCenter && movable is NodeModel node)
if (Diagram.Options.GridSnapToCenter && movable is NodeModel n)
{
position = new Point(movable.Position.X + (node.Size?.Width ?? 0) / 2,
movable.Position.Y + (node.Size?.Height ?? 0) / 2);
position = new Point(movable.Position.X + (n.Size?.Width ?? 0) / 2,
movable.Position.Y + (n.Size?.Height ?? 0) / 2);
}

_initialPositions.Add(movable, position);
}

Expand All @@ -61,9 +66,13 @@ private void OnPointerMove(Model? model, PointerEventArgs e)
var ndx = ApplyGridSize(deltaX + initialPosition.X);
var ndy = ApplyGridSize(deltaY + initialPosition.Y);
if (Diagram.Options.GridSnapToCenter && movable is NodeModel node)
{
node.SetPosition(ndx - (node.Size?.Width ?? 0) / 2, ndy - (node.Size?.Height ?? 0) / 2);
}
else
{
movable.SetPosition(ndx, ndy);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Blazor.Diagrams.Core/Models/GroupModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ public class GroupModel : NodeModel
{
private readonly List<NodeModel> _children;

public GroupModel(IEnumerable<NodeModel> children, byte padding = 30)
public GroupModel(IEnumerable<NodeModel> children, byte padding = 30, bool autoSize = true)
{
_children = new List<NodeModel>();

Size = Size.Zero;
Padding = padding;
AutoSize = autoSize;
Initialize(children);
}

public IReadOnlyList<NodeModel> Children => _children;
public byte Padding { get; }
public bool AutoSize { get; }

public void AddChild(NodeModel child)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using Blazor.Diagrams.Core.Events;
using Blazor.Diagrams.Core.Geometry;
using Blazor.Diagrams.Core.Models;
Expand Down Expand Up @@ -101,4 +100,44 @@ public void Behavior_ShouldNotTriggerMoved_WhenMovableDidntMove()
// Assert
movedTrigger.Should().BeFalse();
}

[Fact]
public void Behavior_ShouldNotCallSetPosition_WhenGroupHasNoAutoSize()
{
// Arrange
var diagram = new TestDiagram();
var nodeMock = new Mock<NodeModel>(Point.Zero);
var group = new GroupModel(new[] { nodeMock.Object }, autoSize: false);
var node = diagram.Nodes.Add(nodeMock.Object);
diagram.SelectModel(node, false);

// Act
diagram.TriggerPointerDown(node,
new PointerEventArgs(100, 100, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));
diagram.TriggerPointerMove(null,
new PointerEventArgs(150, 150, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));

// Assert
nodeMock.Verify(n => n.SetPosition(50, 50), Times.Never);
}

[Fact]
public void Behavior_ShouldCallSetPosition_WhenGroupHasAutoSize()
{
// Arrange
var diagram = new TestDiagram();
var nodeMock = new Mock<NodeModel>(Point.Zero);
var group = new GroupModel(new[] { nodeMock.Object }, autoSize: true);
var node = diagram.Nodes.Add(nodeMock.Object);
diagram.SelectModel(node, false);

// Act
diagram.TriggerPointerDown(node,
new PointerEventArgs(100, 100, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));
diagram.TriggerPointerMove(null,
new PointerEventArgs(150, 150, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));

// Assert
nodeMock.Verify(n => n.SetPosition(50, 50), Times.Once);
}
}

0 comments on commit 9198933

Please sign in to comment.