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>
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package navigation
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
type DropdownMenuOptions struct {
|
|
Disabled bool
|
|
}
|
|
|
|
type DropdownMenuResult struct {
|
|
Element ui.Element
|
|
// Chosen is the item taken this frame, or -1.
|
|
Chosen int
|
|
}
|
|
|
|
// DropdownMenu is a button opening a menu of items.
|
|
func DropdownMenu(c *ui.Context, text string, items []MenuItem, o DropdownMenuOptions) DropdownMenuResult {
|
|
off := o.Disabled || pixel.Current(c).Inert
|
|
b := input.Button(c, text+" ▼", input.ButtonOptions{Label: text, Disabled: o.Disabled})
|
|
m := ui.Local(b.Element, menuKey{}, func() menu { return menu{lit: -1} })
|
|
// The button keeps the focus and parks it while off.
|
|
if off {
|
|
m.open = false
|
|
}
|
|
res := DropdownMenuResult{Element: b.Element, Chosen: -1}
|
|
switch {
|
|
case b.Clicked && m.open:
|
|
m.open = false
|
|
case b.Clicked, !m.open && !off && pixel.Shortcut(b.Element, 0, ui.KeyDown):
|
|
m.show(items)
|
|
}
|
|
res.Chosen = m.keys(b.Element.Shortcut, items)
|
|
if _, clicked := m.list(c, b.Element, items); clicked >= 0 {
|
|
res.Chosen = clicked
|
|
}
|
|
if res.Chosen >= 0 {
|
|
slog.Debug("8bit: dropdown", "label", text, "item", items[res.Chosen].Label)
|
|
}
|
|
return res
|
|
}
|