Compare commits
12 Commits
v0.5.0
...
fad46eafd3
| Author | SHA1 | Date | |
|---|---|---|---|
| fad46eafd3 | |||
| ddde2a79a8 | |||
| 180a5eb8d1 | |||
| a92951f891 | |||
| 37ec962d1f | |||
| 4f89b11799 | |||
| 7809aac72f | |||
| bb082d3989 | |||
| fbb6d61cfc | |||
| e4cba4a7c9 | |||
| 6192a1e9cc | |||
| 5864c74691 |
@@ -69,6 +69,7 @@ func (this *System) newBox (outer anyBox) *box {
|
||||
system: this,
|
||||
outer: outer,
|
||||
drawer: outer,
|
||||
tags: make(util.Set[string]),
|
||||
}
|
||||
box.canvas = util.NewMemo (func () canvas.Canvas {
|
||||
if box.parent == nil { return nil }
|
||||
@@ -335,13 +336,6 @@ func (this *box) handleMouseDown (button input.Button) (caught bool) {
|
||||
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() {
|
||||
if listener(button) { caught = true }
|
||||
}
|
||||
@@ -483,9 +477,6 @@ func (this *box) calculateMinimumSize () image.Point {
|
||||
minSize.Y = userMinSize.Y
|
||||
}
|
||||
|
||||
if this.parent != nil {
|
||||
this.parent.notifyMinimumSizeChange(this)
|
||||
}
|
||||
return minSize
|
||||
}
|
||||
|
||||
@@ -512,7 +503,7 @@ func (this *box) doLayout () {
|
||||
}
|
||||
|
||||
func (this *box) doStyle () {
|
||||
this.styleApplicator.apply(this)
|
||||
this.styleApplicator.apply(this.outer)
|
||||
}
|
||||
|
||||
func (this *box) setParent (parent parent) {
|
||||
@@ -563,6 +554,9 @@ func (this *box) invalidateDraw () {
|
||||
|
||||
func (this *box) invalidateMinimum () {
|
||||
this.minSize.Invalidate()
|
||||
if this.parent != nil {
|
||||
this.parent.notifyMinimumSizeChange(this)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *box) recursiveReApply () {
|
||||
@@ -614,7 +608,8 @@ func (this *box) propagateAlt (callback func (anyBox) bool) bool {
|
||||
func (this *box) transparent () bool {
|
||||
// TODO uncomment once we have
|
||||
// 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())*/
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ type containerBox struct {
|
||||
attrLayout attrHierarchy[tomo.AttrLayout]
|
||||
|
||||
children []anyBox
|
||||
layout tomo.Layout
|
||||
|
||||
on struct {
|
||||
contentBoundsChange event.FuncBroadcaster
|
||||
@@ -187,19 +186,21 @@ func (this *containerBox) setAttr (attr tomo.Attr, user bool) {
|
||||
}
|
||||
|
||||
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
|
||||
} else {
|
||||
return this.layout.RecommendedHeight(this.layoutHints(), this.boxQuerier(), width) +
|
||||
return layout.RecommendedHeight(this.layoutHints(), this.boxQuerier(), width) +
|
||||
this.borderAndPaddingSum().Vertical()
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
} else {
|
||||
return this.layout.RecommendedWidth(this.layoutHints(), this.boxQuerier(), height) +
|
||||
return layout.RecommendedWidth(this.layoutHints(), this.boxQuerier(), height) +
|
||||
this.borderAndPaddingSum().Horizontal()
|
||||
}
|
||||
}
|
||||
@@ -274,8 +275,9 @@ func (this *containerBox) layoutHints () tomo.LayoutHints {
|
||||
func (this *containerBox) contentMinimum () image.Point {
|
||||
overflow := this.attrOverflow.Value()
|
||||
minimum := this.box.contentMinimum()
|
||||
if this.layout != nil {
|
||||
layoutMinimum := this.layout.MinimumSize (
|
||||
layout := this.attrLayout.Value().Layout
|
||||
if layout != nil {
|
||||
layoutMinimum := layout.MinimumSize (
|
||||
this.layoutHints(),
|
||||
this.boxQuerier())
|
||||
if overflow.X { layoutMinimum.X = 0 }
|
||||
@@ -288,12 +290,13 @@ func (this *containerBox) contentMinimum () image.Point {
|
||||
func (this *containerBox) doLayout () {
|
||||
this.box.doLayout()
|
||||
previousContentBounds := this.contentBounds
|
||||
layout := this.attrLayout.Value().Layout
|
||||
|
||||
// by default, use innerBounds (translated to 0, 0) for contentBounds.
|
||||
// if a direction overflows, use the layout's minimum size for it.
|
||||
var minimum image.Point
|
||||
if this.layout != nil {
|
||||
minimum = this.layout.MinimumSize (
|
||||
if layout != nil {
|
||||
minimum = layout.MinimumSize (
|
||||
this.layoutHints(),
|
||||
this.boxQuerier())
|
||||
}
|
||||
@@ -304,10 +307,10 @@ func (this *containerBox) doLayout () {
|
||||
if overflow.Y { this.contentBounds.Max.Y = this.contentBounds.Min.Y + minimum.Y }
|
||||
|
||||
// arrange children
|
||||
if this.layout != nil {
|
||||
if layout != nil {
|
||||
layoutHints := this.layoutHints()
|
||||
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
|
||||
@@ -323,7 +326,7 @@ func (this *containerBox) doLayout () {
|
||||
|
||||
// offset children and contentBounds by scroll
|
||||
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)
|
||||
|
||||
|
||||
@@ -62,7 +62,16 @@ func (this *Hierarchy) HandleKeyUp (key input.Key, numberPad bool) {
|
||||
// information, HandleMouseMove must be called *before* HandleMouseDown.
|
||||
func (this *Hierarchy) HandleMouseDown (button input.Button) {
|
||||
boxes := []anyBox { }
|
||||
first := true
|
||||
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)
|
||||
return !box.handleMouseDown(button)
|
||||
})
|
||||
|
||||
@@ -341,15 +341,21 @@ func (this *Hierarchy) drawBackgroundPart (canvas.Canvas) {
|
||||
// if so, windows should be transparent if the color has transparency
|
||||
}
|
||||
|
||||
// var minimumSizeCount = 0
|
||||
func (this *Hierarchy) doMinimumSize () {
|
||||
this.minimumClean = true
|
||||
// println("doMinimumSize", minimumSizeCount)
|
||||
// minimumSizeCount ++
|
||||
|
||||
previousMinimumSize := this.minimumSize
|
||||
this.minimumSize = image.Point { }
|
||||
if this.root != nil {
|
||||
this.minimumSize = this.root.minimumSize()
|
||||
}
|
||||
|
||||
if previousMinimumSize != this.minimumSize {
|
||||
this.link.NotifyMinimumSizeChange()
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Hierarchy) newStyleApplicator () *styleApplicator {
|
||||
|
||||
@@ -5,20 +5,21 @@ import "git.tebibyte.media/tomo/tomo"
|
||||
type styleApplicator struct {
|
||||
style *tomo.Style
|
||||
role tomo.Role
|
||||
rules []*tomo.Rule
|
||||
rules []tomo.Rule
|
||||
}
|
||||
|
||||
func (this *styleApplicator) apply (box anyBox) {
|
||||
if box.Role() != this.role {
|
||||
this.role = box.Role()
|
||||
// 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 {
|
||||
role := box.Role()
|
||||
// blank fields match anything
|
||||
if rule.Role.Package == "" { role.Package = "" }
|
||||
if rule.Role.Object == "" { role.Object = "" }
|
||||
if rule.Role == role {
|
||||
this.rules = append(this.rules, &rule)
|
||||
this.rules = append(this.rules, rule)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ type textBox struct {
|
||||
attrOverflow attrHierarchy[tomo.AttrOverflow]
|
||||
|
||||
text string
|
||||
runes []rune
|
||||
selectable bool
|
||||
selecting bool
|
||||
selectStart int
|
||||
@@ -71,7 +72,8 @@ func (this *textBox) OnContentBoundsChange (callback func()) event.Cookie {
|
||||
func (this *textBox) SetText (text string) {
|
||||
if this.text == text { return }
|
||||
this.text = text
|
||||
this.drawer.SetText([]rune(text))
|
||||
this.runes = []rune(text)
|
||||
this.drawer.SetText(this.runes)
|
||||
this.invalidateMinimum()
|
||||
this.invalidateLayout()
|
||||
}
|
||||
@@ -109,10 +111,10 @@ func (this *textBox) Draw (can canvas.Canvas) {
|
||||
if col == nil { col = color.Transparent }
|
||||
|
||||
this.drawBorders(can)
|
||||
|
||||
pen := can.Pen()
|
||||
pen.Fill(col)
|
||||
pen.Texture(texture)
|
||||
|
||||
if this.transparent() && this.parent != nil {
|
||||
this.parent.drawBackgroundPart(can)
|
||||
}
|
||||
@@ -150,12 +152,16 @@ func (this *textBox) setAttr (attr tomo.Attr, user bool) {
|
||||
|
||||
case tomo.AttrWrap:
|
||||
if this.attrWrap.Set(attr, user) {
|
||||
this.drawer.SetWrap(bool(attr))
|
||||
this.invalidateMinimum()
|
||||
this.invalidateLayout()
|
||||
}
|
||||
|
||||
case tomo.AttrAlign:
|
||||
if this.attrAlign.Set(attr, user) {
|
||||
this.drawer.SetAlign (
|
||||
typeset.Align(attr.X),
|
||||
typeset.Align(attr.Y))
|
||||
this.invalidateDraw()
|
||||
}
|
||||
|
||||
@@ -182,12 +188,10 @@ func (this *textBox) drawDot (can canvas.Canvas) {
|
||||
face := this.attrFace.Value().Face
|
||||
textColor := this.attrTextColor.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 } }
|
||||
|
||||
pen := can.Pen()
|
||||
pen.Fill(color.Transparent)
|
||||
pen.Stroke(textColor)
|
||||
|
||||
bounds := this.InnerBounds()
|
||||
metrics := face.Metrics()
|
||||
@@ -200,6 +204,7 @@ func (this *textBox) drawDot (can canvas.Canvas) {
|
||||
|
||||
switch {
|
||||
case dot.Empty():
|
||||
pen.Stroke(textColor)
|
||||
pen.StrokeWeight(1)
|
||||
pen.Path(roundPt(start.Add(ascent)), roundPt(start.Sub(descent)))
|
||||
|
||||
@@ -283,6 +288,71 @@ func (this *textBox) runeUnderMouse () int {
|
||||
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 {
|
||||
bounds := this.drawer.LayoutBoundsSpace()
|
||||
return bounds.Sub(bounds.Min)
|
||||
|
||||
@@ -83,6 +83,7 @@ func NewMemo[T any] (update func () T) Memo[T] {
|
||||
func (this *Memo[T]) Value () T {
|
||||
if !this.valid {
|
||||
this.cache = this.update()
|
||||
this.valid = true
|
||||
}
|
||||
return this.cache
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user