final impl

This commit is contained in:
wiiznokes 2024-06-28 00:23:22 +02:00
parent d76911c294
commit 2dff73b8b2
3 changed files with 129 additions and 42 deletions

View file

@ -1,11 +1,14 @@
// Copyright 2024 wiiznokes
// SPDX-License-Identifier: MIT
//! A widget that display toats.
use std::collections::VecDeque;
use std::time::Duration;
use crate::widget::container;
use crate::widget::Column;
use iced::{Command, Length};
use iced::Command;
use iced_core::Element;
use widget::Toaster;
@ -15,81 +18,155 @@ use super::{button, row, text};
mod widget;
/// Create a new Toaster widget.
pub fn toaster<'a, Message>(
toasts: &'a Toasts<Message>,
content: impl Into<Element<'a, Message, crate::Theme, iced::Renderer>>,
) -> Element<'a, Message, crate::Theme, iced::Renderer>
where
Message: Clone + 'a,
Message: From<ToastMessage> + Clone + 'a,
{
let make_toast = |toast: &'a Toast<Message>| {
let row = row().push(text(&toast.message)).push_maybe(
toast
.action
.as_ref()
.map(|t| button(text(t.0.clone())).on_press(t.1.clone())),
);
let row = row()
.push(text(&toast.message))
.push_maybe(toast.action.as_ref().map(|action| {
button(text(action.description.clone())).on_press(action.message.clone())
}))
.push(button("close").on_press(ToastMessage(toast.id).into()))
.align_items(iced::Alignment::Center);
container(row).into()
container(row)
.padding(10)
.style(crate::style::Container::Card)
};
let col = Column::with_children(toasts.toasts.iter().map(make_toast));
let col =
Column::with_children(toasts.toasts.iter().rev().map(|t| make_toast(t).into())).spacing(5);
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 ToastDuration {
#[default]
Short,
Medium,
Long,
Custom(Duration),
}
impl ToastDuration {
fn duration(&self) -> Duration {
match self {
ToastDuration::Short => Duration::from_millis(200),
ToastDuration::Medium => Duration::from_millis(1000),
ToastDuration::Long => Duration::from_millis(5000),
ToastDuration::Short => Duration::from_millis(2000),
ToastDuration::Long => Duration::from_millis(3500),
ToastDuration::Custom(duration) => *duration,
}
}
}
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)]
pub struct Toast<Message> {
message: String,
/// (Description, message)
action: Option<(String, Message)>,
action: Option<ToastAction<Message>>,
duration: ToastDuration,
id: u32,
}
pub struct Toasts<Message> {
id_count: u32,
toasts: Vec<Toast<Message>>,
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)]
pub struct Toasts<Message> {
id_count: u32,
toasts: VecDeque<Toast<Message>>,
limit: usize,
}
// 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)]
pub struct ToastMessage(u32);
impl<Message> Toasts<Message> {
pub fn push(&mut self, mut toast: Toast<Message>) -> Command<ToastMessage> {
/// 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();
}
toast.id = self.id_count;
self.id_count += 1;
let message = ToastMessage(toast.id);
let duration = toast.duration.duration();
self.toasts.push(toast);
self.toasts.push_back(toast);
Command::perform(
async move {
tokio::time::sleep(duration).await;
},
|_| message,
|_| crate::app::Message::App(message.into()),
)
}
pub fn handle_message(&mut self, message: ToastMessage) {
/// Handle the [`ToastMessage`]
pub fn handle_message(&mut self, message: &ToastMessage) {
self.toasts
.iter()
.position(|e| e.id == message.0)

View file

@ -3,22 +3,18 @@
use iced::{Limits, Size};
use iced_core::layout::Node;
use iced_runtime::core::widget::Id;
use iced_runtime::{keyboard, Command};
use iced_core::event::{self, Event};
use iced_core::renderer::{self, Quad, Renderer};
use iced_core::widget::tree::{self, Tree};
use iced_core::layout;
use iced_core::mouse;
use iced_core::overlay;
use iced_core::renderer::{self};
use iced_core::widget::tree::Tree;
use iced_core::widget::Operation;
use iced_core::Element;
use iced_core::{layout, svg};
use iced_core::{mouse, Border};
use iced_core::{overlay, Shadow};
use iced_core::{touch, Overlay};
use iced_core::{
Background, Clipboard, Color, Layout, Length, Padding, Point, Rectangle, Shell, Vector, Widget,
};
use iced_renderer::core::widget::{operation, OperationOutputWrapper};
use iced_core::Overlay;
use iced_core::{Clipboard, Layout, Length, Point, Rectangle, Shell, Vector, Widget};
use iced_renderer::core::widget::OperationOutputWrapper;
pub struct Toaster<'a, Message, Theme, Renderer> {
toasts: Element<'a, Message, Theme, Renderer>,
@ -145,7 +141,7 @@ where
&'b mut self,
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
_renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
if self.is_empty {
None
@ -189,7 +185,23 @@ where
position: Point,
_translation: Vector,
) -> Node {
todo!()
let limits = Limits::new(Size::ZERO, bounds);
let mut node = self
.element
.as_widget()
.layout(self.state, renderer, &limits);
let offset = 15.;
let position = Point::new(
(bounds.width / 2.) - (node.size().width / 2.),
bounds.height - (node.size().height + offset),
);
node.move_to_mut(position);
node
}
fn draw(