libcosmic/src/widget/toaster/mod.rs

177 lines
4.6 KiB
Rust
Raw Normal View History

2024-07-02 17:55:38 +02:00
// Copyright 2024 wiiznokes
// SPDX-License-Identifier: MPL-2.0
//! A widget that displays toasts.
use std::collections::VecDeque;
use crate::widget::container;
use crate::widget::Column;
use crate::Command;
2024-07-02 17:55:38 +02:00
use iced_core::Element;
use widget::Toaster;
use crate::ext::CollectionWidget;
use super::column;
2024-07-11 13:36:53 -06:00
use super::{button, icon, row, text};
2024-07-02 17:55:38 +02:00
mod widget;
/// Create a new Toaster widget.
pub fn toaster<'a, Message: Clone + 'static>(
2024-07-02 17:55:38 +02:00
toasts: &'a Toasts<Message>,
content: impl Into<Element<'a, Message, crate::Theme, iced::Renderer>>,
) -> Element<'a, Message, crate::Theme, iced::Renderer> {
2024-07-11 13:36:53 -06:00
let theme = crate::theme::active();
let cosmic_theme::Spacing {
space_xxxs,
space_xxs,
space_s,
space_m,
..
} = theme.cosmic().spacing;
let make_toast = move |(id, toast): (usize, &'a Toast<Message>)| {
2024-07-02 17:55:38 +02:00
let row = row()
.push(text(&toast.message))
2024-07-11 13:36:53 -06:00
.push(
row()
.push_maybe(toast.action.as_ref().map(|action| {
button::text(&action.description).on_press((action.message)(id))
2024-07-11 13:36:53 -06:00
}))
.push(
button::icon(icon::from_name("window-close-symbolic"))
.on_press((toasts.on_close)(id)),
2024-07-11 13:36:53 -06:00
)
.align_items(iced::Alignment::Center)
.spacing(space_xxs),
)
.align_items(iced::Alignment::Center)
.spacing(space_s);
2024-07-02 17:55:38 +02:00
container(row)
2024-07-11 13:36:53 -06:00
.padding([space_xxs, space_s, space_xxs, space_m])
.style(crate::style::Container::Tooltip)
2024-07-02 17:55:38 +02:00
};
let col = toasts
.toasts
.iter()
.enumerate()
2024-07-02 17:55:38 +02:00
.rev()
.map(make_toast)
.fold(column::with_capacity(toasts.toasts.len()), Column::push)
2024-07-11 13:36:53 -06:00
.spacing(space_xxxs);
2024-07-02 17:55:38 +02:00
Toaster::new(col.into(), content.into(), toasts.toasts.is_empty()).into()
}
/// Duration for the [`Toast`]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum Duration {
2024-07-02 17:55:38 +02:00
#[default]
Short,
Long,
Custom(std::time::Duration),
2024-07-02 17:55:38 +02:00
}
impl Duration {
fn duration(&self) -> std::time::Duration {
2024-07-02 17:55:38 +02:00
match self {
Duration::Short => std::time::Duration::from_millis(5000),
Duration::Long => std::time::Duration::from_millis(15000),
Duration::Custom(duration) => *duration,
2024-07-02 17:55:38 +02:00
}
}
}
impl From<std::time::Duration> for Duration {
fn from(value: std::time::Duration) -> Self {
2024-07-02 17:55:38 +02:00
Self::Custom(value)
}
}
/// Action that can be triggered by the user.
///
/// Example: `undo`
#[derive(Debug, Clone)]
pub struct Action<Message> {
2024-07-02 17:55:38 +02:00
pub description: String,
pub message: fn(usize) -> Message,
2024-07-02 17:55:38 +02:00
}
/// Represent the data used to display a [`Toast`]
#[derive(Debug, Clone)]
pub struct Toast<Message> {
message: String,
action: Option<Action<Message>>,
duration: Duration,
2024-07-02 17:55:38 +02:00
}
impl<Message> Toast<Message> {
/// Construct a new [`Toast`] with the provided message.
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
action: None,
duration: Duration::default(),
2024-07-02 17:55:38 +02:00
}
}
/// Set the [`Action`] of this [`Toast`]
2024-07-02 17:55:38 +02:00
#[must_use]
pub fn action(mut self, action: Action<Message>) -> Self {
2024-07-02 17:55:38 +02:00
self.action.replace(action);
self
}
/// Set the [`Duration`] of this [`Toast`]
2024-07-02 17:55:38 +02:00
#[must_use]
pub fn duration(mut self, duration: impl Into<Duration>) -> Self {
2024-07-02 17:55:38 +02:00
self.duration = duration.into();
self
}
}
#[derive(Debug, Clone)]
pub struct Toasts<Message> {
toasts: VecDeque<Toast<Message>>,
on_close: fn(usize) -> Message,
2024-07-02 17:55:38 +02:00
limit: usize,
}
impl<Message: Clone + Send + 'static> Toasts<Message> {
pub fn new(on_close: fn(usize) -> Message) -> Self {
2024-07-02 17:55:38 +02:00
Self {
toasts: VecDeque::new(),
on_close,
2024-07-02 17:55:38 +02:00
limit: 5,
}
}
/// Add a new [`Toast`]
pub fn push(&mut self, toast: Toast<Message>) -> Command<Message> {
2024-07-02 17:55:38 +02:00
while self.toasts.len() >= self.limit {
self.toasts.pop_front();
}
let duration = toast.duration.duration();
let id = self.toasts.len();
2024-07-02 17:55:38 +02:00
self.toasts.push_back(toast);
let on_close = self.on_close;
2024-07-02 17:55:38 +02:00
crate::command::future(async move {
2024-07-05 14:08:07 -04:00
#[cfg(feature = "tokio")]
2024-07-02 17:55:38 +02:00
tokio::time::sleep(duration).await;
on_close(id)
2024-07-02 17:55:38 +02:00
})
}
/// Remove a [`Toast`]
pub fn remove(&mut self, id: usize) {
self.toasts.remove(id);
2024-07-02 17:55:38 +02:00
}
}