2023-03-30 21:19:04 -06:00
|
|
|
package elements
|
2023-01-17 14:38:57 -07:00
|
|
|
|
|
|
|
import "image"
|
2023-03-30 23:06:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-04-14 20:03:22 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
2023-02-15 16:45:58 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
|
2023-03-30 23:06:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/default/config"
|
2023-01-17 14:38:57 -07:00
|
|
|
|
|
|
|
// Checkbox is a toggle-able checkbox with a label.
|
|
|
|
type Checkbox struct {
|
2023-04-14 20:03:22 -06:00
|
|
|
entity tomo.FocusableEntity
|
2023-02-15 16:45:58 -07:00
|
|
|
drawer textdraw.Drawer
|
2023-01-27 15:55:49 -07:00
|
|
|
|
2023-04-14 20:03:22 -06:00
|
|
|
enabled bool
|
2023-01-27 15:55:49 -07:00
|
|
|
pressed bool
|
|
|
|
checked bool
|
|
|
|
text string
|
2023-01-19 14:49:34 -07:00
|
|
|
|
2023-02-08 12:36:14 -07:00
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
2023-02-07 22:22:40 -07:00
|
|
|
|
2023-01-27 15:55:49 -07:00
|
|
|
onToggle func ()
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCheckbox creates a new cbeckbox with the specified label text.
|
|
|
|
func NewCheckbox (text string, checked bool) (element *Checkbox) {
|
2023-04-14 21:58:14 -06:00
|
|
|
element = &Checkbox { checked: checked, enabled: true }
|
2023-04-14 23:14:36 -06:00
|
|
|
element.entity = tomo.NewEntity(element).(tomo.FocusableEntity)
|
2023-03-30 23:06:29 -06:00
|
|
|
element.theme.Case = tomo.C("tomo", "checkbox")
|
|
|
|
element.drawer.SetFace (element.theme.FontFace (
|
|
|
|
tomo.FontStyleRegular,
|
|
|
|
tomo.FontSizeNormal))
|
2023-01-17 14:38:57 -07:00
|
|
|
element.SetText(text)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-14 23:14:36 -06:00
|
|
|
// Entity returns this element's entity.
|
|
|
|
func (element *Checkbox) Entity () tomo.Entity {
|
|
|
|
return element.entity
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
|
2023-04-15 10:35:00 -06:00
|
|
|
// Draw causes the element to draw to the specified destination canvas.
|
|
|
|
func (element *Checkbox) Draw (destination canvas.Canvas) {
|
|
|
|
bounds := element.entity.Bounds()
|
|
|
|
boxBounds := image.Rect(0, 0, bounds.Dy(), bounds.Dy()).Add(bounds.Min)
|
|
|
|
|
|
|
|
state := tomo.State {
|
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
Focused: element.entity.Focused(),
|
|
|
|
Pressed: element.pressed,
|
|
|
|
On: element.checked,
|
|
|
|
}
|
|
|
|
|
|
|
|
element.entity.DrawBackground(destination)
|
|
|
|
|
|
|
|
pattern := element.theme.Pattern(tomo.PatternButton, state)
|
|
|
|
pattern.Draw(destination, boxBounds)
|
|
|
|
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
margin := element.theme.Margin(tomo.PatternBackground)
|
|
|
|
offset := bounds.Min.Add(image.Point {
|
|
|
|
X: bounds.Dy() + margin.X,
|
|
|
|
})
|
|
|
|
|
|
|
|
offset.Y -= textBounds.Min.Y
|
|
|
|
offset.X -= textBounds.Min.X
|
|
|
|
|
|
|
|
foreground := element.theme.Color(tomo.ColorForeground, state)
|
|
|
|
element.drawer.Draw(destination, foreground, offset)
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:55:49 -07:00
|
|
|
// OnToggle sets the function to be called when the checkbox is toggled.
|
|
|
|
func (element *Checkbox) OnToggle (callback func ()) {
|
|
|
|
element.onToggle = callback
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value reports whether or not the checkbox is currently checked.
|
|
|
|
func (element *Checkbox) Value () (checked bool) {
|
|
|
|
return element.checked
|
|
|
|
}
|
|
|
|
|
2023-04-14 20:03:22 -06:00
|
|
|
// Focus gives this element input focus.
|
|
|
|
func (element *Checkbox) Focus () {
|
|
|
|
if !element.entity.Focused() { element.entity.Focus() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enabled returns whether this checkbox is enabled or not.
|
|
|
|
func (element *Checkbox) Enabled () bool {
|
|
|
|
return element.enabled
|
|
|
|
}
|
|
|
|
|
2023-01-17 14:38:57 -07:00
|
|
|
// SetEnabled sets whether this checkbox can be toggled or not.
|
|
|
|
func (element *Checkbox) SetEnabled (enabled bool) {
|
2023-04-14 20:03:22 -06:00
|
|
|
if element.enabled == enabled { return }
|
|
|
|
element.enabled = enabled
|
|
|
|
element.entity.Invalidate()
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetText sets the checkbox's label text.
|
|
|
|
func (element *Checkbox) SetText (text string) {
|
|
|
|
if element.text == text { return }
|
|
|
|
element.text = text
|
2023-01-17 22:42:04 -07:00
|
|
|
element.drawer.SetText([]rune(text))
|
2023-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-04-14 20:03:22 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetTheme sets the element's theme.
|
2023-03-30 23:06:29 -06:00
|
|
|
func (element *Checkbox) SetTheme (new tomo.Theme) {
|
2023-02-08 12:36:14 -07:00
|
|
|
if new == element.theme.Theme { return }
|
|
|
|
element.theme.Theme = new
|
2023-02-07 22:22:40 -07:00
|
|
|
element.drawer.SetFace (element.theme.FontFace (
|
2023-03-30 23:06:29 -06:00
|
|
|
tomo.FontStyleRegular,
|
|
|
|
tomo.FontSizeNormal))
|
2023-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-04-14 20:03:22 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the element's configuration.
|
2023-03-30 23:06:29 -06:00
|
|
|
func (element *Checkbox) SetConfig (new tomo.Config) {
|
2023-02-08 12:36:14 -07:00
|
|
|
if new == element.config.Config { return }
|
|
|
|
element.config.Config = new
|
2023-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-04-14 20:03:22 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
|
|
|
|
2023-04-15 16:51:42 -06:00
|
|
|
func (element *Checkbox) HandleFocusChange () {
|
|
|
|
element.entity.Invalidate()
|
|
|
|
}
|
|
|
|
|
2023-04-20 12:44:54 -06:00
|
|
|
func (element *Checkbox) HandleMouseDown (
|
|
|
|
position image.Point,
|
|
|
|
button input.Button,
|
|
|
|
modifiers input.Modifiers,
|
|
|
|
) {
|
2023-04-14 20:03:22 -06:00
|
|
|
if !element.Enabled() { return }
|
|
|
|
element.Focus()
|
|
|
|
element.pressed = true
|
|
|
|
element.entity.Invalidate()
|
|
|
|
}
|
|
|
|
|
2023-04-20 12:44:54 -06:00
|
|
|
func (element *Checkbox) HandleMouseUp (
|
|
|
|
position image.Point,
|
|
|
|
button input.Button,
|
|
|
|
modifiers input.Modifiers,
|
|
|
|
) {
|
2023-04-14 20:03:22 -06:00
|
|
|
if button != input.ButtonLeft || !element.pressed { return }
|
|
|
|
|
|
|
|
element.pressed = false
|
2023-04-20 12:44:54 -06:00
|
|
|
within := position.In(element.entity.Bounds())
|
2023-04-14 20:03:22 -06:00
|
|
|
if within {
|
|
|
|
element.checked = !element.checked
|
|
|
|
}
|
|
|
|
|
|
|
|
element.entity.Invalidate()
|
|
|
|
if within && element.onToggle != nil {
|
|
|
|
element.onToggle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Checkbox) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
|
|
|
if key == input.KeyEnter {
|
|
|
|
element.pressed = true
|
|
|
|
element.entity.Invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Checkbox) HandleKeyUp (key input.Key, modifiers input.Modifiers) {
|
|
|
|
if key == input.KeyEnter && element.pressed {
|
|
|
|
element.pressed = false
|
|
|
|
element.checked = !element.checked
|
|
|
|
element.entity.Invalidate()
|
|
|
|
if element.onToggle != nil {
|
|
|
|
element.onToggle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Checkbox) updateMinimumSize () {
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
if element.text == "" {
|
|
|
|
element.entity.SetMinimumSize(textBounds.Dy(), textBounds.Dy())
|
|
|
|
} else {
|
|
|
|
margin := element.theme.Margin(tomo.PatternBackground)
|
|
|
|
element.entity.SetMinimumSize (
|
|
|
|
textBounds.Dy() + margin.X + textBounds.Dx(),
|
|
|
|
textBounds.Dy())
|
|
|
|
}
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|