popover

package
v0.1.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package popover provides floating overlay widgets anchored to a trigger.

Two widget types are provided:

  • Popover -- click-triggered, displays arbitrary widget content
  • Tooltip -- hover-triggered, displays a text label

Both widgets position themselves relative to a trigger widget using one of 12 placement options (4 sides x 3 alignments). If the preferred placement would overflow the viewport, the widget automatically flips to the opposite side and clamps to the window bounds.

Popover

A Popover opens on click and displays any widget.Widget as content. It uses the overlay stack for z-ordering and supports click-outside dismissal.

pop := popover.NewPopover(
    popover.TriggerWidget(myButton),
    popover.Content(menuPanel),
    popover.PlacementOpt(popover.BottomStart),
    popover.DismissOnClickOutside(true),
)

Tooltip

A Tooltip opens after a configurable hover delay and displays a short text label. It closes when the mouse leaves the trigger.

tip := popover.NewTooltip(
    popover.TriggerWidget(saveButton),
    popover.TooltipText("Save document"),
    popover.PlacementOpt(popover.Bottom),
    popover.Delay(500 * time.Millisecond),
)

Visual Style

Rendering is delegated to a Painter interface. If no painter is set, DefaultPainter is used, which draws a rounded rectangle with a shadow.

Overlay Integration

Both widgets push themselves onto the window's overlay stack when shown and remove themselves when hidden. They integrate with widget.OverlayManager obtained from the widget.Context.

Signal Binding

The visible state can be bound to a reactive signal:

visible := state.NewSignal(false)
pop := popover.NewPopover(
    popover.TriggerWidget(btn),
    popover.Content(panel),
    popover.VisibleSignal(visible),
)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculatePosition

func CalculatePosition(
	placement Placement,
	triggerBounds geometry.Rect,
	overlaySize geometry.Size,
	windowSize geometry.Size,
	gap float32,
) geometry.Point

CalculatePosition computes the overlay position given the trigger bounds, overlay size, and window size. It applies auto-flip logic when the overlay would overflow the viewport, then clamps to the window bounds.

Types

type DefaultPainter

type DefaultPainter struct{}

DefaultPainter provides a minimal fallback painter for popovers and tooltips. It draws rounded rectangles with a subtle shadow.

func (DefaultPainter) PaintPopover

func (p DefaultPainter) PaintPopover(canvas widget.Canvas, st *PopoverPaintState)

PaintPopover renders a popover background with shadow.

func (DefaultPainter) PaintTooltip

func (p DefaultPainter) PaintTooltip(canvas widget.Canvas, st *TooltipPaintState)

PaintTooltip renders a tooltip background and text.

type Option

type Option func(*config)

Option configures a Popover or Tooltip during construction.

func Content

func Content(w widget.Widget) Option

Content sets the widget displayed inside the popover.

func ContentSize

func ContentSize(width, height float32) Option

ContentSize sets a fixed size for the popover content area. If not set, the content widget's natural size is used.

func Delay

func Delay(d time.Duration) Option

Delay sets the hover delay before showing a tooltip. Defaults to 500ms. Only applies to Tooltip, not Popover.

func Disabled

func Disabled(d bool) Option

Disabled sets the popover's disabled state. Disabled popovers do not open.

func DisabledFn

func DisabledFn(fn func() bool) Option

DisabledFn sets a dynamic function that determines whether the popover is disabled. When set, this takes precedence over the static value.

func DismissOnClickOutside

func DismissOnClickOutside(dismiss bool) Option

DismissOnClickOutside controls whether clicking outside the popover content dismisses it. Defaults to true.

func Gap

func Gap(g float32) Option

Gap sets the spacing between the trigger and the overlay in logical pixels. Defaults to 4.

func MaxWidth

func MaxWidth(w float32) Option

MaxWidth sets the maximum width for tooltip text wrapping. Defaults to 300 logical pixels.

func OnHide

