Files
MujicaUI/switch.go
T

87 lines
2.3 KiB
Go

package mujica
import (
"log/slog"
"github.com/egoist/mygo/ui"
"mujicaui/internal"
"mujicaui/theme"
)
// SwitchOptions configure a Switch.
type SwitchOptions struct {
Disabled bool
// Loading shows a pending result: the switch keeps its place and takes no input.
Loading bool
// Description is read out after the label.
Description string
}
const (
switchW float32 = 36
switchH float32 = 20
switchK float32 = 14
)
// Switch turns *on over when clicked or with Space. Changed reports the
// new value; the caller applies it and may set Loading while it does.
func Switch(c *ui.Context, on *bool, label string, opts SwitchOptions) *ui.Element {
k := Tokens(c)
d := Density(c)
sw := ui.SwitchBase(c, on).Gap(d.Unit() * 2).AlignItems(ui.Center).MinHeight(theme.MinHitSize).
FocusRing(false).Disabled(opts.Disabled || opts.Loading)
if opts.Description != "" {
sw.Description(opts.Description)
}
if sw.Changed() {
slog.Debug("mujica: switch changed", "label", label, "on", *on)
}
hover := sw.Hovered()
pos := sw.Animate("on", switchFloat(*on), Motion(c, theme.StateDuration))
sw.Children(func() {
track := ui.Box(c).Size(switchW, switchH).Shrink(0).Radius(theme.CardRadius)
bg, border := k.Surface, k.ControlBorder
thumb := k.ControlBorder
if *on {
bg, border, thumb = k.Accent, k.Accent, k.OnAccent
if hover {
bg, border = k.AccentHover, k.AccentHover
}
} else if hover {
bg, border, thumb = k.SurfaceHover, k.Text, k.Text
}
track.Background(bg).Border(theme.BorderWidth, border)
track.Draw(func(p *ui.Painter, r ui.Rect) {
if *on {
// A short upright bar marks "on" without relying on color.
p.Fill(ui.Rect{X: r.X + 9, Y: r.Y + 6, W: 1.5, H: r.H - 12}, k.OnAccent, 0)
}
})
track.DrawOver(func(p *ui.Painter, r ui.Rect) {
if sw.FocusVisible() {
internal.FocusRing(p, r, theme.CardRadius, k.Focus)
}
})
inset := (switchH - switchK) / 2
x := inset + pos*(switchW-switchK-2*inset)
track.Children(func() {
t := ui.Box(c).Absolute().Left(x).Top(inset).Size(switchK, switchK).Radius(theme.ControlRadius).Background(thumb).Center()
if opts.Loading {
t.Children(func() { Spinner(c, SpinnerOptions{Size: 10, Color: bg}) })
}
})
if label != "" {
ui.Text(c, label).SingleLine()
}
})
return sw
}
func switchFloat(b bool) float32 {
if b {
return 1
}
return 0
}