Replace most functionality in internal/util with goutil and slices

This commit is contained in:
2024-09-11 00:08:21 -04:00
parent 0a8bb39265
commit 9d67013e33
9 changed files with 39 additions and 156 deletions

View File

@@ -1,12 +1,12 @@
package system
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/backend/internal/util"
import "git.tebibyte.media/sashakoshka/goutil/container"
type attrHierarchy [T tomo.Attr] struct {
fallback T
style util.Optional[T]
user util.Optional[T]
style ucontainer.Optional[T]
user ucontainer.Optional[T]
}
func (this *attrHierarchy[T]) SetFallback (fallback T) {

View File

@@ -7,20 +7,21 @@ import "git.tebibyte.media/tomo/tomo/data"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/backend/internal/util"
import "git.tebibyte.media/sashakoshka/goutil/container"
import "git.tebibyte.media/sashakoshka/goutil/image/color"
type box struct {
system *System
parent parent
outer anyBox
tags util.Set[string]
tags ucontainer.Set[string]
role tomo.Role
lastStyleNonce int
lastIconSetNonce int
styleApplicator *styleApplicator
minSize util.Memo[image.Point]
minSize ucontainer.Memo[image.Point]
bounds image.Rectangle
innerClippingBounds image.Rectangle
@@ -41,7 +42,7 @@ type box struct {
focused bool
pressed bool
canvas util.Memo[canvas.Canvas]
canvas ucontainer.Memo[canvas.Canvas]
drawer canvas.Drawer
on struct {
@@ -68,10 +69,10 @@ func (this *System) newBox (outer anyBox) *box {
system: this,
outer: outer,
drawer: outer,
tags: make(util.Set[string]),
tags: make(ucontainer.Set[string]),
}
box.attrColor.SetFallback(tomo.AColor(color.Transparent))
box.canvas = util.NewMemo (func () canvas.Canvas {
box.canvas = ucontainer.NewMemo (func () canvas.Canvas {
if box.parent == nil { return nil }
parentCanvas := box.parent.getCanvas()
if parentCanvas == nil { return nil }
@@ -82,7 +83,7 @@ func (this *System) newBox (outer anyBox) *box {
box.drawer = box
box.outer = box
}
box.minSize = util.NewMemo(box.calculateMinimumSize)
box.minSize = ucontainer.NewMemo(box.calculateMinimumSize)
return box
}
@@ -476,7 +477,7 @@ func (this *box) drawBorders (can canvas.Canvas) {
rectangle := func (x0, y0, x1, y1 int, c color.Color) {
area := image.Rect(x0, y0, x1, y1)
if area.Empty() { return }
if util.Transparent(c) && this.parent != nil {
if ucolor.Transparent(c) && this.parent != nil {
this.parent.drawBackgroundPart(can.SubCanvas(area))
}
pen.Fill(c)
@@ -682,7 +683,7 @@ func (this *box) transparent () bool {
// TODO uncomment once we have
// a way to detect texture transparency
col := this.attrColor.Value().Color
return col == nil || util.Transparent(col) /*&&
return col == nil || ucolor.Transparent(col) /*&&
(this.texture == nil || !this.texture.Opaque())*/
}

View File

@@ -1,11 +1,11 @@
package system
import "image"
import "slices"
import "image/color"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/backend/internal/util"
type containerBox struct {
*box
@@ -50,7 +50,7 @@ func (this *containerBox) OnContentBoundsChange (callback func()) event.Cookie {
func (this *containerBox) Add (child tomo.Object) {
box := assertAnyBox(child.GetBox())
if util.IndexOf(this.children, box) > -1 { return }
if slices.Index(this.children, box) > -1 { return }
box.setParent(this)
box.flushActionQueue()
@@ -61,26 +61,26 @@ func (this *containerBox) Add (child tomo.Object) {
func (this *containerBox) Remove (child tomo.Object) {
box := assertAnyBox(child.GetBox())
index := util.IndexOf(this.children, box)
index := slices.Index(this.children, box)
if index < 0 { return }
box.setParent(nil)
this.children = util.Remove(this.children, index)
this.children = slices.Delete(this.children, index, index)
this.invalidateLayout()
this.invalidateMinimum()
}
func (this *containerBox) Insert (child, before tomo.Object) {
box := assertAnyBox(child.GetBox())
if util.IndexOf(this.children, box) > -1 { return }
if slices.Index(this.children, box) > -1 { return }
beforeBox := assertAnyBox(before.GetBox())
index := util.IndexOf(this.children, beforeBox)
index := slices.Index(this.children, beforeBox)
if index < 0 {
this.children = append(this.children, box)
} else {
this.children = util.Insert(this.children, index, box)
this.children = slices.Insert(this.children, index, box)
}
box.setParent(this)

View File

@@ -5,7 +5,7 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/backend/style"
import "git.tebibyte.media/tomo/backend/internal/util"
import "git.tebibyte.media/sashakoshka/goutil/container"
// Hierarchy is coupled to a tomo.Window implementation, and manages a tree of
// Boxes.
@@ -25,9 +25,9 @@ type Hierarchy struct {
drags [10][]anyBox
minimumSize image.Point
needStyle util.Set[anyBox]
needLayout util.Set[anyBox]
needDraw util.Set[anyBox]
needStyle ucontainer.Set[anyBox]
needLayout ucontainer.Set[anyBox]
needDraw ucontainer.Set[anyBox]
needRedo bool
minimumClean bool
}
@@ -53,9 +53,9 @@ func (this *System) NewHierarchy (link WindowLink) *Hierarchy {
hierarchy := &Hierarchy {
system: this,
link: link,
needStyle: make(util.Set[anyBox]),
needLayout: make(util.Set[anyBox]),
needDraw: make(util.Set[anyBox]),
needStyle: make(ucontainer.Set[anyBox]),
needLayout: make(ucontainer.Set[anyBox]),
needDraw: make(ucontainer.Set[anyBox]),
}
this.hierarchies.Add(hierarchy)
return hierarchy

View File

@@ -4,7 +4,7 @@ import "io"
import "image"
import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/backend/style"
import "git.tebibyte.media/tomo/backend/internal/util"
import "git.tebibyte.media/sashakoshka/goutil/container"
// System is coupled to a tomo.Backend implementation, and manages Hierarchies
// and Boxes.
@@ -17,7 +17,7 @@ type System struct {
styleNonce int
iconSetNonce int
hierarchies util.Set[*Hierarchy]
hierarchies ucontainer.Set[*Hierarchy]
}
// BackendLink allows the System to call up into the tomo.Backend implementation
@@ -41,7 +41,7 @@ type SurfaceLink interface {
func New (link BackendLink) *System {
return &System {
link: link,
hierarchies: make(util.Set[*Hierarchy]),
hierarchies: make(ucontainer.Set[*Hierarchy]),
}
}

View File

@@ -12,6 +12,7 @@ import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/backend/internal/util"
import "git.tebibyte.media/sashakoshka/goutil/container"
type textBox struct {
*box
@@ -36,7 +37,7 @@ type textBox struct {
drawer typeset.Drawer
face util.Cycler[font.Face]
lineHeight util.Memo[fixed.Int26_6]
lineHeight ucontainer.Memo[fixed.Int26_6]
on struct {
contentBoundsChange event.FuncBroadcaster
@@ -49,7 +50,7 @@ func (this *System) NewTextBox () tomo.TextBox {
box.box = this.newBox(box)
box.attrTextColor.SetFallback(tomo.ATextColor(color.Black))
box.attrDotColor.SetFallback(tomo.ADotColor(color.RGBA { G: 255, B: 255, A: 255}))
box.lineHeight = util.NewMemo(func () fixed.Int26_6 {
box.lineHeight = ucontainer.NewMemo(func () fixed.Int26_6 {
face := box.face.Value()
if face == nil { return 0 }
metrics := face.Metrics()