Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit c4b38b1

Browse files
The automatic layout algorithm can now be configured on individual views.
1 parent 2e62168 commit c4b38b1

File tree

6 files changed

+255
-1
lines changed

6 files changed

+255
-1
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace Structurizr.Core.Tests.View
5+
{
6+
public class AutomaticLayoutTests
7+
{
8+
[Fact]
9+
public void Test_AutomaticLayout()
10+
{
11+
AutomaticLayout automaticLayout = new AutomaticLayout(RankDirection.LeftRight, 100, 200, 300, true);
12+
13+
Assert.Equal(RankDirection.LeftRight, automaticLayout.RankDirection);
14+
Assert.Equal(100, automaticLayout.RankSeparation);
15+
Assert.Equal(200, automaticLayout.NodeSeparation);
16+
Assert.Equal(300, automaticLayout.EdgeSeparation);
17+
Assert.True(automaticLayout.Vertices);
18+
}
19+
20+
[Fact]
21+
public void Test_RankSeparation_ThrowsAnException_WhenANegativeIntegerIsSpecified()
22+
{
23+
try
24+
{
25+
AutomaticLayout automaticLayout = new AutomaticLayout();
26+
automaticLayout.RankSeparation = -100;
27+
throw new TestFailedException();
28+
}
29+
catch (ArgumentException iae)
30+
{
31+
Assert.Equal("The rank separation must be a positive integer.", iae.Message);
32+
}
33+
}
34+
35+
[Fact]
36+
public void Test_NodeSeparation_ThrowsAnException_WhenANegativeIntegerIsSpecified()
37+
{
38+
try
39+
{
40+
AutomaticLayout automaticLayout = new AutomaticLayout();
41+
automaticLayout.NodeSeparation = -100;
42+
throw new TestFailedException();
43+
}
44+
catch (ArgumentException iae)
45+
{
46+
Assert.Equal("The node separation must be a positive integer.", iae.Message);
47+
}
48+
}
49+
50+
[Fact]
51+
public void Test_EdgeSeparation_ThrowsAnException_WhenANegativeIntegerIsSpecified()
52+
{
53+
try
54+
{
55+
AutomaticLayout automaticLayout = new AutomaticLayout();
56+
automaticLayout.EdgeSeparation = -100;
57+
throw new TestFailedException();
58+
}
59+
catch (ArgumentException iae)
60+
{
61+
Assert.Equal("The edge separation must be a positive integer.", iae.Message);
62+
}
63+
}
64+
65+
}
66+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Xunit;
2+
3+
namespace Structurizr.Core.Tests.View
4+
{
5+
public class ViewTests : AbstractTestBase
6+
{
7+
[Fact]
8+
public void Test_EnableAutomaticLayout_EnablesAutoLayoutWithSomeDefaultValues()
9+
{
10+
SystemLandscapeView view = new Workspace("", "").Views.CreateSystemLandscapeView("key", "Description");
11+
view.EnableAutomaticLayout();
12+
13+
Assert.NotNull(view.AutomaticLayout);
14+
Assert.Equal(RankDirection.TopBottom, view.AutomaticLayout.RankDirection);
15+
Assert.Equal(300, view.AutomaticLayout.RankSeparation);
16+
Assert.Equal(600, view.AutomaticLayout.NodeSeparation);
17+
Assert.Equal(200, view.AutomaticLayout.EdgeSeparation);
18+
Assert.False(view.AutomaticLayout.Vertices);
19+
}
20+
21+
[Fact]
22+
public void Test_DisableAutomaticLayout_DisablesAutoLayout()
23+
{
24+
SystemLandscapeView view = new Workspace("", "").Views.CreateSystemLandscapeView("key", "Description");
25+
view.EnableAutomaticLayout();
26+
Assert.NotNull(view.AutomaticLayout);
27+
28+
view.DisableAutomaticLayout();
29+
Assert.Null(view.AutomaticLayout);
30+
}
31+
32+
[Fact]
33+
public void Test_EnableAutomaticLayout()
34+
{
35+
SystemLandscapeView view = new Workspace("", "").Views.CreateSystemLandscapeView("key", "Description");
36+
view.EnableAutomaticLayout(RankDirection.LeftRight, 100, 200, 300, true);
37+
38+
Assert.NotNull(view.AutomaticLayout);
39+
Assert.Equal(RankDirection.LeftRight, view.AutomaticLayout.RankDirection);
40+
Assert.Equal(100, view.AutomaticLayout.RankSeparation);
41+
Assert.Equal(200, view.AutomaticLayout.NodeSeparation);
42+
Assert.Equal(300, view.AutomaticLayout.EdgeSeparation);
43+
Assert.True(view.AutomaticLayout.Vertices);
44+
}
45+
}
46+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace Structurizr
5+
{
6+
[DataContract]
7+
public class AutomaticLayout
8+
{
9+
10+
/// <summary>
11+
/// The rank direction.
12+
/// </summary>
13+
[DataMember(Name = "rankDirection", EmitDefaultValue = false)]
14+
public RankDirection RankDirection;
15+
16+
private int _rankSeparation;
17+
18+
/// <summary>
19+
/// The rank separation (in pixels).
20+
/// </summary>
21+
[DataMember(Name = "rankSeparation", EmitDefaultValue = false)]
22+
public int RankSeparation
23+
{
24+
get { return _rankSeparation; }
25+
26+
set
27+
{
28+
if (value < 0)
29+
{
30+
throw new ArgumentException("The rank separation must be a positive integer.");
31+
}
32+
33+
_rankSeparation = value;
34+
}
35+
}
36+
37+
private int _nodeSeparation;
38+
39+
[DataMember(Name = "nodeSeparation", EmitDefaultValue = false)]
40+
public int NodeSeparation
41+
{
42+
get { return _nodeSeparation; }
43+
44+
set
45+
{
46+
if (value < 0)
47+
{
48+
throw new ArgumentException("The node separation must be a positive integer.");
49+
}
50+
51+
_nodeSeparation = value;
52+
}
53+
}
54+
55+
private int _edgeSeparation;
56+
57+
[DataMember(Name = "edgeSeparation", EmitDefaultValue = false)]
58+
public int EdgeSeparation
59+
{
60+
get { return _edgeSeparation; }
61+
62+
set
63+
{
64+
if (value < 0)
65+
{
66+
throw new ArgumentException("The edge separation must be a positive integer.");
67+
}
68+
69+
_edgeSeparation = value;
70+
}
71+
}
72+
73+
/// <summary>
74+
/// Whether the automatic layout algorithm should create vertices.
75+
/// </summary>
76+
[DataMember(Name = "vertices", EmitDefaultValue = true)]
77+
public bool Vertices;
78+
79+
internal AutomaticLayout()
80+
{
81+
}
82+
83+
internal AutomaticLayout(RankDirection rankDirection, int rankSeparation, int nodeSeparation,
84+
int edgeSeparation, bool vertices)
85+
{
86+
RankDirection = rankDirection;
87+
RankSeparation = rankSeparation;
88+
NodeSeparation = nodeSeparation;
89+
EdgeSeparation = edgeSeparation;
90+
Vertices = vertices;
91+
}
92+
93+
}
94+
95+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Structurizr
2+
{
3+
public enum RankDirection
4+
{
5+
TopBottom,
6+
BottomTop,
7+
LeftRight,
8+
RightLeft
9+
}
10+
11+
}

Structurizr.Core/View/View.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,5 +285,37 @@ internal virtual RelationshipView FindRelationshipView(RelationshipView sourceRe
285285
return null;
286286
}
287287

288+
[DataMember(Name = "automaticLayout", EmitDefaultValue = false)]
289+
public AutomaticLayout AutomaticLayout { get; internal set; }
290+
291+
/// <summary>
292+
/// Enables automatic layout for this view, with some default settings.
293+
/// </summary>
294+
public void EnableAutomaticLayout()
295+
{
296+
EnableAutomaticLayout(RankDirection.TopBottom, 300, 600, 200, false);
297+
}
298+
299+
/// <summary>
300+
/// Enables the automatic layout for this view, with the specified settings.
301+
/// </summary>
302+
/// <param name="rankDirection">the rank direction</param>
303+
/// <param name="rankSeparation">the separation between ranks (in pixels, a positive integer)</param>
304+
/// <param name="nodeSeparation">the separation between nodes within the same rank (in pixels, a positive integer)</param>
305+
/// <param name="edgeSeparation">the separation between edges (in pixels, a positive integer)</param>
306+
/// <param name="vertices">whether vertices should be created during automatic layout</param>
307+
public void EnableAutomaticLayout(RankDirection rankDirection, int rankSeparation, int nodeSeparation, int edgeSeparation, bool vertices)
308+
{
309+
AutomaticLayout = new AutomaticLayout(rankDirection, rankSeparation, nodeSeparation, edgeSeparation, vertices);
310+
}
311+
312+
/// <summary>
313+
/// Disables automatic layout for this view.
314+
/// </summary>
315+
public void DisableAutomaticLayout()
316+
{
317+
AutomaticLayout = null;
318+
}
319+
288320
}
289-
}
321+
}

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.9.4 (unreleased)
4+
5+
- The automatic layout algorithm can now be configured on individual views.
6+
37
## 0.9.3 (22nd November 2019)
48

59
- Fixes a bug that allows relationships to be created between parents and children.

0 commit comments

Comments
 (0)