transition/slide.go

Functions Structs

Functions

func Child

Child returns the wrapped child widget.

func (s *Slide) Child() widget.Widget {
	return s.child
}

func Children

Children returns the wrapped child as a single-element slice.

func (s *Slide) Children() []widget.Widget {
	if s.child == nil {
		return nil
	}
	return []widget.Widget{s.child}
}

func Draw

Draw renders the child with a translation offset based on animation progress.

func (s *Slide) Draw(ctx widget.Context, canvas widget.Canvas) {
	if s.child == nil {
		return
	}

	// Update animation progress.
	if s.animating {
		s.updateAnimation(ctx)
	}

	// Compute translation offset.
	offset := s.computeOffset()

	// Apply transform, draw child, pop transform.
	if offset.X != 0 || offset.Y != 0 {
		canvas.PushTransform(offset)
		s.child.Draw(ctx, canvas)
		canvas.PopTransform()
	} else {
		s.child.Draw(ctx, canvas)
	}
}

func Event

Event dispatches events to the child widget.

func (s *Slide) Event(ctx widget.Context, e event.Event) bool {
	if s.child == nil {
		return false
	}
	return s.child.Event(ctx, e)
}

func IsAnimating

IsAnimating reports whether a slide animation is currently in progress.

func (s *Slide) IsAnimating() bool {
	return s.animating
}

func Layout

Layout delegates to the child and returns its preferred size.

func (s *Slide) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size {
	if s.child == nil {
		return constraints.Constrain(geometry.Size{})
	}
	size := s.child.Layout(ctx, constraints)
	origin := s.Bounds().Min
	setChildBounds(s.child, geometry.FromPointSize(origin, size))
	return size
}

func Mount

Mount starts the auto-start animation if enabled.

Implements [widget.Lifecycle].

func (s *Slide) Mount(_ widget.Context) {
	if s.autoStart {
		if s.reverse {
			s.SlideOut()
		} else {
			s.SlideIn()
		}
	}
}

func NewSlide

NewSlide creates a new Slide transition wrapping the given child widget.

 

The child's position is animated from off-screen to its natural position

(or vice versa) based on the configured direction. By default the

animation starts automatically when the widget is mounted.

 

slide := transition.NewSlide(myWidget,

transition.SlideFrom(transition.FromTop),

transition.SlideDuration(300 * time.Millisecond),

)

func NewSlide(child widget.Widget, opts ...SlideOption) *Slide {
	cfg := slideConfig{
		direction:	FromTop,
		duration:	defaultSlideDuration,
		easing:		animation.EaseOutCubic,
	}
	for _, opt := range opts {
		opt(&cfg)
	}

	autoStart := true
	if cfg.autoStart != nil {
		autoStart = *cfg.autoStart
	}

	s := &Slide{
		child:		child,
		direction:	cfg.direction,
		duration:	cfg.duration,
		easing:		cfg.easing,
		autoStart:	autoStart,
		reverse:	cfg.reverse,
	}
	s.SetVisible(true)
	s.SetEnabled(true)

	// If autoStart with reverse, start fully visible (will slide out on mount).
	// Otherwise start hidden (will slide in on mount).
	if autoStart && cfg.reverse {
		s.progress = 1.0
	}

	return s
}

func Progress

Progress returns the current animation progress (0.0 to 1.0).

 

For slide-in: 0 = fully offscreen, 1 = fully visible.

For slide-out: 0 = fully visible, 1 = fully offscreen.

func (s *Slide) Progress() float32 {
	return s.progress
}

func SetChild

SetChild replaces the wrapped child widget.

func (s *Slide) SetChild(child widget.Widget) {
	s.child = child
}

func SlideAutoStart

SlideAutoStart controls whether the animation starts automatically on

[Slide.Mount]. Defaults to true.

func SlideAutoStart(start bool) SlideOption {
	return func(c *slideConfig) {
		c.autoStart = &start
	}
}

func SlideDuration

SlideDuration sets the animation duration. Defaults to 300ms.

func SlideDuration(d time.Duration) SlideOption {
	return func(c *slideConfig) {
		c.duration = d
	}
}

func SlideEasing

SlideEasing sets the easing function. Defaults to [animation.EaseOutCubic].

func SlideEasing(e animation.Easing) SlideOption {
	return func(c *slideConfig) {
		c.easing = e
	}
}

func SlideFrom

SlideFrom sets the direction from which the child slides in.

Defaults to [FromTop].

func SlideFrom(dir Direction) SlideOption {
	return func(c *slideConfig) {
		c.direction = dir
	}
}

func SlideIn

SlideIn triggers a slide-in animation (offscreen to natural position).

 

If an animation is already in progress, it is restarted from the

beginning. The child becomes visible immediately.

func (s *Slide) SlideIn() {
	s.slideIn = true
	s.animating = true
	s.progress = 0
	s.startTime = time.Time{}	// set on first Draw from ctx.Now()
}

func SlideOut

SlideOut triggers a slide-out animation (natural position to offscreen).

 

If an animation is already in progress, it is restarted from the

beginning.

func (s *Slide) SlideOut() {
	s.slideIn = false
	s.animating = true
	s.progress = 0
	s.startTime = time.Time{}	// set on first Draw from ctx.Now()
}

func SlideReverse

SlideReverse makes the auto-start animation slide OUT instead of IN.

When true and autoStart is enabled, the widget starts visible and

slides out on Mount. Defaults to false.

func SlideReverse(rev bool) SlideOption {
	return func(c *slideConfig) {
		c.reverse = rev
	}
}

func Unmount

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

Implements [widget.Lifecycle].

func (s *Slide) Unmount() {
	s.animating = false
}

Structs

type Slide struct

Slide animates its child's position using [widget.Canvas.PushTransform].

 

The child slides from off-screen (one full dimension away) to its

natural position (slide-in), or from its natural position to off-screen

(slide-out). This is useful for notification slide-ins, page transitions,

and drawer animations.

 

Create a Slide with [NewSlide] and functional options:

 

slide := transition.NewSlide(myWidget,

transition.SlideFrom(transition.FromTop),

transition.SlideDuration(300 * time.Millisecond),

transition.SlideEasing(animation.EaseOutCubic),

)

 

By default, the animation starts automatically on [Slide.Mount] (autoStart).

Call [Slide.SlideIn] or [Slide.SlideOut] to trigger animations manually.

type Slide struct {
	widget.WidgetBase

	child		widget.Widget
	direction	Direction
	duration	time.Duration
	easing		animation.Easing
	autoStart	bool
	reverse		bool	// true = slide OUT on auto-start instead of IN

	// Animation state.
	progress	float32		// 0.0 = hidden (offscreen), 1.0 = fully visible
	animating	bool		// animation is in progress
	slideIn		bool		// true = sliding in, false = sliding out
	startTime	time.Time	// set on first Draw tick from ctx.Now()
}