ZIm/crates/ui2/src/components/toast.rs
Nate Butler 7ba305e033 Hook up buttons in NotificationToast
Co-Authored-By: Marshall Bowers <1486634+maxdeviant@users.noreply.github.com>
2023-10-13 11:03:59 -04:00

101 lines
2.5 KiB
Rust

use gpui3::AnyElement;
use smallvec::SmallVec;
use crate::prelude::*;
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
pub enum ToastOrigin {
#[default]
Bottom,
BottomRight,
}
/// A toast is a small, temporary window that appears to show a message to the user
/// or indicate a required action.
///
/// Toasts should not persist on the screen for more than a few seconds unless
/// they are actively showing the a process in progress.
///
/// Only one toast may be visible at a time.
#[derive(Element)]
pub struct Toast<S: 'static + Send + Sync> {
origin: ToastOrigin,
children: SmallVec<[AnyElement<S>; 2]>,
}
impl<S: 'static + Send + Sync> Toast<S> {
pub fn new(origin: ToastOrigin) -> Self {
Self {
origin,
children: SmallVec::new(),
}
}
fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
let color = ThemeColor::new(cx);
let mut div = div();
if self.origin == ToastOrigin::Bottom {
div = div.right_1_2();
} else {
div = div.right_4();
}
div.z_index(5)
.absolute()
.bottom_4()
.flex()
.py_2()
.px_1p5()
.min_w_64()
.rounded_md()
.fill(color.elevated_surface)
.max_w_96()
.children(self.children.drain(..))
}
}
impl<S: 'static + Send + Sync> ParentElement for Toast<S> {
type State = S;
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]> {
&mut self.children
}
}
#[cfg(feature = "stories")]
pub use stories::*;
#[cfg(feature = "stories")]
mod stories {
use std::marker::PhantomData;
use crate::{Label, Story};
use super::*;
#[derive(Element)]
pub struct ToastStory<S: 'static + Send + Sync + Clone> {
state_type: PhantomData<S>,
}
impl<S: 'static + Send + Sync + Clone> ToastStory<S> {
pub fn new() -> Self {
Self {
state_type: PhantomData,
}
}
fn render(
&mut self,
_view: &mut S,
cx: &mut ViewContext<S>,
) -> impl Element<ViewState = S> {
Story::container(cx)
.child(Story::title_for::<_, Toast<S>>(cx))
.child(Story::label(cx, "Default"))
.child(Toast::new(ToastOrigin::Bottom).child(Label::new("label")))
}
}
}