Skip to content

Commit e24b83c

Browse files
committed
Fix layout edge cases and gradient generation
Improves Block.SetRect to handle cases where padding exceeds block size, preventing invalid rectangles. Refactors Grid.Draw to correctly calculate item boundaries and prevent overflow. Adds a guard in GenerateGradient to return an empty slice for non-positive lengths.
1 parent 3c75ed4 commit e24b83c

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

block.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,23 @@ func (b *Block) drawTitles(buf *Buffer) {
285285
// SetRect implements the Drawable interface.
286286
func (b *Block) SetRect(x1, y1, x2, y2 int) {
287287
b.Rectangle = image.Rect(x1, y1, x2, y2)
288-
b.Inner = image.Rect(
289-
b.Min.X+1+b.PaddingLeft,
290-
b.Min.Y+1+b.PaddingTop,
291-
b.Max.X-1-b.PaddingRight,
292-
b.Max.Y-1-b.PaddingBottom,
293-
)
288+
innerMinX := b.Min.X + 1 + b.PaddingLeft
289+
innerMinY := b.Min.Y + 1 + b.PaddingTop
290+
innerMaxX := b.Max.X - 1 - b.PaddingRight
291+
innerMaxY := b.Max.Y - 1 - b.PaddingBottom
292+
293+
if innerMinX > innerMaxX {
294+
mid := b.Min.X + (b.Max.X-b.Min.X)/2
295+
innerMinX = mid
296+
innerMaxX = mid
297+
}
298+
if innerMinY > innerMaxY {
299+
mid := b.Min.Y + (b.Max.Y-b.Min.Y)/2
300+
innerMinY = mid
301+
innerMaxY = mid
302+
}
303+
304+
b.Inner = image.Rect(innerMinX, innerMinY, innerMaxX, innerMaxY)
294305
}
295306

296307
// GetRect implements the Drawable interface.

grid.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,24 @@ func (g *Grid) setHelper(item GridItem, parentWidthRatio, parentHeightRatio floa
100100
}
101101

102102
func (g *Grid) Draw(buf *Buffer) {
103-
width := float64(g.Dx()) + 1
104-
height := float64(g.Dy()) + 1
105103

106104
for _, item := range g.Items {
107105
entry, _ := item.Entry.(Drawable)
108106

109-
x := int(width*item.XRatio) + g.Min.X
110-
y := int(height*item.YRatio) + g.Min.Y
111-
w := int(width * item.WidthRatio)
112-
h := int(height * item.HeightRatio)
107+
xStart := int(float64(g.Dx())*item.XRatio) + g.Min.X
108+
yStart := int(float64(g.Dy())*item.YRatio) + g.Min.Y
113109

114-
if x+w > g.Dx() {
115-
w--
110+
xEnd := int(float64(g.Dx())*(item.XRatio+item.WidthRatio)) + g.Min.X
111+
yEnd := int(float64(g.Dy())*(item.YRatio+item.HeightRatio)) + g.Min.Y
112+
113+
if xEnd > g.Max.X {
114+
xEnd = g.Max.X
116115
}
117-
if y+h > g.Dy() {
118-
h--
116+
if yEnd > g.Max.Y {
117+
yEnd = g.Max.Y
119118
}
120119

121-
entry.SetRect(x, y, x+w, y+h)
120+
entry.SetRect(xStart, yStart, xEnd, yEnd)
122121

123122
entry.Lock()
124123
entry.Draw(buf)

style_gradient.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func InterpolateColor(c1, c2 Color, step, steps int) Color {
2525
}
2626

2727
func GenerateGradient(start, end Color, length int) []Color {
28+
if length <= 0 {
29+
return []Color{}
30+
}
2831
colors := make([]Color, length)
2932
for i := 0; i < length; i++ {
3033
colors[i] = InterpolateColor(start, end, i, length)

0 commit comments

Comments
 (0)