Files
8bitMyGoUI/navigation/documenttabs.go
T

67 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package navigation
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
var cross = pixel.MustSprite(`
#...#
.#.#.
..#..
.#.#.
#...#
`)
type DocumentTabsOptions struct {
Disabled bool
}
type DocumentTabsResult struct {
Element *ui.Element
Changed bool
// Closed is the tab × or Delete closed, or -1.
Closed int
}
// DocumentTabs are closable tabs; × or Delete reports Closed.
func DocumentTabs(c *ui.Context, text string, titles []string, selected *int, o DocumentTabsOptions) DocumentTabsResult {
k := pixel.K(c)
res := DocumentTabsResult{Closed: -1}
p := ui.TabsBase(c, selected, len(titles))
p.List.Label(text).Children(func() {
for i, t := range titles {
var x *ui.Element
shell, base, st := pixel.Shell(c, func() *ui.Element {
tab := p.Tab(i).Label(t).Disabled(o.Disabled)
// The × is no Tab stop; Delete closes by keyboard.
tab.Children(func() {
x = ui.Box(c).Absolute().Right(k.D(4)).Top(k.D(6)).Size(k.D(7), k.D(7)).Role(ui.RoleButton).Label(k.Msg("close") + " " + t)
})
return tab
})
if x.Clicked() || base.Shortcut(0, ui.KeyDelete) {
res.Closed = i
}
hot := x.Hovered()
shell.Size(k.D(pixel.Measure(t)+24), k.D(pixel.LineHeight+6)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
folder(g, t, i == *selected, false, st)
ink := pixel.Faint
if hot {
ink = pixel.Bad
}
g.Sprite(g.W-10, 7, cross, 0, ink, 1)
})
}
})
res.Element, res.Changed = p.List, p.List.Changed()
if res.Closed >= 0 {
slog.Debug("8bit: tab closed", "label", text, "tab", titles[res.Closed])
}
return res
}