also use grid-gap for grid container padding
This commit is contained in:
parent
6d95eb7a69
commit
0750d1b3b2
1 changed files with 36 additions and 13 deletions
|
|
@ -96,8 +96,16 @@ func withoutGridDiagrams(ctx context.Context, g *d2graph.Graph, layout d2graph.L
|
||||||
obj.ChildrenArray = nil
|
obj.ChildrenArray = nil
|
||||||
|
|
||||||
if obj.Box != nil {
|
if obj.Box != nil {
|
||||||
|
// CONTAINER_PADDING is default, but use gap value if set
|
||||||
|
horizontalPadding, verticalPadding := CONTAINER_PADDING, CONTAINER_PADDING
|
||||||
|
if obj.GridGap != nil || obj.HorizontalGap != nil {
|
||||||
|
horizontalPadding = gd.horizontalGap
|
||||||
|
}
|
||||||
|
if obj.GridGap != nil || obj.VerticalGap != nil {
|
||||||
|
verticalPadding = gd.verticalGap
|
||||||
|
}
|
||||||
// size shape according to grid
|
// size shape according to grid
|
||||||
obj.SizeToContent(float64(gd.width), float64(gd.height), 2*CONTAINER_PADDING, 2*CONTAINER_PADDING)
|
obj.SizeToContent(float64(gd.width), float64(gd.height), float64(2*horizontalPadding), float64(2*verticalPadding))
|
||||||
|
|
||||||
// compute where the grid should be placed inside shape
|
// compute where the grid should be placed inside shape
|
||||||
dslShape := strings.ToLower(obj.Shape.Value)
|
dslShape := strings.ToLower(obj.Shape.Value)
|
||||||
|
|
@ -109,17 +117,21 @@ func withoutGridDiagrams(ctx context.Context, g *d2graph.Graph, layout d2graph.L
|
||||||
}
|
}
|
||||||
|
|
||||||
var dx, dy float64
|
var dx, dy float64
|
||||||
labelWidth := float64(obj.LabelDimensions.Width) + 2*label.PADDING
|
if obj.LabelDimensions.Width != 0 {
|
||||||
if labelWidth > obj.Width {
|
labelWidth := float64(obj.LabelDimensions.Width) + 2*label.PADDING
|
||||||
dx = (labelWidth - obj.Width) / 2
|
if labelWidth > obj.Width {
|
||||||
obj.Width = labelWidth
|
dx = (labelWidth - obj.Width) / 2
|
||||||
|
obj.Width = labelWidth
|
||||||
|
}
|
||||||
}
|
}
|
||||||
labelHeight := float64(obj.LabelDimensions.Height) + 2*label.PADDING
|
if obj.LabelDimensions.Height != 0 {
|
||||||
if labelHeight > CONTAINER_PADDING {
|
labelHeight := float64(obj.LabelDimensions.Height) + 2*label.PADDING
|
||||||
// if the label doesn't fit within the padding, we need to add more
|
if labelHeight > float64(verticalPadding) {
|
||||||
grow := labelHeight - CONTAINER_PADDING
|
// if the label doesn't fit within the padding, we need to add more
|
||||||
dy = grow / 2
|
grow := labelHeight - float64(verticalPadding)
|
||||||
obj.Height += grow
|
dy = grow
|
||||||
|
obj.Height += grow
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// we need to center children if we have to expand to fit the container label
|
// we need to center children if we have to expand to fit the container label
|
||||||
if dx != 0 || dy != 0 {
|
if dx != 0 || dy != 0 {
|
||||||
|
|
@ -333,6 +345,8 @@ func (gd *gridDiagram) layoutDynamic(g *d2graph.Graph, obj *d2graph.Object) {
|
||||||
maxX = math.Max(maxX, rowWidth)
|
maxX = math.Max(maxX, rowWidth)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO if object is a nested grid, consider growing descendants according to the inner grid layout
|
||||||
|
|
||||||
// then expand thinnest objects to make each row the same width
|
// then expand thinnest objects to make each row the same width
|
||||||
// . ┌A─────────────┐ ┌B──┐ ┌C─────────┐ ┬ maxHeight(A,B,C)
|
// . ┌A─────────────┐ ┌B──┐ ┌C─────────┐ ┬ maxHeight(A,B,C)
|
||||||
// . │ │ │ │ │ │ │
|
// . │ │ │ │ │ │ │
|
||||||
|
|
@ -863,10 +877,19 @@ func cleanup(graph *d2graph.Graph, gridDiagrams map[string]*gridDiagram, objects
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
obj.LabelPosition = go2.Pointer(string(label.InsideTopCenter))
|
obj.LabelPosition = go2.Pointer(string(label.InsideTopCenter))
|
||||||
|
|
||||||
|
horizontalPadding, verticalPadding := CONTAINER_PADDING, CONTAINER_PADDING
|
||||||
|
if obj.GridGap != nil || obj.HorizontalGap != nil {
|
||||||
|
horizontalPadding = gd.horizontalGap
|
||||||
|
}
|
||||||
|
if obj.GridGap != nil || obj.VerticalGap != nil {
|
||||||
|
verticalPadding = gd.verticalGap
|
||||||
|
}
|
||||||
|
|
||||||
// shift the grid from (0, 0)
|
// shift the grid from (0, 0)
|
||||||
gd.shift(
|
gd.shift(
|
||||||
obj.TopLeft.X+CONTAINER_PADDING,
|
obj.TopLeft.X+float64(horizontalPadding),
|
||||||
obj.TopLeft.Y+CONTAINER_PADDING,
|
obj.TopLeft.Y+float64(verticalPadding),
|
||||||
)
|
)
|
||||||
gd.cleanup(obj, graph)
|
gd.cleanup(obj, graph)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue