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 // Follow keeps the view at the end while there. Follow bool Disabled bool } type ScrollAreaResult struct { Element ui.Element // Changed reports the view moved since the last frame. Changed bool } type scrollKey struct{} // area is the last view and the end last reached. type area struct { last, end float32 } // 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() st := ui.Local(row, scrollKey{}, func() area { return area{last: s.Y} }) view := row.Bounds().H // Off freezes the view: any move is undone before paint. inert := pixel.Current(c).Inert off := o.Disabled || inert if off { s.Y = st.last } // Content grew under a view at its old end: follow. grew := func() bool { return o.Follow && s.MaxY > st.end && s.Y >= st.end } // Off, the end waits until it wakes. if !off { if grew() { s.Y = s.MaxY } st.end = s.MaxY } 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) { // Layout has just measured; resize or follow next frame. if r.H != view || !off && grew() { 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 != st.last} st.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() }