Documentation
¶
Overview ¶
Package collapsible provides a collapsible/expandable section widget.
A collapsible section has a clickable header bar and a content area that shows or hides when the header is toggled. The expand/collapse transition can be animated using the animation engine.
Construction uses functional options for immutable configuration:
section := collapsible.New(
collapsible.Title("CPU Usage"),
collapsible.Content(chartWidget),
collapsible.Expanded(true),
collapsible.HeaderHeight(36),
collapsible.Animated(true),
collapsible.Duration(200*time.Millisecond),
)
Visual Style ¶
The header rendering is provided by a Painter implementation. Each design system (Material 3, Fluent, Cupertino) can supply its own painter. If no painter is set, DefaultPainter is used, which draws a minimal header with a background, title text, and an expand/collapse arrow indicator.
Animation ¶
When Animated is true (the default), toggling the section smoothly animates the content height using animation.EaseInOutCubic easing. The animation progress drives content clipping via widget.Canvas.PushClip. Set Animated to false for instant expand/collapse.
Signal Binding ¶
The expanded state can be bound to a reactive signal:
- ExpandedSignal -- TWO-WAY binding: reads from signal AND writes back on toggle
Interaction ¶
Clicking the header or pressing Enter/Space when focused toggles the section. The OnToggle callback is invoked with the new expanded state.
Focus ¶
The collapsible section implements widget.Focusable and participates in tab navigation. A focus ring is drawn on the header when focused.
Index ¶
- type DefaultPainter
- type HeaderState
- type Option
- func Animated(b bool) Option
- func ArrowColor(color widget.Color) Option
- func Content(w widget.Widget) Option
- func Duration(d time.Duration) Option
- func Expanded(b bool) Option
- func ExpandedReadonlySignal(sig state.ReadonlySignal[bool]) Option
- func ExpandedSignal(sig state.Signal[bool]) Option
- func HeaderColor(color widget.Color) Option
- func HeaderHeight(h float32) Option
- func OnToggle(fn func(expanded bool)) Option
- func PainterOpt(p Painter) Option
- func Title(s string) Option
- func TitleFn(fn func() string) Option
- type Painter
- type Widget
- func (w *Widget) Children() []widget.Widget
- func (w *Widget) Draw(ctx widget.Context, canvas widget.Canvas)
- func (w *Widget) Event(ctx widget.Context, e event.Event) bool
- func (w *Widget) IsAnimating() bool
- func (w *Widget) IsExpanded() bool
- func (w *Widget) IsFocusable() bool
- func (w *Widget) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size
- func (w *Widget) Mount(ctx widget.Context)
- func (w *Widget) Progress() float32
- func (w *Widget) SetExpanded(expanded bool)
- func (w *Widget) Toggle()
- func (w *Widget) Unmount()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DefaultPainter ¶
type DefaultPainter struct{}
DefaultPainter provides a minimal fallback header painter. It draws a background, title text, and an arrow indicator.
func (DefaultPainter) PaintHeader ¶
func (p DefaultPainter) PaintHeader(canvas widget.Canvas, s HeaderState)
PaintHeader renders a minimal collapsible header.
type HeaderState ¶
type HeaderState struct {
Title string
Expanded bool
Hovered bool
Pressed bool
Focused bool
Bounds geometry.Rect
ArrowProgress float32 // 0.0 = collapsed (right arrow), 1.0 = expanded (down arrow)
// Styling overrides (zero value means use painter defaults).
HeaderColor widget.Color
ArrowColor widget.Color
}
HeaderState provides the current state to the painter for header rendering.
type Option ¶
type Option func(*config)
Option configures a collapsible section during construction.
func ArrowColor ¶
ArrowColor sets the color of the expand/collapse arrow indicator. Default is a dark gray.
func Expanded ¶
Expanded sets the initial expanded state. When true, the content is visible by default.
func ExpandedReadonlySignal ¶
func ExpandedReadonlySignal(sig state.ReadonlySignal[bool]) Option
ExpandedReadonlySignal binds the expanded state to a read-only signal. This is useful for computed signals created via state.NewComputed. When set, this takes highest precedence over all other expanded sources.
func ExpandedSignal ¶
ExpandedSignal binds the expanded state to a reactive signal. This is a TWO-WAY binding: the widget reads from the signal, and when the user toggles, the new state is written back. When set, the signal value takes precedence over Expanded.
func HeaderColor ¶
HeaderColor sets the background color of the header bar. Default is a light gray.
func HeaderHeight ¶
HeaderHeight sets the height of the header bar in logical pixels. Default is 36.
func OnToggle ¶
OnToggle sets the callback invoked when the section is toggled. The callback receives the new expanded state.
func PainterOpt ¶
PainterOpt sets the painter used to render the header. Each design system provides its own painter. If not set, DefaultPainter is used.
type Painter ¶
type Painter interface {
PaintHeader(canvas widget.Canvas, state HeaderState)
}
Painter draws the visual representation of a collapsible section header. Each design system (Material 3, Fluent, Cupertino) provides its own Painter implementation.
If no Painter is set, the section uses DefaultPainter.
type Widget ¶
type Widget struct {
widget.WidgetBase
// contains filtered or unexported fields
}
Widget implements a collapsible section with a clickable header and expandable content area.
Create with New using functional options:
section := collapsible.New(
collapsible.Title("Details"),
collapsible.Content(detailWidget),
collapsible.Expanded(true),
)
func New ¶
New creates a new collapsible section Widget with the given options.
The returned widget is visible and enabled by default. The content starts collapsed unless Expanded is set to true.
func (*Widget) Children ¶
Children returns the content widget for tree traversal. The content is always returned even when collapsed, to allow the framework to manage lifecycle and focus traversal.
func (*Widget) IsAnimating ¶
IsAnimating reports whether an expand/collapse animation is in progress.
func (*Widget) IsExpanded ¶
IsExpanded returns the current expanded state.
func (*Widget) IsFocusable ¶
IsFocusable reports whether the collapsible section can receive focus.
func (*Widget) Layout ¶
Layout calculates the widget's size given constraints.
The height depends on the animation progress:
- Collapsed: headerHeight only
- Expanded: headerHeight + content height
- Animating: headerHeight + content height * progress
func (*Widget) Mount ¶
Mount creates signal bindings for push-based invalidation. Implements widget.Lifecycle.
func (*Widget) Progress ¶
Progress returns the current animation progress (0.0 = collapsed, 1.0 = expanded). This is useful for testing and external monitoring.
func (*Widget) SetExpanded ¶
SetExpanded sets the expanded state programmatically. If animated, triggers a smooth transition.
func (*Widget) Unmount ¶
func (w *Widget) Unmount()
Unmount is called when the widget is removed from the tree. Implements widget.Lifecycle.