d2/d2layouts/d2grid/layout.go

549 lines
18 KiB
Go
Raw Normal View History

2023-04-01 00:18:17 +00:00
package d2grid
import (
"context"
"math"
2023-04-01 00:18:17 +00:00
"sort"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/label"
"oss.terrastruct.com/util-go/go2"
)
2023-04-03 18:36:01 +00:00
const (
CONTAINER_PADDING = 60
HORIZONTAL_PAD = 40.
VERTICAL_PAD = 40.
)
2023-04-01 00:18:17 +00:00
// Layout runs the grid layout on containers with rows/columns
// Note: children are not allowed edges or descendants
//
// 1. Traverse graph from root, skip objects with no rows/columns
// 2. Construct a grid with the container children
// 3. Remove the children from the main graph
// 4. Run grid layout
// 5. Set the resulting dimensions to the main graph shape
// 6. Run core layouts (without grid children)
// 7. Put grid children back in correct location
func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) d2graph.LayoutGraph {
return func(ctx context.Context, g *d2graph.Graph) error {
2023-04-05 18:11:31 +00:00
gridDiagrams, objectOrder, err := withoutGridDiagrams(ctx, g)
2023-04-01 00:18:17 +00:00
if err != nil {
return err
}
2023-04-05 18:11:31 +00:00
if g.Root.IsGridDiagram() && len(g.Root.ChildrenArray) != 0 {
2023-04-01 00:18:17 +00:00
g.Root.TopLeft = geo.NewPoint(0, 0)
} else if err := layout(ctx, g); err != nil {
return err
}
2023-04-05 18:11:31 +00:00
cleanup(g, gridDiagrams, objectOrder)
2023-04-01 00:18:17 +00:00
return nil
}
}
2023-04-05 18:11:31 +00:00
func withoutGridDiagrams(ctx context.Context, g *d2graph.Graph) (gridDiagrams map[string]*gridDiagram, objectOrder map[string]int, err error) {
2023-04-01 00:18:17 +00:00
toRemove := make(map[*d2graph.Object]struct{})
2023-04-05 18:11:31 +00:00
gridDiagrams = make(map[string]*gridDiagram)
2023-04-01 00:18:17 +00:00
if len(g.Objects) > 0 {
queue := make([]*d2graph.Object, 1, len(g.Objects))
queue[0] = g.Root
for len(queue) > 0 {
obj := queue[0]
queue = queue[1:]
if len(obj.ChildrenArray) == 0 {
continue
}
2023-04-05 18:11:31 +00:00
if !obj.IsGridDiagram() {
2023-04-01 00:18:17 +00:00
queue = append(queue, obj.ChildrenArray...)
continue
}
2023-04-05 18:11:31 +00:00
gd, err := layoutGrid(g, obj)
2023-04-01 00:18:17 +00:00
if err != nil {
return nil, nil, err
}
obj.Children = make(map[string]*d2graph.Object)
obj.ChildrenArray = nil
var dx, dy float64
2023-04-05 18:11:31 +00:00
width := gd.width + 2*CONTAINER_PADDING
labelWidth := float64(obj.LabelDimensions.Width) + 2*label.PADDING
if labelWidth > width {
dx = (labelWidth - width) / 2
width = labelWidth
}
2023-04-05 18:11:31 +00:00
height := gd.height + 2*CONTAINER_PADDING
labelHeight := float64(obj.LabelDimensions.Height) + 2*label.PADDING
if labelHeight > CONTAINER_PADDING {
// if the label doesn't fit within the padding, we need to add more
grow := labelHeight - CONTAINER_PADDING
dy = grow / 2
height += grow
}
// we need to center children if we have to expand to fit the container label
if dx != 0 || dy != 0 {
2023-04-05 18:11:31 +00:00
gd.shift(dx, dy)
}
obj.Box = geo.NewBox(nil, width, height)
2023-04-01 00:18:17 +00:00
obj.LabelPosition = go2.Pointer(string(label.InsideTopCenter))
2023-04-05 18:11:31 +00:00
gridDiagrams[obj.AbsID()] = gd
2023-04-01 00:18:17 +00:00
2023-04-05 18:11:31 +00:00
for _, node := range gd.nodes {
2023-04-01 00:18:17 +00:00
toRemove[node] = struct{}{}
}
}
}
objectOrder = make(map[string]int)
layoutObjects := make([]*d2graph.Object, 0, len(toRemove))
for i, obj := range g.Objects {
objectOrder[obj.AbsID()] = i
if _, exists := toRemove[obj]; !exists {
layoutObjects = append(layoutObjects, obj)
}
}
g.Objects = layoutObjects
2023-04-05 18:11:31 +00:00
return gridDiagrams, objectOrder, nil
2023-04-01 00:18:17 +00:00
}
2023-04-05 18:11:31 +00:00
func layoutGrid(g *d2graph.Graph, obj *d2graph.Object) (*gridDiagram, error) {
gd := newGridDiagram(obj)
2023-04-05 03:02:22 +00:00
2023-04-05 18:11:31 +00:00
if gd.rows != 0 && gd.columns != 0 {
gd.layoutEvenly(g, obj)
2023-04-05 03:02:22 +00:00
} else {
2023-04-05 18:11:31 +00:00
gd.layoutDynamic(g, obj)
2023-04-05 03:02:22 +00:00
}
// position labels and icons
2023-04-05 18:11:31 +00:00
for _, n := range gd.nodes {
2023-04-05 03:02:22 +00:00
if n.Attributes.Icon != nil {
n.LabelPosition = go2.Pointer(string(label.InsideTopCenter))
n.IconPosition = go2.Pointer(string(label.InsideMiddleCenter))
} else {
n.LabelPosition = go2.Pointer(string(label.InsideMiddleCenter))
}
}
2023-04-05 18:11:31 +00:00
return gd, nil
2023-04-05 03:02:22 +00:00
}
2023-04-05 18:11:31 +00:00
func (gd *gridDiagram) layoutEvenly(g *d2graph.Graph, obj *d2graph.Object) {
2023-04-05 03:02:22 +00:00
// layout nodes in a grid with these 2 properties:
// all nodes in the same row should have the same height
// all nodes in the same column should have the same width
getNode := func(rowIndex, columnIndex int) *d2graph.Object {
var index int
2023-04-05 18:11:31 +00:00
if gd.rowDominant {
index = rowIndex*gd.columns + columnIndex
2023-04-05 03:02:22 +00:00
} else {
2023-04-05 18:11:31 +00:00
index = columnIndex*gd.rows + rowIndex
2023-04-05 03:02:22 +00:00
}
2023-04-05 18:11:31 +00:00
if index < len(gd.nodes) {
return gd.nodes[index]
2023-04-05 03:02:22 +00:00
}
return nil
}
2023-04-05 18:11:31 +00:00
rowHeights := make([]float64, 0, gd.rows)
colWidths := make([]float64, 0, gd.columns)
for i := 0; i < gd.rows; i++ {
2023-04-05 03:02:22 +00:00
rowHeight := 0.
2023-04-05 18:11:31 +00:00
for j := 0; j < gd.columns; j++ {
2023-04-05 03:02:22 +00:00
n := getNode(i, j)
if n == nil {
break
}
rowHeight = math.Max(rowHeight, n.Height)
}
rowHeights = append(rowHeights, rowHeight)
}
2023-04-05 18:11:31 +00:00
for j := 0; j < gd.columns; j++ {
2023-04-05 03:02:22 +00:00
columnWidth := 0.
2023-04-05 18:11:31 +00:00
for i := 0; i < gd.rows; i++ {
2023-04-05 03:02:22 +00:00
n := getNode(i, j)
if n == nil {
break
}
columnWidth = math.Max(columnWidth, n.Width)
}
colWidths = append(colWidths, columnWidth)
}
cursor := geo.NewPoint(0, 0)
2023-04-05 18:11:31 +00:00
if gd.rowDominant {
for i := 0; i < gd.rows; i++ {
for j := 0; j < gd.columns; j++ {
2023-04-05 03:02:22 +00:00
n := getNode(i, j)
if n == nil {
break
}
n.Width = colWidths[j]
n.Height = rowHeights[i]
n.TopLeft = cursor.Copy()
cursor.X += n.Width + HORIZONTAL_PAD
}
cursor.X = 0
cursor.Y += rowHeights[i] + VERTICAL_PAD
}
} else {
2023-04-05 18:11:31 +00:00
for j := 0; j < gd.columns; j++ {
for i := 0; i < gd.rows; i++ {
2023-04-05 03:02:22 +00:00
n := getNode(i, j)
if n == nil {
break
}
n.Width = colWidths[j]
n.Height = rowHeights[i]
n.TopLeft = cursor.Copy()
cursor.Y += n.Height + VERTICAL_PAD
}
cursor.X += colWidths[j] + HORIZONTAL_PAD
cursor.Y = 0
}
}
var totalWidth, totalHeight float64
for _, w := range colWidths {
totalWidth += w + HORIZONTAL_PAD
}
for _, h := range rowHeights {
totalHeight += h + VERTICAL_PAD
}
totalWidth -= HORIZONTAL_PAD
totalHeight -= VERTICAL_PAD
2023-04-05 18:11:31 +00:00
gd.width = totalWidth
gd.height = totalHeight
2023-04-05 03:02:22 +00:00
}
2023-04-05 18:11:31 +00:00
func (gd *gridDiagram) layoutDynamic(g *d2graph.Graph, obj *d2graph.Object) {
// assume we have the following nodes to layout:
// . ┌A──────────────┐ ┌B──┐ ┌C─────────┐ ┌D────────┐ ┌E────────────────┐
// . └───────────────┘ │ │ │ │ │ │ │ │
// . │ │ └──────────┘ │ │ │ │
// . │ │ │ │ └─────────────────┘
// . └───┘ │ │
// . └─────────┘
// Note: if the grid is row dominant, all nodes should be the same height (same width if column dominant)
// . ┌A─────────────┐ ┌B──┐ ┌C─────────┐ ┌D────────┐ ┌E────────────────┐
// . ├ ─ ─ ─ ─ ─ ─ ─┤ │ │ │ │ │ │ │ │
// . │ │ │ │ ├ ─ ─ ─ ─ ─┤ │ │ │ │
// . │ │ │ │ │ │ │ │ ├ ─ ─ ─ ─ ─ ─ ─ ─ ┤
// . │ │ ├ ─ ┤ │ │ │ │ │ │
// . └──────────────┘ └───┘ └──────────┘ └─────────┘ └─────────────────┘
// we want to split up the total width across the N rows or columns as evenly as possible
var totalWidth, totalHeight float64
2023-04-05 18:11:31 +00:00
for _, n := range gd.nodes {
totalWidth += n.Width
totalHeight += n.Height
}
2023-04-05 18:11:31 +00:00
totalWidth += HORIZONTAL_PAD * float64(len(gd.nodes)-1)
totalHeight += VERTICAL_PAD * float64(len(gd.nodes)-1)
layout := [][]int{{}}
2023-04-05 18:11:31 +00:00
if gd.rowDominant {
targetWidth := totalWidth / float64(gd.rows)
rowWidth := 0.
rowIndex := 0
2023-04-04 20:42:25 +00:00
addRow := func() {
layout = append(layout, []int{})
rowIndex++
rowWidth = 0
}
addNode := func(i int, n *d2graph.Object) {
layout[rowIndex] = append(layout[rowIndex], i)
rowWidth += n.Width + HORIZONTAL_PAD
2023-04-04 20:42:25 +00:00
}
2023-04-05 18:11:31 +00:00
for i, n := range gd.nodes {
2023-04-04 20:42:25 +00:00
// if the next node will be past the target, start a new row
if rowWidth+n.Width+HORIZONTAL_PAD > targetWidth {
// if the node is mostly past the target, put it on the next row
if rowWidth+n.Width/2 > targetWidth {
addRow()
addNode(i, n)
} else {
addNode(i, n)
2023-04-05 18:11:31 +00:00
if i < len(gd.nodes)-1 {
2023-04-04 20:42:25 +00:00
addRow()
}
}
} else {
addNode(i, n)
}
}
} else {
2023-04-05 18:11:31 +00:00
targetHeight := totalHeight / float64(gd.columns)
2023-04-04 18:38:33 +00:00
colHeight := 0.
colIndex := 0
2023-04-04 20:42:25 +00:00
addCol := func() {
layout = append(layout, []int{})
colIndex++
colHeight = 0
}
addNode := func(i int, n *d2graph.Object) {
2023-04-04 18:38:33 +00:00
layout[colIndex] = append(layout[colIndex], i)
colHeight += n.Height + VERTICAL_PAD
2023-04-04 20:42:25 +00:00
}
2023-04-05 18:11:31 +00:00
for i, n := range gd.nodes {
2023-04-04 20:42:25 +00:00
// if the next node will be past the target, start a new row
if colHeight+n.Height+VERTICAL_PAD > targetHeight {
// if the node is mostly past the target, put it on the next row
if colHeight+n.Height/2 > targetHeight {
addCol()
addNode(i, n)
} else {
addNode(i, n)
2023-04-05 18:11:31 +00:00
if i < len(gd.nodes)-1 {
2023-04-04 20:42:25 +00:00
addCol()
}
}
} else {
addNode(i, n)
}
}
}
2023-04-03 18:36:01 +00:00
cursor := geo.NewPoint(0, 0)
var maxY, maxX float64
2023-04-05 18:11:31 +00:00
if gd.rowDominant {
// if we have 2 rows, then each row's nodes should have the same height
// . ┌A─────────────┐ ┌B──┐ ┌C─────────┐ ┬ maxHeight(A,B,C)
// . ├ ─ ─ ─ ─ ─ ─ ─┤ │ │ │ │ │
// . │ │ │ │ ├ ─ ─ ─ ─ ─┤ │
// . │ │ │ │ │ │ │
// . └──────────────┘ └───┘ └──────────┘ ┴
// . ┌D────────┐ ┌E────────────────┐ ┬ maxHeight(D,E)
// . │ │ │ │ │
// . │ │ │ │ │
// . │ │ ├ ─ ─ ─ ─ ─ ─ ─ ─ ┤ │
// . │ │ │ │ │
// . └─────────┘ └─────────────────┘ ┴
rowWidths := []float64{}
for _, row := range layout {
rowHeight := 0.
for _, nodeIndex := range row {
2023-04-05 18:11:31 +00:00
n := gd.nodes[nodeIndex]
n.TopLeft = cursor.Copy()
cursor.X += n.Width + HORIZONTAL_PAD
rowHeight = math.Max(rowHeight, n.Height)
}
rowWidth := cursor.X - HORIZONTAL_PAD
rowWidths = append(rowWidths, rowWidth)
maxX = math.Max(maxX, rowWidth)
// set all nodes in row to the same height
for _, nodeIndex := range row {
2023-04-05 18:11:31 +00:00
n := gd.nodes[nodeIndex]
n.Height = rowHeight
}
// new row
cursor.X = 0
cursor.Y += rowHeight + VERTICAL_PAD
2023-04-03 18:36:01 +00:00
}
maxY = cursor.Y - VERTICAL_PAD
// then expand thinnest nodes to make each row the same width
// . ┌A─────────────┐ ┌B──┐ ┌C─────────┐ ┬ maxHeight(A,B,C)
// . │ │ │ │ │ │ │
// . │ │ │ │ │ │ │
// . │ │ │ │ │ │ │
// . └──────────────┘ └───┘ └──────────┘ ┴
// . ┌D────────┬────┐ ┌E────────────────┐ ┬ maxHeight(D,E)
// . │ │ │ │ │
// . │ │ │ │ │ │
// . │ │ │ │ │
// . │ │ │ │ │ │
// . └─────────┴────┘ └─────────────────┘ ┴
for i, row := range layout {
rowWidth := rowWidths[i]
if rowWidth == maxX {
continue
}
delta := maxX - rowWidth
nodes := []*d2graph.Object{}
var widest float64
for _, nodeIndex := range row {
2023-04-05 18:11:31 +00:00
n := gd.nodes[nodeIndex]
widest = math.Max(widest, n.Width)
nodes = append(nodes, n)
}
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].Width < nodes[j].Width
})
// expand smaller nodes to fill remaining space
for _, n := range nodes {
if n.Width < widest {
var index int
for i, nodeIndex := range row {
2023-04-05 18:11:31 +00:00
if n == gd.nodes[nodeIndex] {
index = i
break
}
}
grow := math.Min(widest-n.Width, delta)
n.Width += grow
// shift following nodes
for i := index + 1; i < len(row); i++ {
2023-04-05 18:11:31 +00:00
gd.nodes[row[i]].TopLeft.X += grow
}
delta -= grow
if delta <= 0 {
break
}
}
}
if delta > 0 {
grow := delta / float64(len(row))
for i := len(row) - 1; i >= 0; i-- {
2023-04-05 18:11:31 +00:00
n := gd.nodes[row[i]]
n.TopLeft.X += grow * float64(i)
n.Width += grow
delta -= grow
}
}
}
} else {
// if we have 3 columns, then each column's nodes should have the same width
// . ├maxWidth(A,B)─┤ ├maxW(C,D)─┤ ├maxWidth(E)──────┤
// . ┌A─────────────┐ ┌C─────────┐ ┌E────────────────┐
// . └──────────────┘ │ │ │ │
// . ┌B──┬──────────┐ └──────────┘ │ │
// . │ │ ┌D────────┬┐ └─────────────────┘
// . │ │ │ │ │
// . │ │ │ ││
// . └───┴──────────┘ │ │
// . │ ││
// . └─────────┴┘
2023-04-04 18:38:33 +00:00
colHeights := []float64{}
for _, column := range layout {
2023-04-04 18:38:33 +00:00
colWidth := 0.
for _, nodeIndex := range column {
2023-04-05 18:11:31 +00:00
n := gd.nodes[nodeIndex]
n.TopLeft = cursor.Copy()
cursor.Y += n.Height + VERTICAL_PAD
2023-04-04 18:38:33 +00:00
colWidth = math.Max(colWidth, n.Width)
}
2023-04-04 18:38:33 +00:00
colHeight := cursor.Y - VERTICAL_PAD
colHeights = append(colHeights, colHeight)
maxY = math.Max(maxY, colHeight)
// set all nodes in column to the same width
for _, nodeIndex := range column {
2023-04-05 18:11:31 +00:00
n := gd.nodes[nodeIndex]
2023-04-04 18:38:33 +00:00
n.Width = colWidth
}
// new column
cursor.Y = 0
2023-04-04 18:38:33 +00:00
cursor.X += colWidth + HORIZONTAL_PAD
}
maxX = cursor.X - HORIZONTAL_PAD
// then expand shortest nodes to make each column the same height
// . ├maxWidth(A,B)─┤ ├maxW(C,D)─┤ ├maxWidth(E)──────┤
// . ┌A─────────────┐ ┌C─────────┐ ┌E────────────────┐
// . ├ ─ ─ ─ ─ ─ ─ ┤ │ │ │ │
// . │ │ └──────────┘ │ │
// . └──────────────┘ ┌D─────────┐ ├ ─ ─ ─ ─ ─ ─ ─ ─ ┤
// . ┌B─────────────┐ │ │ │ │
// . │ │ │ │ │ │
// . │ │ │ │ │ │
// . │ │ │ │ │ │
// . └──────────────┘ └──────────┘ └─────────────────┘
2023-04-04 18:38:33 +00:00
for i, column := range layout {
colHeight := colHeights[i]
if colHeight == maxY {
continue
}
delta := maxY - colHeight
nodes := []*d2graph.Object{}
var tallest float64
for _, nodeIndex := range column {
2023-04-05 18:11:31 +00:00
n := gd.nodes[nodeIndex]
2023-04-04 18:38:33 +00:00
tallest = math.Max(tallest, n.Height)
nodes = append(nodes, n)
}
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].Height < nodes[j].Height
})
// expand smaller nodes to fill remaining space
for _, n := range nodes {
if n.Height < tallest {
var index int
for i, nodeIndex := range column {
2023-04-05 18:11:31 +00:00
if n == gd.nodes[nodeIndex] {
2023-04-04 18:38:33 +00:00
index = i
break
}
}
grow := math.Min(tallest-n.Height, delta)
n.Height += grow
// shift following nodes
for i := index + 1; i < len(column); i++ {
2023-04-05 18:11:31 +00:00
gd.nodes[column[i]].TopLeft.Y += grow
2023-04-04 18:38:33 +00:00
}
delta -= grow
if delta <= 0 {
break
}
}
}
if delta > 0 {
grow := delta / float64(len(column))
for i := len(column) - 1; i >= 0; i-- {
2023-04-05 18:11:31 +00:00
n := gd.nodes[column[i]]
2023-04-04 18:38:33 +00:00
n.TopLeft.Y += grow * float64(i)
n.Height += grow
delta -= grow
}
}
}
2023-04-03 18:36:01 +00:00
}
2023-04-05 18:11:31 +00:00
gd.width = maxX
gd.height = maxY
2023-04-03 18:36:01 +00:00
}
2023-04-01 00:18:17 +00:00
// cleanup restores the graph after the core layout engine finishes
// - translating the grid to its position placed by the core layout engine
// - restore the children of the grid
// - sorts objects to their original graph order
2023-04-05 18:11:31 +00:00
func cleanup(graph *d2graph.Graph, gridDiagrams map[string]*gridDiagram, objectsOrder map[string]int) {
defer func() {
sort.SliceStable(graph.Objects, func(i, j int) bool {
return objectsOrder[graph.Objects[i].AbsID()] < objectsOrder[graph.Objects[j].AbsID()]
})
}()
2023-04-05 18:11:31 +00:00
if graph.Root.IsGridDiagram() {
gd, exists := gridDiagrams[graph.Root.AbsID()]
if exists {
2023-04-05 18:11:31 +00:00
gd.cleanup(graph.Root, graph)
return
}
2023-04-01 00:18:17 +00:00
}
for _, obj := range graph.Objects {
2023-04-05 18:11:31 +00:00
gd, exists := gridDiagrams[obj.AbsID()]
2023-04-03 18:36:01 +00:00
if !exists {
2023-04-01 00:18:17 +00:00
continue
}
obj.LabelPosition = go2.Pointer(string(label.InsideTopCenter))
// shift the grid from (0, 0)
2023-04-05 18:11:31 +00:00
gd.shift(
2023-04-01 00:18:17 +00:00
obj.TopLeft.X+CONTAINER_PADDING,
obj.TopLeft.Y+CONTAINER_PADDING,
)
2023-04-05 18:11:31 +00:00
gd.cleanup(obj, graph)
2023-04-01 00:18:17 +00:00
}
}