24 Commits

Author SHA1 Message Date
bacdd81f60 Propagate mouse motion events to boxes 2024-07-27 15:13:49 -04:00
d944f6016f Update Tomo API to v0.41.1 2024-07-27 15:04:11 -04:00
01582d4ad1 Same for TextBox 2024-07-27 13:47:22 -04:00
3941dae44a Invalidate container minimum size when overflow is changed 2024-07-27 13:46:52 -04:00
85b8536925 Propagate keyboard events to root if nothing is focused
This makes window-level keybinds possible. Exciting!
2024-07-27 02:20:06 -04:00
6ff5dea308 ContainerBox correctly checks for overflow when reporting recommended size 2024-07-27 02:18:52 -04:00
33969f45e9 BoxQuerier returns box minimum size as a fallback for reccomended sizes 2024-07-27 02:17:56 -04:00
832d7e02ef TextBox can be selected with left, middle, and right buttons 2024-07-26 21:17:30 -04:00
fd6297b4fb And arrow keys! Because, why not! 2024-07-26 20:55:34 -04:00
4deb581667 Use tab for keynav instead of alt-tab 2024-07-26 20:52:49 -04:00
192e6c6235 Keynav skips masked boxes 2024-07-26 20:49:10 -04:00
9729e3dfda Fix selectable detection when using keys on TextBox 2024-07-26 20:48:44 -04:00
fad46eafd3 All selectable TextBoxes have keyboard controls 2024-07-26 18:43:12 -04:00
ddde2a79a8 TextBox defaults to black for a cursor color 2024-07-26 17:34:33 -04:00
180a5eb8d1 Hierarchy is now responsible for focusing boxes when they are clicked 2024-07-26 17:34:14 -04:00
a92951f891 Remove debug message 2024-07-26 00:29:07 -04:00
37ec962d1f TextBox properly gives attributes values to the typeset drawer 2024-07-26 00:27:32 -04:00
4f89b11799 Box applies the style to the outer box (oops!) 2024-07-26 00:22:10 -04:00
7809aac72f Actually use layouts 2024-07-25 21:05:03 -04:00
bb082d3989 Change when the parent is notified of a child's minimum size change 2024-07-25 21:04:32 -04:00
fbb6d61cfc Fix style application 2024-07-25 21:04:21 -04:00
e4cba4a7c9 Add check while calculating min size to prevent goofy situations 2024-07-25 20:37:38 -04:00
6192a1e9cc Fixed util.Memo 2024-07-25 20:37:09 -04:00
5864c74691 Fix some segfaults 2024-07-25 18:17:43 -04:00
10 changed files with 185 additions and 58 deletions

2
go.mod
View File

@@ -3,7 +3,7 @@ module git.tebibyte.media/tomo/backend
go 1.20 go 1.20
require ( require (
git.tebibyte.media/tomo/tomo v0.41.0 git.tebibyte.media/tomo/tomo v0.41.1
git.tebibyte.media/tomo/typeset v0.7.1 git.tebibyte.media/tomo/typeset v0.7.1
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

4
go.sum
View File

@@ -1,6 +1,6 @@
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.41.1 h1:XdbyF3VjsLj1Zppr70gUaufuh49hU32JQo2ENnw4PcA=
git.tebibyte.media/tomo/tomo v0.41.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps= git.tebibyte.media/tomo/tomo v0.41.1/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
git.tebibyte.media/tomo/typeset v0.7.1 h1:aZrsHwCG5ZB4f5CruRFsxLv5ezJUCFUFsQJJso2sXQ8= git.tebibyte.media/tomo/typeset v0.7.1 h1:aZrsHwCG5ZB4f5CruRFsxLv5ezJUCFUFsQJJso2sXQ8=
git.tebibyte.media/tomo/typeset v0.7.1/go.mod h1:PwDpSdBF3l/EzoIsa2ME7QffVVajnTHZN6l3MHEGe1g= git.tebibyte.media/tomo/typeset v0.7.1/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=

View File

@@ -69,6 +69,7 @@ func (this *System) newBox (outer anyBox) *box {
system: this, system: this,
outer: outer, outer: outer,
drawer: outer, drawer: outer,
tags: make(util.Set[string]),
} }
box.canvas = util.NewMemo (func () canvas.Canvas { box.canvas = util.NewMemo (func () canvas.Canvas {
if box.parent == nil { return nil } if box.parent == nil { return nil }
@@ -335,13 +336,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 }
} }
@@ -483,9 +477,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 +503,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) {
@@ -563,6 +554,9 @@ 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 () {
@@ -614,7 +608,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 || util.Transparent(col) /*&&
(this.texture == nil || !this.texture.Opaque())*/ (this.texture == nil || !this.texture.Opaque())*/
} }

View File

@@ -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
} }

