update grid size with desiredWidth/Height after adjusting for label/icon

This commit is contained in:
Gavin Nishizawa 2023-11-14 10:49:08 -08:00
parent bf18eb44e8
commit b87cc8e7e1
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD

View file

@ -47,15 +47,7 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
verticalPadding = gd.verticalGap verticalPadding = gd.verticalGap
} }
// size shape according to grid contentWidth, contentHeight := gd.width, gd.height
obj.SizeToContent(gd.width, gd.height, float64(2*horizontalPadding), float64(2*verticalPadding))
// compute where the grid should be placed inside shape
s := obj.ToShape()
innerBox := s.GetInnerBox()
if innerBox.TopLeft.X != 0 || innerBox.TopLeft.Y != 0 {
gd.shift(innerBox.TopLeft.X, innerBox.TopLeft.Y)
}
var labelPosition, iconPosition label.Position var labelPosition, iconPosition label.Position
if obj.LabelPosition != nil { if obj.LabelPosition != nil {
@ -83,7 +75,7 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
label.InsideTopLeft, label.InsideTopCenter, label.InsideTopRight, label.InsideTopLeft, label.InsideTopCenter, label.InsideTopRight,
label.InsideBottomLeft, label.InsideBottomCenter, label.InsideBottomRight, label.InsideBottomLeft, label.InsideBottomCenter, label.InsideBottomRight,
label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight: label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight:
overflow := labelWidth - (obj.Width - float64(2*horizontalPadding)) overflow := labelWidth - contentWidth
if overflow > 0 { if overflow > 0 {
padding.Left += overflow / 2 padding.Left += overflow / 2
padding.Right += overflow / 2 padding.Right += overflow / 2
@ -95,7 +87,7 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom, case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom,
label.InsideMiddleLeft, label.InsideMiddleCenter, label.InsideMiddleRight, label.InsideMiddleLeft, label.InsideMiddleCenter, label.InsideMiddleRight,
label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom: label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom:
overflow := labelHeight - (obj.Height - float64(2*verticalPadding)) overflow := labelHeight - contentHeight
if overflow > 0 { if overflow > 0 {
padding.Top += overflow / 2 padding.Top += overflow / 2
padding.Bottom += overflow / 2 padding.Bottom += overflow / 2
@ -112,7 +104,7 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
padding.Left = math.Max(padding.Left, iconSize) padding.Left = math.Max(padding.Left, iconSize)
padding.Right = math.Max(padding.Right, iconSize) padding.Right = math.Max(padding.Right, iconSize)
minWidth := 2*iconSize + float64(obj.LabelDimensions.Width) + 2*label.PADDING minWidth := 2*iconSize + float64(obj.LabelDimensions.Width) + 2*label.PADDING
overflow := minWidth - (obj.Width - float64(2*horizontalPadding)) overflow := minWidth - contentWidth
if overflow > 0 { if overflow > 0 {
padding.Left = math.Max(padding.Left, overflow/2) padding.Left = math.Max(padding.Left, overflow/2)
padding.Right = math.Max(padding.Right, overflow/2) padding.Right = math.Max(padding.Right, overflow/2)
@ -121,24 +113,31 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
overflowTop := padding.Top - float64(verticalPadding) overflowTop := padding.Top - float64(verticalPadding)
if overflowTop > 0 { if overflowTop > 0 {
obj.Height += overflowTop contentHeight += overflowTop
dy += overflowTop dy += overflowTop
} }
overflowBottom := padding.Bottom - float64(verticalPadding) overflowBottom := padding.Bottom - float64(verticalPadding)
if overflowBottom > 0 { if overflowBottom > 0 {
obj.Height += overflowBottom contentHeight += overflowBottom
} }
overflowLeft := padding.Left - float64(horizontalPadding) overflowLeft := padding.Left - float64(horizontalPadding)
if overflowLeft > 0 { if overflowLeft > 0 {
obj.Width += overflowLeft contentWidth += overflowLeft
dx += overflowLeft dx += overflowLeft
} }
overflowRight := padding.Right - float64(horizontalPadding) overflowRight := padding.Right - float64(horizontalPadding)
if overflowRight > 0 { if overflowRight > 0 {
obj.Width += overflowRight contentWidth += overflowRight
} }
// we need to center children if we have to expand to fit the container label // size shape according to grid
obj.SizeToContent(contentWidth, contentHeight, float64(2*horizontalPadding), float64(2*verticalPadding))
// compute where the grid should be placed inside shape
s := obj.ToShape()
innerBox := s.GetInnerBox()
dx = innerBox.TopLeft.X + dx
dy = innerBox.TopLeft.Y + dy
if dx != 0 || dy != 0 { if dx != 0 || dy != 0 {
gd.shift(dx, dy) gd.shift(dx, dy)
} }