Skip to content

Commit 9198933

Browse files
committed
Add AutoSize option to groups to control whether moving children resizes the group
1 parent b3caf1a commit 9198933

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,17 @@ private void OnPointerDown(Model? model, PointerEventArgs e)
3333
if (sm is not MovableModel movable || movable.Locked)
3434
continue;
3535

36+
// Special case: groups without auto size on
37+
if (sm is NodeModel node && node.Group != null && !node.Group.AutoSize)
38+
continue;
39+
3640
var position = movable.Position;
37-
if (Diagram.Options.GridSnapToCenter && movable is NodeModel node)
41+
if (Diagram.Options.GridSnapToCenter && movable is NodeModel n)
3842
{
39-
position = new Point(movable.Position.X + (node.Size?.Width ?? 0) / 2,
40-
movable.Position.Y + (node.Size?.Height ?? 0) / 2);
43+
position = new Point(movable.Position.X + (n.Size?.Width ?? 0) / 2,
44+
movable.Position.Y + (n.Size?.Height ?? 0) / 2);
4145
}
46+
4247
_initialPositions.Add(movable, position);
4348
}
4449

@@ -61,9 +66,13 @@ private void OnPointerMove(Model? model, PointerEventArgs e)
6166
var ndx = ApplyGridSize(deltaX + initialPosition.X);
6267
var ndy = ApplyGridSize(deltaY + initialPosition.Y);
6368
if (Diagram.Options.GridSnapToCenter && movable is NodeModel node)
69+
{
6470
node.SetPosition(ndx - (node.Size?.Width ?? 0) / 2, ndy - (node.Size?.Height ?? 0) / 2);
71+
}
6572
else
73+
{
6674
movable.SetPosition(ndx, ndy);
75+
}
6776
}
6877
}
6978

src/Blazor.Diagrams.Core/Models/GroupModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ public class GroupModel : NodeModel
1010
{
1111
private readonly List<NodeModel> _children;
1212

13-
public GroupModel(IEnumerable<NodeModel> children, byte padding = 30)
13+
public GroupModel(IEnumerable<NodeModel> children, byte padding = 30, bool autoSize = true)
1414
{
1515
_children = new List<NodeModel>();
1616

1717
Size = Size.Zero;
1818
Padding = padding;
19+
AutoSize = autoSize;
1920
Initialize(children);
2021
}
2122

2223
public IReadOnlyList<NodeModel> Children => _children;
2324
public byte Padding { get; }
25+
public bool AutoSize { get; }
2426

2527
public void AddChild(NodeModel child)
2628
{

tests/Blazor.Diagrams.Core.Tests/Behaviors/DragMovablesBehaviorTests.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Linq;
21
using Blazor.Diagrams.Core.Events;
32
using Blazor.Diagrams.Core.Geometry;
43
using Blazor.Diagrams.Core.Models;
@@ -101,4 +100,44 @@ public void Behavior_ShouldNotTriggerMoved_WhenMovableDidntMove()
101100
// Assert
102101
movedTrigger.Should().BeFalse();
103102
}
103+
104+
[Fact]
105+
public void Behavior_ShouldNotCallSetPosition_WhenGroupHasNoAutoSize()
106+
{
107+
// Arrange
108+
var diagram = new TestDiagram();
109+
var nodeMock = new Mock<NodeModel>(Point.Zero);
110+
var group = new GroupModel(new[] { nodeMock.Object }, autoSize: false);
111+
var node = diagram.Nodes.Add(nodeMock.Object);
112+
diagram.SelectModel(node, false);
113+
114+
// Act
115+
diagram.TriggerPointerDown(node,
116+
new PointerEventArgs(100, 100, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));
117+
diagram.TriggerPointerMove(null,
118+
new PointerEventArgs(150, 150, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));
119+
120+
// Assert
121+
nodeMock.Verify(n => n.SetPosition(50, 50), Times.Never);
122+
}
123+
124+
[Fact]
125+
public void Behavior_ShouldCallSetPosition_WhenGroupHasAutoSize()
126+
{
127+
// Arrange
128+
var diagram = new TestDiagram();
129+
var nodeMock = new Mock<NodeModel>(Point.Zero);
130+
var group = new GroupModel(new[] { nodeMock.Object }, autoSize: true);
131+
var node = diagram.Nodes.Add(nodeMock.Object);
132+
diagram.SelectModel(node, false);
133+
134+
// Act
135+
diagram.TriggerPointerDown(node,
136+
new PointerEventArgs(100, 100, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));
137+
diagram.TriggerPointerMove(null,
138+
new PointerEventArgs(150, 150, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));
139+
140+
// Assert
141+
nodeMock.Verify(n => n.SetPosition(50, 50), Times.Once);
142+
}
104143
}

0 commit comments

Comments
 (0)