From 8c84ed037849ff43da992e86114dcc2d50473059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20C=C3=A9sar=20Batista?= Date: Tue, 29 Nov 2022 15:14:36 -0800 Subject: [PATCH] Refactor --- d2layouts/d2sequence/constants.go | 2 +- d2layouts/d2sequence/layout.go | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/d2layouts/d2sequence/constants.go b/d2layouts/d2sequence/constants.go index 0dfb3f2a7..b042db3d9 100644 --- a/d2layouts/d2sequence/constants.go +++ b/d2layouts/d2sequence/constants.go @@ -21,4 +21,4 @@ const ACTIVATION_BOX_EDGE_PAD = 5. const ACTIVATION_BOX_DEPTH_GROW_FACTOR = 10. // when a activation box has a single edge -const DEFAULT_ACTIVATION_BOX_HEIGHT = MIN_EDGE_DISTANCE / 2. +const MIN_ACTIVATION_BOX_HEIGHT = MIN_EDGE_DISTANCE / 2. diff --git a/d2layouts/d2sequence/layout.go b/d2layouts/d2sequence/layout.go index 99ba69d63..1411f1cf9 100644 --- a/d2layouts/d2sequence/layout.go +++ b/d2layouts/d2sequence/layout.go @@ -42,11 +42,13 @@ type sequenceDiagram struct { activations []*d2graph.Object // can be either actors or activation boxes - objectRank map[*d2graph.Object]int + // rank: left to right position of actors/activations + objectRank map[*d2graph.Object]int + // depth: the nested levels of a given actor/activation objectDepth map[*d2graph.Object]int // keep track of the first and last edge of a given actor - // needed for activation boxes + // the edge rank is the order in which it appears from top to bottom minEdgeRank map[*d2graph.Object]int maxEdgeRank map[*d2graph.Object]int @@ -69,7 +71,7 @@ func (sd *sequenceDiagram) init() { sd.actors = append(sd.actors, obj) sd.objectRank[obj] = len(sd.actors) sd.objectDepth[obj] = 0 - sd.maxActorHeight = math.Max(sd.maxActorHeight, obj.Height+VERTICAL_PAD) + sd.maxActorHeight = math.Max(sd.maxActorHeight, obj.Height) } else { // activations boxes are always rectangles and have no labels obj.Attributes.Label = d2graph.Scalar{Value: ""} @@ -83,7 +85,7 @@ func (sd *sequenceDiagram) init() { } for rank, edge := range sd.edges { - sd.edgeYStep = math.Max(sd.edgeYStep, float64(edge.LabelDimensions.Height)+VERTICAL_PAD) + sd.edgeYStep = math.Max(sd.edgeYStep, float64(edge.LabelDimensions.Height)) sd.setMinMaxEdgeRank(edge.Src, rank) sd.setMinMaxEdgeRank(edge.Dst, rank) @@ -94,6 +96,9 @@ func (sd *sequenceDiagram) init() { distributedLabelWidth := float64(edge.LabelDimensions.Width) / rankDiff sd.actorXStep = math.Max(sd.actorXStep, distributedLabelWidth+HORIZONTAL_PAD) } + + sd.maxActorHeight += VERTICAL_PAD + sd.edgeYStep += VERTICAL_PAD } func (sd *sequenceDiagram) setMinMaxEdgeRank(actor *d2graph.Object, rank int) { @@ -213,7 +218,7 @@ func (sd *sequenceDiagram) placeActivationBoxes() { maxY += ACTIVATION_BOX_EDGE_PAD } - height := math.Max(maxY-minY, DEFAULT_ACTIVATION_BOX_HEIGHT) + height := math.Max(maxY-minY, MIN_ACTIVATION_BOX_HEIGHT) width := ACTIVATION_BOX_WIDTH + (float64(sd.objectDepth[activation]-1) * ACTIVATION_BOX_DEPTH_GROW_FACTOR) x := rankToX[sd.objectRank[activation]] - (width / 2.) activation.Box = geo.NewBox(geo.NewPoint(x, minY), width, height)