use std::rc::Rc;
use gpui::{
AnyElement, AnyView, App, AppContext, ClickEvent, Context, ElementId, FontWeight, Hsla,
InteractiveElement, IntoElement, MouseButton, ParentElement, Pixels, Point, Render, RenderOnce,
Rgba, SharedString, StatefulInteractiveElement, Styled, Window, anchored, deferred, div, px,
};
use gpui::prelude::FluentBuilder as _;
use super::{ClickHandler, RoundedExt, all};
use crate::motion::{Spring, animate_f32, entrance, key};
use crate::theme::{Scheme, Theme, alpha, font_family};
pub(super) fn over(color: Hsla, base: Hsla) -> Hsla {
let (c, b) = (Rgba::from(color), Rgba::from(base));
let a = c.a.clamp(0., 1.);
let mix = |x: f32, y: f32| x * a + y * (1. - a);
Hsla::from(Rgba {
r: mix(c.r, b.r),
g: mix(c.g, b.g),
b: mix(c.b, b.b),
a: 1.,
})
}
pub(super) fn ground(s: &Scheme) -> Hsla {
let fallback = if s.on_surface.l > 0.5 {
gpui::hsla(0., 0., 0.12, 1.)
} else {
gpui::hsla(0., 0., 0.97, 1.)
};
over(s.surface_container, over(s.surface, fallback))
}
#[derive(IntoElement)]
pub struct Dialog {
id: ElementId,
title: Option<SharedString>,
icon: Option<SharedString>,
width: Pixels,
height: Option<Pixels>,
padding: Pixels,
radius: Pixels,
children: Vec<AnyElement>,
actions: Vec<AnyElement>,
on_dismiss: Option<ClickHandler>,
shown: f32,
}
impl Dialog {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
title: None,
icon: None,
width: px(400.),
height: None,
padding: px(24.),
radius: px(28.),
children: Vec::new(),
actions: Vec::new(),
on_dismiss: None,
shown: 1.,
}
}
pub fn title(mut self, title: impl Into<SharedString>) -> Self {
self.title = Some(title.into());
self
}
pub fn icon(mut self, icon: impl Into<SharedString>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn width(mut self, width: Pixels) -> Self {
self.width = width;
self
}
pub fn sized(mut self, width: Pixels, height: Pixels) -> Self {
self.width = width;
self.height = Some(height);
self
}
pub fn padding(mut self, padding: Pixels) -> Self {
self.padding = padding;
self
}
pub fn radius(mut self, radius: Pixels) -> Self {
self.radius = radius;
self
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.actions.push(action.into_any_element());
self
}
pub fn exit(mut self, progress: f32) -> Self {
self.shown = progress.clamp(0., 1.);
self
}
pub fn on_dismiss(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
self.on_dismiss = Some(Rc::new(f));
self
}
}
impl ParentElement for Dialog {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Dialog {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let translucent = s.surface.a < 1.;
let fade = entrance(key(&self.id, "scrim"), Spring::EFFECTS, 0., window, cx).clamp(0., 1.)
* self.shown;
let p = entrance(key(&self.id, "grow"), Spring::SPATIAL, 0., window, cx);
let rest = 1. - p;
let radius = self.radius + (px(56.) - self.radius) * rest.max(0.);
let mut card = div()
.id(key(&self.id, "card"))
.flex()
.flex_col()
.gap(px(16.))
.w(self.width * (0.86 + 0.14 * p))
.max_w(window.viewport_size().width - px(48.))
.when_some(self.height, |d, h| {
d.h(h)
.max_h(window.viewport_size().height - px(48.))
.overflow_hidden()
})
.p(self.padding)
.rounded(radius)
.bg(if translucent {
over(s.surface_container_high, ground(&s))
} else {
s.surface_container_high
})
.text_color(s.on_surface_variant)
.text_size(px(13.5))
.shadow(super::shadow_pop(&s))
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
.on_click(|_, _, cx| cx.stop_propagation());
let centered = self.icon.is_some();
if let Some(icon) = self.icon {
card = card.child(
div()
.flex()
.justify_center()
.child(super::Icon::new(icon).size(px(24.)).color(s.primary)),
);
}
if let Some(title) = self.title {
card = card.child(
div()
.when(centered, |d| d.text_center())
.text_size(px(22.))
.font_weight(FontWeight(600.))
.text_color(s.on_surface)
.child(title),
);
}
card = card.children(self.children);
if !self.actions.is_empty() {
card = card.child(
div()
.flex()
.justify_end()
.gap(px(8.))
.pt(px(8.))
.children(self.actions),
);
}
let card = div()
.relative()
.top(px(18. * rest + 8. * (1. - self.shown)))
.opacity(p.clamp(0., 1.) * self.shown)
.child(card);
let viewport = window.viewport_size();
let mut scrim = div()
.id(self.id.clone())
.w(viewport.width)
.h(viewport.height)
.flex()
.items_center()
.justify_center()
.bg(if translucent {
alpha(ground(&s), 0.7 * fade)
} else {
alpha(gpui::black(), 0.32 * fade)
})
.font_family(font_family())
.occlude()
.child(card);
if let Some(f) = self.on_dismiss {
scrim = scrim.on_click(move |e, w, cx| f(e, w, cx));
}
deferred(
anchored()
.position(gpui::point(px(0.), px(0.)))
.child(scrim),
)
.with_priority(1)
}
}
#[derive(IntoElement)]
pub struct Toast {
id: ElementId,
text: SharedString,
action: Option<(SharedString, ClickHandler)>,
}
impl Toast {
pub fn new(id: impl Into<ElementId>, text: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
text: text.into(),
action: None,
}
}
pub fn action(
mut self,
label: impl Into<SharedString>,
f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.action = Some((label.into(), Rc::new(f)));
self
}
}
impl RenderOnce for Toast {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let p = entrance(key(&self.id, "grow"), Spring::SPATIAL_FAST, 0., window, cx);
let rest = 1. - p;
let grow = 0.8 + 0.2 * p;
let mut el = div()
.relative()
.top(px(28. * rest))
.opacity(p.clamp(0., 1.))
.flex()
.items_center()
.gap(px(12.))
.min_h(px(48. * grow))
.pl(px(16. * grow))
.pr(px(if self.action.is_some() { 8. } else { 16. } * grow))
.rounded(px(14. - 6. * rest))
.bg(inverse(&s).0)
.text_color(inverse(&s).1)
.font_family(font_family())
.text_size(px(13.5))
.shadow(super::shadow_pop(&s))
.child(div().flex_1().child(self.text));
if let Some((label, f)) = self.action {
el = el.child(
div()
.id(key(&self.id, "action"))
.px(px(12.))
.h(px(36.))
.flex()
.items_center()
.rounded(px(18.))
.cursor_pointer()
.font_weight(FontWeight(600.))
.text_color(s.primary_container)
.hover(|d| d.bg(alpha(s.surface, 0.1)))
.on_click(move |e, w, cx| f(e, w, cx))
.child(label),
);
}
el
}
}
pub fn context_menu(position: Point<Pixels>, content: impl IntoElement) -> impl IntoElement {
deferred(
anchored()
.position(position)
.snap_to_window_with_margin(px(8.))
.child(content),
)
.with_priority(1)
}
fn inverse(s: &Scheme) -> (gpui::Hsla, gpui::Hsla) {
let bg = gpui::Hsla {
a: 1.,
..s.on_surface
};
let fg = if s.surface.a >= 0.95 {
s.surface
} else if bg.l > 0.5 {
gpui::hsla(0., 0., 0.1, 1.)
} else {
gpui::hsla(0., 0., 0.96, 1.)
};
(bg, fg)
}
pub struct Tooltip {
text: SharedString,
}
impl Tooltip {
pub fn text(
text: impl Into<SharedString>,
) -> impl Fn(&mut Window, &mut App) -> AnyView + 'static {
let text = text.into();
move |_, cx| cx.new(|_| Tooltip { text: text.clone() }).into()
}
}
impl Render for Tooltip {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let s = *Theme::of(cx);
let (bg, fg) = inverse(&s);
div()
.px(px(8.))
.py(px(4.))
.rounded(px(4.))
.bg(bg)
.text_color(fg)
.font_family(font_family())
.text_size(px(12.))
.child(self.text.clone())
}
}
#[derive(IntoElement)]
pub struct LinearProgress {
id: ElementId,
value: f32,
height: Pixels,
}
impl LinearProgress {
pub fn new(id: impl Into<ElementId>, value: f32) -> Self {
Self {
id: id.into(),
value,
height: px(8.),
}
}
pub fn height(mut self, height: Pixels) -> Self {
self.height = height;
self
}
}
impl RenderOnce for LinearProgress {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let value = animate_f32(
key(&self.id, "value"),
Spring::SPATIAL,
self.value.clamp(0., 1.),
window,
cx,
);
let radius = self.height / 2.;
div()
.w_full()
.h(self.height)
.corners(all(radius))
.bg(s.secondary_container)
.overflow_hidden()
.child(
div()
.h_full()
.w(gpui::relative(value.clamp(0., 1.)))
.corners(all(radius))
.bg(s.primary),
)
}
}