Files
8bitMyGoUI/layout/scrollarea.go
T
ZacharyZhang-NY 455f5f2a05 Add chat components 321-345; named List rows, inert scroll views
Speech bubbles, a chat log that follows its end, a composer that sends
on Enter, typing dots, chat and channel lists, read receipts,
reactions, emoji and sticker pickers, mentions, date and unread rules,
system lines, member lists, user cards, a mailbox, mail reader and
letter, a notification bell, call controls, a voice meter, attachment
chips, link previews and polls.

pixel's editor takes EnterSends; display's Bubble takes a Fill and
exports EmojiSprite. data.List names its rows, which also lets typing
choose one. layout.ScrollArea and input.TextArea hold still under
Inert, and an inert scroll view leaves the Tab order until it wakes.
2026-10-12 05:02:36 -04:00

160 lines
4.3 KiB
Go

package layout
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
const barW = 10
var (
upArrow = pixel.MustSprite(`
..#..
.###.
#####
`)
downArrow = pixel.MustSprite(`
#####
.###.
..#..
`)
)
type ScrollAreaOptions struct {
// Label names the region for assistive technology.
Label string
Disabled bool
}
type ScrollAreaResult struct {
Element *ui.Element
// Changed reports the view moved since the last frame.
Changed bool
}
type scrollKey struct{}
// ScrollArea scrolls children vertically beside a pixel scrollbar.
func ScrollArea(c *ui.Context, s *ui.ScrollState, o ScrollAreaOptions, children func()) ScrollAreaResult {
k := pixel.K(c)
// The bar comes first: TrackScroll reads s when called.
row := ui.Row(c).AlignItems(ui.Stretch).Reverse()
last := ui.Local(row, scrollKey{}, func() float32 { return s.Y })
view := row.Bounds().H
// Off freezes the view: any move is undone before paint.
inert := pixel.Current(c).Inert
if o.Disabled || inert {
s.Y = *last
}
var v *ui.Element
row.Children(func() {
scrollBar(c, k, s, view, o.Disabled || inert, o.Disabled)
// A disabled view is a new element; the focus leaves.
v = ui.Scroll(c).Key(o.Disabled).TrackScroll(s).Grow(1).Label(o.Label).FocusRing(false)
switch {
case o.Disabled:
v.Role(ui.RoleGroup).Description(k.Msg("disabled"))
case inert:
// Out of the Tab order, so Tab moves past it.
v.Role(ui.RoleScroll)
default:
v.Role(ui.RoleScroll).Focusable()
}
v.Children(children)
// Inert parks the focus and gives it back after.
pixel.Park(c, v, inert)
})
focus := v.FocusVisible()
row.Draw(func(p *ui.Painter, r ui.Rect) {
if r.H != view {
c.Invalidate()
}
}).DrawOver(func(p *ui.Painter, r ui.Rect) {
if focus {
g := k.Canvas(p, r)
w := g.W - barW
g.Checker(0, 0, w, 1, pixel.Accent)
g.Checker(0, g.H-1, w, 1, pixel.Accent)
g.Checker(0, 0, 1, g.H, pixel.Accent)
g.Checker(w-1, 0, 1, g.H, pixel.Accent)
}
})
res := ScrollAreaResult{Element: row, Changed: s.Y != *last}
*last = s.Y
if res.Changed {
slog.Debug("8bit: scrollarea scrolled", "y", s.Y)
}
return res
}
// scrollBar moves s unless off; dim draws it disabled.
func scrollBar(c *ui.Context, k pixel.Kit, s *ui.ScrollState, view float32, disabled, dim bool) {
clamp := func(y float32) { s.Y = max(0, min(y, s.MaxY)) }
ui.Column(c).Width(k.D(barW)).DrawOver(func(p *ui.Painter, r ui.Rect) {
if dim {
g := k.Canvas(p, r)
g.Checker(0, 0, g.W, g.H, pixel.Face)
}
}).Children(func() {
if arrow(c, k, upArrow, k.Msg("scroll_up"), disabled) {
clamp(s.Y - k.D(16))
}
track := ui.Box(c).Grow(1)
h := track.Bounds().H
size, at := thumb(h, view, s.Y, s.MaxY, k.Dot())
track.Draw(func(p *ui.Painter, r ui.Rect) {
if r.H != h {
c.Invalidate()
}
box(k.Canvas(p, r), pixel.Sunken, pixel.Well)
}).Children(func() {
th := ui.Box(c).Absolute().Top(at).Left(0).FillWidth().Height(size)
th.Clicked()
if _, dy, ok := th.Dragged(); ok && !disabled && dy != 0 && h > size {
clamp(s.Y + dy*s.MaxY/(h-size))
}
th.Draw(func(p *ui.Painter, r ui.Rect) { box(k.Canvas(p, r), pixel.Raised, pixel.Face) })
})
if track.Clicked() && !disabled {
if _, y, _ := track.PointerPosition(); y < at {
clamp(s.Y - view)
} else if y > at+size {
clamp(s.Y + view)
}
}
if arrow(c, k, downArrow, k.Msg("scroll_down"), disabled) {
clamp(s.Y + k.D(16))
}
})
}
// thumb sizes and places the thumb in track h.
func thumb(h, view, y, maxY, dot float32) (size, at float32) {
if maxY <= 0 || view <= 0 {
return h, 0
}
size = min(h, max(8*dot, h*view/(view+maxY)))
size = float32(int(size/dot)) * dot
return size, float32(int((h-size)*y/maxY/dot)) * dot
}
// arrow is a scrollbar button; keys scroll the view instead.
func arrow(c *ui.Context, k pixel.Kit, sp *pixel.Sprite, label string, disabled bool) bool {
shell, base, st := pixel.Shell(c, func() *ui.Element {
return ui.Box(c).Role(ui.RoleButton).Label(label).Disabled(disabled)
})
shell.Size(k.D(barW), k.D(barW)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
style, y := pixel.Raised, 0
if st.Press {
style, y = pixel.Sunken, 1
}
box(g, style, pixel.Face)
g.Sprite((g.W-sp.W)/2, (g.H-sp.H)/2+y, sp, 0, pixel.Ink, 1)
})
return base.Clicked()
}