Overlays: review r1 fixes

This commit is contained in:
ZacharyZhang-NY committed 2026-10-05 12:59:33 -04:00
1 parent a27727bc71
commit e631f88cc0
41 files changed
+770 -278

No files matched your search

+19 -44
View File
@@ -20,17 +20,12 @@ type HoverCardOptions struct {
// hoverCardState is what a card keeps between frames while its anchor lives.
type hoverCardState struct {
open, dismissed bool
// keyMode keeps the card in the focus order while the keyboard is on it.
keyMode bool
enterAt, leaveAt time.Time
panel ui.Rect
open, dismissed, focusIn bool
enterAt, leaveAt time.Time
panel ui.Rect
}
// HoverCard previews content beside anchor while the pointer rests on it or
// it has the keyboard focus. The pointer may move into the card; with the
// keyboard, Tab from the anchor enters it. It closes after CloseDelay once
// neither holds it, or at once on Escape. It returns the card, or nil.
// HoverCard previews content by a hovered or focused anchor; nil while hidden.
func HoverCard(c *ui.Context, anchor *ui.Element, opts HoverCardOptions, content func()) *ui.Element {
if anchor == nil {
panic("mujica: HoverCard needs an anchor")
@@ -50,11 +45,11 @@ func HoverCard(c *ui.Context, anchor *ui.Element, opts HoverCardOptions, content
keyboard := anchor.FocusVisible()
pointer := anchor.Hovered()
if st.open {
// The card's layer covers the anchor, so test the pointer by geometry.
// The popover's backdrop covers the anchor, so test the pointer by geometry.
x, y, in := c.Root().PointerPosition()
pointer = pointer || in && (b.Contains(x, y) || st.panel.Contains(x, y))
}
hot := pointer || keyboard || st.keyMode
hot := pointer || keyboard || st.focusIn
if !hot {
st.dismissed = false
}
@@ -87,7 +82,7 @@ func HoverCard(c *ui.Context, anchor *ui.Element, opts HoverCardOptions, content
c.After(wait)
} else {
slog.Debug("mujica: hover_card close")
st.open, st.keyMode, st.leaveAt = false, false, time.Time{}
st.open, st.focusIn, st.leaveAt = false, false, time.Time{}
return nil
}
}
@@ -95,43 +90,23 @@ func HoverCard(c *ui.Context, anchor *ui.Element, opts HoverCardOptions, content
if width == 0 {
width = 280
}
style := func(panel *ui.Element, p float32) {
overlayStyle(c, panel, p)
panel.Width(width).Padding(Density(c).Unit() * 4).Gap(Density(c).Unit() * 2).Label(opts.Label).
MaxHeight(popoverRoom(c, b))
overlayLayer(c, st)
// A popover keeps the card in the anchor's focus order and modal scope, even inside a Dialog.
shown := true
panel := ui.PopoverBase(c, anchor, &shown, func(panel *ui.Element) {
overlayStyle(c, panel, overlayAnchored(c, panel, b))
panel.Margin(6, 0, 0, 0).Width(popoverWide(c, width)).Padding(Density(c).Unit() * 4).Gap(Density(c).Unit() * 2).
Label(opts.Label).MaxHeight(popoverRoom(c, b))
ui.Scroll(c).Shrink(1).Children(content)
}
var panel *ui.Element
dismissed, keyMode := false, keyboard || st.keyMode
if keyMode {
shown := true
panel = ui.PopoverBase(c, anchor, &shown, func(panel *ui.Element) {
style(panel.Margin(6, 0, 0, 0), overlayAnchored(c, panel, b))
})
dismissed = !shown
} else {
ui.Overlay(c, func() {
panel = ui.Column(c).Absolute().Role(ui.RolePopup)
prev := panel.Bounds()
x, y := overlayPlace(c, b, width, prev.H, 6)
panel.Left(x).Top(y)
p := overlayEnter(c, panel)
if prev.H == 0 {
p = 0 // not measured yet
c.AnimationFrame()
}
panel.Children(func() { style(panel, p) })
})
dismissed = c.Shortcut(0, ui.KeyEscape)
}
if dismissed {
})
if !shown {
slog.Debug("mujica: hover_card dismiss")
st.open, st.dismissed, st.keyMode = false, true, false
if keyMode {
if keyboard || st.focusIn {
anchor.Focus() // at once, so the anchor stays hot and the card stays dismissed
}
st.open, st.dismissed, st.focusIn = false, true, false
return panel
}
st.panel, st.keyMode = panel.Bounds(), keyboard || keyMode && panel.FocusWithin()
st.panel, st.focusIn = panel.Bounds(), panel.FocusWithin()
return panel
}