Files
8bitMyGoUI/media/volume.go
T
ZacharyZhang-NY 0e21870b64 Add media components 346-365; input.SlideBase, guarded Pad
An audio player with transport, scrubber and volume; an equalizer and
a visualizer; a playlist; a cassette, cartridge and record that turn
while they play and hold where they stop; piano keys, a step sequencer
and a tracker you can type into; a waveform, a 16:9 video frame, an
image compare, a gallery, a filmstrip, a record button and an FM tuner.

input exports SlideBase, the undrawn rail its sliders share; the
gallery treats exported Bases as building blocks, not components.
pixel's micro font gains '#' for notes like C#4. pixel.Pad and the
slide rail read input only while their base is enabled, so Inert or a
fresh Disabled stops a drag or a key already sent. AGENTS records why
a drawn container keeps the DISABLED description: MyGo paints a
disabled subtree at half opacity.
2026-10-12 06:17:12 -04:00

68 lines
1.7 KiB
Go

package media
import (
"fmt"
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type VolumeOptions struct {
// Bars is how many steps; zero means 8.
Bars int
Disabled bool
}
type VolumeResult struct {
Element *ui.Element
Changed bool
}
// Volume sets *level in stepped bars; the speaker mutes.
func Volume(c *ui.Context, text string, level *int, muted *bool, o VolumeOptions) VolumeResult {
k := pixel.K(c)
n := pixel.Or("Volume Bars", o.Bars, 8)
if n < 2 || *level < 0 || *level > n {
panic(fmt.Sprintf("media: Volume level %d of %d bars", *level, n))
}
var res VolumeResult
res.Element = ui.Row(c).Gap(k.D(3)).AlignItems(ui.Center).Children(func() {
sp := icon.Volume
if *muted {
sp = icon.Mute
}
mute := input.Toggle(c, "", muted, input.ToggleOptions{Icon: sp, Label: k.Msg("mute"), Disabled: o.Disabled}).Changed
v := float64(*level)
shell, st, moved := input.SlideBase(c, text, &v, input.RailOptions{Max: float64(n), Step: 1, Disabled: o.Disabled})
*level = int(v)
lit, quiet := *level, *muted
shell.Size(k.D(4*n), k.D(14)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
for i := range n {
h := 4 + i*10/(n-1)
s := pixel.Well
switch {
case i >= lit:
case quiet:
s = pixel.Faint
case i >= n*3/4:
s = pixel.Warn
default:
s = pixel.Good
}
g.Frame(4*i, 14-h, 3, h, pixel.Flat, s)
}
g.Finish(st)
})
res.Changed = mute || moved
})
if res.Changed {
slog.Debug("8bit: volume", "label", text, "level", *level, "muted", *muted)
}
return res
}