Skip to main content

Toast

Struct Toast 

Source
pub struct Toast {
    pub id: ToastId,
    pub content: ToastContent,
    pub config: ToastConfig,
    pub state: ToastState,
    pub actions: Vec<ToastAction>,
    /* private fields */
}
Expand description

A toast notification widget.

Toasts display transient messages to the user, typically in a corner of the screen. They can auto-dismiss after a duration or be manually dismissed.

§Example

let toast = Toast::new("Operation completed")
    .icon(ToastIcon::Success)
    .position(ToastPosition::TopRight)
    .duration(Duration::from_secs(3));

// Render the toast
toast.render(area, frame);

Fields§

§id: ToastId

Unique identifier.

§content: ToastContent

Toast content.

§config: ToastConfig

Configuration.

§state: ToastState

Internal state.

§actions: Vec<ToastAction>

Interactive action buttons (e.g., “Undo”, “Retry”).

Implementations§

Source§

impl Toast

Source

pub fn new(message: impl Into<String>) -> Self

Create a new toast with the given message.

Source

pub fn with_id(id: ToastId, message: impl Into<String>) -> Self

Create a toast with a specific ID.

Source

pub fn icon(self, icon: ToastIcon) -> Self

Set the toast icon.

Source

pub fn title(self, title: impl Into<String>) -> Self

Set the toast title.

Source

pub fn position(self, position: ToastPosition) -> Self

Set the toast position.

Source

pub fn duration(self, duration: Duration) -> Self

Set the auto-dismiss duration.

Source

pub fn persistent(self) -> Self

Make the toast persistent (no auto-dismiss).

Source

pub fn style_variant(self, variant: ToastStyle) -> Self

Set the style variant.

Source

pub fn max_width(self, width: u16) -> Self

Set the maximum width.

Source

pub fn margin(self, margin: u16) -> Self

Set the margin from screen edges.

Source

pub fn dismissable(self, dismissable: bool) -> Self

Set whether the toast is dismissable.

Source

pub fn style(self, style: Style) -> Self

Set the base style.

Source

pub fn with_icon_style(self, style: Style) -> Self

Set the icon style.

Source

pub fn with_title_style(self, style: Style) -> Self

Set the title style.

Source

pub fn entrance_animation(self, animation: ToastEntranceAnimation) -> Self

Set the entrance animation.

Source

pub fn exit_animation(self, animation: ToastExitAnimation) -> Self

Set the exit animation.

Source

pub fn entrance_duration(self, duration: Duration) -> Self

Set the entrance animation duration.

Source

pub fn exit_duration(self, duration: Duration) -> Self

Set the exit animation duration.

Source

pub fn entrance_easing(self, easing: ToastEasing) -> Self

Set the entrance easing function.

Source

pub fn exit_easing(self, easing: ToastEasing) -> Self

Set the exit easing function.

Source

pub fn action(self, action: ToastAction) -> Self

Add a single action button to the toast.

Source

pub fn actions(self, actions: Vec<ToastAction>) -> Self

Set all action buttons at once.

Source

pub fn with_action_style(self, style: Style) -> Self

Set the style for action buttons.

Source

pub fn with_action_focus_style(self, style: Style) -> Self

Set the style for the focused action button.

Source

pub fn no_animation(self) -> Self

Disable all animations.

Source

pub fn reduced_motion(self, enabled: bool) -> Self

Enable reduced motion mode (skips animations).

Source

pub fn is_expired(&self) -> bool

Check if the toast has expired based on its duration.

Accounts for time spent paused (when actions are focused).

Source

pub fn is_visible(&self) -> bool

Check if the toast should be visible.

A toast is visible if it’s not dismissed, not expired, and not in the Hidden animation phase.

Source

pub fn is_animating(&self) -> bool

Check if the toast is currently animating.

Source

pub fn dismiss(&mut self)

Dismiss the toast, starting exit animation.

Source

pub fn dismiss_immediately(&mut self)

Dismiss immediately without animation.

Source

pub fn tick_animation(&mut self) -> bool

Update the animation state. Call this each frame.

Returns true if the animation phase changed.

Source

pub fn animation_phase(&self) -> ToastAnimationPhase

Get the current animation phase.

Source

pub fn animation_offset(&self) -> (i16, i16)

Get the current animation offset for rendering.

Returns (dx, dy) offset to apply to the position.

Source

pub fn animation_opacity(&self) -> f64

Get the current opacity for rendering (0.0 to 1.0).

Source

pub fn remaining_time(&self) -> Option<Duration>

Get the remaining time before auto-dismiss.

Accounts for paused time.

Source

pub fn handle_key(&mut self, key: KeyEvent) -> ToastEvent

Handle a key event for toast interaction.

Supported keys:

  • Esc: Dismiss the toast (if dismissable).
  • Tab: Cycle focus through action buttons (round-robin).
  • Enter: Invoke the focused action. Returns ToastEvent::Action(id).
Source

pub fn pause_timer(&mut self)

Pause the auto-dismiss timer.

Source

pub fn resume_timer(&mut self)

Resume the auto-dismiss timer.

Source

pub fn clear_focus(&mut self)

Clear action focus and resume the timer.

Source

pub fn has_focus(&self) -> bool

Check whether any action is currently focused.

Source

pub fn focused_action(&self) -> Option<&ToastAction>

Get the currently focused action, if any.

Source

pub fn calculate_dimensions(&self) -> (u16, u16)

Calculate the toast dimensions based on content.

Trait Implementations§

Source§

impl Clone for Toast

Source§

fn clone(&self) -> Toast

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Toast

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Widget for Toast

Source§

fn render(&self, area: Rect, frame: &mut Frame<'_>)

Render the widget into the frame at the given area. Read more
Source§

fn is_essential(&self) -> bool

Whether this widget is essential and should always render. Read more

Auto Trait Implementations§

§

impl Freeze for Toast

§

impl RefUnwindSafe for Toast

§

impl Send for Toast

§

impl Sync for Toast

§

impl Unpin for Toast

§

impl UnwindSafe for Toast

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more