positioning

This commit is contained in:
Gavin Nishizawa 2023-11-08 16:17:58 -08:00
parent a363584245
commit 0571137e1d
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD

View file

@ -30,6 +30,13 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
return err return err
} }
if obj.HasLabel() && obj.LabelPosition == nil {
obj.LabelPosition = go2.Pointer(label.InsideTopCenter.String())
}
if obj.Icon != nil && obj.IconPosition == nil {
obj.IconPosition = go2.Pointer(label.InsideTopLeft.String())
}
if obj.Box != nil { if obj.Box != nil {
// CONTAINER_PADDING is default, but use gap value if set // CONTAINER_PADDING is default, but use gap value if set
horizontalPadding, verticalPadding := CONTAINER_PADDING, CONTAINER_PADDING horizontalPadding, verticalPadding := CONTAINER_PADDING, CONTAINER_PADDING
@ -50,9 +57,17 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
gd.shift(innerBox.TopLeft.X, innerBox.TopLeft.Y) gd.shift(innerBox.TopLeft.X, innerBox.TopLeft.Y)
} }
var labelPosition, iconPosition label.Position
if obj.LabelPosition != nil {
labelPosition = label.FromString(*obj.LabelPosition)
}
if obj.IconPosition != nil {
iconPosition = label.FromString(*obj.IconPosition)
}
// compute how much space the label and icon occupy // compute how much space the label and icon occupy
var occupiedWidth, occupiedHeight float64 var occupiedWidth, occupiedHeight float64
if obj.Icon != nil { if obj.Icon != nil && !iconPosition.IsOutside() {
iconSpace := float64(d2target.MAX_ICON_SIZE + 2*label.PADDING) iconSpace := float64(d2target.MAX_ICON_SIZE + 2*label.PADDING)
occupiedWidth = iconSpace occupiedWidth = iconSpace
occupiedHeight = iconSpace occupiedHeight = iconSpace
@ -64,6 +79,15 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
occupiedHeight, occupiedHeight,
float64(obj.LabelDimensions.Height)+2*label.PADDING, float64(obj.LabelDimensions.Height)+2*label.PADDING,
) )
if occupiedHeight > obj.Height {
switch labelPosition {
case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight,
label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight:
default:
dy = (occupiedHeight - obj.Height) / 2
obj.Height = occupiedHeight
}
}
} }
if obj.LabelDimensions.Width != 0 { if obj.LabelDimensions.Width != 0 {
// . ├────┤───────├────┤ // . ├────┤───────├────┤
@ -72,18 +96,25 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
occupiedWidth *= 2 occupiedWidth *= 2
occupiedWidth += float64(obj.LabelDimensions.Width) + 2*label.PADDING occupiedWidth += float64(obj.LabelDimensions.Width) + 2*label.PADDING
if occupiedWidth > obj.Width { if occupiedWidth > obj.Width {
switch labelPosition {
case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom,
label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom:
default:
dx = (occupiedWidth - obj.Width) / 2 dx = (occupiedWidth - obj.Width) / 2
obj.Width = occupiedWidth obj.Width = occupiedWidth
} }
} }
}
if obj.LabelPosition != nil {
labelPosition := label.FromString(*obj.LabelPosition)
if !labelPosition.IsOutside() {
labelOverflowY := occupiedHeight - float64(verticalPadding) labelOverflowY := occupiedHeight - float64(verticalPadding)
// if the label doesn't fit within the padding, we need to add more // if the label doesn't fit within the padding, we need to add more
if labelOverflowY > 0 { if labelOverflowY > 0 {
switch labelPosition {
case
label.InsideTopLeft, label.InsideTopCenter, label.InsideTopRight,
label.InsideBottomLeft, label.InsideBottomCenter, label.InsideBottomRight:
obj.Height += labelOverflowY obj.Height += labelOverflowY
}
// if label is top, need to shift contents down // if label is top, need to shift contents down
switch labelPosition { switch labelPosition {
case label.InsideTopLeft, label.InsideTopCenter, label.InsideTopRight: case label.InsideTopLeft, label.InsideTopCenter, label.InsideTopRight:
@ -93,27 +124,20 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
labelOverflowX := occupiedWidth - float64(horizontalPadding) labelOverflowX := occupiedWidth - float64(horizontalPadding)
if labelOverflowX > 0 { if labelOverflowX > 0 {
switch labelPosition {
case label.InsideMiddleLeft, label.InsideMiddleRight:
obj.Width += labelOverflowX obj.Width += labelOverflowX
}
if labelPosition == label.InsideMiddleLeft { if labelPosition == label.InsideMiddleLeft {
dx = labelOverflowX dx = labelOverflowX
} }
} }
}
}
// 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 {
gd.shift(dx, dy) gd.shift(dx, dy)
} }
} }
if obj.HasLabel() && obj.LabelPosition == nil {
obj.LabelPosition = go2.Pointer(label.InsideTopCenter.String())
}
if obj.Icon != nil && obj.IconPosition == nil {
obj.IconPosition = go2.Pointer(label.InsideTopLeft.String())
}
// simple straight line edge routing between grid objects // simple straight line edge routing between grid objects
for _, e := range g.Edges { for _, e := range g.Edges {
if !e.Src.Parent.IsDescendantOf(obj) && !e.Dst.Parent.IsDescendantOf(obj) { if !e.Src.Parent.IsDescendantOf(obj) && !e.Dst.Parent.IsDescendantOf(obj) {