func OnHide(fn func()) Option

OnHide sets a callback invoked when the popover or tooltip is hidden.

func OnShow

func OnShow(fn func()) Option

OnShow sets a callback invoked when the popover or tooltip becomes visible.

func PainterOpt

func PainterOpt(p Painter) Option

PainterOpt sets the painter used to render the popover or tooltip.

func PlacementOpt

func PlacementOpt(p Placement) Option

PlacementOpt sets the preferred placement relative to the trigger.

func TooltipText

func TooltipText(text string) Option

TooltipText sets the text displayed in a tooltip.

func TriggerWidget

func TriggerWidget(w widget.Widget) Option

TriggerWidget sets the widget that anchors the popover/tooltip.

func VisibleSignal

func VisibleSignal(sig state.Signal[bool]) Option

VisibleSignal binds the visible state to a reactive signal for two-way data binding. When the signal is set externally, the popover shows/hides.

type Painter

type Painter interface {
	// PaintPopover draws the popover background (rounded rect + shadow).
	PaintPopover(canvas widget.Canvas, state *PopoverPaintState)

	// PaintTooltip draws the tooltip background and text.
	PaintTooltip(canvas widget.Canvas, state *TooltipPaintState)
}

Painter draws the visual representation of popovers and tooltips. Each design system (Material 3, Fluent, Cupertino) provides its own Painter implementation. If no Painter is set, DefaultPainter is used.

type Placement

type Placement uint8

Placement defines where the popover or tooltip appears relative to its trigger widget. There are 12 placements: 4 sides with 3 alignment variants each (start, center, end).

const (
	// Bottom places the overlay below the trigger, centered horizontally.
	Bottom Placement = iota

	// BottomStart places the overlay below, aligned to the trigger's start edge.
	BottomStart

	// BottomEnd places the overlay below, aligned to the trigger's end edge.
	BottomEnd

	// Top places the overlay above the trigger, centered horizontally.
	Top

	// TopStart places the overlay above, aligned to the trigger's start edge.
	TopStart

	// TopEnd places the overlay above, aligned to the trigger's end edge.
	TopEnd

	// Left places the overlay to the left of the trigger, centered vertically.
	Left

	// LeftStart places the overlay to the left, aligned to the trigger's top edge.
	LeftStart

	// LeftEnd places the overlay to the left, aligned to the trigger's bottom edge.
	LeftEnd

	// Right places the overlay to the right of the trigger, centered vertically.
	Right

	// RightStart places the overlay to the right, aligned to the trigger's top edge.
	RightStart

	// RightEnd places the overlay to the right, aligned to the trigger's bottom edge.
	RightEnd
)

Placement constants.

func (Placement) String

func (p Placement) String() string

String returns a human-readable name for the placement.

type Popover

type Popover struct {
	widget.WidgetBase
	// contains filtered or unexported fields
}

Popover is a click-triggered floating overlay that displays arbitrary widget content anchored to a trigger widget. It integrates with the overlay stack for z-ordering and supports click-outside dismissal.

Create with NewPopover using functional options.

func NewPopover

func NewPopover(opts ...Option) *Popover

NewPopover creates a new Popover with the given options.

The returned widget is visible and enabled by default, but the popover content is initially hidden until opened via click or Popover.Show.

func (*Popover) Children

func (p *Popover) Children() []widget.Widget

Children returns the trigger widget as the sole child. The popover content is rendered in the overlay, not as a child.

func (*Popover) Draw

func (p *Popover) Draw(ctx widget.Context, canvas widget.Canvas)

Draw renders the trigger widget. The popover content is drawn by the overlay stack, not as part of this widget's draw call.

func (*Popover) Event

func (p *Popover) Event(ctx widget.Context, e event.Event) bool

Event handles input events for the popover trigger.

func (*Popover) Hide

func (p *Popover) Hide(ctx widget.Context)

Hide closes the popover content overlay.

func (*Popover) IsFocusable

