use std::rc::Rc;
use gpui::{
Anchor, App, Div, ElementId, Hsla, InteractiveElement, IntoElement, RenderOnce, Role,
ScrollHandle, SharedString, Stateful, StatefulInteractiveElement, Styled, Window,
accesskit::Action, div,
};
use gpui_material_theme::Direction;
use crate::{current_material_theme, scrim_color};
type ScrimClick = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct Scrim {
id: ElementId,
color: Option<Hsla>,
opacity: f32,
accessibility_label: Option<SharedString>,
on_click: Option<ScrimClick>,
}
impl Scrim {
const DEFAULT_OPACITY: f32 = 0.32;
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
color: None,
opacity: Self::DEFAULT_OPACITY,
accessibility_label: None,
on_click: None,
}
}
pub fn color(mut self, color: impl Into<Hsla>) -> Self {
self.color = Some(color.into());
self
}
pub fn opacity(mut self, opacity: f32) -> Self {
self.opacity = if opacity.is_nan() {
Self::DEFAULT_OPACITY
} else {
opacity.clamp(0., 1.)
};
self
}
pub fn accessibility_label(mut self, label: impl Into<SharedString>) -> Self {
self.accessibility_label = Some(label.into());
self
}
pub fn on_click(mut self, on_click: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(on_click));
self
}
}
impl RenderOnce for Scrim {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let fill = self.color.map_or_else(
|| scrim_color(¤t_material_theme(window, cx), self.opacity),
|color| color.opacity(self.opacity),
);
let backdrop = div().id(self.id).absolute().inset_0().bg(fill);
match self.on_click {
Some(on_click) => {
let on_accessibility_click = on_click.clone();
backdrop
.occlude()
.role(Role::Button)
.aria_label(
self.accessibility_label
.unwrap_or_else(|| "Dismiss overlay".into()),
)
.on_a11y_action(Action::Click, move |_, window, cx| {
on_accessibility_click(window, cx);
})
.on_click(move |_, window, cx| {
cx.stop_propagation();
on_click(window, cx);
})
}
None => backdrop,
}
}
}
pub fn logical_anchor(anchor: Anchor, direction: Direction) -> Anchor {
if direction == Direction::Ltr {
return anchor;
}
match anchor {
Anchor::TopLeft => Anchor::TopRight,
Anchor::TopRight => Anchor::TopLeft,
Anchor::BottomLeft => Anchor::BottomRight,
Anchor::BottomRight => Anchor::BottomLeft,
Anchor::LeftCenter => Anchor::RightCenter,
Anchor::RightCenter => Anchor::LeftCenter,
Anchor::TopCenter | Anchor::BottomCenter => anchor,
}
}
pub fn isolated_scroll(id: impl Into<ElementId>, handle: &ScrollHandle) -> Stateful<Div> {
div()
.id(id)
.min_h_0()
.overflow_y_scroll()
.occlude()
.track_scroll(handle)
}