Added extended selection capabilities to the API

This commit is contained in:
2023-01-11 15:46:48 -05:00
parent 5b850ef183
commit c2a76fcaf6
8 changed files with 72 additions and 68 deletions

View File

@@ -12,7 +12,6 @@ type Button struct {
pressed bool
enabled bool
selected bool
onClick func ()
text string
@@ -91,10 +90,10 @@ func (element *Button) Handle (event tomo.Event) {
}
case tomo.EventSelect:
element.selected = true
element.core.SetSelected(true)
case tomo.EventDeselect:
element.selected = false
element.core.SetSelected(false)
}
return
}
@@ -103,16 +102,6 @@ func (element *Button) OnClick (callback func ()) {
element.onClick = callback
}
func (element *Button) AdvanceSelection (direction int) (ok bool) {
wasSelected := element.selected
element.selected = false
if element.core.HasImage() && wasSelected {
element.draw()
element.core.PushAll()
}
return
}
func (element *Button) Select () {
element.core.Select()
}
@@ -150,7 +139,7 @@ func (element *Button) draw () {
theme.RaisedProfile (
element.pressed,
element.enabled,
element.selected),
element.Selected()),
bounds)
innerBounds := bounds

View File

@@ -13,6 +13,7 @@ type Container struct {
layout tomo.Layout
children []tomo.LayoutEntry
selectable bool
selected bool
drags [10]tomo.Element
}
@@ -183,6 +184,16 @@ func (element *Container) Handle (event tomo.Event) {
Y: mouseMoveEvent.Y - childPosition.Y,
})
}
case tomo.EventSelect:
if !element.Selectable() { break }
element.core.SetSelected(true)
case tomo.EventDeselect:
element.core.SetSelected(false)
// TODO: propogate deselect event to all children who report
// themselves as selected.
}
return
}
@@ -198,6 +209,7 @@ func (element *Container) updateSelectable () {
if entry.Selectable() { selectable = true }
}
element.core.SetSelectable(selectable)
if !selectable { element.selected = false }
}
func (element *Container) updateMinimumSize () {

View File

@@ -84,10 +84,6 @@ func (element *Label) updateMinimumSize () {
}
}
func (element *Label) AdvanceSelection (direction int) (ok bool) {
return
}
func (element *Label) draw () {
bounds := element.core.Bounds()

View File

@@ -3,6 +3,7 @@ package basic
import "image"
import "image/color"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
@@ -33,9 +34,7 @@ func (element *Test) Handle (event tomo.Event) {
resizeEvent.Height)
artist.Rectangle (
element.core,
artist.NewUniform (color.RGBA {
R: 0x40, G: 0x80, B: 0x90, A: 0xFF,
}),
theme.AccentImage(),
artist.NewUniform(color.Black),
1, element.Bounds())
artist.Line (
@@ -46,7 +45,6 @@ func (element *Test) Handle (event tomo.Event) {
element.core, artist.NewUniform(color.White), 1,
image.Pt(1, resizeEvent.Height - 2),
image.Pt(resizeEvent.Width - 2, 1))
// println(resizeEvent.Width, resizeEvent.Height)
case tomo.EventMouseDown:
element.drawing = true
@@ -78,7 +76,3 @@ func (element *Test) Handle (event tomo.Event) {
}
return
}
func (element *Test) AdvanceSelection (direction int) (ok bool) {
return
}

View File

@@ -16,6 +16,7 @@ type Core struct {
}
selectable bool
selected bool
hooks tomo.ParentHooks
}
@@ -47,14 +48,22 @@ func (core Core) Bounds () (bounds image.Rectangle) {
return
}
func (core *Core) SetParentHooks (hooks tomo.ParentHooks) {
core.hooks = hooks
}
func (core Core) Selectable () (selectable bool) {
return core.selectable
}
func (core Core) Selected () (selected bool) {
return core.selected
}
func (core Core) AdvanceSelection (direction int) (ok bool) {
return
}
func (core *Core) SetParentHooks (hooks tomo.ParentHooks) {
core.hooks = hooks
}
func (core Core) MinimumSize () (width, height int) {
return core.metrics.minimumWidth, core.metrics.minimumHeight
}
@@ -76,6 +85,18 @@ func (control CoreControl) Select () {
control.core.hooks.RunSelectionRequest()
}
func (control CoreControl) SetSelected (selected bool) {
if !control.core.selectable { return }
control.core.selected = selected
}
func (control CoreControl) SetSelectable (selectable bool) {
if control.core.selectable == selectable { return }
control.core.selectable = selectable
if !selectable { control.core.selected = false }
control.core.hooks.RunSelectabilityChange(selectable)
}
func (control CoreControl) PushRegion (bounds image.Rectangle) {
control.core.hooks.RunDraw(control.SubImage(bounds).(*image.RGBA))
}
@@ -91,38 +112,31 @@ func (control *CoreControl) AllocateCanvas (width, height int) {
control.RGBA = core.canvas
}
func (control CoreControl) SetSelectable (selectable bool) {
changed := control.core.selectable != selectable
control.core.selectable = selectable
if changed {
control.core.hooks.RunSelectabilityChange(selectable)
}
}
func (control CoreControl) SetMinimumSize (width, height int) {
core := control.core
if width != core.metrics.minimumWidth ||
height != core.metrics.minimumHeight {
if width == core.metrics.minimumWidth &&
height == core.metrics.minimumHeight {
return
}
core.metrics.minimumWidth = width
core.metrics.minimumHeight = height
core.hooks.RunMinimumSizeChange(width, height)
core.metrics.minimumWidth = width
core.metrics.minimumHeight = height
core.hooks.RunMinimumSizeChange(width, height)
// if there is an image buffer, and the current size is less
// than this new minimum size, send core.parent a resize event.
if control.HasImage() {
bounds := control.Bounds()
imageWidth,
imageHeight,
constrained := control.ConstrainSize (
bounds.Dx(),
bounds.Dy())
if constrained {
core.parent.Handle (tomo.EventResize {
Width: imageWidth,
Height: imageHeight,
})
}
// if there is an image buffer, and the current size is less
// than this new minimum size, send core.parent a resize event.
if control.HasImage() {
bounds := control.Bounds()
imageWidth,
imageHeight,
constrained := control.ConstrainSize (
bounds.Dx(),
bounds.Dy())
if constrained {
core.parent.Handle (tomo.EventResize {
Width: imageWidth,
Height: imageHeight,
})
}
}
}