From 79f81688bb1c627e87fd71d8ee3263e462b2d920 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 15 May 2024 01:07:52 -0400 Subject: [PATCH] SetBorder compares borders before doing re-layout/draw --- box.go | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/box.go b/box.go index da9b78b..baf968e 100644 --- a/box.go +++ b/box.go @@ -121,10 +121,29 @@ func (this *box) SetTexture (texture canvas.Texture) { this.invalidateDraw() } -func (this *box) SetBorder (border ...tomo.Border) { - this.border = border - this.invalidateLayout() - this.invalidateMinimum() +func (this *box) SetBorder (borders ...tomo.Border) { + previousBorderSum := this.borderSum() + previousBorders := this.border + this.border = borders + + // only invalidate the layout if the border is sized differently + if this.borderSum() != previousBorderSum { + this.invalidateLayout() + this.invalidateMinimum() + return + } + + // if the border takes up the same amount of space, only invalidate the + // drawing if it looks different + for index, newBorder := range this.border { + different := + index >= len(previousBorders) || + newBorder != previousBorders[index] + if different { + this.invalidateDraw() + return + } + } } func (this *box) SetMinimumSize (size image.Point) { @@ -356,7 +375,11 @@ func (this *box) doMinimumSize () { } } +// var drawcnt int func (this *box) doDraw () { + // println("DRAW", drawcnt) + // drawcnt ++ + if this.canvas == nil { return } if this.drawer != nil { this.drawBorders(this.canvas) @@ -364,7 +387,11 @@ func (this *box) doDraw () { } } +// var laycnt int func (this *box) doLayout () { + // println("LAYOUT", laycnt) + // laycnt ++ + this.innerClippingBounds = this.borderSum().Apply(this.bounds) if this.parent == nil { this.canvas = nil; return } parentCanvas := this.parent.canvas()