Compare commits
63 Commits
Author | SHA1 | Date | |
---|---|---|---|
6d071bd1d3 | |||
dfc5c4514a | |||
51afca6a3f | |||
b58932f02b | |||
ee64650c19 | |||
55215dedc2 | |||
eb98d143db | |||
42deb40c2d | |||
b6850ee702 | |||
9d67013e33 | |||
0a8bb39265 | |||
b92308fc80 | |||
6f9eca99e7 | |||
381f5f88bd | |||
1c38ed2d87 | |||
9079aca993 | |||
70d6759884 | |||
2f828b1ae8 | |||
c1c0d2125d | |||
3b4ab56914 | |||
e7f16645eb | |||
ccbbb735fd | |||
ab6bdeaba3 | |||
93d7eed21f | |||
b18f747f0c | |||
fa2ef954b2 | |||
e4fdde3da1 | |||
d166d88388 | |||
74025aac97 | |||
e1cf524c57 | |||
919f000073 | |||
8aa8dc9570 | |||
a60a729ad9 | |||
2af42a3568 | |||
e2b3b84993 | |||
925e011465 | |||
d4c08a0f8c | |||
38054a95bb | |||
2ae5e2e30f | |||
bacdd81f60 | |||
d944f6016f | |||
01582d4ad1 | |||
3941dae44a | |||
85b8536925 | |||
6ff5dea308 | |||
33969f45e9 | |||
832d7e02ef | |||
fd6297b4fb | |||
4deb581667 | |||
192e6c6235 | |||
9729e3dfda | |||
fad46eafd3 | |||
ddde2a79a8 | |||
180a5eb8d1 | |||
a92951f891 | |||
37ec962d1f | |||
4f89b11799 | |||
7809aac72f | |||
bb082d3989 | |||
fbb6d61cfc | |||
e4cba4a7c9 | |||
6192a1e9cc | |||
5864c74691 |
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.41.0
|
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.41.0 h1:Z+7FHhbGiKjs+kQNvuJOfz47xIct5qxvSJqyDuoNIOs=
|
git.tebibyte.media/tomo/tomo v0.48.0 h1:AE21ElHwUSPsX82ZWCnoNxJFi9Oswyd3dPDPMbxTueQ=
|
||||||
git.tebibyte.media/tomo/tomo v0.41.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
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,26 +1,48 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
import "git.tebibyte.media/sashakoshka/goutil/container"
|
||||||
|
|
||||||
type attrHierarchy [T tomo.Attr] struct {
|
type attrHierarchy [T tomo.Attr] struct {
|
||||||
style T
|
fallback T
|
||||||
user T
|
style ucontainer.Optional[T]
|
||||||
userExists bool
|
user ucontainer.Optional[T]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *attrHierarchy[T]) SetFallback (fallback T) {
|
||||||
|
this.fallback = fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *attrHierarchy[T]) SetStyle (style T) (different bool) {
|
func (this *attrHierarchy[T]) SetStyle (style T) (different bool) {
|
||||||
styleEquals := this.style.Equals(style)
|
styleEquals := false
|
||||||
this.style = style
|
if previous, ok := this.style.Value(); ok {
|
||||||
return !styleEquals && !this.userExists
|
styleEquals = previous.Equals(style)
|
||||||
|
}
|
||||||
|
this.style.Set(style)
|
||||||
|
return !styleEquals && !this.user.Exists()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *attrHierarchy[T]) UnsetStyle () (different bool) {
|
||||||
|
different = this.style.Exists()
|
||||||
|
this.style.Unset()
|
||||||
|
return different
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *attrHierarchy[T]) SetUser (user T) (different bool) {
|
func (this *attrHierarchy[T]) SetUser (user T) (different bool) {
|
||||||
userEquals := this.user.Equals(user)
|
userEquals := false
|
||||||
this.user = user
|
if previous, ok := this.user.Value(); ok {
|
||||||
this.userExists = true
|
userEquals = previous.Equals(user)
|
||||||
|
}
|
||||||
|
this.user.Set(user)
|
||||||
return !userEquals
|
return !userEquals
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *attrHierarchy[T]) UnsetUser () (different bool) {
|
||||||
|
different = this.user.Exists()
|
||||||
|
this.user.Unset()
|
||||||
|
return different
|
||||||
|
}
|
||||||
|
|
||||||
func (this *attrHierarchy[T]) Set (attr T, user bool) (different bool) {
|
func (this *attrHierarchy[T]) Set (attr T, user bool) (different bool) {
|
||||||
if user {
|
if user {
|
||||||
return this.SetUser(attr)
|
return this.SetUser(attr)
|
||||||
@ -29,10 +51,20 @@ func (this *attrHierarchy[T]) Set (attr T, user bool) (different bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *attrHierarchy[T]) Value () T {
|
func (this *attrHierarchy[T]) Unset (user bool) (different bool) {
|
||||||
if this.userExists {
|
if user {
|
||||||
return this.user
|
return this.UnsetUser()
|
||||||
} else {
|
} else {
|
||||||
return this.style
|
return this.UnsetStyle()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *attrHierarchy[T]) Value () T {
|
||||||
|
if user, ok := this.user.Value(); ok {
|
||||||
|
return user
|
||||||
|
} else if style, ok := this.style.Value(); ok{
|
||||||
|
return style
|
||||||
|
} else {
|
||||||
|
return this.fallback
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
lastIconsNonce 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,8 +42,7 @@ type box struct {
|
|||||||
focused bool
|
focused bool
|
||||||
pressed bool
|
pressed bool
|
||||||
|
|
||||||
|
canvas ucontainer.Memo[canvas.Canvas]
|
||||||
canvas util.Memo[canvas.Canvas]
|
|
||||||
drawer canvas.Drawer
|
drawer canvas.Drawer
|
||||||
|
|
||||||
on struct {
|
on struct {
|
||||||
@ -69,18 +69,21 @@ func (this *System) newBox (outer anyBox) *box {
|
|||||||
system: this,
|
system: this,
|
||||||
outer: outer,
|
outer: outer,
|
||||||
drawer: outer,
|
drawer: outer,
|
||||||
|
tags: make(ucontainer.Set[string]),
|
||||||
}
|
}
|
||||||
box.canvas = util.NewMemo (func () canvas.Canvas {
|
box.attrColor.SetFallback(tomo.AColor(color.Transparent))
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,17 +132,25 @@ func (this *box) Tag (tag string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *box) SetTag (tag string, on bool) {
|
func (this *box) SetTag (tag string, on bool) {
|
||||||
|
wasOn := this.tags.Has(tag)
|
||||||
if on {
|
if on {
|
||||||
this.tags.Add(tag)
|
this.tags.Add(tag)
|
||||||
} else {
|
} else {
|
||||||
delete(this.tags, tag)
|
delete(this.tags, tag)
|
||||||
}
|
}
|
||||||
|
if wasOn != on {
|
||||||
|
this.invalidateStyle()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *box) SetAttr (attr tomo.Attr) {
|
func (this *box) SetAttr (attr tomo.Attr) {
|
||||||
this.outer.setAttr(attr, true)
|
this.outer.setAttr(attr, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *box) UnsetAttr (kind tomo.AttrKind) {
|
||||||
|
this.outer.unsetAttr(kind, true)
|
||||||
|
}
|
||||||
|
|
||||||
func (this *box) SetDNDData (dat data.Data) {
|
func (this *box) SetDNDData (dat data.Data) {
|
||||||
this.dndData = dat
|
this.dndData = dat
|
||||||
}
|
}
|
||||||
@ -200,18 +211,7 @@ func (this *box) setAttr (attr tomo.Attr, user bool) {
|
|||||||
case tomo.AttrBorder:
|
case tomo.AttrBorder:
|
||||||
previousBorderSum := this.borderSum()
|
previousBorderSum := this.borderSum()
|
||||||
different := this.attrBorder.Set(attr, user)
|
different := this.attrBorder.Set(attr, user)
|
||||||
|
this.handleBorderChange(previousBorderSum, different)
|
||||||
// only invalidate the layout if the border is sized differently
|
|
||||||
if this.borderSum() != previousBorderSum {
|
|
||||||
this.invalidateLayout()
|
|
||||||
this.invalidateMinimum()
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the border takes up the same amount of space, only invalidate the
|
|
||||||
// drawing if it looks different
|
|
||||||
if different {
|
|
||||||
this.invalidateDraw()
|
|
||||||
}
|
|
||||||
|
|
||||||
case tomo.AttrMinimumSize:
|
case tomo.AttrMinimumSize:
|
||||||
if this.attrMinimumSize.Set(attr, user) {
|
if this.attrMinimumSize.Set(attr, user) {
|
||||||
@ -219,13 +219,66 @@ func (this *box) setAttr (attr tomo.Attr, user bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case tomo.AttrPadding:
|
case tomo.AttrPadding:
|
||||||
if this.attrPadding.Set(attr, true) {
|
if this.attrPadding.Set(attr, user) {
|
||||||
this.invalidateLayout()
|
this.invalidateLayout()
|
||||||
this.invalidateMinimum()
|
this.invalidateMinimum()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *box) unsetAttr (kind tomo.AttrKind, user bool) {
|
||||||
|
switch kind {
|
||||||
|
case tomo.AttrKindColor:
|
||||||
|
if this.attrColor.Unset(user) {
|
||||||
|
this.invalidateDraw()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindTexture:
|
||||||
|
if this.attrTexture.Unset(user) {
|
||||||
|
this.invalidateDraw()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindTextureMode:
|
||||||
|
if this.attrTextureMode.Unset(user) {
|
||||||
|
this.invalidateDraw()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindBorder:
|
||||||
|
previousBorderSum := this.borderSum()
|
||||||
|
different := this.attrBorder.Unset(user)
|
||||||
|
this.handleBorderChange(previousBorderSum, different)
|
||||||
|
|
||||||
|
case tomo.AttrKindMinimumSize:
|
||||||
|
if this.attrMinimumSize.Unset(user) {
|
||||||
|
this.invalidateMinimum()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindPadding:
|
||||||
|
if this.attrPadding.Unset(user) {
|
||||||
|
this.invalidateLayout()
|
||||||
|
this.invalidateMinimum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
@ -335,13 +388,6 @@ func (this *box) handleMouseDown (button input.Button) (caught bool) {
|
|||||||
this.invalidateStyle()
|
this.invalidateStyle()
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.focusable {
|
|
||||||
this.SetFocused(true)
|
|
||||||
} else {
|
|
||||||
hierarchy := this.getHierarchy()
|
|
||||||
if hierarchy == nil { return }
|
|
||||||
hierarchy.focus(nil)
|
|
||||||
}
|
|
||||||
for _, listener := range this.on.buttonDown.Listeners() {
|
for _, listener := range this.on.buttonDown.Listeners() {
|
||||||
if listener(button) { caught = true }
|
if listener(button) { caught = true }
|
||||||
}
|
}
|
||||||
@ -401,22 +447,28 @@ func (this *box) Draw (can canvas.Canvas) {
|
|||||||
|
|
||||||
// centered texture
|
// centered texture
|
||||||
if textureMode == tomo.TextureModeCenter && texture != nil {
|
if textureMode == tomo.TextureModeCenter && texture != nil {
|
||||||
textureBounds := texture.Bounds()
|
this.centeredTexture(can, texture)
|
||||||
textureOrigin :=
|
|
||||||
bounds.Min.
|
|
||||||
Add(image.Pt (
|
|
||||||
bounds.Dx() / 2,
|
|
||||||
bounds.Dy() / 2)).
|
|
||||||
Sub(image.Pt (
|
|
||||||
textureBounds.Dx() / 2,
|
|
||||||
textureBounds.Dy() / 2))
|
|
||||||
|
|
||||||
pen.Fill(color.Transparent)
|
|
||||||
pen.Texture(texture)
|
|
||||||
pen.Rectangle(textureBounds.Sub(textureBounds.Min).Add(textureOrigin))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *box) centeredTexture (can canvas.Canvas, texture canvas.Texture) {
|
||||||
|
pen := can.Pen()
|
||||||
|
bounds := this.Bounds()
|
||||||
|
textureBounds := texture.Bounds()
|
||||||
|
textureOrigin :=
|
||||||
|
bounds.Min.
|
||||||
|
Add(image.Pt (
|
||||||
|
bounds.Dx() / 2,
|
||||||
|
bounds.Dy() / 2)).
|
||||||
|
Sub(image.Pt (
|
||||||
|
textureBounds.Dx() / 2,
|
||||||
|
textureBounds.Dy() / 2))
|
||||||
|
|
||||||
|
pen.Fill(color.Transparent)
|
||||||
|
pen.Texture(texture)
|
||||||
|
pen.Rectangle(textureBounds.Sub(textureBounds.Min).Add(textureOrigin))
|
||||||
|
}
|
||||||
|
|
||||||
func (this *box) drawBorders (can canvas.Canvas) {
|
func (this *box) drawBorders (can canvas.Canvas) {
|
||||||
if can == nil { return }
|
if can == nil { return }
|
||||||
pen := can.Pen()
|
pen := can.Pen()
|
||||||
@ -425,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)
|
||||||
@ -483,9 +535,6 @@ func (this *box) calculateMinimumSize () image.Point {
|
|||||||
minSize.Y = userMinSize.Y
|
minSize.Y = userMinSize.Y
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.parent != nil {
|
|
||||||
this.parent.notifyMinimumSizeChange(this)
|
|
||||||
}
|
|
||||||
return minSize
|
return minSize
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -512,7 +561,7 @@ func (this *box) doLayout () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *box) doStyle () {
|
func (this *box) doStyle () {
|
||||||
this.styleApplicator.apply(this)
|
this.styleApplicator.apply(this.outer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *box) setParent (parent parent) {
|
func (this *box) setParent (parent parent) {
|
||||||
@ -540,7 +589,21 @@ func (this *box) recursiveRedo () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *box) recursiveLoseCanvas () {
|
func (this *box) recursiveLoseCanvas () {
|
||||||
this.canvas.InvalidateTo(nil)
|
this.canvas.Invalidate()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *box) handleBorderChange (previousBorderSum tomo.Inset, different bool) {
|
||||||
|
// only invalidate the layout if the border is sized differently
|
||||||
|
if this.borderSum() != previousBorderSum {
|
||||||
|
this.invalidateLayout()
|
||||||
|
this.invalidateMinimum()
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the border takes up the same amount of space, only invalidate the
|
||||||
|
// drawing if it looks different
|
||||||
|
if different {
|
||||||
|
this.invalidateDraw()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *box) invalidateStyle () {
|
func (this *box) invalidateStyle () {
|
||||||
@ -563,10 +626,14 @@ func (this *box) invalidateDraw () {
|
|||||||
|
|
||||||
func (this *box) invalidateMinimum () {
|
func (this *box) invalidateMinimum () {
|
||||||
this.minSize.Invalidate()
|
this.minSize.Invalidate()
|
||||||
|
if this.parent != nil {
|
||||||
|
this.parent.notifyMinimumSizeChange(this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *box) recursiveReApply () {
|
func (this *box) recursiveReApply () {
|
||||||
if this.getHierarchy() == nil { return }
|
hierarchy := this.getHierarchy()
|
||||||
|
if hierarchy == nil { return }
|
||||||
|
|
||||||
// re-apply styling, icons *if needed*
|
// re-apply styling, icons *if needed*
|
||||||
|
|
||||||
@ -577,16 +644,17 @@ 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 = this.getHierarchy().newStyleApplicator()
|
this.styleApplicator = hierarchy.newStyleApplicator()
|
||||||
this.invalidateStyle()
|
this.invalidateStyle()
|
||||||
this.on.styleChange.Broadcast()
|
this.on.styleChange.Broadcast()
|
||||||
}
|
}
|
||||||
|
|
||||||
// icons
|
// icons
|
||||||
hierarchyIconsNonce := this.getIconsNonce()
|
hierarchyIconSetNonce := this.getIconSetNonce()
|
||||||
if this.lastIconsNonce != hierarchyIconsNonce {
|
if this.lastIconSetNonce != hierarchyIconSetNonce {
|
||||||
this.lastIconsNonce = hierarchyIconsNonce
|
this.lastIconSetNonce = hierarchyIconSetNonce
|
||||||
this.on.iconSetChange.Broadcast()
|
this.on.iconSetChange.Broadcast()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -614,7 +682,8 @@ func (this *box) propagateAlt (callback func (anyBox) bool) bool {
|
|||||||
func (this *box) transparent () bool {
|
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
|
||||||
return util.Transparent(this.attrColor.Value()) /*&&
|
col := this.attrColor.Value().Color
|
||||||
|
return col == nil || ucolor.Transparent(col) /*&&
|
||||||
(this.texture == nil || !this.texture.Opaque())*/
|
(this.texture == nil || !this.texture.Opaque())*/
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -634,7 +703,7 @@ func (this *box) getStyleNonce () int {
|
|||||||
return this.getHierarchy().getStyleNonce()
|
return this.getHierarchy().getStyleNonce()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *box) getIconsNonce () int {
|
func (this *box) getIconSetNonce () int {
|
||||||
// should panic if not in the tree
|
// should panic if not in the tree
|
||||||
return this.getHierarchy().getIconsNonce()
|
return this.getHierarchy().getIconSetNonce()
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ func (querier boxQuerier) RecommendedWidth (index int, height int) int {
|
|||||||
if box, ok := box.(anyContentBox); ok {
|
if box, ok := box.(anyContentBox); ok {
|
||||||
return box.recommendedWidth(height)
|
return box.recommendedWidth(height)
|
||||||
}
|
}
|
||||||
|
return box.minimumSize().X
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -29,6 +30,7 @@ func (querier boxQuerier) RecommendedHeight (index int, width int) int {
|
|||||||
if box, ok := box.(anyContentBox); ok {
|
if box, ok := box.(anyContentBox); ok {
|
||||||
return box.recommendedHeight(width)
|
return box.recommendedHeight(width)
|
||||||
}
|
}
|
||||||
|
return box.minimumSize().Y
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
@ -20,7 +20,6 @@ type containerBox struct {
|
|||||||
attrLayout attrHierarchy[tomo.AttrLayout]
|
attrLayout attrHierarchy[tomo.AttrLayout]
|
||||||
|
|
||||||
children []anyBox
|
children []anyBox
|
||||||
layout tomo.Layout
|
|
||||||
|
|
||||||
on struct {
|
on struct {
|
||||||
contentBoundsChange event.FuncBroadcaster
|
contentBoundsChange event.FuncBroadcaster
|
||||||
@ -51,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()
|
||||||
@ -62,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 + 1)
|
||||||
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)
|
||||||
|
|
||||||
@ -174,6 +173,7 @@ func (this *containerBox) setAttr (attr tomo.Attr, user bool) {
|
|||||||
case tomo.AttrOverflow:
|
case tomo.AttrOverflow:
|
||||||
if this.attrOverflow.Set(attr, user) {
|
if this.attrOverflow.Set(attr, user) {
|
||||||
this.invalidateLayout()
|
this.invalidateLayout()
|
||||||
|
this.invalidateMinimum()
|
||||||
}
|
}
|
||||||
|
|
||||||
case tomo.AttrLayout:
|
case tomo.AttrLayout:
|
||||||
@ -186,20 +186,69 @@ func (this *containerBox) setAttr (attr tomo.Attr, user bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *containerBox) unsetAttr (kind tomo.AttrKind, user bool) {
|
||||||
|
switch kind {
|
||||||
|
case tomo.AttrKindColor:
|
||||||
|
if this.attrColor.Unset(user) {
|
||||||
|
this.invalidateTransparentChildren()
|
||||||
|
this.invalidateDraw()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindTexture:
|
||||||
|
if this.attrTexture.Unset(user) {
|
||||||
|
this.invalidateTransparentChildren()
|
||||||
|
this.invalidateDraw()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindTextureMode:
|
||||||
|
if this.attrTextureMode.Unset(user) {
|
||||||
|
this.invalidateTransparentChildren()
|
||||||
|
this.invalidateDraw()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindGap:
|
||||||
|
if this.attrGap.Unset(user) {
|
||||||
|
this.invalidateLayout()
|
||||||
|
this.invalidateMinimum()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindAlign:
|
||||||
|
if this.attrAlign.Unset(user) {
|
||||||
|
this.invalidateLayout()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindOverflow:
|
||||||
|
if this.attrOverflow.Unset(user) {
|
||||||
|
this.invalidateLayout()
|
||||||
|
this.invalidateMinimum()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindLayout:
|
||||||
|
if this.attrLayout.Unset(user) {
|
||||||
|
this.invalidateLayout()
|
||||||
|
this.invalidateMinimum()
|
||||||
|
}
|
||||||
|
|
||||||
|
default: this.box.unsetAttr(kind, user)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (this *containerBox) recommendedHeight (width int) int {
|
func (this *containerBox) recommendedHeight (width int) int {
|
||||||
if this.layout == nil || this.attrOverflow.Value().Y {
|
layout := this.attrLayout.Value().Layout
|
||||||
|
if layout == nil || !this.attrOverflow.Value().Y {
|
||||||
return this.minSize.Value().Y
|
return this.minSize.Value().Y
|
||||||
} else {
|
} else {
|
||||||
return this.layout.RecommendedHeight(this.layoutHints(), this.boxQuerier(), width) +
|
return layout.RecommendedHeight(this.layoutHints(), this.boxQuerier(), width) +
|
||||||
this.borderAndPaddingSum().Vertical()
|
this.borderAndPaddingSum().Vertical()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *containerBox) recommendedWidth (height int) int {
|
func (this *containerBox) recommendedWidth (height int) int {
|
||||||
if this.layout == nil || this.attrOverflow.Value().X {
|
layout := this.attrLayout.Value().Layout
|
||||||
|
if layout == nil || !this.attrOverflow.Value().X {
|
||||||
return this.minSize.Value().X
|
return this.minSize.Value().X
|
||||||
} else {
|
} else {
|
||||||
return this.layout.RecommendedWidth(this.layoutHints(), this.boxQuerier(), height) +
|
return layout.RecommendedWidth(this.layoutHints(), this.boxQuerier(), height) +
|
||||||
this.borderAndPaddingSum().Horizontal()
|
this.borderAndPaddingSum().Horizontal()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -249,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()
|
||||||
@ -274,8 +327,9 @@ func (this *containerBox) layoutHints () tomo.LayoutHints {
|
|||||||
func (this *containerBox) contentMinimum () image.Point {
|
func (this *containerBox) contentMinimum () image.Point {
|
||||||
overflow := this.attrOverflow.Value()
|
overflow := this.attrOverflow.Value()
|
||||||
minimum := this.box.contentMinimum()
|
minimum := this.box.contentMinimum()
|
||||||
if this.layout != nil {
|
layout := this.attrLayout.Value().Layout
|
||||||
layoutMinimum := this.layout.MinimumSize (
|
if layout != nil {
|
||||||
|
layoutMinimum := layout.MinimumSize (
|
||||||
this.layoutHints(),
|
this.layoutHints(),
|
||||||
this.boxQuerier())
|
this.boxQuerier())
|
||||||
if overflow.X { layoutMinimum.X = 0 }
|
if overflow.X { layoutMinimum.X = 0 }
|
||||||
@ -288,12 +342,13 @@ func (this *containerBox) contentMinimum () image.Point {
|
|||||||
func (this *containerBox) doLayout () {
|
func (this *containerBox) doLayout () {
|
||||||
this.box.doLayout()
|
this.box.doLayout()
|
||||||
previousContentBounds := this.contentBounds
|
previousContentBounds := this.contentBounds
|
||||||
|
layout := this.attrLayout.Value().Layout
|
||||||
|
|
||||||
// by default, use innerBounds (translated to 0, 0) for contentBounds.
|
// by default, use innerBounds (translated to 0, 0) for contentBounds.
|
||||||
// if a direction overflows, use the layout's minimum size for it.
|
// if a direction overflows, use the layout's minimum size for it.
|
||||||
var minimum image.Point
|
var minimum image.Point
|
||||||
if this.layout != nil {
|
if layout != nil {
|
||||||
minimum = this.layout.MinimumSize (
|
minimum = layout.MinimumSize (
|
||||||
this.layoutHints(),
|
this.layoutHints(),
|
||||||
this.boxQuerier())
|
this.boxQuerier())
|
||||||
}
|
}
|
||||||
@ -304,10 +359,10 @@ func (this *containerBox) doLayout () {
|
|||||||
if overflow.Y { this.contentBounds.Max.Y = this.contentBounds.Min.Y + minimum.Y }
|
if overflow.Y { this.contentBounds.Max.Y = this.contentBounds.Min.Y + minimum.Y }
|
||||||
|
|
||||||
// arrange children
|
// arrange children
|
||||||
if this.layout != nil {
|
if layout != nil {
|
||||||
layoutHints := this.layoutHints()
|
layoutHints := this.layoutHints()
|
||||||
layoutHints.Bounds = this.contentBounds
|
layoutHints.Bounds = this.contentBounds
|
||||||
this.layout.Arrange(layoutHints, this.boxArranger())
|
layout.Arrange(layoutHints, this.boxArranger())
|
||||||
}
|
}
|
||||||
|
|
||||||
// build an accurate contentBounds by unioning the bounds of all child
|
// build an accurate contentBounds by unioning the bounds of all child
|
||||||
@ -323,7 +378,7 @@ func (this *containerBox) doLayout () {
|
|||||||
|
|
||||||
// offset children and contentBounds by scroll
|
// offset children and contentBounds by scroll
|
||||||
for _, box := range this.children {
|
for _, box := range this.children {
|
||||||
assertAnyBox(box).setBounds(box.Bounds().Add(this.scroll).Add(innerBounds.Min))
|
box.setBounds(box.Bounds().Add(this.scroll).Add(innerBounds.Min))
|
||||||
}
|
}
|
||||||
this.contentBounds = this.contentBounds.Add(this.scroll)
|
this.contentBounds = this.contentBounds.Add(this.scroll)
|
||||||
|
|
||||||
|
@ -2,8 +2,8 @@ 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: redo all of this because there are new event propogation rules
|
|
||||||
// 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
|
||||||
|
|
||||||
@ -25,21 +25,29 @@ func (this *Hierarchy) HandleModifiers (modifiers input.Modifiers) {
|
|||||||
// HandleModifiers must be called *before* HandleKeyDown.
|
// HandleModifiers must be called *before* HandleKeyDown.
|
||||||
func (this *Hierarchy) HandleKeyDown (key input.Key, numberPad bool) {
|
func (this *Hierarchy) HandleKeyDown (key input.Key, numberPad bool) {
|
||||||
caught := false
|
caught := false
|
||||||
this.keyboardTargets(func (target anyBox) bool {
|
if this.anyFocused() {
|
||||||
if target.handleKeyDown(key, numberPad) {
|
this.keyboardTargets(func (target anyBox) bool {
|
||||||
caught = true
|
if target.handleKeyDown(key, numberPad) {
|
||||||
return false
|
caught = true
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
if this.root != nil {
|
||||||
|
caught = this.root.handleKeyDown(key, numberPad)
|
||||||
}
|
}
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
if caught { return }
|
if caught { return }
|
||||||
|
|
||||||
if key == input.KeyTab && this.modifiers.Alt {
|
switch input.KC(key, this.modifiers) {
|
||||||
if this.modifiers.Shift {
|
case config.KeyChordFocusNext: this.focusNext()
|
||||||
this.focusPrevious()
|
case config.KeyChordFocusPrevious: this.focusPrevious()
|
||||||
} else {
|
// TODO: up, down, left, and right should find a box to the top, bottom,
|
||||||
this.focusNext()
|
// left, and right respectively to move the focus to. we might want to
|
||||||
}
|
// have four corresponding key chords in tomo/config.
|
||||||
|
case input.KC(input.KeyDown, input.ModNone): this.focusNext()
|
||||||
|
case input.KC(input.KeyUp, input.ModNone): this.focusPrevious()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,12 +55,18 @@ func (this *Hierarchy) HandleKeyDown (key input.Key, numberPad bool) {
|
|||||||
// which triggers this comes with modifier key information, HandleModifiers must
|
// which triggers this comes with modifier key information, HandleModifiers must
|
||||||
// be called *before* HandleKeyUp.
|
// be called *before* HandleKeyUp.
|
||||||
func (this *Hierarchy) HandleKeyUp (key input.Key, numberPad bool) {
|
func (this *Hierarchy) HandleKeyUp (key input.Key, numberPad bool) {
|
||||||
this.keyboardTargets(func (target anyBox) bool {
|
if this.anyFocused() {
|
||||||
if target.handleKeyUp(key, numberPad) {
|
this.keyboardTargets(func (target anyBox) bool {
|
||||||
return false
|
if target.handleKeyUp(key, numberPad) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
if this.root != nil {
|
||||||
|
this.root.handleKeyUp(key, numberPad)
|
||||||
}
|
}
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleMouseDown sends a mouse down event to the Boxes positioned underneath
|
// HandleMouseDown sends a mouse down event to the Boxes positioned underneath
|
||||||
@ -62,7 +76,16 @@ func (this *Hierarchy) HandleKeyUp (key input.Key, numberPad bool) {
|
|||||||
// information, HandleMouseMove must be called *before* HandleMouseDown.
|
// information, HandleMouseMove must be called *before* HandleMouseDown.
|
||||||
func (this *Hierarchy) HandleMouseDown (button input.Button) {
|
func (this *Hierarchy) HandleMouseDown (button input.Button) {
|
||||||
boxes := []anyBox { }
|
boxes := []anyBox { }
|
||||||
|
first := true
|
||||||
this.boxesUnder(this.mousePosition)(func (box anyBox) bool {
|
this.boxesUnder(this.mousePosition)(func (box anyBox) bool {
|
||||||
|
if first {
|
||||||
|
if box.canBeFocused() {
|
||||||
|
this.focus(box)
|
||||||
|
} else {
|
||||||
|
this.focus(nil)
|
||||||
|
}
|
||||||
|
first = false
|
||||||
|
}
|
||||||
boxes = append(boxes, box)
|
boxes = append(boxes, box)
|
||||||
return !box.handleMouseDown(button)
|
return !box.handleMouseDown(button)
|
||||||
})
|
})
|
||||||
@ -92,17 +115,18 @@ func (this *Hierarchy) HandleMouseMove (position image.Point) {
|
|||||||
for _, dragSet := range this.drags {
|
for _, dragSet := range this.drags {
|
||||||
for _, box := range dragSet {
|
for _, box := range dragSet {
|
||||||
if box.handleMouseMove() { break }
|
if box.handleMouseMove() { break }
|
||||||
|
dragged = true
|
||||||
}
|
}
|
||||||
dragged = true
|
|
||||||
}
|
}
|
||||||
if dragged { return }
|
if dragged { return }
|
||||||
|
|
||||||
// TODO we can hover over multiple boxes at once. however, any way of
|
// TODO we can hover over multiple boxes at once. however, any way of
|
||||||
// detecting this involves several slice allocations every time we
|
// detecting this involves several slice allocations every time we
|
||||||
// process a MouseMove event. perhaps we just ought to suck it up and do
|
// process a MouseMove event. perhaps we just ought to suck it up and do
|
||||||
// it.
|
// it. or perhaps doing *this* is the better way? we may never know.
|
||||||
box := this.boxUnder(position)
|
box := this.boxUnder(position)
|
||||||
if box != nil {
|
if box != nil {
|
||||||
|
box := this.considerMaskingParents(box)
|
||||||
this.hover(box)
|
this.hover(box)
|
||||||
box.handleMouseMove()
|
box.handleMouseMove()
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ import "image"
|
|||||||
import "git.tebibyte.media/tomo/tomo"
|
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/internal/util"
|
import "git.tebibyte.media/tomo/backend/style"
|
||||||
|
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.
|
||||||
@ -24,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
|
||||||
}
|
}
|
||||||
@ -52,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
|
||||||
@ -168,22 +169,34 @@ func (this *Hierarchy) getWindow () tomo.Window {
|
|||||||
return this.link.GetWindow()
|
return this.link.GetWindow()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Hierarchy) getStyle () *tomo.Style {
|
func (this *Hierarchy) getStyle () *style.Style {
|
||||||
return this.system.style
|
return this.system.style
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Hierarchy) getIconSet () style.IconSet {
|
||||||
|
return this.system.iconSet
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Hierarchy) getFaceSet () style.FaceSet {
|
||||||
|
return this.system.faceSet
|
||||||
|
}
|
||||||
|
|
||||||
func (this *Hierarchy) getStyleNonce () int {
|
func (this *Hierarchy) getStyleNonce () int {
|
||||||
return this.system.styleNonce
|
return this.system.styleNonce
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Hierarchy) getIconsNonce () int {
|
func (this *Hierarchy) getIconSetNonce () int {
|
||||||
return this.system.iconsNonce
|
return this.system.iconSetNonce
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Hierarchy) getCanvas () canvas.Canvas {
|
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
|
||||||
}
|
}
|
||||||
@ -287,13 +300,26 @@ func (this *Hierarchy) considerMaskingParents (box anyBox) anyBox {
|
|||||||
return box
|
return box
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Hierarchy) isMasked (box anyBox) bool {
|
||||||
|
parent := box.getParent()
|
||||||
|
for {
|
||||||
|
parentBox, ok := parent.(anyBox)
|
||||||
|
if !ok { break }
|
||||||
|
if parent.masks() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
parent = parentBox.getParent()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (this *Hierarchy) focusNext () {
|
func (this *Hierarchy) focusNext () {
|
||||||
found := !this.anyFocused()
|
found := !this.anyFocused()
|
||||||
focused := false
|
focused := false
|
||||||
this.propagateAlt(func (box anyBox) bool {
|
this.propagateAlt(func (box anyBox) bool {
|
||||||
if found {
|
if found {
|
||||||
// looking for the next box to select
|
// looking for the next box to select
|
||||||
if box.canBeFocused() {
|
if box.canBeFocused() && !this.isMasked(box) {
|
||||||
// found it
|
// found it
|
||||||
this.focus(box)
|
this.focus(box)
|
||||||
focused = true
|
focused = true
|
||||||
@ -318,7 +344,7 @@ func (this *Hierarchy) focusPrevious () {
|
|||||||
if box == this.focused {
|
if box == this.focused {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if box.canBeFocused() { behind = box }
|
if box.canBeFocused() && !this.isMasked(box) { behind = box }
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
this.focus(behind)
|
this.focus(behind)
|
||||||
@ -341,15 +367,21 @@ func (this *Hierarchy) drawBackgroundPart (canvas.Canvas) {
|
|||||||
// if so, windows should be transparent if the color has transparency
|
// if so, windows should be transparent if the color has transparency
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// var minimumSizeCount = 0
|
||||||
func (this *Hierarchy) doMinimumSize () {
|
func (this *Hierarchy) doMinimumSize () {
|
||||||
this.minimumClean = true
|
this.minimumClean = true
|
||||||
|
// println("doMinimumSize", minimumSizeCount)
|
||||||
|
// minimumSizeCount ++
|
||||||
|
|
||||||
|
previousMinimumSize := this.minimumSize
|
||||||
this.minimumSize = image.Point { }
|
this.minimumSize = image.Point { }
|
||||||
if this.root != nil {
|
if this.root != nil {
|
||||||
this.minimumSize = this.root.minimumSize()
|
this.minimumSize = this.root.minimumSize()
|
||||||
}
|
}
|
||||||
|
|
||||||
this.link.NotifyMinimumSizeChange()
|
if previousMinimumSize != this.minimumSize {
|
||||||
|
this.link.NotifyMinimumSizeChange()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Hierarchy) newStyleApplicator () *styleApplicator {
|
func (this *Hierarchy) newStyleApplicator () *styleApplicator {
|
||||||
|
@ -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)
|
||||||
@ -75,6 +78,9 @@ type anyBox interface {
|
|||||||
// setAttr sets an attribute at the user or style level depending
|
// setAttr sets an attribute at the user or style level depending
|
||||||
// on the value of user.
|
// on the value of user.
|
||||||
setAttr (attr tomo.Attr, user bool)
|
setAttr (attr tomo.Attr, user bool)
|
||||||
|
// unsetAttr unsets an attribute at the user or style level depending
|
||||||
|
// on the value of user.
|
||||||
|
unsetAttr (kind tomo.AttrKind, user bool)
|
||||||
|
|
||||||
// propagate recursively calls a function on this anyBox, and all of its
|
// propagate recursively calls a function on this anyBox, and all of its
|
||||||
// children (if applicable) The normal propagate behavior calls the
|
// children (if applicable) The normal propagate behavior calls the
|
||||||
|
@ -1,30 +1,33 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
import "git.tebibyte.media/tomo/backend/style"
|
||||||
|
|
||||||
type styleApplicator struct {
|
type styleApplicator struct {
|
||||||
style *tomo.Style
|
style *style.Style
|
||||||
role tomo.Role
|
role tomo.Role
|
||||||
rules []*tomo.Rule
|
rules []style.Rule
|
||||||
|
currentSet style.AttrSet
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *styleApplicator) apply (box anyBox) {
|
func (this *styleApplicator) apply (box anyBox) {
|
||||||
if box.Role() != this.role {
|
if box.Role() != this.role {
|
||||||
|
this.role = box.Role()
|
||||||
// the role has changed, so re-cache the list of rules
|
// the role has changed, so re-cache the list of rules
|
||||||
this.rules = make([]*tomo.Rule, 4)
|
this.rules = nil
|
||||||
for _, rule := range this.style.Rules {
|
for _, rule := range this.style.Rules {
|
||||||
role := box.Role()
|
role := box.Role()
|
||||||
// blank fields match anything
|
// blank fields match anything
|
||||||
if rule.Role.Package == "" { role.Package = "" }
|
if rule.Role.Package == "" { role.Package = "" }
|
||||||
if rule.Role.Object == "" { role.Object = "" }
|
if rule.Role.Object == "" { role.Object = "" }
|
||||||
if rule.Role == role {
|
if rule.Role == role {
|
||||||
this.rules = append(this.rules, &rule)
|
this.rules = append(this.rules, rule)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// compile list of attributes by searching through the cached ruleset
|
// compile list of attributes by searching through the cached ruleset
|
||||||
attrs := make(tomo.AttrSet)
|
attrs := make(style.AttrSet)
|
||||||
for _, rule := range this.rules {
|
for _, rule := range this.rules {
|
||||||
satisifed := true
|
satisifed := true
|
||||||
for _, tag := range rule.Tags {
|
for _, tag := range rule.Tags {
|
||||||
@ -39,7 +42,18 @@ func (this *styleApplicator) apply (box anyBox) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reset an attribute if it is no longer specified
|
||||||
|
if this.currentSet != nil {
|
||||||
|
for kind := range this.currentSet {
|
||||||
|
_, exists := attrs[kind]
|
||||||
|
if !exists {
|
||||||
|
box.unsetAttr(kind, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// apply that list of attributes
|
// apply that list of attributes
|
||||||
|
this.currentSet = attrs
|
||||||
for _, attr := range attrs {
|
for _, attr := range attrs {
|
||||||
box.setAttr(attr, false)
|
box.setAttr(attr, false)
|
||||||
}
|
}
|
||||||
|
@ -2,20 +2,22 @@ package system
|
|||||||
|
|
||||||
import "io"
|
import "io"
|
||||||
import "image"
|
import "image"
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
|
||||||
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/style"
|
||||||
|
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.
|
||||||
type System struct {
|
type System struct {
|
||||||
link BackendLink
|
link BackendLink
|
||||||
|
|
||||||
style *tomo.Style
|
style *style.Style
|
||||||
styleNonce int
|
iconSet style.IconSet
|
||||||
iconsNonce int
|
faceSet style.FaceSet
|
||||||
|
styleNonce 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
|
||||||
@ -39,13 +41,13 @@ 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]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetStyle sets the tomo.Style that is applied to objects, and notifies them
|
// SetStyle sets the style that is applied to objects, and notifies them
|
||||||
// that the style has changed.
|
// that the style has changed.
|
||||||
func (this *System) SetStyle (style *tomo.Style) {
|
func (this *System) SetStyle (style *style.Style) {
|
||||||
this.style = style
|
this.style = style
|
||||||
this.styleNonce ++
|
this.styleNonce ++
|
||||||
for hierarchy := range this.hierarchies {
|
for hierarchy := range this.hierarchies {
|
||||||
@ -53,14 +55,21 @@ func (this *System) SetStyle (style *tomo.Style) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetIconSet notifies objects that the icons have changed.
|
// SetIconSet sets the icon set that provides icon textures, and notifies
|
||||||
func (this *System) SetIconSet (iconSet tomo.IconSet) {
|
// objects that the icons have changed.
|
||||||
this.iconsNonce ++
|
func (this *System) SetIconSet (iconSet style.IconSet) {
|
||||||
|
this.iconSet = iconSet
|
||||||
|
this.iconSetNonce ++
|
||||||
for hierarchy := range this.hierarchies {
|
for hierarchy := range this.hierarchies {
|
||||||
hierarchy.setIconSet()
|
hierarchy.setIconSet()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetFaceSet sets the face set that provides font faces.
|
||||||
|
func (this *System) SetFaceSet (faceSet style.FaceSet) {
|
||||||
|
this.faceSet = faceSet
|
||||||
|
}
|
||||||
|
|
||||||
func (this *System) removeHierarchy (hierarchy *Hierarchy) {
|
func (this *System) removeHierarchy (hierarchy *Hierarchy) {
|
||||||
delete(this.hierarchies, hierarchy)
|
delete(this.hierarchies, hierarchy)
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import "image"
|
import "image"
|
||||||
|
import "unicode"
|
||||||
import "image/color"
|
import "image/color"
|
||||||
|
import "golang.org/x/image/font"
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
import "golang.org/x/image/math/fixed"
|
import "golang.org/x/image/math/fixed"
|
||||||
import "git.tebibyte.media/tomo/typeset"
|
import "git.tebibyte.media/tomo/typeset"
|
||||||
@ -9,6 +11,8 @@ import "git.tebibyte.media/tomo/tomo/text"
|
|||||||
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"
|
||||||
|
|
||||||
type textBox struct {
|
type textBox struct {
|
||||||
*box
|
*box
|
||||||
@ -24,12 +28,16 @@ type textBox struct {
|
|||||||
attrOverflow attrHierarchy[tomo.AttrOverflow]
|
attrOverflow attrHierarchy[tomo.AttrOverflow]
|
||||||
|
|
||||||
text string
|
text string
|
||||||
|
runes []rune
|
||||||
selectable bool
|
selectable bool
|
||||||
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]
|
||||||
|
lineHeight ucontainer.Memo[fixed.Int26_6]
|
||||||
|
|
||||||
on struct {
|
on struct {
|
||||||
contentBoundsChange event.FuncBroadcaster
|
contentBoundsChange event.FuncBroadcaster
|
||||||
@ -40,6 +48,14 @@ type textBox struct {
|
|||||||
func (this *System) NewTextBox () tomo.TextBox {
|
func (this *System) NewTextBox () tomo.TextBox {
|
||||||
box := &textBox { }
|
box := &textBox { }
|
||||||
box.box = this.newBox(box)
|
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 = 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,8 +86,9 @@ func (this *textBox) OnContentBoundsChange (callback func()) event.Cookie {
|
|||||||
|
|
||||||
func (this *textBox) SetText (text string) {
|
func (this *textBox) SetText (text string) {
|
||||||
if this.text == text { return }
|
if this.text == text { return }
|
||||||
this.text = text
|
this.text = text
|
||||||
this.drawer.SetText([]rune(text))
|
this.runes = []rune(text)
|
||||||
|
this.drawer.SetText(this.runes)
|
||||||
this.invalidateMinimum()
|
this.invalidateMinimum()
|
||||||
this.invalidateLayout()
|
this.invalidateLayout()
|
||||||
}
|
}
|
||||||
@ -82,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 {
|
||||||
@ -104,15 +149,14 @@ func (this *textBox) OnDotChange (callback func ()) event.Cookie {
|
|||||||
func (this *textBox) Draw (can canvas.Canvas) {
|
func (this *textBox) Draw (can canvas.Canvas) {
|
||||||
if can == nil { return }
|
if can == nil { return }
|
||||||
|
|
||||||
texture := this.attrTexture.Value().Texture
|
texture := this.attrTexture.Value().Texture
|
||||||
col := this.attrColor.Value().Color
|
col := this.attrColor.Value().Color
|
||||||
if col == nil { col = color.Transparent }
|
|
||||||
|
|
||||||
this.drawBorders(can)
|
this.drawBorders(can)
|
||||||
|
|
||||||
pen := can.Pen()
|
pen := can.Pen()
|
||||||
pen.Fill(col)
|
pen.Fill(col)
|
||||||
pen.Texture(texture)
|
pen.Texture(texture)
|
||||||
|
|
||||||
if this.transparent() && this.parent != nil {
|
if this.transparent() && this.parent != nil {
|
||||||
this.parent.drawBackgroundPart(can)
|
this.parent.drawBackgroundPart(can)
|
||||||
}
|
}
|
||||||
@ -122,9 +166,8 @@ func (this *textBox) Draw (can canvas.Canvas) {
|
|||||||
this.drawDot(can)
|
this.drawDot(can)
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.attrFace.Value().Face != nil {
|
if this.face.Value() != nil {
|
||||||
textColor := this.attrTextColor.Value().Color
|
textColor := this.attrTextColor.Value().Color
|
||||||
if textColor == nil { textColor = color.Black }
|
|
||||||
this.drawer.Draw(can, textColor, this.textOffset())
|
this.drawer.Draw(can, textColor, this.textOffset())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -143,24 +186,28 @@ func (this *textBox) setAttr (attr tomo.Attr, user bool) {
|
|||||||
|
|
||||||
case tomo.AttrFace:
|
case tomo.AttrFace:
|
||||||
if this.attrFace.Set(attr, user) {
|
if this.attrFace.Set(attr, user) {
|
||||||
this.drawer.SetFace(attr.Face)
|
this.handleFaceChange()
|
||||||
this.invalidateMinimum()
|
|
||||||
this.invalidateLayout()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case tomo.AttrWrap:
|
case tomo.AttrWrap:
|
||||||
if this.attrWrap.Set(attr, user) {
|
if this.attrWrap.Set(attr, user) {
|
||||||
|
this.drawer.SetWrap(bool(this.attrWrap.Value()))
|
||||||
this.invalidateMinimum()
|
this.invalidateMinimum()
|
||||||
this.invalidateLayout()
|
this.invalidateLayout()
|
||||||
}
|
}
|
||||||
|
|
||||||
case tomo.AttrAlign:
|
case tomo.AttrAlign:
|
||||||
if this.attrAlign.Set(attr, user) {
|
if this.attrAlign.Set(attr, user) {
|
||||||
|
align := this.attrAlign.Value()
|
||||||
|
this.drawer.SetAlign (
|
||||||
|
typeset.Align(align.X),
|
||||||
|
typeset.Align(align.Y))
|
||||||
this.invalidateDraw()
|
this.invalidateDraw()
|
||||||
}
|
}
|
||||||
|
|
||||||
case tomo.AttrOverflow:
|
case tomo.AttrOverflow:
|
||||||
if this.attrOverflow.Set(attr, user) {
|
if this.attrOverflow.Set(attr, user) {
|
||||||
|
this.invalidateMinimum()
|
||||||
this.invalidateLayout()
|
this.invalidateLayout()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,6 +215,49 @@ func (this *textBox) setAttr (attr tomo.Attr, user bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *textBox) unsetAttr (kind tomo.AttrKind, user bool) {
|
||||||
|
switch kind {
|
||||||
|
case tomo.AttrKindTextColor:
|
||||||
|
if this.attrTextColor.Unset(user) && !this.dot.Empty() {
|
||||||
|
this.invalidateDraw()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindDotColor:
|
||||||
|
if this.attrDotColor.Unset(user) && !this.dot.Empty() {
|
||||||
|
this.invalidateDraw()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindFace:
|
||||||
|
if this.attrFace.Unset(user) {
|
||||||
|
this.handleFaceChange()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindWrap:
|
||||||
|
if this.attrWrap.Unset(user) {
|
||||||
|
this.drawer.SetWrap(bool(this.attrWrap.Value()))
|
||||||
|
this.invalidateMinimum()
|
||||||
|
this.invalidateLayout()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindAlign:
|
||||||
|
if this.attrAlign.Unset(user) {
|
||||||
|
align := this.attrAlign.Value()
|
||||||
|
this.drawer.SetAlign (
|
||||||
|
typeset.Align(align.X),
|
||||||
|
typeset.Align(align.Y))
|
||||||
|
this.invalidateDraw()
|
||||||
|
}
|
||||||
|
|
||||||
|
case tomo.AttrKindOverflow:
|
||||||
|
if this.attrOverflow.Unset(user) {
|
||||||
|
this.invalidateMinimum()
|
||||||
|
this.invalidateLayout()
|
||||||
|
}
|
||||||
|
|
||||||
|
default: this.box.unsetAttr(kind, user)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func roundPt (point fixed.Point26_6) image.Point {
|
func roundPt (point fixed.Point26_6) image.Point {
|
||||||
return image.Pt(point.X.Round(), point.Y.Round())
|
return image.Pt(point.X.Round(), point.Y.Round())
|
||||||
}
|
}
|
||||||
@ -177,31 +267,27 @@ func fixPt (point image.Point) fixed.Point26_6 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *textBox) drawDot (can canvas.Canvas) {
|
func (this *textBox) drawDot (can canvas.Canvas) {
|
||||||
if this.attrFace.Value().Face == nil { return }
|
face := this.face.Value()
|
||||||
|
if face == nil { return }
|
||||||
|
|
||||||
face := this.attrFace.Value().Face
|
|
||||||
textColor := this.attrTextColor.Value().Color
|
textColor := this.attrTextColor.Value().Color
|
||||||
dotColor := this.attrDotColor.Value().Color
|
dotColor := this.attrDotColor.Value().Color
|
||||||
if textColor == nil { textColor = color.Transparent }
|
|
||||||
if dotColor == nil { dotColor = color.RGBA { G: 255, B: 255, A: 255 } }
|
|
||||||
|
|
||||||
pen := can.Pen()
|
pen := can.Pen()
|
||||||
pen.Fill(color.Transparent)
|
|
||||||
pen.Stroke(textColor)
|
|
||||||
|
|
||||||
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()))
|
||||||
height := this.drawer.LineHeight().Round()
|
end := this.drawer.PositionAt(canonDot.End ).Add(fixPt(this.textOffset()))
|
||||||
ascent := fixed.Point26_6 { Y: metrics.Descent }
|
canonEnd := this.drawer.PositionAt(dot.End ).Add(fixPt(this.textOffset()))
|
||||||
descent := fixed.Point26_6 { Y: metrics.Ascent }
|
height := this.drawer.LineHeight().Round()
|
||||||
|
ascent := fixed.Point26_6 { Y: metrics.Descent }
|
||||||
|
descent := fixed.Point26_6 { Y: metrics.Ascent }
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case dot.Empty():
|
case canonDot.Empty():
|
||||||
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)
|
||||||
@ -235,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 {
|
||||||
@ -243,35 +342,45 @@ func (this *textBox) textOffset () image.Point {
|
|||||||
Sub(this.drawer.LayoutBoundsSpace().Min)
|
Sub(this.drawer.LayoutBoundsSpace().Min)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *textBox) handleFocusEnter () {
|
||||||
|
this.invalidateDraw()
|
||||||
|
this.box.handleFocusEnter()
|
||||||
|
}
|
||||||
|
|
||||||
func (this *textBox) handleFocusLeave () {
|
func (this *textBox) handleFocusLeave () {
|
||||||
this.on.dotChange.Broadcast()
|
|
||||||
this.invalidateDraw()
|
this.invalidateDraw()
|
||||||
this.box.handleFocusLeave()
|
this.box.handleFocusLeave()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *textBox) handleMouseDown (button input.Button) bool {
|
func (this *textBox) handleMouseDown (button input.Button) bool {
|
||||||
if button == input.ButtonLeft {
|
if this.mouseButtonCanDrag(button) {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *textBox) handleMouseUp (button input.Button) bool {
|
func (this *textBox) handleMouseUp (button input.Button) bool {
|
||||||
if button == input.ButtonLeft && 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *textBox) mouseButtonCanDrag (button input.Button) bool {
|
||||||
|
return button == input.ButtonLeft ||
|
||||||
|
button == input.ButtonMiddle ||
|
||||||
|
button == input.ButtonRight
|
||||||
|
}
|
||||||
|
|
||||||
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()
|
||||||
}
|
}
|
||||||
@ -283,6 +392,127 @@ 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 {
|
||||||
|
if this.box.handleKeyDown(key, numberPad) { return true }
|
||||||
|
if !this.selectable { return false }
|
||||||
|
|
||||||
|
// because fuck you thats why!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
modifiers := this.Window().Modifiers()
|
||||||
|
dot := this.Dot()
|
||||||
|
sel := modifiers.Shift()
|
||||||
|
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 {
|
||||||
|
case key == input.KeyHome || (modifiers.Alt() && key == input.KeyLeft):
|
||||||
|
if word {
|
||||||
|
dot.End = 0
|
||||||
|
} else {
|
||||||
|
dot.End = lineHomeSoft(this.runes, dot.End)
|
||||||
|
}
|
||||||
|
if !sel { dot.Start = dot.End }
|
||||||
|
this.userSelect(dot)
|
||||||
|
return true
|
||||||
|
case key == input.KeyEnd || (modifiers.Alt() && key == input.KeyRight):
|
||||||
|
if word {
|
||||||
|
dot.End = len(this.runes)
|
||||||
|
} else {
|
||||||
|
dot.End = lineEnd(this.runes, dot.End)
|
||||||
|
}
|
||||||
|
if !sel { dot.Start = dot.End }
|
||||||
|
this.userSelect(dot)
|
||||||
|
return true
|
||||||
|
case key == input.KeyLeft:
|
||||||
|
if sel {
|
||||||
|
this.userSelect(text.SelectLeft(this.runes, dot, word))
|
||||||
|
} else {
|
||||||
|
this.userSelect(text.MoveLeft(this.runes, dot, word))
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
case key == input.KeyRight:
|
||||||
|
if sel {
|
||||||
|
this.userSelect(text.SelectRight(this.runes, dot, word))
|
||||||
|
} else {
|
||||||
|
this.userSelect(text.MoveRight(this.runes, dot, word))
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
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.End = len(this.runes)
|
||||||
|
this.userSelect(dot)
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *textBox) handleKeyUp (key input.Key, numberPad bool) bool {
|
||||||
|
if this.box.handleKeyUp(key, numberPad) { return true }
|
||||||
|
if !this.selectable { return false }
|
||||||
|
|
||||||
|
modifiers := this.Window().Modifiers()
|
||||||
|
switch {
|
||||||
|
case key == input.KeyHome || (modifiers.Alt() && key == input.KeyLeft):
|
||||||
|
return true
|
||||||
|
case key == input.KeyEnd || (modifiers.Alt() && key == input.KeyRight):
|
||||||
|
return true
|
||||||
|
case key == input.KeyUp:
|
||||||
|
return true
|
||||||
|
case key == input.KeyDown:
|
||||||
|
return true
|
||||||
|
case key == input.KeyLeft:
|
||||||
|
return true
|
||||||
|
case key == input.KeyRight:
|
||||||
|
return true
|
||||||
|
case key == input.Key('a') && modifiers.Control():
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (this *textBox) normalizedLayoutBoundsSpace () image.Rectangle {
|
func (this *textBox) normalizedLayoutBoundsSpace () image.Rectangle {
|
||||||
bounds := this.drawer.LayoutBoundsSpace()
|
bounds := this.drawer.LayoutBoundsSpace()
|
||||||
return bounds.Sub(bounds.Min)
|
return bounds.Sub(bounds.Min)
|
||||||
@ -366,3 +596,104 @@ func (this *textBox) scrollToDot () {
|
|||||||
|
|
||||||
this.ScrollTo(scroll)
|
this.ScrollTo(scroll)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *textBox) handleFaceChange () {
|
||||||
|
hierarchy := this.getHierarchy()
|
||||||
|
if hierarchy == nil { return }
|
||||||
|
faceSet := hierarchy.getFaceSet()
|
||||||
|
if faceSet == nil { return }
|
||||||
|
|
||||||
|
face := faceSet.Face(tomo.Face(this.attrFace.Value()))
|
||||||
|
this.face.Set(face, face)
|
||||||
|
this.drawer.SetFace(face)
|
||||||
|
this.invalidateMinimum()
|
||||||
|
this.invalidateLayout()
|
||||||
|
this.lineHeight.Invalidate()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *textBox) recursiveReApply () {
|
||||||
|
this.box.recursiveReApply()
|
||||||
|
|
||||||
|
hierarchy := this.getHierarchy()
|
||||||
|
if hierarchy == nil { return }
|
||||||
|
|
||||||
|
previousFace := this.face.Value()
|
||||||
|
if previousFace == nil {
|
||||||
|
faceSet := hierarchy.getFaceSet()
|
||||||
|
if faceSet == nil { return }
|
||||||
|
face := faceSet.Face(tomo.Face(this.attrFace.Value()))
|
||||||
|
if face != previousFace {
|
||||||
|
this.face.Set(face, face)
|
||||||
|
this.drawer.SetFace(face)
|
||||||
|
this.invalidateMinimum()
|
||||||
|
this.invalidateLayout()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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,105 +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()
|
|
||||||
}
|
|
||||||
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 () {
|
|
||||||
this.valid = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// InvalidateTo invalidates the Memo and sets its value. The new value will be
|
|
||||||
// entirely inaccessible. This is only intended to be used for setting a
|
|
||||||
// reference to nil
|
|
||||||
func (this *Memo[T]) InvalidateTo (value T) {
|
|
||||||
this.Invalidate()
|
|
||||||
this.cache = value
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
||||||
|
13
style/faceset.go
Normal file
13
style/faceset.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package style
|
||||||
|
|
||||||
|
import "golang.org/x/image/font"
|
||||||
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
|
||||||
|
// FaceSet holds a set of font faces.
|
||||||
|
type FaceSet interface {
|
||||||
|
// Face returns the font face which most closely matches the given
|
||||||
|
// tomo.Face. The face must be closed when it is done being used. If no
|
||||||
|
// suitable face could be found, This behavior must return a fallback
|
||||||
|
// face (such as basicfont.Face7x13) instead of nil.
|
||||||
|
Face (tomo.Face) font.Face
|
||||||
|
}
|
28
style/iconset.go
Normal file
28
style/iconset.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package style
|
||||||
|
|
||||||
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/data"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||||
|
|
||||||
|
// IconSet holds a set of icon textures.
|
||||||
|
type IconSet interface {
|
||||||
|
// A word on textures:
|
||||||
|
//
|
||||||
|
// Because textures can be linked to some resource that is outside of
|
||||||
|
// the control of Go's garbage collector, methods of IconSet must not
|
||||||
|
// allocate new copies of a texture each time they are called. It is
|
||||||
|
// fine to lazily load textures and save them for later use, but the
|
||||||
|
// same texture must never be allocated multiple times as this could
|
||||||
|
// cause a memory leak.
|
||||||
|
//
|
||||||
|
// As such, textures returned by these methods must be protected.
|
||||||
|
|
||||||
|
// Icon returns a texture of the corresponding icon ID. If there is no
|
||||||
|
// suitable option, it should return nil.
|
||||||
|
Icon (tomo.Icon, tomo.IconSize) canvas.Texture
|
||||||
|
|
||||||
|
// MimeIcon returns a texture of an icon corresponding to a MIME type.
|
||||||
|
// If there is no suitable specific option, it should return a more
|
||||||
|
// generic icon or a plain file icon.
|
||||||
|
MimeIcon (data.Mime, tomo.IconSize) canvas.Texture
|
||||||
|
}
|
70
style/style.go
Normal file
70
style/style.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package style
|
||||||
|
|
||||||
|
import "image/color"
|
||||||
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
|
||||||
|
// Style can apply a visual style to different objects.
|
||||||
|
type Style struct {
|
||||||
|
// Rules determines which styles get applied to which Objects.
|
||||||
|
Rules []Rule
|
||||||
|
|
||||||
|
// Colors maps tomo.Color values to color.RGBA values.
|
||||||
|
Colors map[tomo.Color] color.Color
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rule describes under what circumstances should certain style attributes be
|
||||||
|
// active.
|
||||||
|
type Rule struct {
|
||||||
|
Role tomo.Role
|
||||||
|
Tags []string
|
||||||
|
Set AttrSet
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ru is shorthand for creating a rule structure
|
||||||
|
func Ru (set AttrSet, role tomo.Role, tags ...string) Rule {
|
||||||
|
return Rule {
|
||||||
|
Role: role,
|
||||||
|
Tags: tags,
|
||||||
|
Set: set,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AttrSet is a set of attributes wherein only one/zero of each attribute type
|
||||||
|
// can exist. It is keyed by the AttrKind of each attribute and must not be
|
||||||
|
// modified directly.
|
||||||
|
type AttrSet map[tomo.AttrKind] tomo.Attr
|
||||||
|
|
||||||
|
// AS builds an AttrSet out of a vararg list of Attr values. If multiple Attrs
|
||||||
|
// of the same kind are specified, the last one will override the others.
|
||||||
|
func AS (attrs ...tomo.Attr) AttrSet {
|
||||||
|
set := AttrSet { }
|
||||||
|
set.Add(attrs...)
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add adds attributes to the set.
|
||||||
|
func (this AttrSet) Add (attrs ...tomo.Attr) {
|
||||||
|
for _, attr := range attrs {
|
||||||
|
this[attr.Kind()] = attr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MergeUnder takes attributes from another set and adds them if they don't
|
||||||
|
// already exist in this one.
|
||||||
|
func (this AttrSet) MergeUnder (other AttrSet) {
|
||||||
|
if other == nil { return }
|
||||||
|
for _, attr := range other {
|
||||||
|
if _, exists := this[attr.Kind()]; !exists {
|
||||||
|
this.Add(attr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MergeOver takes attributes from another set and adds them, overriding this
|
||||||
|
// one.
|
||||||
|
func (this AttrSet) MergeOver (other AttrSet) {
|
||||||
|
if other == nil { return }
|
||||||
|
for _, attr := range other {
|
||||||
|
this.Add(attr)
|
||||||
|
}
|
||||||
|
}
|
32
x/backend.go
32
x/backend.go
@ -4,7 +4,9 @@ import "image"
|
|||||||
import "errors"
|
import "errors"
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
import "git.tebibyte.media/tomo/xgbkb"
|
import "git.tebibyte.media/tomo/xgbkb"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/data"
|
||||||
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/x/canvas"
|
import "git.tebibyte.media/tomo/backend/x/canvas"
|
||||||
import "git.tebibyte.media/tomo/backend/internal/system"
|
import "git.tebibyte.media/tomo/backend/internal/system"
|
||||||
|
|
||||||
@ -18,6 +20,9 @@ type Backend struct {
|
|||||||
x *xgbutil.XUtil
|
x *xgbutil.XUtil
|
||||||
system *system.System
|
system *system.System
|
||||||
|
|
||||||
|
style *style.Style
|
||||||
|
iconSet style.IconSet
|
||||||
|
|
||||||
doChannel chan func()
|
doChannel chan func()
|
||||||
windows map[xproto.Window] *window
|
windows map[xproto.Window] *window
|
||||||
open bool
|
open bool
|
||||||
@ -126,12 +131,33 @@ func (this *Backend) NewCanvas (bounds image.Rectangle) canvas.CanvasCloser {
|
|||||||
return xcanvas.NewCanvas(this.x, bounds)
|
return xcanvas.NewCanvas(this.x, bounds)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Backend) SetStyle (style *tomo.Style) {
|
func (this *Backend) ColorRGBA (id tomo.Color) (r, g, b, a uint32) {
|
||||||
|
if col, ok := this.style.Colors[id]; ok {
|
||||||
|
return col.RGBA()
|
||||||
|
}
|
||||||
|
return 0xFFFF, 0, 0xFFFF, 0xFFFF // punish bad styles
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Backend) IconTexture (id tomo.Icon, size tomo.IconSize) canvas.Texture {
|
||||||
|
return this.iconSet.Icon(id, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Backend) MimeIconTexture (mime data.Mime, size tomo.IconSize) canvas.Texture {
|
||||||
|
return this.iconSet.MimeIcon(mime, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Backend) SetStyle (style *style.Style) {
|
||||||
|
this.style = style
|
||||||
this.system.SetStyle(style)
|
this.system.SetStyle(style)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Backend) SetIconSet (icons tomo.IconSet) {
|
func (this *Backend) SetIconSet (iconSet style.IconSet) {
|
||||||
this.system.SetIconSet(icons)
|
this.iconSet = iconSet
|
||||||
|
this.system.SetIconSet(iconSet)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Backend) SetFaceSet (faceSet style.FaceSet) {
|
||||||
|
this.system.SetFaceSet(faceSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Backend) assert () {
|
func (this *Backend) assert () {
|
||||||
|
@ -132,3 +132,32 @@ func convertColor (c color.Color) xgraphics.BGRA {
|
|||||||
A: uint8(a >> 8),
|
A: uint8(a >> 8),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For some reason, xgraphics.BGRA does not specify whether or not it uses
|
||||||
|
// premultiplied alpha, and information regarding this is contradictory.
|
||||||
|
// Basically:
|
||||||
|
// - BGRAModel just takes the result of c.RGBA and bit shifts it, without
|
||||||
|
// un-doing the aplha premultiplication that is required by Color.RGBA,
|
||||||
|
// suggesting that xgraphics.BGRA stores alpha-premultiplied color.
|
||||||
|
// - xgraphics.BlendBGRA lerps between dest and src using only the alpha of
|
||||||
|
// src (temporarily converting the colors to fucking floats for some reason)
|
||||||
|
// which seems to suggest that xgraphics.BGRA *does not* store alpha-
|
||||||
|
// premultiplied color.
|
||||||
|
// There is no issues page on xgbutil so we may never get an answer to this
|
||||||
|
// question. However, in this package we just use xgraphics.BGRA to store alpha-
|
||||||
|
// premultiplied color anyway because its way faster, and I would sooner eat
|
||||||
|
// spaghetti with a spoon than convert to and from float64 to blend pixels.
|
||||||
|
func blendPremultipliedBGRA (dst, src xgraphics.BGRA) xgraphics.BGRA {
|
||||||
|
// https://en.wikipedia.org/wiki/Alpha_compositing
|
||||||
|
return xgraphics.BGRA {
|
||||||
|
B: blendPremultipliedChannel(dst.B, src.B, src.A),
|
||||||
|
G: blendPremultipliedChannel(dst.G, src.G, src.A),
|
||||||
|
R: blendPremultipliedChannel(dst.R, src.R, src.A),
|
||||||
|
A: blendPremultipliedChannel(dst.A, src.A, src.A),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func blendPremultipliedChannel (dst, src, a uint8) uint8 {
|
||||||
|
dst16, src16, a16 := uint16(dst), uint16(src), uint16(a)
|
||||||
|
return uint8(src16 + ((dst16 * (255 - a16)) >> 8))
|
||||||
|
}
|
||||||
|
@ -46,7 +46,7 @@ func (this *pen) textureRectangleTransparent (bounds image.Rectangle) {
|
|||||||
srcPos := pos.Add(offset)
|
srcPos := pos.Add(offset)
|
||||||
dstIndex := this.image.PixOffset(pos.X, pos.Y)
|
dstIndex := this.image.PixOffset(pos.X, pos.Y)
|
||||||
srcIndex := this.texture.PixOffset(srcPos.X, srcPos.Y)
|
srcIndex := this.texture.PixOffset(srcPos.X, srcPos.Y)
|
||||||
pixel := xgraphics.BlendBGRA(xgraphics.BGRA {
|
pixel := blendPremultipliedBGRA(xgraphics.BGRA {
|
||||||
B: dst[dstIndex + 0],
|
B: dst[dstIndex + 0],
|
||||||
G: dst[dstIndex + 1],
|
G: dst[dstIndex + 1],
|
||||||
R: dst[dstIndex + 2],
|
R: dst[dstIndex + 2],
|
||||||
@ -93,7 +93,7 @@ func (this *pen) fillRectangleTransparent (c xgraphics.BGRA, bounds image.Rectan
|
|||||||
for pos.Y = bounds.Min.Y; pos.Y < bounds.Max.Y; pos.Y ++ {
|
for pos.Y = bounds.Min.Y; pos.Y < bounds.Max.Y; pos.Y ++ {
|
||||||
for pos.X = bounds.Min.X; pos.X < bounds.Max.X; pos.X ++ {
|
for pos.X = bounds.Min.X; pos.X < bounds.Max.X; pos.X ++ {
|
||||||
index := this.image.PixOffset(pos.X, pos.Y)
|
index := this.image.PixOffset(pos.X, pos.Y)
|
||||||
pixel := xgraphics.BlendBGRA(xgraphics.BGRA {
|
pixel := blendPremultipliedBGRA(xgraphics.BGRA {
|
||||||
B: this.image.Pix[index + 0],
|
B: this.image.Pix[index + 0],
|
||||||
G: this.image.Pix[index + 1],
|
G: this.image.Pix[index + 1],
|
||||||
R: this.image.Pix[index + 2],
|
R: this.image.Pix[index + 2],
|
||||||
@ -256,7 +256,7 @@ func (context *fillingContext) fillPolygonHotTransparent () {
|
|||||||
// fill pixels in between
|
// fill pixels in between
|
||||||
for x := left; x < right; x ++ {
|
for x := left; x < right; x ++ {
|
||||||
index := context.image.PixOffset(x, context.y)
|
index := context.image.PixOffset(x, context.y)
|
||||||
pixel := xgraphics.BlendBGRA(xgraphics.BGRA {
|
pixel := blendPremultipliedBGRA(xgraphics.BGRA {
|
||||||
B: context.image.Pix[index + 0],
|
B: context.image.Pix[index + 0],
|
||||||
G: context.image.Pix[index + 1],
|
G: context.image.Pix[index + 1],
|
||||||
R: context.image.Pix[index + 2],
|
R: context.image.Pix[index + 2],
|
||||||
|
@ -32,7 +32,7 @@ func (context plottingContext) plot (center image.Point) {
|
|||||||
for y := square.Min.Y; y < square.Max.Y; y ++ {
|
for y := square.Min.Y; y < square.Max.Y; y ++ {
|
||||||
for x := square.Min.X; x < square.Max.X; x ++ {
|
for x := square.Min.X; x < square.Max.X; x ++ {
|
||||||
index := context.image.PixOffset(x, y)
|
index := context.image.PixOffset(x, y)
|
||||||
pixel := xgraphics.BlendBGRA(xgraphics.BGRA {
|
pixel := blendPremultipliedBGRA(xgraphics.BGRA {
|
||||||
B: context.image.Pix[index + 0],
|
B: context.image.Pix[index + 0],
|
||||||
G: context.image.Pix[index + 1],
|
G: context.image.Pix[index + 1],
|
||||||
R: context.image.Pix[index + 2],
|
R: context.image.Pix[index + 2],
|
||||||
|
39
x/event.go
39
x/event.go
@ -7,6 +7,7 @@ import "github.com/jezek/xgb/xproto"
|
|||||||
import "git.tebibyte.media/tomo/xgbkb"
|
import "git.tebibyte.media/tomo/xgbkb"
|
||||||
import "github.com/jezek/xgbutil/xevent"
|
import "github.com/jezek/xgbutil/xevent"
|
||||||
import "git.tebibyte.media/tomo/tomo/input"
|
import "git.tebibyte.media/tomo/tomo/input"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/config"
|
||||||
|
|
||||||
type scrollSum struct {
|
type scrollSum struct {
|
||||||
x, y int
|
x, y int
|
||||||
@ -145,12 +146,15 @@ func (window *window) updateBounds () {
|
|||||||
// need to sum up all their positions.
|
// need to sum up all their positions.
|
||||||
decorGeometry, _ := window.xWindow.DecorGeometry()
|
decorGeometry, _ := window.xWindow.DecorGeometry()
|
||||||
windowGeometry, _ := window.xWindow.Geometry()
|
windowGeometry, _ := window.xWindow.Geometry()
|
||||||
origin := image.Pt(
|
origin := image.Pt (
|
||||||
windowGeometry.X() + decorGeometry.X(),
|
decorGeometry.X(),
|
||||||
windowGeometry.Y() + decorGeometry.Y())
|
decorGeometry.Y())
|
||||||
window.metrics.bounds = image.Rectangle {
|
innerOrigin := origin.Add(image.Pt (
|
||||||
Min: origin,
|
windowGeometry.X(),
|
||||||
Max: origin.Add(image.Pt(windowGeometry.Width(), windowGeometry.Height())),
|
windowGeometry.Y()))
|
||||||
|
window.metrics.innerBounds = image.Rectangle {
|
||||||
|
Min: innerOrigin,
|
||||||
|
Max: innerOrigin.Add(image.Pt(windowGeometry.Width(), windowGeometry.Height())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,9 +165,9 @@ func (window *window) handleConfigureNotify (
|
|||||||
configureEvent := *event.ConfigureNotifyEvent
|
configureEvent := *event.ConfigureNotifyEvent
|
||||||
configureEvent = window.compressConfigureNotify(configureEvent)
|
configureEvent = window.compressConfigureNotify(configureEvent)
|
||||||
|
|
||||||
oldBounds := window.metrics.bounds
|
oldBounds := window.metrics.innerBounds
|
||||||
window.updateBounds()
|
window.updateBounds()
|
||||||
newBounds := window.metrics.bounds
|
newBounds := window.metrics.innerBounds
|
||||||
|
|
||||||
sizeChanged :=
|
sizeChanged :=
|
||||||
oldBounds.Dx() != newBounds.Dx() ||
|
oldBounds.Dx() != newBounds.Dx() ||
|
||||||
@ -223,7 +227,7 @@ func (window *window) handleKeyPress (
|
|||||||
key, numberPad := keycodeToKey(keyEvent.Detail, keyEvent.State)
|
key, numberPad := keycodeToKey(keyEvent.Detail, keyEvent.State)
|
||||||
window.updateModifiers(keyEvent.State)
|
window.updateModifiers(keyEvent.State)
|
||||||
|
|
||||||
if key == input.KeyEscape && window.shy {
|
if config.KeyChordClose.Pressed(key, window.Modifiers()) && window.shy {
|
||||||
window.Close()
|
window.Close()
|
||||||
} else {
|
} else {
|
||||||
window.hierarchy.HandleKeyDown(key, numberPad)
|
window.hierarchy.HandleKeyDown(key, numberPad)
|
||||||
@ -430,14 +434,15 @@ func (window *window) compressMotionNotify (
|
|||||||
|
|
||||||
func (window *window) updateModifiers (state uint16) {
|
func (window *window) updateModifiers (state uint16) {
|
||||||
xModifiers := xgbkb.StateToModifiers(state)
|
xModifiers := xgbkb.StateToModifiers(state)
|
||||||
window.hierarchy.HandleModifiers(input.Modifiers {
|
var modifiers input.Modifiers
|
||||||
Shift: xModifiers.Shift || xModifiers.ShiftLock,
|
if xModifiers.Shift { modifiers |= input.ModShift }
|
||||||
Control: xModifiers.Control,
|
if xModifiers.ShiftLock { modifiers |= input.ModShift }
|
||||||
Alt: xModifiers.Alt,
|
if xModifiers.Control { modifiers |= input.ModControl }
|
||||||
Meta: xModifiers.Meta,
|
if xModifiers.Alt { modifiers |= input.ModAlt }
|
||||||
Super: xModifiers.Super,
|
if xModifiers.Meta { modifiers |= input.ModMeta }
|
||||||
Hyper: xModifiers.Hyper,
|
if xModifiers.Super { modifiers |= input.ModSuper }
|
||||||
})
|
if xModifiers.Hyper { modifiers |= input.ModHyper }
|
||||||
|
window.hierarchy.HandleModifiers(modifiers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (window *window) updateMousePosition (x, y int16) {
|
func (window *window) updateMousePosition (x, y int16) {
|
||||||
|
152
x/window.go
152
x/window.go
@ -1,6 +1,7 @@
|
|||||||
package x
|
package x
|
||||||
|
|
||||||
import "image"
|
import "image"
|
||||||
|
import "strings"
|
||||||
|
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
import "git.tebibyte.media/tomo/tomo/data"
|
import "git.tebibyte.media/tomo/tomo/data"
|
||||||
@ -37,10 +38,14 @@ type window struct {
|
|||||||
resizeY bool
|
resizeY bool
|
||||||
|
|
||||||
metrics struct {
|
metrics struct {
|
||||||
bounds image.Rectangle
|
bounds image.Rectangle // bounds, including frame
|
||||||
|
innerBounds image.Rectangle // bounds of the drawable area
|
||||||
}
|
}
|
||||||
|
|
||||||
onClose event.FuncBroadcaster
|
on struct {
|
||||||
|
close event.FuncBroadcaster
|
||||||
|
tryClose event.Broadcaster[func () bool]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type windowLink struct {
|
type windowLink struct {
|
||||||
@ -63,27 +68,12 @@ func (this *windowLink) NotifyMinimumSizeChange () {
|
|||||||
this.window.doMinimumSize()
|
this.window.doMinimumSize()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Backend) NewWindow (
|
func (this *Backend) NewWindow (kind tomo.WindowKind, bounds image.Rectangle) (tomo.Window, error) {
|
||||||
bounds image.Rectangle,
|
|
||||||
) (
|
|
||||||
output tomo.Window,
|
|
||||||
err error,
|
|
||||||
) {
|
|
||||||
this.assert()
|
this.assert()
|
||||||
return this.newWindow(bounds, false)
|
window, err := this.newWindow(bounds, kind == tomo.WindowKindMenu)
|
||||||
}
|
if err != nil { return nil, err }
|
||||||
|
window.setKind(kind)
|
||||||
func (this *Backend) NewPlainWindow (
|
return window, nil
|
||||||
bounds image.Rectangle,
|
|
||||||
) (
|
|
||||||
output tomo.Window,
|
|
||||||
err error,
|
|
||||||
) {
|
|
||||||
this.assert()
|
|
||||||
window, err := this.newWindow(bounds, false)
|
|
||||||
window.setType("dock")
|
|
||||||
|
|
||||||
return window, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Backend) newWindow (
|
func (this *Backend) newWindow (
|
||||||
@ -130,7 +120,11 @@ func (this *Backend) newWindow (
|
|||||||
if err != nil { return }
|
if err != nil { return }
|
||||||
|
|
||||||
window.xWindow.WMGracefulClose (func (xWindow *xwindow.Window) {
|
window.xWindow.WMGracefulClose (func (xWindow *xwindow.Window) {
|
||||||
window.Close()
|
holdOff := false
|
||||||
|
for _, callback := range window.on.tryClose.Listeners() {
|
||||||
|
if !callback() { holdOff = true }
|
||||||
|
}
|
||||||
|
if !holdOff { window.Close() }
|
||||||
})
|
})
|
||||||
|
|
||||||
xevent.ExposeFun(window.handleExpose).
|
xevent.ExposeFun(window.handleExpose).
|
||||||
@ -156,7 +150,7 @@ func (this *Backend) newWindow (
|
|||||||
// xevent.SelectionRequestFun(window.handleSelectionRequest).
|
// xevent.SelectionRequestFun(window.handleSelectionRequest).
|
||||||
// Connect(this.x, window.xWindow.Id)
|
// Connect(this.x, window.xWindow.Id)
|
||||||
|
|
||||||
window.metrics.bounds = bounds
|
window.metrics.innerBounds = bounds
|
||||||
window.doMinimumSize()
|
window.doMinimumSize()
|
||||||
|
|
||||||
this.windows[window.xWindow.Id] = window
|
this.windows[window.xWindow.Id] = window
|
||||||
@ -165,6 +159,14 @@ func (this *Backend) newWindow (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *window) Bounds () image.Rectangle {
|
||||||
|
return this.metrics.bounds.Sub(this.metrics.innerBounds.Min)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *window) InnerBounds () image.Rectangle {
|
||||||
|
return this.metrics.innerBounds.Sub(this.metrics.innerBounds.Min)
|
||||||
|
}
|
||||||
|
|
||||||
func (this *window) SetRoot (root tomo.Object) {
|
func (this *window) SetRoot (root tomo.Object) {
|
||||||
if root == nil {
|
if root == nil {
|
||||||
this.hierarchy.SetRoot(nil)
|
this.hierarchy.SetRoot(nil)
|
||||||
@ -238,56 +240,29 @@ func (this *window) SetBounds (bounds image.Rectangle) {
|
|||||||
bounds.Min.Y + bounds.Dy())
|
bounds.Min.Y + bounds.Dy())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *window) NewChild (bounds image.Rectangle) (tomo.Window, error) {
|
func (this *window) NewChild (kind tomo.WindowKind, bounds image.Rectangle) (tomo.Window, error) {
|
||||||
leader := this.leader
|
leader := this.leader
|
||||||
|
|
||||||
child, err := this.backend.newWindow (
|
child, err := this.backend.newWindow (
|
||||||
bounds.Add(this.metrics.bounds.Min), false)
|
bounds.Add(this.metrics.innerBounds.Min), kind == tomo.WindowKindMenu)
|
||||||
child.leader = leader
|
|
||||||
if err != nil { return nil, err }
|
if err != nil { return nil, err }
|
||||||
|
|
||||||
child.setClientLeader(leader)
|
child.leader = leader
|
||||||
leader.setClientLeader(leader)
|
err = child.setKind(kind)
|
||||||
|
if err != nil { return nil, err }
|
||||||
|
if kind == tomo.WindowKindModal {
|
||||||
|
this.hasModal = true
|
||||||
|
child.modalParent = this
|
||||||
|
}
|
||||||
|
|
||||||
icccm.WmTransientForSet (
|
icccm.WmTransientForSet (
|
||||||
this.backend.x,
|
this.backend.x,
|
||||||
child.xWindow.Id,
|
child.xWindow.Id,
|
||||||
leader.xWindow.Id)
|
leader.xWindow.Id)
|
||||||
child.setType("UTILITY")
|
|
||||||
// child.inheritProperties(leader.window)
|
// child.inheritProperties(leader.window)
|
||||||
return child, err
|
return child, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *window) NewMenu (bounds image.Rectangle) (tomo.Window, error) {
|
|
||||||
menu, err := this.backend.newWindow (
|
|
||||||
bounds.Add(this.metrics.bounds.Min), true)
|
|
||||||
menu.shy = true
|
|
||||||
icccm.WmTransientForSet (
|
|
||||||
this.backend.x,
|
|
||||||
menu.xWindow.Id,
|
|
||||||
this.xWindow.Id)
|
|
||||||
menu.setType("POPUP_MENU")
|
|
||||||
// menu.inheritProperties(this)
|
|
||||||
return menu, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *window) NewModal (bounds image.Rectangle) (tomo.Window, error) {
|
|
||||||
modal, err := this.backend.newWindow (
|
|
||||||
bounds.Add(this.metrics.bounds.Min), false)
|
|
||||||
icccm.WmTransientForSet (
|
|
||||||
this.backend.x,
|
|
||||||
modal.xWindow.Id,
|
|
||||||
this.xWindow.Id)
|
|
||||||
ewmh.WmStateSet (
|
|
||||||
this.backend.x,
|
|
||||||
modal.xWindow.Id,
|
|
||||||
[]string { "_NET_WM_STATE_MODAL" })
|
|
||||||
modal.modalParent = this
|
|
||||||
this.hasModal = true
|
|
||||||
// modal.inheritProperties(window)
|
|
||||||
return modal, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *window) Modifiers () input.Modifiers {
|
func (this *window) Modifiers () input.Modifiers {
|
||||||
return this.hierarchy.Modifiers()
|
return this.hierarchy.Modifiers()
|
||||||
}
|
}
|
||||||
@ -321,12 +296,12 @@ func (this *window) Visible () bool {
|
|||||||
return this.visible
|
return this.visible
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *window) Close () {
|
func (this *window) Close () error {
|
||||||
xevent .Detach(this.backend.x, this.xWindow.Id)
|
xevent .Detach(this.backend.x, this.xWindow.Id)
|
||||||
keybind .Detach(this.backend.x, this.xWindow.Id)
|
keybind .Detach(this.backend.x, this.xWindow.Id)
|
||||||
mousebind.Detach(this.backend.x, this.xWindow.Id)
|
mousebind.Detach(this.backend.x, this.xWindow.Id)
|
||||||
|
|
||||||
this.onClose.Broadcast()
|
this.on.close.Broadcast()
|
||||||
if this.modalParent != nil {
|
if this.modalParent != nil {
|
||||||
// we are a modal dialog, so unlock the parent
|
// we are a modal dialog, so unlock the parent
|
||||||
this.modalParent.hasModal = false
|
this.modalParent.hasModal = false
|
||||||
@ -336,10 +311,15 @@ func (this *window) Close () {
|
|||||||
delete(this.backend.windows, this.xWindow.Id)
|
delete(this.backend.windows, this.xWindow.Id)
|
||||||
this.xWindow.Destroy()
|
this.xWindow.Destroy()
|
||||||
this.hierarchy.Close()
|
this.hierarchy.Close()
|
||||||
|
return nil // TODO maybe return an error? maybe join them?
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *window) OnClose (callback func ()) event.Cookie {
|
func (this *window) OnClose (callback func ()) event.Cookie {
|
||||||
return this.onClose.Connect(callback)
|
return this.on.close.Connect(callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *window) OnTryClose (callback func () bool) event.Cookie {
|
||||||
|
return this.on.tryClose.Connect(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *window) grabInput () {
|
func (this *window) grabInput () {
|
||||||
@ -355,6 +335,19 @@ func (this *window) ungrabInput () {
|
|||||||
mousebind.UngrabPointer(this.backend.x)
|
mousebind.UngrabPointer(this.backend.x)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *window) setKind (kind tomo.WindowKind) error {
|
||||||
|
err := this.setType(windowKindToType(kind))
|
||||||
|
if err != nil { return err }
|
||||||
|
if kind == tomo.WindowKindModal {
|
||||||
|
err = this.setState("MODAL")
|
||||||
|
if err != nil { return err }
|
||||||
|
}
|
||||||
|
if kind == tomo.WindowKindMenu {
|
||||||
|
this.shy = true
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (this *window) setType (ty string) error {
|
func (this *window) setType (ty string) error {
|
||||||
return ewmh.WmWindowTypeSet (
|
return ewmh.WmWindowTypeSet (
|
||||||
this.backend.x,
|
this.backend.x,
|
||||||
@ -362,6 +355,13 @@ func (this *window) setType (ty string) error {
|
|||||||
[]string { "_NET_WM_WINDOW_TYPE_" + ty })
|
[]string { "_NET_WM_WINDOW_TYPE_" + ty })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *window) setState (state string) error {
|
||||||
|
return ewmh.WmStateSet (
|
||||||
|
this.backend.x,
|
||||||
|
this.xWindow.Id,
|
||||||
|
[]string { "_NET_WM_STATE_" + state })
|
||||||
|
}
|
||||||
|
|
||||||
func (this *window) setClientLeader (leader *window) error {
|
func (this *window) setClientLeader (leader *window) error {
|
||||||
hints, _ := icccm.WmHintsGet(this.backend.x, this.xWindow.Id)
|
hints, _ := icccm.WmHintsGet(this.backend.x, this.xWindow.Id)
|
||||||
if hints == nil {
|
if hints == nil {
|
||||||
@ -382,8 +382,8 @@ func (this *window) reallocateCanvas () {
|
|||||||
previousHeight = this.xCanvas.Bounds().Dy()
|
previousHeight = this.xCanvas.Bounds().Dy()
|
||||||
}
|
}
|
||||||
|
|
||||||
newWidth := this.metrics.bounds.Dx()
|
newWidth := this.metrics.innerBounds.Dx()
|
||||||
newHeight := this.metrics.bounds.Dy()
|
newHeight := this.metrics.innerBounds.Dy()
|
||||||
larger := newWidth > previousWidth || newHeight > previousHeight
|
larger := newWidth > previousWidth || newHeight > previousHeight
|
||||||
smaller := newWidth < previousWidth / 2 || newHeight < previousHeight / 2
|
smaller := newWidth < previousWidth / 2 || newHeight < previousHeight / 2
|
||||||
|
|
||||||
@ -403,7 +403,7 @@ func (this *window) reallocateCanvas () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.hierarchy.SetCanvas(this.xCanvas.SubCanvas (
|
this.hierarchy.SetCanvas(this.xCanvas.SubCanvas (
|
||||||
this.metrics.bounds.Sub(this.metrics.bounds.Min)))
|
this.metrics.innerBounds.Sub(this.metrics.innerBounds.Min)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *window) pushAll () {
|
func (this *window) pushAll () {
|
||||||
@ -458,12 +458,24 @@ func (this *window) doMinimumSize () {
|
|||||||
this.backend.x,
|
this.backend.x,
|
||||||
this.xWindow.Id,
|
this.xWindow.Id,
|
||||||
&hints)
|
&hints)
|
||||||
newWidth := this.metrics.bounds.Dx()
|
newWidth := this.metrics.innerBounds.Dx()
|
||||||
newHeight := this.metrics.bounds.Dy()
|
newHeight := this.metrics.innerBounds.Dy()
|
||||||
if newWidth < size.X { newWidth = size.X }
|
if newWidth < size.X { newWidth = size.X }
|
||||||
if newHeight < size.Y { newHeight = size.Y }
|
if newHeight < size.Y { newHeight = size.Y }
|
||||||
if newWidth != this.metrics.bounds.Dx() ||
|
if newWidth != this.metrics.innerBounds.Dx() ||
|
||||||
newHeight != this.metrics.bounds.Dy() {
|
newHeight != this.metrics.innerBounds.Dy() {
|
||||||
this.xWindow.Resize(newWidth, newHeight)
|
this.xWindow.Resize(newWidth, newHeight)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func windowKindToType (kind tomo.WindowKind) string {
|
||||||
|
switch kind {
|
||||||
|
case tomo.WindowKindNormal: return "NORMAL"
|
||||||
|
case tomo.WindowKindPlain: return "DOCK"
|
||||||
|
case tomo.WindowKindUtility: return "UTILITY"
|
||||||
|
case tomo.WindowKindToolbar: return "MENU"
|
||||||
|
case tomo.WindowKindMenu: return "POPUP_MENU"
|
||||||
|
case tomo.WindowKindModal: return "NORMAL"
|
||||||
|
default: return strings.ReplaceAll(strings.ToUpper(string(kind)), " ", "_")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user