2023-01-27 22:55:49 +00:00
|
|
|
package basic
|
2023-01-29 06:49:01 +00:00
|
|
|
|
|
|
|
import "image"
|
|
|
|
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"
|
|
|
|
|
2023-01-30 06:30:13 +00:00
|
|
|
var switchCase = theme.C("basic", "switch")
|
|
|
|
|
2023-01-29 06:49:01 +00: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 {
|
|
|
|
*core.Core
|
2023-01-30 22:01:47 +00:00
|
|
|
*core.FocusableCore
|
2023-01-29 06:49:01 +00:00
|
|
|
core core.CoreControl
|
2023-01-30 22:01:47 +00:00
|
|
|
focusableControl core.FocusableCoreControl
|
2023-01-29 06:49:01 +00:00
|
|
|
drawer artist.TextDrawer
|
|
|
|
|
|
|
|
pressed bool
|
|
|
|
checked bool
|
|
|
|
text string
|
|
|
|
|
|
|
|
onToggle func ()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSwitch creates a new switch with the specified label text.
|
|
|
|
func NewSwitch (text string, on bool) (element *Switch) {
|
|
|
|
element = &Switch { checked: on, text: text }
|
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,
|
|
|
|
element.focusableControl = core.NewFocusableCore (func () {
|
2023-01-29 06:49:01 +00:00
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
element.drawer.SetFace(theme.FontFaceRegular())
|
|
|
|
element.drawer.SetText([]rune(text))
|
|
|
|
element.calculateMinimumSize()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Switch) HandleMouseDown (x, y int, button tomo.Button) {
|
|
|
|
if !element.Enabled() { return }
|
2023-01-30 22:01:47 +00:00
|
|
|
element.Focus()
|
2023-01-29 06:49:01 +00:00
|
|
|
element.pressed = true
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Switch) HandleMouseUp (x, y int, button tomo.Button) {
|
|
|
|
if button != tomo.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) { }
|
|
|
|
|
|
|
|
func (element *Switch) HandleKeyDown (key tomo.Key, modifiers tomo.Modifiers) {
|
|
|
|
if key == tomo.KeyEnter {
|
|
|
|
element.pressed = true
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Switch) HandleKeyUp (key tomo.Key, modifiers tomo.Modifiers) {
|
|
|
|
if key == tomo.KeyEnter && element.pressed {
|
|
|
|
element.pressed = false
|
|
|
|
element.checked = !element.checked
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
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)
|
2023-01-29 06:49:01 +00: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))
|
|
|
|
element.calculateMinimumSize()
|
|
|
|
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Switch) calculateMinimumSize () {
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
lineHeight := element.drawer.LineHeight().Round()
|
|
|
|
|
|
|
|
if element.text == "" {
|
|
|
|
element.core.SetMinimumSize(lineHeight * 2, lineHeight)
|
|
|
|
} else {
|
|
|
|
element.core.SetMinimumSize (
|
|
|
|
lineHeight * 2 + theme.Padding() + textBounds.Dx(),
|
|
|
|
lineHeight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Switch) draw () {
|
2023-01-31 21:18:10 +00:00
|
|
|
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-01-30 06:30:13 +00:00
|
|
|
backgroundPattern, _ := theme.BackgroundPattern(theme.PatternState {
|
|
|
|
Case: switchCase,
|
|
|
|
})
|
2023-01-31 21:18:10 +00:00
|
|
|
artist.FillRectangle (element, backgroundPattern, bounds)
|
2023-01-29 06:49:01 +00:00
|
|
|
|
|
|
|
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, _ := theme.GutterPattern(theme.PatternState {
|
2023-01-30 06:30:13 +00:00
|
|
|
Case: switchCase,
|
2023-01-29 06:49:01 +00:00
|
|
|
Disabled: !element.Enabled(),
|
2023-01-30 22:01:47 +00:00
|
|
|
Focused: element.Focused(),
|
2023-01-29 06:49:01 +00:00
|
|
|
Pressed: element.pressed,
|
|
|
|
})
|
2023-01-31 21:18:10 +00:00
|
|
|
artist.FillRectangle(element, gutterPattern, gutterBounds)
|
2023-01-29 06:49:01 +00:00
|
|
|
|
|
|
|
handlePattern, _ := theme.HandlePattern(theme.PatternState {
|
2023-01-30 06:30:13 +00:00
|
|
|
Case: switchCase,
|
2023-01-29 06:49:01 +00:00
|
|
|
Disabled: !element.Enabled(),
|
2023-01-30 22:01:47 +00:00
|
|
|
Focused: element.Focused(),
|
2023-01-29 06:49:01 +00:00
|
|
|
Pressed: element.pressed,
|
|
|
|
})
|
2023-01-31 21:18:10 +00:00
|
|
|
artist.FillRectangle(element, handlePattern, handleBounds)
|
2023-01-29 06:49:01 +00:00
|
|
|
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
2023-01-31 23:39:17 +00:00
|
|
|
offset := bounds.Min.Add(image.Point {
|
2023-01-29 06:49:01 +00:00
|
|
|
X: bounds.Dy() * 2 + theme.Padding(),
|
2023-01-31 23:39:17 +00:00
|
|
|
})
|
2023-01-29 06:49:01 +00:00
|
|
|
|
|
|
|
offset.Y -= textBounds.Min.Y
|
|
|
|
offset.X -= textBounds.Min.X
|
|
|
|
|
|
|
|
foreground, _ := theme.ForegroundPattern (theme.PatternState {
|
2023-01-30 06:30:13 +00:00
|
|
|
Case: switchCase,
|
2023-01-29 06:49:01 +00:00
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
})
|
2023-01-31 21:18:10 +00:00
|
|
|
element.drawer.Draw(element, foreground, offset)
|
2023-01-29 06:49:01 +00:00
|
|
|
}
|