From db2ed06daf6993fcbbe85668088a5db3fe8f0633 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 7 Sep 2023 18:22:04 -0400 Subject: [PATCH] Fixed transparent boxes not redrawing sometimes --- containerbox.go | 36 +++++++++++++++++++++++++++++------- system.go | 13 +++++++------ 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/containerbox.go b/containerbox.go index 0a597fa..9432766 100644 --- a/containerbox.go +++ b/containerbox.go @@ -1,6 +1,7 @@ package x import "image" +import "image/color" import "git.tebibyte.media/tomo/tomo" import "git.tebibyte.media/tomo/tomo/event" import "git.tebibyte.media/tomo/tomo/canvas" @@ -12,7 +13,7 @@ type containerBox struct { hAlign, vAlign tomo.Align contentBounds image.Rectangle scroll image.Point - + gap image.Point children []tomo.Box layout tomo.Layout @@ -29,6 +30,16 @@ func (backend *Backend) NewContainerBox() tomo.ContainerBox { return this } +func (this *containerBox) SetColor (c color.Color) { + this.box.SetColor(c) + this.invalidateTransparentChildren() +} + +func (this *containerBox) SetTexture (texture canvas.Texture) { + this.box.SetTexture(texture) + this.invalidateTransparentChildren() +} + func (this *containerBox) SetOverflow (horizontal, vertical bool) { if this.hOverflow == horizontal && this.vOverflow == vertical { return } this.hOverflow = horizontal @@ -71,7 +82,7 @@ func (this *containerBox) SetGap (gap image.Point) { func (this *containerBox) Add (child tomo.Object) { box := assertAnyBox(child.GetBox()) if indexOf(this.children, tomo.Box(box)) > -1 { return } - + box.setParent(this) box.flushActionQueue() this.children = append(this.children, box) @@ -83,7 +94,7 @@ func (this *containerBox) Delete (child tomo.Object) { box := assertAnyBox(child.GetBox()) index := indexOf(this.children, tomo.Box(box)) if index < 0 { return } - + box.setParent(nil) this.children = remove(this.children, index) this.invalidateLayout() @@ -97,7 +108,7 @@ func (this *containerBox) Insert (child, before tomo.Object) { beforeBox := assertAnyBox(before.GetBox()) index := indexOf(this.children, tomo.Box(beforeBox)) if index < 0 { return } - + box.setParent(this) this.children = insert(this.children, index, tomo.Box(box)) this.invalidateLayout() @@ -155,13 +166,24 @@ func (this *containerBox) drawBackgroundPart (can canvas.Canvas) { pen := can.Pen() pen.Fill(this.color) pen.Texture(this.texture) - + if this.transparent() && this.parent != nil { this.parent.drawBackgroundPart(can) } pen.Rectangle(this.innerClippingBounds) } +func (this *containerBox) invalidateTransparentChildren () { + window := this.window() + if this.window == nil { return } + for _, box := range this.children { + box := assertAnyBox(box) + if box.transparent() { + window.invalidateDraw(box) + } + } +} + func (this *containerBox) flushActionQueue () { for _, box := range this.children { box.(anyBox).flushActionQueue() @@ -251,11 +273,11 @@ func (this *containerBox) propagate (callback func (anyBox) bool) bool { func (this *containerBox) propagateAlt (callback func (anyBox) bool) bool { if !callback(this) { return false} - + for _, box := range this.children { box := box.(anyBox) if !box.propagateAlt(callback) { return false } } - + return true } diff --git a/system.go b/system.go index 9b6a6e2..ac06803 100644 --- a/system.go +++ b/system.go @@ -42,7 +42,7 @@ type parent interface { type anyBox interface { tomo.Box canvas.Drawer - + doDraw () doLayout () doMinimumSize () @@ -52,6 +52,7 @@ type anyBox interface { recursiveRedo () canBeFocused () bool boxUnder (image.Point) anyBox + transparent () bool propagate (func (anyBox) bool) bool propagateAlt (func (anyBox) bool) bool @@ -122,10 +123,10 @@ func (window *window) invalidateLayout (box anyBox) { func (window *window) focus (box anyBox) { if window.focused == box { return } - + previous := window.focused window.focused = box - + if previous != nil { previous.handleFocusLeave() } @@ -136,10 +137,10 @@ func (window *window) focus (box anyBox) { func (window *window) hover (box anyBox) { if window.hovered == box { return } - + previous := window.hovered window.hovered = box - + if previous != nil { previous.handleMouseLeave() } @@ -213,7 +214,7 @@ func (window *window) afterEvent () { childBounds = childBounds.Sub(childBounds.Min) window.root.SetBounds(childBounds) - // full relayout/redraw + // full relayout/redraw if window.root != nil { window.root.recursiveRedo() }