func (p *Popover) IsFocusable() bool

IsFocusable reports whether the popover trigger can receive focus.

func (*Popover) IsOpen

func (p *Popover) IsOpen() bool

IsOpen returns true if the popover content is currently visible.

func (*Popover) Layout

func (p *Popover) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size

Layout calculates the popover's size. The popover itself takes the size of its trigger widget.

func (*Popover) Mount

func (p *Popover) Mount(ctx widget.Context)

Mount creates signal bindings for push-based invalidation.

func (*Popover) Show

func (p *Popover) Show(ctx widget.Context)

Show opens the popover content overlay.

func (*Popover) Toggle

func (p *Popover) Toggle(ctx widget.Context)

Toggle opens the popover if closed, closes it if open.

func (*Popover) Unmount

func (p *Popover) Unmount()

Unmount is called when the popover is removed from the widget tree.

type PopoverColorScheme

type PopoverColorScheme struct {
	Background widget.Color
	Border     widget.Color
	Shadow     widget.Color
	ShadowBlur float32
}

PopoverColorScheme provides theme-derived colors for popover painting. Zero value means the painter should use its built-in defaults.

type PopoverPaintState

type PopoverPaintState struct {
	// Bounds is the popover content area bounds in window coordinates.
	Bounds geometry.Rect

	// Placement is the resolved placement direction.
	Placement Placement

	// ColorScheme provides theme-derived colors. Zero value means use defaults.
	ColorScheme PopoverColorScheme
}

PopoverPaintState provides the current popover state to the painter.

type Tooltip

type Tooltip struct {
	widget.WidgetBase
	// contains filtered or unexported fields
}

Tooltip is a hover-triggered floating label anchored to a trigger widget. It appears after a configurable delay when the mouse enters the trigger, and disappears when the mouse leaves.

Create with NewTooltip using functional options.

func NewTooltip

func NewTooltip(opts ...Option) *Tooltip

NewTooltip creates a new Tooltip with the given options.

The tooltip is initially hidden and appears on hover after the configured delay.

func (*Tooltip) Children

func (t *Tooltip) Children() []widget.Widget

Children returns the trigger widget as the sole child.

func (*Tooltip) Draw

func (t *Tooltip) Draw(ctx widget.Context, canvas widget.Canvas)

Draw renders the trigger widget. The tooltip content is drawn by the overlay stack.

func (*Tooltip) Event

func (t *Tooltip) Event(ctx widget.Context, e event.Event) bool

Event handles input events, primarily mouse enter/leave on the trigger.

func (*Tooltip) IsOpen

func (t *Tooltip) IsOpen() bool

IsOpen returns true if the tooltip is currently visible.

func (*Tooltip) Layout

func (t *Tooltip) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size

Layout calculates the tooltip's size. The tooltip itself takes the size of its trigger widget.

func (*Tooltip) Mount

func (t *Tooltip) Mount(ctx widget.Context)

Mount creates signal bindings for push-based invalidation.

func (*Tooltip) Text

func (t *Tooltip) Text() string

Text returns the tooltip text.

func (*Tooltip) Unmount

func (t *Tooltip) Unmount()

Unmount is called when the tooltip is removed from the widget tree.

type TooltipColorScheme

type TooltipColorScheme struct {
	Background widget.Color
	TextColor  widget.Color
	Border     widget.Color
}

TooltipColorScheme provides theme-derived colors for tooltip painting. Zero value means the painter should use its built-in defaults.

type TooltipPaintState

type TooltipPaintState struct {
	// Bounds is the tooltip's bounds in window coordinates.
	Bounds geometry.Rect

	// Text is the tooltip text to display.
	Text string

	// Placement is the resolved placement direction.
	Placement Placement

	// ColorScheme provides theme-derived colors. Zero value means use defaults.
	ColorScheme TooltipColorScheme
}

TooltipPaintState provides the current tooltip state to the painter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL