mod host;
mod render;
use std::{rc::Rc, time::Duration};
use gpui::{AnyElement, App, EntityId, Hsla, IntoElement, SharedString, Window};
use gpui_material_theme::CornerShape;
use crate::SemanticTone;
pub use host::SnackbarHost;
pub(super) type Action = Rc<dyn Fn(&mut Window, &mut App)>;
pub(super) type Content = Box<dyn Fn(&mut Window, &mut App) -> AnyElement>;
pub(super) type Completion = Rc<dyn Fn(SnackbarResult, &mut Window, &mut App)>;
pub(super) type HostRenderer =
Box<dyn Fn(&Snackbar, SnackbarParts, &mut Window, &mut App) -> AnyElement>;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SnackbarResult {
ActionPerformed,
Dismissed,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct SnackbarId {
pub(super) host: EntityId,
pub(super) sequence: usize,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SnackbarColors {
pub container: Option<Hsla>,
pub content: Option<Hsla>,
pub action: Option<Hsla>,
pub dismiss: Option<Hsla>,
}
#[non_exhaustive]
pub struct SnackbarParts {
pub message: AnyElement,
pub action: Option<AnyElement>,
pub dismiss: Option<AnyElement>,
pub scale: f32,
}
pub struct Snackbar {
pub(super) message: SharedString,
pub(super) timeout: Option<Duration>,
pub(super) action: Option<(SharedString, Action)>,
pub(super) message_content: Option<Content>,
pub(super) action_content: Option<Content>,
pub(super) dismiss_label: Option<SharedString>,
pub(super) dismiss_content: Option<(SharedString, Content)>,
pub(super) show_dismiss: bool,
pub(super) action_on_new_line: bool,
pub(super) shape: CornerShape,
pub(super) tone: SemanticTone,
pub(super) colors: SnackbarColors,
pub(super) on_dismiss: Option<Action>,
pub(super) on_result: Option<Completion>,
}
impl Snackbar {
pub fn new(message: impl Into<SharedString>) -> Self {
Self {
message: message.into(),
timeout: Some(Duration::from_secs(4)),
action: None,
message_content: None,
action_content: None,
dismiss_label: None,
dismiss_content: None,
show_dismiss: true,
action_on_new_line: false,
shape: CornerShape::Radii([4.; 4]),
tone: SemanticTone::Default,
colors: SnackbarColors::default(),
on_dismiss: None,
on_result: None,
}
}
pub fn timeout(mut self, timeout: Option<Duration>) -> Self {
self.timeout = timeout;
self
}
pub fn action(
mut self,
label: impl Into<SharedString>,
action: impl Fn(&mut Window, &mut App) + 'static,
) -> Self {
self.action = Some((label.into(), Rc::new(action)));
self.timeout = None;
self
}
pub fn message(&self) -> &SharedString {
&self.message
}
pub fn action_label(&self) -> Option<&SharedString> {
self.action.as_ref().map(|(label, _)| label)
}
pub fn message_content<E: IntoElement>(
mut self,
content: impl Fn(&mut Window, &mut App) -> E + 'static,
) -> Self {
self.message_content = Some(Box::new(move |window, cx| {
content(window, cx).into_any_element()
}));
self
}
pub fn action_content<E: IntoElement>(
mut self,
content: impl Fn(&mut Window, &mut App) -> E + 'static,
) -> Self {
self.action_content = Some(Box::new(move |window, cx| {
content(window, cx).into_any_element()
}));
self
}
pub fn dismiss_label(mut self, label: impl Into<SharedString>) -> Self {
self.dismiss_label = Some(label.into());
self
}
pub fn dismiss_content<E: IntoElement>(
mut self,
label: impl Into<SharedString>,
content: impl Fn(&mut Window, &mut App) -> E + 'static,
) -> Self {
self.dismiss_content = Some((
label.into(),
Box::new(move |window, cx| content(window, cx).into_any_element()),
));
self
}
pub fn show_dismiss(mut self, show: bool) -> Self {
self.show_dismiss = show;
self
}
pub fn action_on_new_line(mut self, enabled: bool) -> Self {
self.action_on_new_line = enabled;
self
}
pub fn shape(mut self, shape: CornerShape) -> Self {
self.shape = shape;
self
}
pub fn tone(mut self, tone: SemanticTone) -> Self {
self.tone = tone;
self
}
pub fn colors(mut self, colors: SnackbarColors) -> Self {
self.colors = colors;
self
}
pub fn on_dismiss(mut self, dismissed: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_dismiss = Some(Rc::new(dismissed));
self
}
pub fn on_result(
mut self,
complete: impl Fn(SnackbarResult, &mut Window, &mut App) + 'static,
) -> Self {
self.on_result = Some(Rc::new(complete));
self
}
}