transition/transition.go
Functions
func Child
func (t *Transition) Child() widget.Widget {
return t.child
}
func Children
Children returns the wrapped child as a single-element slice.
func (t *Transition) Children() []widget.Widget {
if t.child == nil {
return nil
}
return []widget.Widget{t.child}
}
func Draw
Draw renders the child widget with transition effects applied.
func (t *Transition) Draw(ctx widget.Context, canvas widget.Canvas) {
if !t.shown && !t.animating {
return // fully hidden, nothing to draw
}
if t.child == nil {
return
}
// Update animation progress.
if t.animating {
t.updateAnimation(ctx)
}
// Determine the active effect and eased progress.
eff, easedProgress := t.currentEffect()
// Apply effects, draw child, then pop effects in reverse.
t.drawWithEffects(ctx, canvas, eff, easedProgress)
}
func Duration
Duration sets the animation duration for both enter and exit effects.
func Duration(d time.Duration) Option {
return func(c *transitionConfig) {
c.duration = d
}
}
func Easing
Easing sets the easing function for both enter and exit effects.
Defaults to [animation.EaseOutCubic] if not set.
func Easing(e animation.Easing) Option {
return func(c *transitionConfig) {
c.easing = e
}
}
func EnterEffect
EnterEffect sets the effect played when the widget appears.
func EnterEffect(e Effect) Option {
return func(c *transitionConfig) {
c.enterEffect = e
}
}
func Event
Event dispatches events to the child widget if visible.
func (t *Transition) Event(ctx widget.Context, e event.Event) bool {
if !t.shown || t.child == nil {
return false
}
return t.child.Event(ctx, e)
}
func ExitEffect
ExitEffect sets the effect played when the widget disappears.
func ExitEffect(e Effect) Option {
return func(c *transitionConfig) {
c.exitEffect = e
}
}
func Hide
Hide hides the widget, playing the exit effect if configured.
If an enter animation is currently playing, it is replaced by the
exit animation starting from the beginning.
func (t *Transition) Hide() {
if !t.shown && !t.animating {
return // already fully hidden
}
if t.exitEffect.IsNone() || t.duration <= 0 {
t.shown = false
t.animating = false
t.progress = 0
return
}
t.entering = false
t.animating = true
t.progress = 0
t.startTime = time.Time{} // set on first Draw from ctx.Now()
}
func IsAnimating
IsAnimating reports whether a transition animation is currently in progress.
func (t *Transition) IsAnimating() bool {
return t.animating
}
func IsShown
IsShown reports whether the widget is logically visible (enter complete
or in progress). Returns false if hidden or exit animation is complete.
func (t *Transition) IsShown() bool {
return t.shown
}
func Layout
Layout calculates the transition widget size by delegating to the child.
func (t *Transition) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size {
if t.child == nil {
return constraints.Constrain(geometry.Size{})
}
size := t.child.Layout(ctx, constraints)
t.childSize = size
origin := t.Bounds().Min
setChildBounds(t.child, geometry.FromPointSize(origin, size))
return size
}
func Show
Show makes the widget visible, playing the enter effect if configured.
If an exit animation is currently playing, it is replaced by the
enter animation starting from the beginning.
func (t *Transition) Show() {
if t.shown && !t.animating {
return // already fully visible
}
t.shown = true
if t.enterEffect.IsNone() || t.duration <= 0 {
t.animating = false
t.progress = 1.0
return
}
t.entering = true
t.animating = true
t.progress = 0
t.startTime = time.Time{} // set on first Draw from ctx.Now()
}
func Wrap
Wrap creates a new Transition that wraps the given child widget.
By default the widget starts visible with no animation effects.
Use [EnterEffect], [ExitEffect], [Duration], and [Easing] options
to configure the transition behavior.
wrapped := transition.Wrap(myWidget,
transition.EnterEffect(transition.FadeIn()),
transition.ExitEffect(transition.FadeOut()),
transition.Duration(300 * time.Millisecond),
)
func Wrap(child widget.Widget, opts ...Option) *Transition {
cfg := transitionConfig{
enterEffect: None(),
exitEffect: None(),
duration: defaultDuration,
easing: animation.EaseOutCubic,
}
for _, opt := range opts {
opt(&cfg)
}
t := &Transition{
child: child,
enterEffect: cfg.enterEffect,
exitEffect: cfg.exitEffect,
duration: cfg.duration,
easing: cfg.easing,
shown: true,
}
t.SetVisible(true)
t.SetEnabled(true)
return t
}
Structs
type Transition struct
Transition wraps a child widget with enter/exit animation effects.
When Show is called, the enter effect is played and the child becomes
visible. When Hide is called, the exit effect is played and the child
is hidden after the animation completes.
Transition implements [widget.Widget].
type Transition struct {
widget.WidgetBase
child widget.Widget
enterEffect Effect
exitEffect Effect
duration time.Duration
easing animation.Easing
// Animation state.
shown bool // logical visibility (true after enter, false after exit completes)
animating bool // animation is in progress
entering bool // true = enter animation, false = exit animation
progress float64 // 0.0 -> 1.0 normalized progress
startTime time.Time
// Cached child size from last layout (used for slide/scale calculations).
childSize geometry.Size
}
Interfaces
type OpacityPusher interface
OpacityPusher is an optional Canvas capability for opacity effects.
If the Canvas passed to Draw implements this interface, fade effects
will use PushOpacity/PopOpacity. Otherwise, fade effects are silently
skipped (graceful degradation).
type OpacityPusher interface {
// PushOpacity pushes an opacity multiplier onto the draw state stack.
// Opacity is in the range [0, 1]. Nested calls multiply.
PushOpacity(opacity float64)
// PopOpacity removes the most recently pushed opacity.
PopOpacity()
}
Child returns the wrapped child widget.