Compare commits
16 Commits
v0.7.0
...
55215dedc2
| Author | SHA1 | Date | |
|---|---|---|---|
| 55215dedc2 | |||
| eb98d143db | |||
| 42deb40c2d | |||
| b6850ee702 | |||
| 9d67013e33 | |||
| 0a8bb39265 | |||
| b92308fc80 | |||
| 6f9eca99e7 | |||
| 381f5f88bd | |||
| 1c38ed2d87 | |||
| 9079aca993 | |||
| 70d6759884 | |||
| 2f828b1ae8 | |||
| c1c0d2125d | |||
| 3b4ab56914 | |||
| e7f16645eb |
9
go.mod
9
go.mod
@@ -1,10 +1,13 @@
|
|||||||
module git.tebibyte.media/tomo/backend
|
module git.tebibyte.media/tomo/backend
|
||||||
|
|
||||||
go 1.20
|
go 1.21.0
|
||||||
|
|
||||||
|
toolchain go1.22.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.tebibyte.media/tomo/tomo v0.46.1
|
git.tebibyte.media/sashakoshka/goutil v0.3.0
|
||||||
git.tebibyte.media/tomo/typeset v0.7.1
|
git.tebibyte.media/tomo/tomo v0.48.0
|
||||||
|
git.tebibyte.media/tomo/typeset v0.8.0
|
||||||
git.tebibyte.media/tomo/xgbkb v1.0.1
|
git.tebibyte.media/tomo/xgbkb v1.0.1
|
||||||
github.com/jezek/xgb v1.1.1
|
github.com/jezek/xgb v1.1.1
|
||||||
github.com/jezek/xgbutil v0.0.0-20231116234834-47f30c120111
|
github.com/jezek/xgbutil v0.0.0-20231116234834-47f30c120111
|
||||||
|
|||||||
10
go.sum
10
go.sum
@@ -1,8 +1,10 @@
|
|||||||
|
git.tebibyte.media/sashakoshka/goutil v0.3.0 h1:dcZ/9/or7m8eTpf2B1Pu4CscplXh2INTXFartz+ExwE=
|
||||||
|
git.tebibyte.media/sashakoshka/goutil v0.3.0/go.mod h1:e1OXLa+wX7x/F8n8gyxz2hnfVCEkWzGrZNX8/k/lR/M=
|
||||||
git.tebibyte.media/sashakoshka/xgbkb v1.0.0/go.mod h1:pNcE6TRO93vHd6q42SdwLSTTj25L0Yzggz7yLe0JV6Q=
|
git.tebibyte.media/sashakoshka/xgbkb v1.0.0/go.mod h1:pNcE6TRO93vHd6q42SdwLSTTj25L0Yzggz7yLe0JV6Q=
|
||||||
git.tebibyte.media/tomo/tomo v0.46.1 h1:/8fT6I9l4TK529zokrThbNDHGRvUsNgif1Zs++0PBSQ=
|
git.tebibyte.media/tomo/tomo v0.48.0 h1:AE21ElHwUSPsX82ZWCnoNxJFi9Oswyd3dPDPMbxTueQ=
|
||||||
git.tebibyte.media/tomo/tomo v0.46.1/go.mod h1:WrtilgKB1y8O2Yu7X4mYcRiqOlPR8NuUnoA/ynkQWrs=
|
git.tebibyte.media/tomo/tomo v0.48.0/go.mod h1:WrtilgKB1y8O2Yu7X4mYcRiqOlPR8NuUnoA/ynkQWrs=
|
||||||
git.tebibyte.media/tomo/typeset v0.7.1 h1:aZrsHwCG5ZB4f5CruRFsxLv5ezJUCFUFsQJJso2sXQ8=
|
git.tebibyte.media/tomo/typeset v0.8.0 h1:4qA6oW4/3oPHj6/Zrp+JFJ53OmFSDvxs+J6BhO3DW00=
|
||||||
git.tebibyte.media/tomo/typeset v0.7.1/go.mod h1:PwDpSdBF3l/EzoIsa2ME7QffVVajnTHZN6l3MHEGe1g=
|
git.tebibyte.media/tomo/typeset v0.8.0/go.mod h1:PwDpSdBF3l/EzoIsa2ME7QffVVajnTHZN6l3MHEGe1g=
|
||||||
git.tebibyte.media/tomo/xgbkb v1.0.1 h1:b3HDUopjdQp1MZrb5Vpil4bOtk3NnNXtfQW27Blw2kE=
|
git.tebibyte.media/tomo/xgbkb v1.0.1 h1:b3HDUopjdQp1MZrb5Vpil4bOtk3NnNXtfQW27Blw2kE=
|
||||||
git.tebibyte.media/tomo/xgbkb v1.0.1/go.mod h1:P5Du0yo5hUsojchW08t+Mds0XPIJXwMi733ZfklzjRw=
|
git.tebibyte.media/tomo/xgbkb v1.0.1/go.mod h1:P5Du0yo5hUsojchW08t+Mds0XPIJXwMi733ZfklzjRw=
|
||||||
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 h1:1qlsVAQJXZHsaM8b6OLVo6muQUQd4CwkH/D3fnnbHXA=
|
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 h1:1qlsVAQJXZHsaM8b6OLVo6muQUQd4CwkH/D3fnnbHXA=
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
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 {
|
type attrHierarchy [T tomo.Attr] struct {
|
||||||
fallback T
|
fallback T
|
||||||
style util.Optional[T]
|
style ucontainer.Optional[T]
|
||||||
user util.Optional[T]
|
user ucontainer.Optional[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *attrHierarchy[T]) SetFallback (fallback T) {
|
func (this *attrHierarchy[T]) SetFallback (fallback T) {
|
||||||
|
|||||||
@@ -7,20 +7,21 @@ import "git.tebibyte.media/tomo/tomo/data"
|
|||||||
import "git.tebibyte.media/tomo/tomo/input"
|
import "git.tebibyte.media/tomo/tomo/input"
|
||||||
import "git.tebibyte.media/tomo/tomo/event"
|
import "git.tebibyte.media/tomo/tomo/event"
|
||||||
import "git.tebibyte.media/tomo/tomo/canvas"
|
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 {
|
type box struct {
|
||||||
system *System
|
system *System
|
||||||
parent parent
|
parent parent
|
||||||
outer anyBox
|
outer anyBox
|
||||||
|
|
||||||
tags util.Set[string]
|
tags ucontainer.Set[string]
|
||||||
role tomo.Role
|
role tomo.Role
|
||||||
lastStyleNonce int
|
lastStyleNonce int
|
||||||
lastIconSetNonce int
|
lastIconSetNonce int
|
||||||
styleApplicator *styleApplicator
|
styleApplicator *styleApplicator
|
||||||
|
|
||||||
minSize util.Memo[image.Point]
|
minSize ucontainer.Memo[image.Point]
|
||||||
bounds image.Rectangle
|
bounds image.Rectangle
|
||||||
innerClippingBounds image.Rectangle
|
innerClippingBounds image.Rectangle
|
||||||
|
|
||||||
@@ -41,7 +42,7 @@ type box struct {
|
|||||||
focused bool
|
focused bool
|
||||||
pressed bool
|
pressed bool
|
||||||
|
|
||||||
canvas util.Memo[canvas.Canvas]
|
canvas ucontainer.Memo[canvas.Canvas]
|
||||||
drawer canvas.Drawer
|
drawer canvas.Drawer
|
||||||
|
|
||||||
on struct {
|
on struct {
|
||||||
@@ -68,20 +69,21 @@ func (this *System) newBox (outer anyBox) *box {
|
|||||||
system: this,
|
system: this,
|
||||||
outer: outer,
|
outer: outer,
|
||||||
drawer: outer,
|
drawer: outer,
|
||||||
tags: make(util.Set[string]),
|
tags: make(ucontainer.Set[string]),
|
||||||
}
|
}
|
||||||
box.attrColor.SetFallback(tomo.AColor(color.Transparent))
|
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 }
|
if box.parent == nil { return nil }
|
||||||
parentCanvas := box.parent.getCanvas()
|
parentCanvas := box.parent.getCanvas()
|
||||||
if parentCanvas == nil { return nil }
|
if parentCanvas == nil { return nil }
|
||||||
return parentCanvas.SubCanvas(box.bounds)
|
drawableArea := box.bounds.Intersect(box.parent.getInnerClippingBounds())
|
||||||
|
return parentCanvas.SubCanvas(drawableArea)
|
||||||
})
|
})
|
||||||
if outer == nil {
|
if outer == nil {
|
||||||
box.drawer = box
|
box.drawer = box
|
||||||
box.outer = box
|
box.outer = box
|
||||||
}
|
}
|
||||||
box.minSize = util.NewMemo(box.calculateMinimumSize)
|
box.minSize = ucontainer.NewMemo(box.calculateMinimumSize)
|
||||||
return box
|
return box
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,6 +261,24 @@ func (this *box) unsetAttr (kind tomo.AttrKind, user bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *box) unsetAllAttrs (user bool) {
|
||||||
|
// keep this in sync with tomo.AttrKind!
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindColor, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindTexture, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindTextureMode, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindBorder, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindMinimumSize, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindPadding, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindGap, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindTextColor, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindDotColor, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindFace, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindWrap, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindAlign, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindOverflow, user)
|
||||||
|
this.outer.unsetAttr(tomo.AttrKindLayout, user)
|
||||||
|
}
|
||||||
|
|
||||||
func (this *box) setBounds (bounds image.Rectangle) {
|
func (this *box) setBounds (bounds image.Rectangle) {
|
||||||
if this.bounds == bounds { return }
|
if this.bounds == bounds { return }
|
||||||
this.bounds = bounds
|
this.bounds = bounds
|
||||||
@@ -457,7 +477,7 @@ func (this *box) drawBorders (can canvas.Canvas) {
|
|||||||
rectangle := func (x0, y0, x1, y1 int, c color.Color) {
|
rectangle := func (x0, y0, x1, y1 int, c color.Color) {
|
||||||
area := image.Rect(x0, y0, x1, y1)
|
area := image.Rect(x0, y0, x1, y1)
|
||||||
if area.Empty() { return }
|
if area.Empty() { return }
|
||||||
if util.Transparent(c) && this.parent != nil {
|
if ucolor.Transparent(c) && this.parent != nil {
|
||||||
this.parent.drawBackgroundPart(can.SubCanvas(area))
|
this.parent.drawBackgroundPart(can.SubCanvas(area))
|
||||||
}
|
}
|
||||||
pen.Fill(c)
|
pen.Fill(c)
|
||||||
@@ -624,6 +644,7 @@ func (this *box) recursiveReApply () {
|
|||||||
// applicator for every box, it's so style applicators can cache
|
// applicator for every box, it's so style applicators can cache
|
||||||
// information about the boxes they're linked to (like all rules
|
// information about the boxes they're linked to (like all rules
|
||||||
// with a matching role).
|
// with a matching role).
|
||||||
|
this.unsetAllAttrs(false)
|
||||||
this.lastStyleNonce = hierarchyStyleNonce
|
this.lastStyleNonce = hierarchyStyleNonce
|
||||||
this.styleApplicator = hierarchy.newStyleApplicator()
|
this.styleApplicator = hierarchy.newStyleApplicator()
|
||||||
this.invalidateStyle()
|
this.invalidateStyle()
|
||||||
@@ -662,7 +683,7 @@ func (this *box) transparent () bool {
|
|||||||
// TODO uncomment once we have
|
// TODO uncomment once we have
|
||||||
// a way to detect texture transparency
|
// a way to detect texture transparency
|
||||||
col := this.attrColor.Value().Color
|
col := this.attrColor.Value().Color
|
||||||
return col == nil || util.Transparent(col) /*&&
|
return col == nil || ucolor.Transparent(col) /*&&
|
||||||
(this.texture == nil || !this.texture.Opaque())*/
|
(this.texture == nil || !this.texture.Opaque())*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import "image"
|
import "image"
|
||||||
|
import "slices"
|
||||||
import "image/color"
|
import "image/color"
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
import "git.tebibyte.media/tomo/tomo/event"
|
import "git.tebibyte.media/tomo/tomo/event"
|
||||||
import "git.tebibyte.media/tomo/tomo/canvas"
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||||
import "git.tebibyte.media/tomo/backend/internal/util"
|
|
||||||
|
|
||||||
type containerBox struct {
|
type containerBox struct {
|
||||||
*box
|
*box
|
||||||
@@ -50,7 +50,7 @@ func (this *containerBox) OnContentBoundsChange (callback func()) event.Cookie {
|
|||||||
|
|
||||||
func (this *containerBox) Add (child tomo.Object) {
|
func (this *containerBox) Add (child tomo.Object) {
|
||||||
box := assertAnyBox(child.GetBox())
|
box := assertAnyBox(child.GetBox())
|
||||||
if util.IndexOf(this.children, box) > -1 { return }
|
if slices.Index(this.children, box) > -1 { return }
|
||||||
|
|
||||||
box.setParent(this)
|
box.setParent(this)
|
||||||
box.flushActionQueue()
|
box.flushActionQueue()
|
||||||
@@ -61,26 +61,26 @@ func (this *containerBox) Add (child tomo.Object) {
|
|||||||
|
|
||||||
func (this *containerBox) Remove (child tomo.Object) {
|
func (this *containerBox) Remove (child tomo.Object) {
|
||||||
box := assertAnyBox(child.GetBox())
|
box := assertAnyBox(child.GetBox())
|
||||||
index := util.IndexOf(this.children, box)
|
index := slices.Index(this.children, box)
|
||||||
if index < 0 { return }
|
if index < 0 { return }
|
||||||
|
|
||||||
box.setParent(nil)
|
box.setParent(nil)
|
||||||
this.children = util.Remove(this.children, index)
|
this.children = slices.Delete(this.children, index, index)
|
||||||
this.invalidateLayout()
|
this.invalidateLayout()
|
||||||
this.invalidateMinimum()
|
this.invalidateMinimum()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *containerBox) Insert (child, before tomo.Object) {
|
func (this *containerBox) Insert (child, before tomo.Object) {
|
||||||
box := assertAnyBox(child.GetBox())
|
box := assertAnyBox(child.GetBox())
|
||||||
if util.IndexOf(this.children, box) > -1 { return }
|
if slices.Index(this.children, box) > -1 { return }
|
||||||
|
|
||||||
beforeBox := assertAnyBox(before.GetBox())
|
beforeBox := assertAnyBox(before.GetBox())
|
||||||
index := util.IndexOf(this.children, beforeBox)
|
index := slices.Index(this.children, beforeBox)
|
||||||
|
|
||||||
if index < 0 {
|
if index < 0 {
|
||||||
this.children = append(this.children, box)
|
this.children = append(this.children, box)
|
||||||
} else {
|
} else {
|
||||||
this.children = util.Insert(this.children, index, box)
|
this.children = slices.Insert(this.children, index, box)
|
||||||
}
|
}
|
||||||
box.setParent(this)
|
box.setParent(this)
|
||||||
|
|
||||||
@@ -298,6 +298,10 @@ func (this *containerBox) getCanvas () canvas.Canvas {
|
|||||||
return this.canvas.Value()
|
return this.canvas.Value()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *containerBox) getInnerClippingBounds () image.Rectangle {
|
||||||
|
return this.innerClippingBounds
|
||||||
|
}
|
||||||
|
|
||||||
func (this *containerBox) notifyMinimumSizeChange (child anyBox) {
|
func (this *containerBox) notifyMinimumSizeChange (child anyBox) {
|
||||||
this.invalidateMinimum()
|
this.invalidateMinimum()
|
||||||
size := child.minimumSize()
|
size := child.minimumSize()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package system
|
|||||||
|
|
||||||
import "image"
|
import "image"
|
||||||
import "git.tebibyte.media/tomo/tomo/input"
|
import "git.tebibyte.media/tomo/tomo/input"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/config"
|
||||||
|
|
||||||
// TODO: once go v1.23 comes out, replace the explicit iterator calls here with
|
// TODO: once go v1.23 comes out, replace the explicit iterator calls here with
|
||||||
// range loops
|
// range loops
|
||||||
@@ -39,15 +40,14 @@ func (this *Hierarchy) HandleKeyDown (key input.Key, numberPad bool) {
|
|||||||
}
|
}
|
||||||
if caught { return }
|
if caught { return }
|
||||||
|
|
||||||
switch key {
|
switch input.KC(key, this.modifiers) {
|
||||||
case input.KeyTab:
|
case config.KeyChordFocusNext: this.focusNext()
|
||||||
if this.modifiers.Shift {
|
case config.KeyChordFocusPrevious: this.focusPrevious()
|
||||||
this.focusPrevious()
|
// TODO: up, down, left, and right should find a box to the top, bottom,
|
||||||
} else {
|
// left, and right respectively to move the focus to. we might want to
|
||||||
this.focusNext()
|
// have four corresponding key chords in tomo/config.
|
||||||
}
|
case input.KC(input.KeyDown, input.ModNone): this.focusNext()
|
||||||
case input.KeyUp: this.focusPrevious()
|
case input.KC(input.KeyUp, input.ModNone): this.focusPrevious()
|
||||||
case input.KeyDown: this.focusNext()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import "git.tebibyte.media/tomo/tomo"
|
|||||||
import "git.tebibyte.media/tomo/tomo/input"
|
import "git.tebibyte.media/tomo/tomo/input"
|
||||||
import "git.tebibyte.media/tomo/tomo/canvas"
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||||
import "git.tebibyte.media/tomo/backend/style"
|
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
|
// Hierarchy is coupled to a tomo.Window implementation, and manages a tree of
|
||||||
// Boxes.
|
// Boxes.
|
||||||
@@ -25,9 +25,9 @@ type Hierarchy struct {
|
|||||||
drags [10][]anyBox
|
drags [10][]anyBox
|
||||||
minimumSize image.Point
|
minimumSize image.Point
|
||||||
|
|
||||||
needStyle util.Set[anyBox]
|
needStyle ucontainer.Set[anyBox]
|
||||||
needLayout util.Set[anyBox]
|
needLayout ucontainer.Set[anyBox]
|
||||||
needDraw util.Set[anyBox]
|
needDraw ucontainer.Set[anyBox]
|
||||||
needRedo bool
|
needRedo bool
|
||||||
minimumClean bool
|
minimumClean bool
|
||||||
}
|
}
|
||||||
@@ -53,9 +53,9 @@ func (this *System) NewHierarchy (link WindowLink) *Hierarchy {
|
|||||||
hierarchy := &Hierarchy {
|
hierarchy := &Hierarchy {
|
||||||
system: this,
|
system: this,
|
||||||
link: link,
|
link: link,
|
||||||
needStyle: make(util.Set[anyBox]),
|
needStyle: make(ucontainer.Set[anyBox]),
|
||||||
needLayout: make(util.Set[anyBox]),
|
needLayout: make(ucontainer.Set[anyBox]),
|
||||||
needDraw: make(util.Set[anyBox]),
|
needDraw: make(ucontainer.Set[anyBox]),
|
||||||
}
|
}
|
||||||
this.hierarchies.Add(hierarchy)
|
this.hierarchies.Add(hierarchy)
|
||||||
return hierarchy
|
return hierarchy
|
||||||
@@ -193,6 +193,10 @@ func (this *Hierarchy) getCanvas () canvas.Canvas {
|
|||||||
return this.canvas
|
return this.canvas
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Hierarchy) getInnerClippingBounds () image.Rectangle {
|
||||||
|
return this.canvas.Bounds()
|
||||||
|
}
|
||||||
|
|
||||||
func (this *Hierarchy) getModifiers () input.Modifiers {
|
func (this *Hierarchy) getModifiers () input.Modifiers {
|
||||||
return this.modifiers
|
return this.modifiers
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ type parent interface {
|
|||||||
getHierarchy () *Hierarchy
|
getHierarchy () *Hierarchy
|
||||||
// canvas returns the canvas held by the parent.
|
// canvas returns the canvas held by the parent.
|
||||||
getCanvas () canvas.Canvas
|
getCanvas () canvas.Canvas
|
||||||
|
// getInnerClippingBounds returns the area of the canvas that children
|
||||||
|
// can draw to.
|
||||||
|
getInnerClippingBounds () image.Rectangle
|
||||||
// notifyMinimumSizeChange informs the parent that the minimum size of
|
// notifyMinimumSizeChange informs the parent that the minimum size of
|
||||||
// one of its children has changed.
|
// one of its children has changed.
|
||||||
notifyMinimumSizeChange (anyBox)
|
notifyMinimumSizeChange (anyBox)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import "io"
|
|||||||
import "image"
|
import "image"
|
||||||
import "git.tebibyte.media/tomo/tomo/canvas"
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||||
import "git.tebibyte.media/tomo/backend/style"
|
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
|
// System is coupled to a tomo.Backend implementation, and manages Hierarchies
|
||||||
// and Boxes.
|
// and Boxes.
|
||||||
@@ -17,7 +17,7 @@ type System struct {
|
|||||||
styleNonce int
|
styleNonce int
|
||||||
iconSetNonce int
|
iconSetNonce int
|
||||||
|
|
||||||
hierarchies util.Set[*Hierarchy]
|
hierarchies ucontainer.Set[*Hierarchy]
|
||||||
}
|
}
|
||||||
|
|
||||||
// BackendLink allows the System to call up into the tomo.Backend implementation
|
// BackendLink allows the System to call up into the tomo.Backend implementation
|
||||||
@@ -41,7 +41,7 @@ type SurfaceLink interface {
|
|||||||
func New (link BackendLink) *System {
|
func New (link BackendLink) *System {
|
||||||
return &System {
|
return &System {
|
||||||
link: link,
|
link: link,
|
||||||
hierarchies: make(util.Set[*Hierarchy]),
|
hierarchies: make(ucontainer.Set[*Hierarchy]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import "image"
|
import "image"
|
||||||
|
import "unicode"
|
||||||
import "image/color"
|
import "image/color"
|
||||||
import "golang.org/x/image/font"
|
import "golang.org/x/image/font"
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
@@ -11,6 +12,7 @@ import "git.tebibyte.media/tomo/tomo/input"
|
|||||||
import "git.tebibyte.media/tomo/tomo/event"
|
import "git.tebibyte.media/tomo/tomo/event"
|
||||||
import "git.tebibyte.media/tomo/tomo/canvas"
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||||
import "git.tebibyte.media/tomo/backend/internal/util"
|
import "git.tebibyte.media/tomo/backend/internal/util"
|
||||||
|
import "git.tebibyte.media/sashakoshka/goutil/container"
|
||||||
|
|
||||||
type textBox struct {
|
type textBox struct {
|
||||||
*box
|
*box
|
||||||
@@ -31,9 +33,11 @@ type textBox struct {
|
|||||||
selecting bool
|
selecting bool
|
||||||
selectStart int
|
selectStart int
|
||||||
dot text.Dot
|
dot text.Dot
|
||||||
|
desiredX fixed.Int26_6
|
||||||
|
|
||||||
drawer typeset.Drawer
|
drawer typeset.Drawer
|
||||||
face util.Cycler[font.Face]
|
face util.Cycler[font.Face]
|
||||||
|
lineHeight ucontainer.Memo[fixed.Int26_6]
|
||||||
|
|
||||||
on struct {
|
on struct {
|
||||||
contentBoundsChange event.FuncBroadcaster
|
contentBoundsChange event.FuncBroadcaster
|
||||||
@@ -46,6 +50,12 @@ func (this *System) NewTextBox () tomo.TextBox {
|
|||||||
box.box = this.newBox(box)
|
box.box = this.newBox(box)
|
||||||
box.attrTextColor.SetFallback(tomo.ATextColor(color.Black))
|
box.attrTextColor.SetFallback(tomo.ATextColor(color.Black))
|
||||||
box.attrDotColor.SetFallback(tomo.ADotColor(color.RGBA { G: 255, B: 255, A: 255}))
|
box.attrDotColor.SetFallback(tomo.ADotColor(color.RGBA { G: 255, B: 255, A: 255}))
|
||||||
|
box.lineHeight = ucontainer.NewMemo(func () fixed.Int26_6 {
|
||||||
|
face := box.face.Value()
|
||||||
|
if face == nil { return 0 }
|
||||||
|
metrics := face.Metrics()
|
||||||
|
return metrics.Height
|
||||||
|
})
|
||||||
return box
|
return box
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,13 +99,41 @@ func (this *textBox) SetSelectable (selectable bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *textBox) Select (dot text.Dot) {
|
func (this *textBox) Select (dot text.Dot) {
|
||||||
if !this.selectable { return }
|
this.selec(dot)
|
||||||
if this.dot == dot { return }
|
}
|
||||||
|
|
||||||
|
func (this *textBox) selec (dot text.Dot) bool {
|
||||||
|
if this.selectWithoutResettingDesiredX(dot) {
|
||||||
|
this.desiredX = fixed.I(0)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *textBox) selectWithoutResettingDesiredX (dot text.Dot) bool {
|
||||||
|
if !this.selectable { return false }
|
||||||
|
if this.dot == dot { return false }
|
||||||
this.SetFocused(true)
|
this.SetFocused(true)
|
||||||
this.dot = dot
|
this.dot = dot
|
||||||
this.scrollToDot()
|
this.scrollToDot()
|
||||||
this.on.dotChange.Broadcast()
|
|
||||||
this.invalidateDraw()
|
this.invalidateDraw()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *textBox) userSelect (dot text.Dot) bool {
|
||||||
|
if this.selec(dot) {
|
||||||
|
this.on.dotChange.Broadcast()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *textBox) userSelectWithoutResettingDesiredX (dot text.Dot) bool {
|
||||||
|
if this.selectWithoutResettingDesiredX(dot) {
|
||||||
|
this.on.dotChange.Broadcast()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *textBox) Dot () text.Dot {
|
func (this *textBox) Dot () text.Dot {
|
||||||
@@ -239,18 +277,17 @@ func (this *textBox) drawDot (can canvas.Canvas) {
|
|||||||
|
|
||||||
bounds := this.InnerBounds()
|
bounds := this.InnerBounds()
|
||||||
metrics := face.Metrics()
|
metrics := face.Metrics()
|
||||||
dot := this.dot.Canon()
|
dot := this.dot
|
||||||
start := this.drawer.PositionAt(dot.Start).Add(fixPt(this.textOffset()))
|
canonDot := dot.Canon()
|
||||||
end := this.drawer.PositionAt(dot.End ).Add(fixPt(this.textOffset()))
|
start := this.drawer.PositionAt(canonDot.Start).Add(fixPt(this.textOffset()))
|
||||||
|
end := this.drawer.PositionAt(canonDot.End ).Add(fixPt(this.textOffset()))
|
||||||
|
canonEnd := this.drawer.PositionAt(dot.End ).Add(fixPt(this.textOffset()))
|
||||||
height := this.drawer.LineHeight().Round()
|
height := this.drawer.LineHeight().Round()
|
||||||
ascent := fixed.Point26_6 { Y: metrics.Descent }
|
ascent := fixed.Point26_6 { Y: metrics.Descent }
|
||||||
descent := fixed.Point26_6 { Y: metrics.Ascent }
|
descent := fixed.Point26_6 { Y: metrics.Ascent }
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case dot.Empty():
|
case canonDot.Empty():
|
||||||
pen.Stroke(textColor)
|
|
||||||
pen.StrokeWeight(1)
|
|
||||||
pen.Path(roundPt(start.Add(ascent)), roundPt(start.Sub(descent)))
|
|
||||||
|
|
||||||
case start.Y == end.Y:
|
case start.Y == end.Y:
|
||||||
pen.Fill(dotColor)
|
pen.Fill(dotColor)
|
||||||
@@ -284,6 +321,19 @@ func (this *textBox) drawDot (can canvas.Canvas) {
|
|||||||
rect.Min.X = bounds.Min.X
|
rect.Min.X = bounds.Min.X
|
||||||
pen.Rectangle(rect)
|
pen.Rectangle(rect)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pen.Stroke(textColor)
|
||||||
|
pen.StrokeWeight(1)
|
||||||
|
beamTop := roundPt(canonEnd.Add(ascent)).Sub(image.Pt(0, 1))
|
||||||
|
beamBottom := roundPt(canonEnd.Sub(descent))
|
||||||
|
beamSerif := 3
|
||||||
|
pen.Path(beamTop, beamBottom)
|
||||||
|
pen.Path (
|
||||||
|
beamTop.Sub(image.Pt(beamSerif - 1, 0)),
|
||||||
|
beamTop.Add(image.Pt(beamSerif, 0)))
|
||||||
|
pen.Path (
|
||||||
|
beamBottom.Sub(image.Pt(beamSerif - 1, 0)),
|
||||||
|
beamBottom.Add(image.Pt(beamSerif, 0)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *textBox) textOffset () image.Point {
|
func (this *textBox) textOffset () image.Point {
|
||||||
@@ -307,7 +357,7 @@ func (this *textBox) handleMouseDown (button input.Button) bool {
|
|||||||
index := this.runeUnderMouse()
|
index := this.runeUnderMouse()
|
||||||
this.selectStart = index
|
this.selectStart = index
|
||||||
this.selecting = true
|
this.selecting = true
|
||||||
this.Select(text.Dot { Start: this.selectStart, End: index })
|
this.userSelect(text.Dot { Start: this.selectStart, End: index })
|
||||||
}
|
}
|
||||||
return this.box.handleMouseDown(button)
|
return this.box.handleMouseDown(button)
|
||||||
}
|
}
|
||||||
@@ -316,7 +366,7 @@ func (this *textBox) handleMouseUp (button input.Button) bool {
|
|||||||
if this.mouseButtonCanDrag(button) && this.selecting {
|
if this.mouseButtonCanDrag(button) && this.selecting {
|
||||||
index := this.runeUnderMouse()
|
index := this.runeUnderMouse()
|
||||||
this.selecting = false
|
this.selecting = false
|
||||||
this.Select(text.Dot { Start: this.selectStart, End: index })
|
this.userSelect(text.Dot { Start: this.selectStart, End: index })
|
||||||
}
|
}
|
||||||
return this.box.handleMouseUp(button)
|
return this.box.handleMouseUp(button)
|
||||||
}
|
}
|
||||||
@@ -330,7 +380,7 @@ func (this *textBox) mouseButtonCanDrag (button input.Button) bool {
|
|||||||
func (this *textBox) handleMouseMove () bool {
|
func (this *textBox) handleMouseMove () bool {
|
||||||
if this.selecting {
|
if this.selecting {
|
||||||
index := this.runeUnderMouse()
|
index := this.runeUnderMouse()
|
||||||
this.Select(text.Dot { Start: this.selectStart, End: index })
|
this.userSelect(text.Dot { Start: this.selectStart, End: index })
|
||||||
}
|
}
|
||||||
return this.box.handleMouseMove()
|
return this.box.handleMouseMove()
|
||||||
}
|
}
|
||||||
@@ -342,6 +392,8 @@ func (this *textBox) runeUnderMouse () int {
|
|||||||
return this.drawer.AtPosition(fixPt(position))
|
return this.drawer.AtPosition(fixPt(position))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO the keynav here should make better use of input key chords.
|
||||||
|
|
||||||
func (this *textBox) handleKeyDown (key input.Key, numberPad bool) bool {
|
func (this *textBox) handleKeyDown (key input.Key, numberPad bool) bool {
|
||||||
if this.box.handleKeyDown(key, numberPad) { return true }
|
if this.box.handleKeyDown(key, numberPad) { return true }
|
||||||
if !this.selectable { return false }
|
if !this.selectable { return false }
|
||||||
@@ -349,37 +401,87 @@ func (this *textBox) handleKeyDown (key input.Key, numberPad bool) bool {
|
|||||||
// because fuck you thats why!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
// because fuck you thats why!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
modifiers := this.Window().Modifiers()
|
modifiers := this.Window().Modifiers()
|
||||||
dot := this.Dot()
|
dot := this.Dot()
|
||||||
sel := modifiers.Shift
|
sel := modifiers.Shift()
|
||||||
word := modifiers.Control
|
word := modifiers.Control()
|
||||||
|
|
||||||
|
moveVertically := func (delta fixed.Int26_6) {
|
||||||
|
currentDot := 0
|
||||||
|
if sel {
|
||||||
|
currentDot = dot.End
|
||||||
|
} else {
|
||||||
|
currentDot = dot.Canon().Start
|
||||||
|
if delta > fixed.I(0) { currentDot = dot.Canon().End }
|
||||||
|
}
|
||||||
|
|
||||||
|
nextDot := 0
|
||||||
|
if word {
|
||||||
|
if delta > fixed.I(0) {
|
||||||
|
nextDot = nextParagraph(this.runes, currentDot)
|
||||||
|
} else {
|
||||||
|
nextDot = previousParagraph(this.runes, currentDot)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
currentPosition := this.drawer.PositionAt(currentDot)
|
||||||
|
if this.desiredX != fixed.I(0) {
|
||||||
|
currentPosition.X = this.desiredX
|
||||||
|
}
|
||||||
|
nextPosition := currentPosition
|
||||||
|
nextPosition.Y += this.lineHeight.Value().Mul(delta)
|
||||||
|
this.desiredX = nextPosition.X
|
||||||
|
nextDot = this.drawer.AtPosition(nextPosition)
|
||||||
|
}
|
||||||
|
|
||||||
|
if sel {
|
||||||
|
dot.End = nextDot
|
||||||
|
this.userSelectWithoutResettingDesiredX(dot)
|
||||||
|
} else {
|
||||||
|
this.userSelectWithoutResettingDesiredX(text.EmptyDot(nextDot))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case key == input.KeyHome || (modifiers.Alt && key == input.KeyLeft):
|
case key == input.KeyHome || (modifiers.Alt() && key == input.KeyLeft):
|
||||||
|
if word {
|
||||||
dot.End = 0
|
dot.End = 0
|
||||||
|
} else {
|
||||||
|
dot.End = lineHomeSoft(this.runes, dot.End)
|
||||||
|
}
|
||||||
if !sel { dot.Start = dot.End }
|
if !sel { dot.Start = dot.End }
|
||||||
this.Select(dot)
|
this.userSelect(dot)
|
||||||
return true
|
return true
|
||||||
case key == input.KeyEnd || (modifiers.Alt && key == input.KeyRight):
|
case key == input.KeyEnd || (modifiers.Alt() && key == input.KeyRight):
|
||||||
dot.End = len(this.text)
|
if word {
|
||||||
|
dot.End = len(this.runes)
|
||||||
|
} else {
|
||||||
|
dot.End = lineEnd(this.runes, dot.End)
|
||||||
|
}
|
||||||
if !sel { dot.Start = dot.End }
|
if !sel { dot.Start = dot.End }
|
||||||
this.Select(dot)
|
this.userSelect(dot)
|
||||||
return true
|
return true
|
||||||
case key == input.KeyLeft:
|
case key == input.KeyLeft:
|
||||||
if sel {
|
if sel {
|
||||||
this.Select(text.SelectLeft(this.runes, dot, word))
|
this.userSelect(text.SelectLeft(this.runes, dot, word))
|
||||||
} else {
|
} else {
|
||||||
this.Select(text.MoveLeft(this.runes, dot, word))
|
this.userSelect(text.MoveLeft(this.runes, dot, word))
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
case key == input.KeyRight:
|
case key == input.KeyRight:
|
||||||
if sel {
|
if sel {
|
||||||
this.Select(text.SelectRight(this.runes, dot, word))
|
this.userSelect(text.SelectRight(this.runes, dot, word))
|
||||||
} else {
|
} else {
|
||||||
this.Select(text.MoveRight(this.runes, dot, word))
|
this.userSelect(text.MoveRight(this.runes, dot, word))
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
case key == input.Key('a') && modifiers.Control:
|
case key == input.KeyUp:
|
||||||
|
moveVertically(fixed.I(-1))
|
||||||
|
return true
|
||||||
|
case key == input.KeyDown:
|
||||||
|
moveVertically(fixed.I(1))
|
||||||
|
return true
|
||||||
|
case key == input.Key('a') && modifiers.Control():
|
||||||
dot.Start = 0
|
dot.Start = 0
|
||||||
dot.End = len(this.text)
|
dot.End = len(this.runes)
|
||||||
this.Select(dot)
|
this.userSelect(dot)
|
||||||
return true
|
return true
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
@@ -392,15 +494,19 @@ func (this *textBox) handleKeyUp (key input.Key, numberPad bool) bool {
|
|||||||
|
|
||||||
modifiers := this.Window().Modifiers()
|
modifiers := this.Window().Modifiers()
|
||||||
switch {
|
switch {
|
||||||
case key == input.KeyHome || (modifiers.Alt && key == input.KeyLeft):
|
case key == input.KeyHome || (modifiers.Alt() && key == input.KeyLeft):
|
||||||
return true
|
return true
|
||||||
case key == input.KeyEnd || (modifiers.Alt && key == input.KeyRight):
|
case key == input.KeyEnd || (modifiers.Alt() && key == input.KeyRight):
|
||||||
|
return true
|
||||||
|
case key == input.KeyUp:
|
||||||
|
return true
|
||||||
|
case key == input.KeyDown:
|
||||||
return true
|
return true
|
||||||
case key == input.KeyLeft:
|
case key == input.KeyLeft:
|
||||||
return true
|
return true
|
||||||
case key == input.KeyRight:
|
case key == input.KeyRight:
|
||||||
return true
|
return true
|
||||||
case key == input.Key('a') && modifiers.Control:
|
case key == input.Key('a') && modifiers.Control():
|
||||||
return true
|
return true
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
@@ -502,6 +608,7 @@ func (this *textBox) handleFaceChange () {
|
|||||||
this.drawer.SetFace(face)
|
this.drawer.SetFace(face)
|
||||||
this.invalidateMinimum()
|
this.invalidateMinimum()
|
||||||
this.invalidateLayout()
|
this.invalidateLayout()
|
||||||
|
this.lineHeight.Invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *textBox) recursiveReApply () {
|
func (this *textBox) recursiveReApply () {
|
||||||
@@ -523,3 +630,70 @@ func (this *textBox) recursiveReApply () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: these two functions really could be better.
|
||||||
|
|
||||||
|
func previousParagraph (text []rune, index int) int {
|
||||||
|
consecLF := 0
|
||||||
|
if index >= len(text) { index = len(text) - 1 }
|
||||||
|
for ; index > 0; index -- {
|
||||||
|
char := text[index]
|
||||||
|
if char == '\n' {
|
||||||
|
consecLF ++
|
||||||
|
} else if !unicode.IsSpace(char) {
|
||||||
|
if consecLF >= 2 { return index + 1 }
|
||||||
|
consecLF = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
|
||||||
|
func nextParagraph (text []rune, index int) int {
|
||||||
|
consecLF := 0
|
||||||
|
for ; index < len(text); index ++ {
|
||||||
|
char := text[index]
|
||||||
|
if char == '\n' {
|
||||||
|
consecLF ++
|
||||||
|
} else if !unicode.IsSpace(char) {
|
||||||
|
if consecLF >= 2 { return index }
|
||||||
|
consecLF = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
|
||||||
|
func lineHome (text []rune, index int) int {
|
||||||
|
liminal := index < len(text) && text[index] == '\n'
|
||||||
|
if index >= len(text) { index = len(text) - 1 }
|
||||||
|
for index := index; index >= 0; index -- {
|
||||||
|
char := text[index]
|
||||||
|
if char == '\n' && !liminal {
|
||||||
|
return index + 1
|
||||||
|
}
|
||||||
|
liminal = false
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func lineHomeSoft (text []rune, index int) int {
|
||||||
|
home := lineHome(text, index)
|
||||||
|
start := home
|
||||||
|
for start < len(text) && unicode.IsSpace(text[start]) {
|
||||||
|
start ++
|
||||||
|
}
|
||||||
|
if index == start {
|
||||||
|
return home
|
||||||
|
} else {
|
||||||
|
return start
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func lineEnd (text []rune, index int) int {
|
||||||
|
for ; index < len(text); index ++ {
|
||||||
|
char := text[index]
|
||||||
|
if char == '\n' {
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,100 +1,6 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import "io"
|
import "io"
|
||||||
import "image/color"
|
|
||||||
|
|
||||||
// IndexOf returns the index of needle within haystack. If needle does not exist
|
|
||||||
// within haystack, it returns -1.
|
|
||||||
func IndexOf[T comparable] (haystack []T, needle T) int {
|
|
||||||
for index, test := range haystack {
|
|
||||||
if test == needle {
|
|
||||||
return index
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove removes an element from slice at index.
|
|
||||||
func Remove[T any] (slice []T, index int) []T {
|
|
||||||
return append(slice[:index], slice[index + 1:]...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert inserts an element into slice at index.
|
|
||||||
func Insert[T any] (slice []T, index int, element T) []T {
|
|
||||||
slice = append(slice[:index + 1], slice[index:]...)
|
|
||||||
slice[index] = element
|
|
||||||
return slice
|
|
||||||
}
|
|
||||||
|
|
||||||
// Transparent returns whether or not a color has transparency.
|
|
||||||
func Transparent (c color.Color) bool {
|
|
||||||
_, _, _, a := c.RGBA()
|
|
||||||
return a != 0xFFFF
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set is a set of unique items, built on top of map.
|
|
||||||
type Set[T comparable] map[T] struct { }
|
|
||||||
|
|
||||||
// Empty returns true if there are no items in the set.
|
|
||||||
func (set Set[T]) Empty () bool {
|
|
||||||
return set == nil || len(set) == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Has returns true if the set contains item.
|
|
||||||
func (set Set[T]) Has (item T) bool {
|
|
||||||
if set == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
_, ok := set[item]
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add adds an item to the set.
|
|
||||||
func (set Set[T]) Add (item T) {
|
|
||||||
set[item] = struct { } { }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pop removes the first accessible item from the set and returns it.
|
|
||||||
func (set Set[T]) Pop () (item T) {
|
|
||||||
for item := range set {
|
|
||||||
delete(set, item)
|
|
||||||
return item
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Memo holds a cached value.
|
|
||||||
type Memo[T any] struct {
|
|
||||||
cache T
|
|
||||||
valid bool
|
|
||||||
update func () T
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMemo creates a new Memo which will take its value from the specified
|
|
||||||
// update callback.
|
|
||||||
func NewMemo[T any] (update func () T) Memo[T] {
|
|
||||||
return Memo[T] {
|
|
||||||
update: update,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Value returns the Memo's value, updating it if the current cached value is
|
|
||||||
// invalid.
|
|
||||||
func (this *Memo[T]) Value () T {
|
|
||||||
if !this.valid {
|
|
||||||
this.cache = this.update()
|
|
||||||
this.valid = true
|
|
||||||
}
|
|
||||||
return this.cache
|
|
||||||
}
|
|
||||||
|
|
||||||
// Invalidate marks the Memo's value as invalid, which will cause it to be
|
|
||||||
// updated the next time Value is called.
|
|
||||||
func (this *Memo[T]) Invalidate () {
|
|
||||||
var zero T
|
|
||||||
this.cache = zero
|
|
||||||
this.valid = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cycler stores a value and an accompanying io.Closer. When the value is set,
|
// Cycler stores a value and an accompanying io.Closer. When the value is set,
|
||||||
// the closer associated with the previous value is closed.
|
// the closer associated with the previous value is closed.
|
||||||
@@ -124,33 +30,3 @@ func (this *Cycler[T]) Close () error {
|
|||||||
this.closer = nil
|
this.closer = nil
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional is an optional value.
|
|
||||||
type Optional[T any] struct {
|
|
||||||
value T
|
|
||||||
exists bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Value returns the value and true if the value exists. If not, it returns the
|
|
||||||
// last set value and false.
|
|
||||||
func (this *Optional[T]) Value () (T, bool) {
|
|
||||||
return this.value, this.exists
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets the value.
|
|
||||||
func (this *Optional[T]) Set (value T) {
|
|
||||||
this.value = value
|
|
||||||
this.exists = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unset unsets the value.
|
|
||||||
func (this *Optional[T]) Unset () {
|
|
||||||
var zero T
|
|
||||||
this.value = zero
|
|
||||||
this.exists = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exists returns if the value is currently set.
|
|
||||||
func (this *Optional[T]) Exists () bool {
|
|
||||||
return this.exists
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user