overlay/container.go
Functions
func Children
func (c *Container) Children() []widget.Widget {
if c.content == nil {
return nil
}
return []widget.Widget{c.content}
}
func Content
Content returns the wrapped content widget.
func (c *Container) Content() widget.Widget {
return c.content
}
func Dismiss
Dismiss triggers the dismissal callback.
func (c *Container) Dismiss() {
if c.onDismiss != nil {
c.onDismiss()
}
}
func Draw
Draw renders the optional scrim and then the content.
func (c *Container) Draw(ctx widget.Context, canvas widget.Canvas) {
if canvas == nil {
return
}
if c.modal {
scrim := widget.RGBA(0, 0, 0, scrimAlpha)
canvas.DrawRect(c.Bounds(), scrim)
}
if c.content != nil {
c.content.Draw(ctx, canvas)
}
}
func Event
Event dispatches events. Content gets first chance. If content does not
consume the event, Escape key and mouse press outside content trigger
dismissal. Modal containers consume all remaining events.
func (c *Container) Event(ctx widget.Context, e event.Event) bool {
// Give content first chance at the event.
if c.content != nil && c.content.Event(ctx, e) {
return true
}
// Escape key dismisses.
if ke, ok := e.(*event.KeyEvent); ok {
if ke.KeyType == event.KeyPress && ke.Key == event.KeyEscape {
c.Dismiss()
return true
}
}
// Mouse press outside content dismisses.
if c.handleMouseDismiss(e) {
return true
}
// Modal overlays consume all events to prevent interaction with content below.
return c.modal
}
func Layout
Layout fills the entire window and lays out the content unconstrained.
func (c *Container) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size {
size := constraints.Constrain(c.windowSize)
c.SetBounds(geometry.NewRect(0, 0, size.Width, size.Height))
if c.content != nil {
// Content is laid out with loose constraints so it can take its natural size.
c.content.Layout(ctx, geometry.Loose(size))
}
return size
}
func Modal
Modal returns true if this container blocks interaction with content below.
func (c *Container) Modal() bool {
return c.modal
}
func NewContainer
NewContainer creates a new overlay container wrapping the given content.
The windowSize is used to size the backdrop to fill the entire window.
func NewContainer(content widget.Widget, windowSize geometry.Size, opts ...ContainerOption) *Container {
c := &Container{
content: content,
windowSize: windowSize,
}
c.SetVisible(true)
c.SetEnabled(true)
// Register content as child so tree walkers (dirty collector, hit test,
// Layer Tree builder) can discover it. Without this, Container.Children()
// returns nil and overlay content is invisible to dirty tracking.
if content != nil {
c.AddChild(content)
}
for _, opt := range opts {
opt(c)
}
return c
}
func SetWindowSize
SetWindowSize updates the backdrop size. Call this when the window is resized.
func (c *Container) SetWindowSize(size geometry.Size) {
c.windowSize = size
}
func WithModal
WithModal makes the container modal. A modal container draws a
semi-transparent scrim and consumes all events not handled by the content.
func WithModal(modal bool) ContainerOption {
return func(c *Container) {
c.modal = modal
}
}
func WithOnDismiss
WithOnDismiss sets the callback invoked when the container is dismissed
(click outside content or Escape key).
func WithOnDismiss(fn func()) ContainerOption {
return func(c *Container) {
c.onDismiss = fn
}
}
Structs
type Container struct
Container wraps overlay content with a full-window transparent backdrop.
Clicks on the backdrop (outside the content) trigger dismissal. For modal
overlays, a semi-transparent scrim is drawn behind the content.
Container implements the [Overlay] interface.
type Container struct {
widget.WidgetBase
content widget.Widget
onDismiss func()
modal bool
windowSize geometry.Size
}
Children returns the content widget as the sole child.