Overlays: components 081-090
This commit is contained in:
58 files changed
+3801
-1
No files matched your search
+86
@@ -0,0 +1,86 @@
|
||||
package mujica
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"mujicaui/theme"
|
||||
)
|
||||
|
||||
// TooltipOptions configure a Tooltip.
|
||||
type TooltipOptions struct {
|
||||
// Delay is how long hover or focus rests before the tip shows: 500 ms when zero.
|
||||
Delay time.Duration
|
||||
}
|
||||
|
||||
// tooltipState tracks when the anchor became hot and whether Escape hid the tip.
|
||||
type tooltipState struct {
|
||||
since time.Time
|
||||
dismissed bool
|
||||
}
|
||||
|
||||
const tooltipMaxWidth = 240
|
||||
|
||||
// Tooltip shows text near anchor once the pointer rests on it or it has
|
||||
// the keyboard focus. The tip holds no interactive content, passes the
|
||||
// pointer through, stays in the window and hides on leave, blur or Escape.
|
||||
// It also describes anchor to assistive technology.
|
||||
func Tooltip(c *ui.Context, anchor *ui.Element, text string, opts TooltipOptions) *ui.Element {
|
||||
if anchor == nil {
|
||||
panic("mujica: Tooltip needs an anchor")
|
||||
}
|
||||
if opts.Delay < 0 {
|
||||
panic("mujica: Tooltip Delay is negative")
|
||||
}
|
||||
if text == "" {
|
||||
return anchor
|
||||
}
|
||||
anchor.Description(text)
|
||||
st := ui.Local(anchor, "mujica.tooltip", func() tooltipState { return tooltipState{} })
|
||||
hot := anchor.Hovered() || anchor.FocusVisible()
|
||||
if !hot {
|
||||
st.since, st.dismissed = time.Time{}, false
|
||||
return anchor
|
||||
}
|
||||
if st.dismissed {
|
||||
return anchor
|
||||
}
|
||||
if st.since.IsZero() {
|
||||
st.since = c.Now()
|
||||
}
|
||||
delay := opts.Delay
|
||||
if delay == 0 {
|
||||
delay = 500 * time.Millisecond
|
||||
}
|
||||
if wait := delay - c.Now().Sub(st.since); wait > 0 {
|
||||
c.After(wait)
|
||||
return anchor
|
||||
}
|
||||
if c.Shortcut(0, ui.KeyEscape) {
|
||||
slog.Debug("mujica: tooltip dismiss")
|
||||
st.dismissed = true
|
||||
return anchor
|
||||
}
|
||||
tooltipShow(c, anchor.Bounds(), text)
|
||||
return anchor
|
||||
}
|
||||
|
||||
// tooltipShow draws the tip below a, or above it when there is no room.
|
||||
func tooltipShow(c *ui.Context, a ui.Rect, text string) {
|
||||
k := Tokens(c)
|
||||
u := Density(c).Unit()
|
||||
size := theme.CaptionSize + 1
|
||||
tw, th := c.MeasureText(tooltipMaxWidth, ui.Span{Text: text, Size: size, Font: c.Theme().Font})
|
||||
padX, padY := u*2.5, u*1.5
|
||||
w, h := tw+2*(padX+theme.BorderWidth)+1, th+2*(padY+theme.BorderWidth)
|
||||
x, y := overlayPlace(c, ui.Rect{X: a.X + a.W/2 - w/2, Y: a.Y, W: w, H: a.H}, w, h, 6)
|
||||
ui.Overlay(c, func() {
|
||||
ui.Box(c).Absolute().Left(x).Top(y).Width(w).Padding(padY, padX).Radius(theme.ControlRadius).
|
||||
Background(k.Text).TextColor(k.Background).Border(theme.BorderWidth, k.Text).
|
||||
PassThrough().Role(ui.RoleTooltip).Children(func() {
|
||||
ui.Text(c, text).FontSize(size)
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user