Files
8bitMyGoUI/files/pathbar.go
T
ZacharyZhang-NYandClaude Opus 5.5 03e1f91706 Upgrade MyGo to v0.5.5
Elements are now pass-scoped value handles: tests that read bounds
after a frame bind a ui.Handle. pixel tells frames apart by the build
pass's root, since the Tester clock no longer moves on its own.

Fix the regressions the upgrade brought:
- Clipboard reads in input handlers go through c.Services().
- MyGo notes an overlay's opener after building it, so AutoFocus
  inside lost it; pixel.AutoFocus focuses through a Handle, and
  archtest bans the raw call. Dialogs hand the focus back again.
- Elements around a press stay hovered: Tooltip tells a press-given
  focus by focus arriving under the pointer.
- The web host moves the Tester clock with real time, at most 500 ms
  a call, and gives components the real clock.

The gallery's source index also accepts CRLF checkouts.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:31:08 -04:00

73 lines
2.2 KiB
Go

package files
import (
"fmt"
"log/slog"
"slices"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/layout"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type PathBarOptions struct {
Disabled bool
}
type PathBarResult struct {
Element ui.Element
// Clicked is the folder clicked, else -1.
Clicked int
}
// PathBar sets a path's folders in a well, ▸ between.
func PathBar(c *ui.Context, text string, parts []string, o PathBarOptions) PathBarResult {
k := pixel.K(c)
if len(parts) == 0 || slices.Contains(parts, "") {
panic(fmt.Sprintf("files: PathBar parts %q", parts))
}
res := PathBarResult{Clicked: -1}
res.Element = layout.Panel(c, layout.PanelOptions{Style: pixel.Sunken, Fill: pixel.Well, Padding: 1}, func() {
// One Tab stop; arrows walk the folders.
ui.Row(c).Gap(k.D(2)).AlignItems(ui.Center).FocusGroup(ui.Horizontal).Role(ui.RoleToolbar).Label(text).Children(func() {
ui.Box(c).Size(k.D(icon.FolderOpen.W), k.D(icon.FolderOpen.H)).Draw(func(p *ui.Painter, r ui.Rect) {
k.Canvas(p, r).Sprite(0, 0, icon.FolderOpen, 0, pixel.Warn, 1)
})
for i, part := range parts {
if i > 0 {
ui.Box(c).Size(k.D(pixel.Measure("▸")), k.D(pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
k.Canvas(p, r).Text(0, 0, "▸", pixel.Faint, 1)
})
}
// The last folder is where you are; it stays put.
if i == len(parts)-1 {
ui.Box(c).Size(k.D(pixel.Measure(part)+4), k.D(pixel.LineHeight)).Role(ui.RoleText).Label(part).Draw(func(p *ui.Painter, r ui.Rect) {
k.Canvas(p, r).Text(2, 0, part, pixel.Accent, 1)
})
break
}
shell, base, st := pixel.Shell(c, func() ui.Element {
return ui.ButtonBase(c).Label(part).Disabled(o.Disabled)
})
if base.Clicked() {
res.Clicked = i
}
shell.Size(k.D(pixel.Measure(part)+4), k.D(pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
if st.Hover || st.Press {
g.Rect(0, 0, g.W, g.H, pixel.Face)
}
g.Text(2, 0, part, pixel.Ink, 1)
g.Finish(st)
})
}
})
})
if res.Clicked >= 0 {
slog.Debug("8bit: path bar", "label", text, "part", res.Clicked)
}
return res
}