Styling now supports selection

This commit is contained in:
2023-01-09 15:14:36 -05:00
parent d1ec5f2cec
commit 70e0566f3f
6 changed files with 125 additions and 120 deletions

View File

@@ -1,7 +1,6 @@
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"
@@ -10,8 +9,9 @@ type Button struct {
*Core
core CoreControl
pressed bool
enabled bool
pressed bool
enabled bool
selected bool
onClick func ()
text string
@@ -39,6 +39,7 @@ func (element *Button) Handle (event tomo.Event) {
if !element.enabled { break }
mouseDownEvent := event.(tomo.EventMouseDown)
element.Select()
if mouseDownEvent.Button != tomo.ButtonLeft { break }
element.pressed = true
if element.core.HasImage() {
@@ -63,11 +64,39 @@ func (element *Button) Handle (event tomo.Event) {
if within && element.onClick != nil {
element.onClick()
}
case tomo.EventSelect:
element.selected = true
case tomo.EventDeselect:
element.selected = false
// TODO: handle selection events, and the enter key
}
return
}
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) Selectable () (selectable bool) {
return true
}
func (element *Button) Select () {
element.core.Select()
}
func (element *Button) SetEnabled (enabled bool) {
if element.enabled == enabled { return }
element.enabled = enabled
@@ -92,24 +121,15 @@ func (element *Button) SetText (text string) {
}
}
func (element *Button) OnClick (callback func ()) {
element.onClick = callback
}
func (element *Button) AdvanceSelection (direction int) (ok bool) {
return
}
func (element *Button) Selectable () (selectable bool) {
return true
}
func (element *Button) draw () {
bounds := element.core.Bounds()
artist.ChiseledRectangle (
element.core,
theme.RaisedProfile(element.pressed, element.enabled),
theme.RaisedProfile (
element.pressed,
element.enabled,
element.selected),
bounds)
innerBounds := bounds

View File

@@ -67,16 +67,19 @@ func (control CoreControl) HasImage () (has bool) {
return
}
func (control CoreControl) Select () {
control.core.hooks.RunSelectionRequest()
}
func (control CoreControl) PushRegion (bounds image.Rectangle) {
core := control.core
core.hooks.RunDraw(control.SubImage(bounds).(*image.RGBA))
control.core.hooks.RunDraw(control.SubImage(bounds).(*image.RGBA))
}
func (control CoreControl) PushAll () {
control.PushRegion(control.Bounds())
}
func (control CoreControl) AllocateCanvas (width, height int) {
func (control *CoreControl) AllocateCanvas (width, height int) {
core := control.core
width, height, _ = control.ConstrainSize(width, height)
core.canvas = image.NewRGBA(image.Rect (0, 0, width, height))

View File

@@ -1,24 +1,24 @@
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"
type Label struct {
core Core
*Core
core CoreControl
text string
drawer artist.TextDrawer
}
func NewLabel (text string) (element *Label) {
element = &Label { }
element.core = NewCore(element)
element.Core, element.core = NewCore(element)
face := theme.FontFaceRegular()
element.drawer.SetFace(face)
element.SetText(text)
// FIXME: set the minimum size to one char
metrics := face.Metrics()
emspace, _ := face.GlyphAdvance('M')
intEmspace := emspace.Round()
@@ -52,46 +52,11 @@ func (element *Label) SetText (text string) {
}
}
func (element *Label) ColorModel () (model color.Model) {
return color.RGBAModel
}
func (element *Label) At (x, y int) (pixel color.Color) {
pixel = element.core.At(x, y)
return
}
func (element *Label) RGBAAt (x, y int) (pixel color.RGBA) {
pixel = element.core.RGBAAt(x, y)
return
}
func (element *Label) Bounds () (bounds image.Rectangle) {
bounds = element.core.Bounds()
return
}
func (element *Label) SetDrawCallback (draw func (region tomo.Image)) {
element.core.SetDrawCallback(draw)
}
func (element *Label) SetMinimumSizeChangeCallback (
notify func (width, height int),
) {
element.core.SetMinimumSizeChangeCallback(notify)
}
func (element *Label) Selectable () (selectable bool) {
return
}
func (element *Label) MinimumWidth () (minimum int) {
minimum = element.core.MinimumWidth()
return
}
func (element *Label) MinimumHeight () (minimum int) {
minimum = element.core.MinimumHeight()
func (element *Label) AdvanceSelection (direction int) (ok bool) {
return
}

View File

@@ -7,13 +7,14 @@ import "git.tebibyte.media/sashakoshka/tomo/artist"
// Test is a simple element that can be used as a placeholder.
type Test struct {
core Core
*Core
core CoreControl
}
// NewTest creates a new test element.
func NewTest () (element *Test) {
element = &Test { }
element.core = NewCore(element)
element.Core, element.core = NewCore(element)
element.core.SetMinimumSize(32, 32)
return
}
@@ -46,45 +47,10 @@ func (element *Test) Handle (event tomo.Event) {
return
}
func (element *Test) ColorModel () (model color.Model) {
return color.RGBAModel
}
func (element *Test) At (x, y int) (pixel color.Color) {
pixel = element.core.At(x, y)
return
}
func (element *Test) RGBAAt (x, y int) (pixel color.RGBA) {
pixel = element.core.RGBAAt(x, y)
return
}
func (element *Test) Bounds () (bounds image.Rectangle) {
bounds = element.core.Bounds()
return
}
func (element *Test) SetDrawCallback (draw func (region tomo.Image)) {
element.core.SetDrawCallback(draw)
}
func (element *Test) SetMinimumSizeChangeCallback (
notify func (width, height int),
) {
element.core.SetMinimumSizeChangeCallback(notify)
}
func (element *Test) Selectable () (selectable bool) {
return
}
func (element *Test) MinimumWidth () (minimum int) {
minimum = element.core.MinimumWidth()
return
}
func (element *Test) MinimumHeight () (minimum int) {
minimum = element.core.MinimumHeight()
func (element *Test) AdvanceSelection (direction int) (ok bool) {
return
}