From d3721fd4f005472526010449f06b45fad908cd4d Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Tue, 26 Sep 2023 17:38:06 -0700 Subject: [PATCH] refactor, fix --- d2graph/layout.go | 44 ++++++++++++++++++++++++++++++++++++++ d2layouts/d2grid/layout.go | 40 +++------------------------------- 2 files changed, 47 insertions(+), 37 deletions(-) diff --git a/d2graph/layout.go b/d2graph/layout.go index 8c705b713..f1c4a7ecc 100644 --- a/d2graph/layout.go +++ b/d2graph/layout.go @@ -1,6 +1,7 @@ package d2graph import ( + "math" "sort" "strings" @@ -285,6 +286,49 @@ func (obj *Object) GetModifierElementAdjustments() (dx, dy float64) { return dx, dy } +func (obj *Object) GetMargin() geo.Spacing { + margin := geo.Spacing{} + + if obj.HasLabel() && obj.LabelPosition != nil { + position := label.Position(*obj.LabelPosition) + + labelWidth := float64(obj.LabelDimensions.Width + label.PADDING) + labelHeight := float64(obj.LabelDimensions.Height + label.PADDING) + + switch position { + case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight: + margin.Top = labelHeight + case label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight: + margin.Bottom = labelHeight + case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom: + margin.Left = labelWidth + case label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom: + margin.Right = labelWidth + } + } + + if obj.Icon != nil && obj.IconPosition != nil && obj.Shape.Value != d2target.ShapeImage { + position := label.Position(*obj.IconPosition) + + iconSize := float64(d2target.MAX_ICON_SIZE + label.PADDING) + switch position { + case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight: + margin.Top = math.Max(margin.Top, iconSize) + case label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight: + margin.Bottom = math.Max(margin.Bottom, iconSize) + case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom: + margin.Left = math.Max(margin.Left, iconSize) + case label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom: + margin.Right = math.Max(margin.Right, iconSize) + } + } + + dx, dy := obj.GetModifierElementAdjustments() + margin.Right += dx + margin.Top += dy + return margin +} + func (obj *Object) ToShape() shape.Shape { tl := obj.TopLeft if tl == nil { diff --git a/d2layouts/d2grid/layout.go b/d2layouts/d2grid/layout.go index 05a35186c..52910a0ec 100644 --- a/d2layouts/d2grid/layout.go +++ b/d2layouts/d2grid/layout.go @@ -845,41 +845,7 @@ func (gd *gridDiagram) sizeForOutsideLabels() (revertTemp func()) { margins := make(map[*d2graph.Object]geo.Spacing) for _, o := range gd.objects { - margin := geo.Spacing{} - - if o.HasLabel() && o.LabelPosition != nil { - position := label.Position(*o.LabelPosition) - - labelWidth := float64(o.LabelDimensions.Width + 2*label.PADDING) - labelHeight := float64(o.LabelDimensions.Height + 2*label.PADDING) - - switch position { - case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight: - margin.Top = labelHeight - case label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight: - margin.Bottom = labelHeight - case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom: - margin.Left = labelWidth - case label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom: - margin.Right = labelWidth - } - } - - if o.Icon != nil && o.IconPosition != nil && o.Shape.Value != d2target.ShapeImage { - position := label.Position(*o.IconPosition) - - iconSize := float64(d2target.MAX_ICON_SIZE + 2*label.PADDING) - switch position { - case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight: - margin.Top = math.Max(margin.Top, iconSize) - case label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight: - margin.Bottom = math.Max(margin.Bottom, iconSize) - case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom: - margin.Left = math.Max(margin.Left, iconSize) - case label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom: - margin.Right = math.Max(margin.Right, iconSize) - } - } + margin := o.GetMargin() if margin.Top > 0 { o.Height += margin.Top @@ -907,8 +873,8 @@ func (gd *gridDiagram) sizeForOutsideLabels() (revertTemp func()) { o.Height -= margin.Top + margin.Bottom o.Width -= margin.Left + margin.Right - if margin.Top > 0 || margin.Left > 0 { - o.MoveWithDescendants(margin.Top, margin.Left) + if margin.Left > 0 || margin.Top > 0 { + o.MoveWithDescendants(margin.Left, margin.Top) } } }