use std::rc::Rc;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, ClickEvent, ElementId, ExternalPaths, FileDropEvent, FontWeight, ImageSource,
InteractiveElement, IntoElement, ObjectFit, ParentElement, Pixels, RenderOnce, SharedString,
StatefulInteractiveElement, Styled, StyledImage, Window, canvas, div, img, px,
};
use super::{ClickHandler, Icon};
use crate::motion::{Spring, animate_f32, animate_from, key, presence};
use crate::theme::{Theme, alpha, font_family, mix};
type DropHandler = Rc<dyn Fn(&ExternalPaths, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct DropZone {
id: ElementId,
label: SharedString,
icon: SharedString,
radius: Pixels,
on_drop: Option<DropHandler>,
children: Vec<AnyElement>,
}
impl DropZone {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
label: "Drop to attach".into(),
icon: "clip".into(),
radius: px(26.),
on_drop: None,
children: Vec::new(),
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = label.into();
self
}
pub fn icon(mut self, icon: impl Into<SharedString>) -> Self {
self.icon = icon.into();
self
}
pub fn radius(mut self, radius: Pixels) -> Self {
self.radius = radius;
self
}
pub fn on_drop(mut self, f: impl Fn(&ExternalPaths, &mut Window, &mut App) + 'static) -> Self {
self.on_drop = Some(Rc::new(f));
self
}
}
impl ParentElement for DropZone {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for DropZone {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let over_state = window.use_keyed_state(key(&self.id, "over"), cx, |_, _| false);
let over = *over_state.read(cx) && cx.has_active_drag();
let set_over = {
let state = over_state.clone();
move |value: bool, cx: &mut App| {
state.update(cx, |over, cx| {
if *over != value {
*over = value;
cx.notify();
}
})
}
};
let layer = presence(key(&self.id, "layer"), over, window, cx).map(|fade| {
let [grow] = animate_from(
key(&self.id, "grow"),
Spring::SPATIAL,
[0.6],
[1.],
window,
cx,
);
let inset = px((1. - grow) * 8.);
div()
.absolute()
.top(inset)
.left(inset)
.right(inset)
.bottom(inset)
.flex()
.items_center()
.justify_center()
.gap(px(8.))
.rounded(self.radius)
.border_2()
.border_color(s.primary)
.bg(mix(s.primary, s.surface_container, 0.12))
.text_color(s.primary)
.font_family(font_family())
.font_weight(FontWeight(600.))
.opacity(fade.min(grow.clamp(0., 1.) * 1.6).clamp(0., 1.))
.child(Icon::new(self.icon.clone()).size(px(18.)).color(s.primary))
.child(self.label.clone())
});
let moved = set_over.clone();
let hovered = set_over.clone();
let dropped = set_over.clone();
let exited = set_over;
let on_drop = self.on_drop;
div()
.id(self.id.clone())
.relative()
.on_drag_move::<ExternalPaths>(move |event, _, cx| {
moved(event.bounds.contains(&event.event.position), cx)
})
.on_hover(move |hovering, _, cx| {
if !*hovering || !cx.has_active_drag() {
hovered(false, cx)
}
})
.on_drop::<ExternalPaths>(move |paths, window, cx| {
dropped(false, cx);
if let Some(f) = &on_drop {
f(paths, window, cx);
}
})
.children(self.children)
.children(layer)
.child(
canvas(
|_, _, _| {},
move |_, _, window, _| {
let exited = exited.clone();
window.on_mouse_event(move |event: &FileDropEvent, _, _, cx| {
if matches!(event, FileDropEvent::Exited) {
exited(false, cx);
}
});
},
)
.absolute()
.size_0(),
)
}
}
#[derive(IntoElement)]
pub struct ImageThumb {
id: ElementId,
source: ImageSource,
size: Pixels,
radius: Pixels,
aspect: Option<(f32, f32)>,
on_remove: Option<ClickHandler>,
remove_label: SharedString,
on_click: Option<ClickHandler>,
}
impl ImageThumb {
pub fn new(id: impl Into<ElementId>, source: impl Into<ImageSource>) -> Self {
Self {
id: id.into(),
source: source.into(),
size: px(56.),
radius: px(12.),
aspect: None,
on_remove: None,
remove_label: "Remove".into(),
on_click: None,
}
}
pub fn size(mut self, size: Pixels) -> Self {
self.size = size;
self
}
pub fn radius(mut self, radius: Pixels) -> Self {
self.radius = radius;
self
}
pub fn aspect(mut self, width: f32, height: f32) -> Self {
if width > 0. && height > 0. {
self.aspect = Some((width, height));
}
self
}
pub fn on_remove(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
self.on_remove = Some(Rc::new(f));
self
}
pub fn remove_label(mut self, label: impl Into<SharedString>) -> Self {
self.remove_label = label.into();
self
}
pub fn on_click(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
impl RenderOnce for ImageThumb {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let hovered = window.use_keyed_state(key(&self.id, "hover"), cx, |_, _| false);
let show_x = animate_f32(
key(&self.id, "x"),
Spring::EFFECTS,
if *hovered.read(cx) { 1. } else { 0. },
window,
cx,
);
let radius = self.radius;
let remove = self.on_remove.map(|f| {
let label = self.remove_label.clone();
div()
.id(key(&self.id, "remove"))
.absolute()
.top(px(3.))
.right(px(3.))
.size(px(18.))
.flex()
.items_center()
.justify_center()
.rounded_full()
.bg(alpha(gpui::black(), 0.6))
.opacity(show_x.clamp(0., 1.))
.cursor_pointer()
.tooltip(super::Tooltip::text(label))
.child(Icon::new("x").size(px(11.)).color(gpui::white()))
.on_click(move |e, w, cx| {
cx.stop_propagation();
f(e, w, cx)
})
});
let hover = hovered.clone();
let (box_w, box_h) = match self.aspect {
Some((w, h)) if h <= w => (self.size, self.size * (h / w)),
Some((w, h)) => (self.size * (w / h), self.size),
None => (self.size, self.size),
};
div()
.id(self.id.clone())
.relative()
.flex_none()
.w(box_w)
.h(box_h)
.rounded(radius)
.bg(s.surface_container_highest)
.on_hover(move |on, _, cx| {
hover.update(cx, |h, cx| {
*h = *on;
cx.notify();
})
})
.when_some(self.on_click, |d, f| {
d.cursor_pointer().on_click(move |e, w, cx| f(e, w, cx))
})
.child(img(self.source).size_full().rounded(radius).object_fit(
if self.aspect.is_some() {
ObjectFit::Contain
} else {
ObjectFit::Cover
},
))
.children(remove)
}
}