libcosmic/src/widget/toaster/mod.rs

174 lines
4.4 KiB
Rust
Raw Normal View History

2024-06-27 01:25:30 +02:00
// Copyright 2024 wiiznokes
// SPDX-License-Identifier: MIT
2024-07-02 17:51:31 +02:00
//! A widget that displays toasts.
2024-06-28 00:23:22 +02:00
use std::collections::VecDeque;
2024-06-27 01:25:30 +02:00
use std::time::Duration;
2024-07-01 21:41:49 +02:00
use crate::app::Command;
2024-06-27 01:25:30 +02:00
use crate::widget::container;
use crate::widget::Column;
2024-06-27 02:05:14 +02:00
use iced_core::Element;
2024-06-27 01:25:30 +02:00
use widget::Toaster;
use crate::ext::CollectionWidget;
use super::{button, row, text};
mod widget;
2024-06-28 00:23:22 +02:00
/// Create a new Toaster widget.
2024-06-27 02:05:14 +02:00
pub fn toaster<'a, Message>(
2024-06-27 01:25:30 +02:00
toasts: &'a Toasts<Message>,
2024-06-27 02:05:14 +02:00
content: impl Into<Element<'a, Message, crate::Theme, iced::Renderer>>,
) -> Element<'a, Message, crate::Theme, iced::Renderer>
2024-06-27 01:25:30 +02:00
where
2024-07-01 21:41:49 +02:00
Message: From<ToastMessage> + Clone + 'static,
2024-06-27 01:25:30 +02:00
{
2024-06-27 02:05:14 +02:00
let make_toast = |toast: &'a Toast<Message>| {
2024-06-28 00:23:22 +02:00
let row = row()
.push(text(&toast.message))
.push_maybe(toast.action.as_ref().map(|action| {
2024-07-01 21:41:49 +02:00
button::standard(&action.description).on_press(action.message.clone())
2024-06-28 00:23:22 +02:00
}))
2024-07-01 21:41:49 +02:00
.push(button::standard("close").on_press(ToastMessage(toast.id).into()))
2024-06-28 00:23:22 +02:00
.align_items(iced::Alignment::Center);
container(row)
2024-07-02 17:51:31 +02:00
.padding(crate::theme::active().cosmic().space_xs())
2024-06-28 00:23:22 +02:00
.style(crate::style::Container::Card)
2024-06-27 01:25:30 +02:00
};
2024-06-28 00:23:22 +02:00
let col =
Column::with_children(toasts.toasts.iter().rev().map(|t| make_toast(t).into())).spacing(5);
2024-06-27 01:25:30 +02:00
2024-06-27 02:05:14 +02:00
Toaster::new(col.into(), content.into(), toasts.toasts.is_empty()).into()
2024-06-27 01:25:30 +02:00
}
2024-06-28 00:23:22 +02:00
/// Duration for the [`Toast`]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
2024-06-27 01:25:30 +02:00
pub enum ToastDuration {
2024-06-28 00:23:22 +02:00
#[default]
2024-06-27 01:25:30 +02:00
Short,
Long,
2024-06-28 00:23:22 +02:00
Custom(Duration),
2024-06-27 01:25:30 +02:00
}
impl ToastDuration {
fn duration(&self) -> Duration {
match self {
2024-06-28 00:23:22 +02:00
ToastDuration::Short => Duration::from_millis(2000),
ToastDuration::Long => Duration::from_millis(3500),
ToastDuration::Custom(duration) => *duration,
2024-06-27 01:25:30 +02:00
}
}
}
2024-06-28 00:23:22 +02:00
impl From<Duration> for ToastDuration {
fn from(value: Duration) -> Self {
Self::Custom(value)
}
}
/// Action that can be triggered by the user.
///
/// Example: `undo`
#[derive(Debug, Clone)]
pub struct ToastAction<Message> {
pub description: String,
pub message: Message,
}
/// Represent the data used to display a [`Toast`]
#[derive(Debug, Clone)]
2024-06-27 01:25:30 +02:00
pub struct Toast<Message> {
message: String,
2024-06-28 00:23:22 +02:00
action: Option<ToastAction<Message>>,
2024-06-27 01:25:30 +02:00
duration: ToastDuration,
id: u32,
}
2024-06-28 00:23:22 +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: ToastDuration::default(),
id: 0,
}
}
/// Set the [`ToastAction`] of this [`Toast`]
#[must_use]
pub fn action(mut self, action: ToastAction<Message>) -> Self {
self.action.replace(action);
self
}
/// Set the [`ToastDuration`] of this [`Toast`]
#[must_use]
pub fn duration(mut self, duration: impl Into<ToastDuration>) -> Self {
self.duration = duration.into();
self
}
}
#[derive(Debug, Clone)]
2024-06-27 01:25:30 +02:00
pub struct Toasts<Message> {
id_count: u32,
2024-06-28 00:23:22 +02:00
toasts: VecDeque<Toast<Message>>,
limit: usize,
2024-06-27 01:25:30 +02:00
}
2024-06-28 00:23:22 +02:00
// need custom impl to not require Message: Clone
impl<M> Default for Toasts<M> {
fn default() -> Self {
Self {
id_count: 0,
toasts: VecDeque::new(),
limit: 5,
}
}
}
#[derive(Debug, Clone)]
2024-06-27 01:25:30 +02:00
pub struct ToastMessage(u32);
impl<Message> Toasts<Message> {
2024-06-28 00:23:22 +02:00
/// Add a new [`Toast`]
pub fn push(
&mut self,
mut toast: Toast<Message>,
) -> Command<crate::app::message::Message<Message>>
where
Message: From<ToastMessage>,
{
while self.toasts.len() >= self.limit {
self.toasts.pop_front();
}
2024-06-27 01:25:30 +02:00
toast.id = self.id_count;
self.id_count += 1;
let message = ToastMessage(toast.id);
let duration = toast.duration.duration();
2024-06-28 00:23:22 +02:00
self.toasts.push_back(toast);
2024-06-27 01:25:30 +02:00
2024-07-01 21:41:49 +02:00
crate::command::future(async move {
tokio::time::sleep(duration).await;
crate::app::Message::App(Message::from(message))
})
2024-06-27 01:25:30 +02:00
}
2024-06-28 00:23:22 +02:00
/// Handle the [`ToastMessage`]
pub fn handle_message(&mut self, message: &ToastMessage) {
2024-06-27 01:25:30 +02:00
self.toasts
.iter()
.position(|e| e.id == message.0)
.map(|index| self.toasts.remove(index));
}
}