2023-03-30 21:19:04 -06:00
|
|
|
package elements
|
2023-01-28 23:49:01 -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-15 10:35:00 -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-28 23:49:01 -07:00
|
|
|
|
|
|
|
// Switch is a toggle-able on/off switch with an optional label. It is
|
|
|
|
// functionally identical to Checkbox, but plays a different semantic role.
|
|
|
|
type Switch struct {
|
2023-04-15 10:35:00 -06:00
|
|
|
entity tomo.FocusableEntity
|
2023-02-15 16:45:58 -07:00
|
|
|
drawer textdraw.Drawer
|
2023-01-28 23:49:01 -07:00
|
|
|
|
2023-04-15 10:35:00 -06:00
|
|
|
enabled bool
|
2023-01-28 23:49:01 -07:00
|
|
|
pressed bool
|
|
|
|
checked bool
|
|
|
|
text string
|
|
|
|
|
2023-02-08 12:36:14 -07:00
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
2023-02-07 22:22:40 -07:00
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
onToggle func ()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSwitch creates a new switch with the specified label text.
|
|
|
|
func NewSwitch (text string, on bool) (element *Switch) {
|
2023-02-07 22:22:40 -07:00
|
|
|
element = &Switch {
|
|
|
|
checked: on,
|
2023-04-15 10:35:00 -06:00
|
|
|
text: text,
|
|
|
|
enabled: true,
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
2023-04-15 10:35:00 -06:00
|
|
|
element.entity = tomo.NewEntity(element).(tomo.FocusableEntity)
|
2023-03-30 23:06:29 -06:00
|
|
|
element.theme.Case = tomo.C("tomo", "switch")
|
|
|
|
element.drawer.SetFace (element.theme.FontFace (
|
|
|
|
tomo.FontStyleRegular,
|
|
|
|
tomo.FontSizeNormal))
|
2023-01-28 23:49:01 -07:00
|
|
|
element.drawer.SetText([]rune(text))
|
2023-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-01-28 23:49:01 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-15 10:35:00 -06:00
|
|
|
// Entity returns this element's entity.
|
|
|
|
func (element *Switch) Entity () tomo.Entity {
|
|
|
|
return element.entity
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw causes the element to draw to the specified destination canvas.
|
|
|
|
func (element *Switch) Draw (destination canvas.Canvas) {
|
|
|
|
bounds := element.entity.Bounds()
|
|
|
|
handleBounds := image.Rect(0, 0, bounds.Dy(), bounds.Dy()).Add(bounds.Min)
|
|
|
|
gutterBounds := image.Rect(0, 0, bounds.Dy() * 2, 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)
|
|
|
|
|
|
|
|
if element.checked {
|
|
|
|
handleBounds.Min.X += bounds.Dy()
|
|
|
|
handleBounds.Max.X += bounds.Dy()
|
|
|
|
if element.pressed {
|
|
|
|
handleBounds.Min.X -= 2
|
|
|
|
handleBounds.Max.X -= 2
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if element.pressed {
|
|
|
|
handleBounds.Min.X += 2
|
|
|
|
handleBounds.Max.X += 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gutterPattern := element.theme.Pattern (
|
|
|
|
tomo.PatternGutter, state)
|
|
|
|
gutterPattern.Draw(destination, gutterBounds)
|
|
|
|
|
|
|
|
handlePattern := element.theme.Pattern (
|
|
|
|
tomo.PatternHandle, state)
|
|
|
|
handlePattern.Draw(destination, handleBounds)
|
|
|
|
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
offset := bounds.Min.Add(image.Point {
|
|
|
|
X: bounds.Dy() * 2 +
|
|
|
|
element.theme.Margin(tomo.PatternBackground).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-04-15 16:51:42 -06:00
|
|
|
func (element *Switch) HandleFocusChange () {
|
|
|
|
element.entity.Invalidate()
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Switch) HandleMouseDown (x, y int, button input.Button) {
|
2023-01-28 23:49:01 -07:00
|
|
|
if !element.Enabled() { return }
|
2023-01-30 15:01:47 -07:00
|
|
|
element.Focus()
|
2023-01-28 23:49:01 -07:00
|
|
|
element.pressed = true
|
2023-04-15 10:35:00 -06:00
|
|
|
element.entity.Invalidate()
|
2023-01-28 23:49:01 -07:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Switch) HandleMouseUp (x, y int, button input.Button) {
|
|
|
|
if button != input.ButtonLeft || !element.pressed { return }
|
2023-01-28 23:49:01 -07:00
|
|
|
|
|
|
|
element.pressed = false
|
|
|
|
within := image.Point { x, y }.
|
2023-04-15 10:35:00 -06:00
|
|
|
In(element.entity.Bounds())
|
2023-01-28 23:49:01 -07:00
|
|
|
if within {
|
|
|
|
element.checked = !element.checked
|
|
|
|
}
|
|
|
|
|
2023-04-15 10:35:00 -06:00
|
|
|
element.entity.Invalidate()
|
2023-01-28 23:49:01 -07:00
|
|
|
if within && element.onToggle != nil {
|
|
|
|
element.onToggle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Switch) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
|
|
|
if key == input.KeyEnter {
|
2023-01-28 23:49:01 -07:00
|
|
|
element.pressed = true
|
2023-04-15 10:35:00 -06:00
|
|
|
element.entity.Invalidate()
|
2023-01-28 23:49:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Switch) HandleKeyUp (key input.Key, modifiers input.Modifiers) {
|
|
|
|
if key == input.KeyEnter && element.pressed {
|
2023-01-28 23:49:01 -07:00
|
|
|
element.pressed = false
|
|
|
|
element.checked = !element.checked
|
2023-04-15 10:35:00 -06:00
|
|
|
element.entity.Invalidate()
|
2023-01-28 23:49:01 -07:00
|
|
|
if element.onToggle != nil {
|
|
|
|
element.onToggle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnToggle sets the function to be called when the switch is flipped.
|
|
|
|
func (element *Switch) OnToggle (callback func ()) {
|
|
|
|
element.onToggle = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value reports whether or not the switch is currently on.
|
|
|
|
func (element *Switch) Value () (on bool) {
|
|
|
|
return element.checked
|
|
|
|
}
|
|
|
|
|
2023-04-15 10:35:00 -06:00
|
|
|
// Focus gives this element input focus.
|
|
|
|
func (element *Switch) Focus () {
|
|
|
|
if !element.entity.Focused() { element.entity.Focus() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enabled returns whether this switch is enabled or not.
|
|
|
|
func (element *Switch) Enabled () bool {
|
|
|
|
return element.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEnabled sets whether this switch can be toggled or not.
|
2023-01-28 23:49:01 -07:00
|
|
|
func (element *Switch) SetEnabled (enabled bool) {
|
2023-04-15 10:35:00 -06:00
|
|
|
if element.enabled == enabled { return }
|
|
|
|
element.enabled = enabled
|
|
|
|
element.entity.Invalidate()
|
2023-01-28 23:49:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetText sets the checkbox's label text.
|
|
|
|
func (element *Switch) SetText (text string) {
|
|
|
|
if element.text == text { return }
|
|
|
|
|
|
|
|
element.text = text
|
|
|
|
element.drawer.SetText([]rune(text))
|
2023-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-04-15 10:35:00 -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 *Switch) 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-15 10:35:00 -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 *Switch) 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-15 10:35:00 -06:00
|
|
|
element.entity.Invalidate()
|
2023-01-28 23:49:01 -07:00
|
|
|
}
|
|
|
|
|
2023-02-07 22:22:40 -07:00
|
|
|
func (element *Switch) updateMinimumSize () {
|
2023-01-28 23:49:01 -07:00
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
lineHeight := element.drawer.LineHeight().Round()
|
|
|
|
|
|
|
|
if element.text == "" {
|
2023-04-15 10:35:00 -06:00
|
|
|
element.entity.SetMinimumSize(lineHeight * 2, lineHeight)
|
2023-01-28 23:49:01 -07:00
|
|
|
} else {
|
2023-04-15 10:35:00 -06:00
|
|
|
element.entity.SetMinimumSize (
|
2023-02-07 22:22:40 -07:00
|
|
|
lineHeight * 2 +
|
2023-03-30 23:06:29 -06:00
|
|
|
element.theme.Margin(tomo.PatternBackground).X +
|
2023-02-07 22:22:40 -07:00
|
|
|
textBounds.Dx(),
|
2023-01-28 23:49:01 -07:00
|
|
|
lineHeight)
|
|
|
|
}
|
|
|
|
}
|