Add Toast component

This commit is contained in:
Marshall Bowers 2023-10-09 11:31:56 -04:00
parent 333e3e4f01
commit 02d32de044
6 changed files with 122 additions and 0 deletions

View file

@ -20,6 +20,7 @@ mod tab;
mod tab_bar;
mod terminal;
mod title_bar;
mod toast;
mod toolbar;
mod traffic_lights;
mod workspace;
@ -46,6 +47,7 @@ pub use tab::*;
pub use tab_bar::*;
pub use terminal::*;
pub use title_bar::*;
pub use toast::*;
pub use toolbar::*;
pub use traffic_lights::*;
pub use workspace::*;

View file

@ -0,0 +1,66 @@
use crate::prelude::*;
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
pub enum ToastOrigin {
#[default]
Bottom,
BottomRight,
}
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
pub enum ToastVariant {
#[default]
Toast,
Status,
}
/// 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: HackyChildren<S>,
payload: HackyChildrenPayload,
}
impl<S: 'static + Send + Sync> Toast<S> {
pub fn new(
origin: ToastOrigin,
children: HackyChildren<S>,
payload: HackyChildrenPayload,
) -> Self {
Self {
origin,
children,
payload,
}
}
fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = 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.absolute()
.bottom_4()
.flex()
.py_2()
.px_1p5()
.min_w_40()
.rounded_md()
.fill(color.elevated_surface)
.max_w_64()
.children_any((self.children)(cx, self.payload.as_ref()))
}
}

View file

@ -30,6 +30,26 @@ impl SystemColor {
}
}
#[derive(Clone, Copy)]
pub struct ThemeColor {
pub border: Hsla,
pub border_variant: Hsla,
/// The background color of an elevated surface, like a modal, tooltip or toast.
pub elevated_surface: Hsla,
}
impl ThemeColor {
pub fn new(cx: &WindowContext) -> Self {
let theme = theme(cx);
Self {
border: theme.lowest.base.default.border,
border_variant: theme.lowest.variant.default.border,
elevated_surface: theme.middle.base.default.background,
}
}
}
#[derive(Default, PartialEq, EnumIter, Clone, Copy)]
pub enum HighlightColor {
#[default]