This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/basic/switch.go

207 lines
5.3 KiB
Go
Raw Normal View History

2023-02-02 06:48:16 +00:00
package basicElements
import "image"
2023-02-02 06:48:16 +00:00
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/theme"
2023-02-08 05:22:40 +00:00
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-02-15 23:45:58 +00:00
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
// 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 {
*core.Core
2023-01-30 22:01:47 +00:00
*core.FocusableCore
core core.CoreControl
2023-01-30 22:01:47 +00:00
focusableControl core.FocusableCoreControl
2023-02-15 23:45:58 +00:00
drawer textdraw.Drawer
pressed bool
checked bool
text string
2023-02-08 19:36:14 +00:00
config config.Wrapped
theme theme.Wrapped
2023-02-08 05:22:40 +00:00
onToggle func ()
}
// NewSwitch creates a new switch with the specified label text.
func NewSwitch (text string, on bool) (element *Switch) {
2023-02-08 05:22:40 +00:00
element = &Switch {
checked: on,
text: text,
}
2023-02-08 19:36:14 +00:00
element.theme.Case = theme.C("basic", "switch")
2023-01-31 19:54:43 +00:00
element.Core, element.core = core.NewCore(element.draw)
2023-01-30 22:01:47 +00:00
element.FocusableCore,
2023-02-08 05:22:40 +00:00
element.focusableControl = core.NewFocusableCore(element.redo)
element.drawer.SetText([]rune(text))
2023-02-08 05:22:40 +00:00
element.updateMinimumSize()
return
}
2023-02-02 06:48:16 +00:00
func (element *Switch) HandleMouseDown (x, y int, button input.Button) {
if !element.Enabled() { return }
2023-01-30 22:01:47 +00:00
element.Focus()
element.pressed = true
2023-02-08 05:22:40 +00:00
element.redo()
}
2023-02-02 06:48:16 +00:00
func (element *Switch) HandleMouseUp (x, y int, button input.Button) {
if button != input.ButtonLeft || !element.pressed { return }
element.pressed = false
within := image.Point { x, y }.
In(element.Bounds())
if within {
element.checked = !element.checked
}
if element.core.HasImage() {
element.draw()
element.core.DamageAll()
}
if within && element.onToggle != nil {
element.onToggle()
}
}
func (element *Switch) HandleMouseMove (x, y int) { }
func (element *Switch) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
2023-02-02 06:48:16 +00:00
func (element *Switch) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
if key == input.KeyEnter {
element.pressed = true
2023-02-08 05:22:40 +00:00
element.redo()
}
}
2023-02-02 06:48:16 +00:00
func (element *Switch) HandleKeyUp (key input.Key, modifiers input.Modifiers) {
if key == input.KeyEnter && element.pressed {
element.pressed = false
element.checked = !element.checked
2023-02-08 05:22:40 +00:00
element.redo()
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
}
// SetEnabled sets whether this switch can be flipped or not.
func (element *Switch) SetEnabled (enabled bool) {
2023-01-30 22:01:47 +00:00
element.focusableControl.SetEnabled(enabled)
}
// 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-08 05:22:40 +00:00
element.updateMinimumSize()
element.redo()
}
// SetTheme sets the element's theme.
func (element *Switch) SetTheme (new theme.Theme) {
2023-02-08 19:36:14 +00:00
if new == element.theme.Theme { return }
element.theme.Theme = new
2023-02-08 05:22:40 +00:00
element.drawer.SetFace (element.theme.FontFace (
theme.FontStyleRegular,
2023-02-08 19:36:14 +00:00
theme.FontSizeNormal))
2023-02-08 05:22:40 +00:00
element.updateMinimumSize()
element.redo()
}
// SetConfig sets the element's configuration.
func (element *Switch) SetConfig (new config.Config) {
2023-02-08 19:36:14 +00:00
if new == element.config.Config { return }
element.config.Config = new
2023-02-08 05:22:40 +00:00
element.updateMinimumSize()
element.redo()
}
func (element *Switch) redo () {
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
}
2023-02-08 05:22:40 +00:00
func (element *Switch) updateMinimumSize () {
textBounds := element.drawer.LayoutBounds()
lineHeight := element.drawer.LineHeight().Round()
if element.text == "" {
element.core.SetMinimumSize(lineHeight * 2, lineHeight)
} else {
element.core.SetMinimumSize (
2023-02-08 05:22:40 +00:00
lineHeight * 2 +
2023-02-27 03:20:17 +00:00
element.theme.Margin(theme.PatternBackground).X +
2023-02-08 05:22:40 +00:00
textBounds.Dx(),
lineHeight)
}
}
func (element *Switch) draw () {
bounds := element.Bounds()
2023-01-31 23:39:17 +00:00
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)
2023-02-08 05:22:40 +00:00
2023-02-27 03:20:17 +00:00
state := theme.State {
2023-02-08 05:22:40 +00:00
Disabled: !element.Enabled(),
Focused: element.Focused(),
Pressed: element.pressed,
}
backgroundPattern := element.theme.Pattern (
2023-02-08 19:36:14 +00:00
theme.PatternBackground, state)
2023-02-27 03:20:17 +00:00
backgroundPattern.Draw(element.core, bounds)
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
}
}
2023-02-08 05:22:40 +00:00
gutterPattern := element.theme.Pattern (
2023-02-08 19:36:14 +00:00
theme.PatternGutter, state)
2023-02-27 03:20:17 +00:00
artist.DrawBounds(element.core, gutterPattern, gutterBounds)
2023-02-08 05:22:40 +00:00
handlePattern := element.theme.Pattern (
2023-02-08 19:36:14 +00:00
theme.PatternHandle, state)
2023-02-27 03:20:17 +00:00
artist.DrawBounds(element.core, handlePattern, handleBounds)
textBounds := element.drawer.LayoutBounds()
2023-01-31 23:39:17 +00:00
offset := bounds.Min.Add(image.Point {
2023-02-27 03:20:17 +00:00
X: bounds.Dy() * 2 +
element.theme.Margin(theme.PatternBackground).X,
2023-01-31 23:39:17 +00:00
})
offset.Y -= textBounds.Min.Y
offset.X -= textBounds.Min.X
2023-02-27 03:20:17 +00:00
foreground := element.theme.Color (
theme.ColorForeground, state)
2023-02-13 06:49:33 +00:00
element.drawer.Draw(element.core, foreground, offset)
}