View File

@@ -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
@@ -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:
@@ -187,19 +187,21 @@ func (this *containerBox) setAttr (attr tomo.Attr, user bool) {
} }
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()
} }
} }
@@ -274,8 +276,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 +291,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 +308,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 +327,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)

View File

@@ -3,7 +3,6 @@ package system
import "image" import "image"
import "git.tebibyte.media/tomo/tomo/input" import "git.tebibyte.media/tomo/tomo/input"
// 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 +24,30 @@ 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 key {
case input.KeyTab:
if this.modifiers.Shift { if this.modifiers.Shift {
this.focusPrevious() this.focusPrevious()
} else { } else {
this.focusNext() this.focusNext()
} }
case input.KeyUp: this.focusPrevious()
case input.KeyDown: this.focusNext()
} }
} }
@@ -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()
} }

View File

@@ -287,13 +287,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 +331,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 +354,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 {

View File

@@ -5,20 +5,21 @@ import "git.tebibyte.media/tomo/tomo"
type styleApplicator struct { type styleApplicator struct {
style *tomo.Style style *tomo.Style
role tomo.Role role tomo.Role
rules []*tomo.Rule rules []tomo.Rule
} }
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)
} }
} }
} }

View File

@@ -24,6 +24,7 @@ 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
@@ -70,8 +71,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()
} }
@@ -109,10 +111,10 @@ func (this *textBox) Draw (can canvas.Canvas) {
if col == nil { col = color.Transparent } 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)
} }
@@ -150,17 +152,22 @@ func (this *textBox) setAttr (attr tomo.Attr, user bool) {
case tomo.AttrWrap: case tomo.AttrWrap:
if this.attrWrap.Set(attr, user) { if this.attrWrap.Set(attr, user) {
this.drawer.SetWrap(bool(attr))
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) {
this.drawer.SetAlign (
typeset.Align(attr.X),
typeset.Align(attr.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()
} }
@@ -182,12 +189,10 @@ func (this *textBox) drawDot (can canvas.Canvas) {
face := this.attrFace.Value().Face 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 textColor == nil { textColor = color.Black }
if dotColor == nil { dotColor = color.RGBA { G: 255, B: 255, A: 255 } } 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()
@@ -200,6 +205,7 @@ func (this *textBox) drawDot (can canvas.Canvas) {
switch { switch {
case dot.Empty(): case dot.Empty():
pen.Stroke(textColor)
pen.StrokeWeight(1) pen.StrokeWeight(1)
pen.Path(roundPt(start.Add(ascent)), roundPt(start.Sub(descent))) pen.Path(roundPt(start.Add(ascent)), roundPt(start.Sub(descent)))
@@ -243,14 +249,18 @@ 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
@@ -260,7 +270,7 @@ func (this *textBox) handleMouseDown (button input.Button) bool {
} }
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.Select(text.Dot { Start: this.selectStart, End: index })
@@ -268,6 +278,12 @@ func (this *textBox) handleMouseUp (button input.Button) bool {
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()
@@ -283,6 +299,71 @@ func (this *textBox) runeUnderMouse () int {
return this.drawer.AtPosition(fixPt(position)) return this.drawer.AtPosition(fixPt(position))
} }
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
switch {
case key == input.KeyHome || (modifiers.Alt && key == input.KeyLeft):
dot.End = 0
if !sel { dot.Start = dot.End }
this.Select(dot)
return true
case key == input.KeyEnd || (modifiers.Alt && key == input.KeyRight):
dot.End = len(this.text)
if !sel { dot.Start = dot.End }
this.Select(dot)
return true
case key == input.KeyLeft:
if sel {
this.Select(text.SelectLeft(this.runes, dot, word))
} else {
this.Select(text.MoveLeft(this.runes, dot, word))
}
return true
case key == input.KeyRight:
if sel {
this.Select(text.SelectRight(this.runes, dot, word))
} else {
this.Select(text.MoveRight(this.runes, dot, word))
}
return true
case key == input.Key('a') && modifiers.Control:
dot.Start = 0
dot.End = len(this.text)
this.Select(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.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)

View File

@@ -83,6 +83,7 @@ func NewMemo[T any] (update func () T) Memo[T] {
func (this *Memo[T]) Value () T { func (this *Memo[T]) Value () T {
if !this.valid { if !this.valid {
this.cache = this.update() this.cache = this.update()
this.valid = true
} }
return this.cache return this.cache
} }