fc_ui/capabilities/controls/
drag_drop.rs1use gpui::{prelude::FluentBuilder as _, *};
4use std::fmt::Debug;
5
6use crate::capabilities::foundation::theme::use_theme;
7
8use std::rc::Rc;
9
10pub struct DragData<T: Clone + Debug> {
11 pub data: T,
12 pub label: Option<SharedString>,
13 pub preview_factory: Option<Rc<dyn Fn() -> AnyElement>>,
14 pub position: Point<Pixels>,
15}
16impl<T: Clone + Debug> Clone for DragData<T> {
17 fn clone(&self) -> Self {
18 Self {
19 data: self.data.clone(),
20 label: self.label.clone(),
21 preview_factory: self.preview_factory.clone(),
22 position: self.position,
23 }
24 }
25}
26impl<T: Clone + Debug> Debug for DragData<T> {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 f.debug_struct("DragData")
29 .field("data", &self.data)
30 .field("label", &self.label)
31 .field("preview_factory", &self.preview_factory.is_some())
32 .field("position", &self.position)
33 .finish()
34 }
35}
36
37impl<T: Clone + Debug> DragData<T> {
38 pub fn new(data: T) -> Self {
39 Self {
40 data,
41 label: None,
42 preview_factory: None,
43 position: Point::default(),
44 }
45 }
46
47 pub fn with_label(mut self, label: impl Into<SharedString>) -> Self {
48 self.label = Some(label.into());
49 self
50 }
51
52 pub fn with_preview<F>(mut self, factory: F) -> Self
53 where
54 F: Fn() -> AnyElement + 'static,
55 {
56 self.preview_factory = Some(Rc::new(factory));
57 self
58 }
59
60 pub fn with_position(mut self, position: Point<Pixels>) -> Self {
61 self.position = position;
62 self
63 }
64}
65
66impl<T: Clone + Debug + 'static> Render for DragData<T> {
67 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
68 let theme = use_theme(cx);
69
70 if let Some(factory) = &self.preview_factory {
71 let preview = factory();
72 return div()
73 .absolute()
74 .left(self.position.x)
75 .top(self.position.y)
76 .child(preview);
77 }
78
79 let size = gpui::size(px(250.0), px(80.0));
80
81 div()
82 .pl((self.position.x - size.width / 2.0).max(px(0.0)))
83 .pt((self.position.y - size.height / 2.0).max(px(0.0)))
84 .child(
85 div()
86 .flex()
87 .justify_center()
88 .items_center()
89 .min_w(size.width)
90 .max_w(px(300.0))
91 .min_h(size.height)
92 .px(px(16.0))
93 .py(px(12.0))
94 .bg(theme.tokens.card.opacity(0.95))
95 .border_1()
96 .border_color(theme.tokens.border)
97 .text_color(theme.tokens.foreground)
98 .font_family(theme.tokens.font_family.clone())
99 .text_size(px(14.0))
100 .font_weight(FontWeight::MEDIUM)
101 .rounded(theme.tokens.radius_md)
102 .shadow(smallvec::smallvec![BoxShadow {
103 color: hsla(0.0, 0.0, 0.0, 0.3),
104 offset: point(px(0.0), px(4.0)),
105 blur_radius: px(12.0),
106 spread_radius: px(0.0),
107 inset: false,
108 }])
109 .when_some(self.label.clone(), |this, label| this.child(label))
110 .when(self.label.is_none(), |this| this.child("Dragging...")),
111 )
112 }
113}
114
115#[derive(IntoElement)]
116pub struct Draggable<T: Clone + Debug + 'static> {
117 base: Stateful<Div>,
118 drag_data: DragData<T>,
119 cursor_style: CursorStyle,
120 hover_bg: Option<Hsla>,
121 children: Vec<AnyElement>,
122 style: StyleRefinement,
123}
124
125impl<T: Clone + Debug + 'static> Draggable<T> {
126 pub fn new(id: impl Into<ElementId>, drag_data: DragData<T>) -> Self {
127 Self {
128 base: div().id(id.into()),
129 drag_data,
130 cursor_style: CursorStyle::PointingHand,
131 hover_bg: None,
132 children: Vec::new(),
133 style: StyleRefinement::default(),
134 }
135 }
136
137 pub fn cursor_style(mut self, cursor: CursorStyle) -> Self {
138 self.cursor_style = cursor;
139 self
140 }
141
142 pub fn hover_bg(mut self, color: Hsla) -> Self {
143 self.hover_bg = Some(color);
144 self
145 }
146
147 pub fn child(mut self, child: impl IntoElement) -> Self {
148 self.children.push(child.into_any_element());
149 self
150 }
151
152 pub fn children<I>(mut self, children: impl IntoIterator<Item = I>) -> Self
153 where
154 I: IntoElement,
155 {
156 for child in children {
157 self.children.push(child.into_any_element());
158 }
159 self
160 }
161}
162
163impl<T: Clone + Debug + 'static> Styled for Draggable<T> {
164 fn style(&mut self) -> &mut StyleRefinement {
165 &mut self.style
166 }
167}
168
169impl<T: Clone + Debug + 'static> ParentElement for Draggable<T> {
170 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
171 self.children.extend(elements);
172 }
173}
174
175impl<T: Clone + Debug + 'static> RenderOnce for Draggable<T> {
176 fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
177 let drag_data = self.drag_data.clone();
178 let user_style = self.style;
179
180 self.base
181 .cursor(self.cursor_style)
182 .when_some(self.hover_bg, |this, bg| {
183 this.hover(move |style| style.bg(bg))
184 })
185 .on_drag(drag_data, |data: &DragData<T>, position, _, cx| {
186 cx.new(|_| data.clone().with_position(position))
187 })
188 .map(|this| {
189 let mut div = this;
190 div.style().refine(&user_style);
191 div
192 })
193 .children(self.children)
194 }
195}
196
197#[derive(Clone, Copy, Debug, PartialEq)]
198pub enum DropZoneStyle {
199 Dashed,
200 Solid,
201 Filled,
202}
203
204#[derive(IntoElement)]
205pub struct DropZone<T: Clone + Debug + 'static> {
206 base: Stateful<Div>,
207 drop_style: DropZoneStyle,
208 active: bool,
209 min_height: Option<Pixels>,
210 children: Vec<AnyElement>,
211 user_style: StyleRefinement,
212 on_drop: Option<Rc<dyn Fn(&DragData<T>, &mut Window, &mut App)>>,
213 _phantom: std::marker::PhantomData<T>,
214}
215
216impl<T: Clone + Debug + 'static> DropZone<T> {
217 pub fn new(id: impl Into<ElementId>) -> Self {
218 Self {
219 base: div().id(id.into()),
220 drop_style: DropZoneStyle::Dashed,
221 active: false,
222 min_height: None,
223 children: Vec::new(),
224 user_style: StyleRefinement::default(),
225 on_drop: None,
226 _phantom: std::marker::PhantomData,
227 }
228 }
229
230 pub fn drop_zone_style(mut self, style: DropZoneStyle) -> Self {
231 self.drop_style = style;
232 self
233 }
234
235 pub fn on_drop<F>(mut self, handler: F) -> Self
236 where
237 F: Fn(&DragData<T>, &mut Window, &mut App) + 'static,
238 {
239 self.on_drop = Some(Rc::new(handler));
240 self
241 }
242
243 pub fn active(mut self, active: bool) -> Self {
244 self.active = active;
245 self
246 }
247
248 pub fn min_h(mut self, height: impl Into<Pixels>) -> Self {
249 self.min_height = Some(height.into());
250 self
251 }
252
253 pub fn child(mut self, child: impl IntoElement) -> Self {
254 self.children.push(child.into_any_element());
255 self
256 }
257
258 pub fn children<I>(mut self, children: impl IntoIterator<Item = I>) -> Self
259 where
260 I: IntoElement,
261 {
262 for child in children {
263 self.children.push(child.into_any_element());
264 }
265 self
266 }
267}
268
269impl<T: Clone + Debug + 'static> Styled for DropZone<T> {
270 fn style(&mut self) -> &mut StyleRefinement {
271 &mut self.user_style
272 }
273}
274
275impl<T: Clone + Debug + 'static> InteractiveElement for DropZone<T> {
276 fn interactivity(&mut self) -> &mut Interactivity {
277 self.base.interactivity()
278 }
279}
280
281impl<T: Clone + Debug + 'static> StatefulInteractiveElement for DropZone<T> {}
282
283impl<T: Clone + Debug + 'static> ParentElement for DropZone<T> {
284 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
285 self.children.extend(elements);
286 }
287}
288
289impl<T: Clone + Debug + 'static> RenderOnce for DropZone<T> {
290 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
291 let theme = use_theme(cx);
292 let user_style = self.user_style;
293
294 let (border_width, border_color, bg_color) = match (self.drop_style, self.active) {
295 (DropZoneStyle::Dashed, false) => {
296 (px(2.0), theme.tokens.border, gpui::transparent_black())
297 }
298 (DropZoneStyle::Dashed, true) => (
299 px(2.0),
300 theme.tokens.primary,
301 theme.tokens.primary.opacity(0.05),
302 ),
303 (DropZoneStyle::Solid, false) => {
304 (px(2.0), theme.tokens.border, gpui::transparent_black())
305 }
306 (DropZoneStyle::Solid, true) => (
307 px(2.0),
308 theme.tokens.primary,
309 theme.tokens.primary.opacity(0.1),
310 ),
311 (DropZoneStyle::Filled, false) => (px(1.0), theme.tokens.border, theme.tokens.muted),
312 (DropZoneStyle::Filled, true) => (
313 px(2.0),
314 theme.tokens.primary,
315 theme.tokens.primary.opacity(0.15),
316 ),
317 };
318
319 self.base
320 .flex()
321 .flex_col()
322 .items_center()
323 .justify_center()
324 .w_full()
325 .when_some(self.min_height, |this, h| this.min_h(h))
326 .px(px(16.0))
327 .py(px(16.0))
328 .rounded(theme.tokens.radius_lg)
329 .bg(bg_color)
330 .border_color(border_color)
331 .when(self.drop_style == DropZoneStyle::Dashed, |this| {
332 this.border(border_width)
333 })
334 .when(self.drop_style != DropZoneStyle::Dashed, |this| {
335 this.border(border_width)
336 })
337 .map(|this| {
338 let mut div = this;
339 div.style().refine(&user_style);
340 div
341 })
342 .when_some(self.on_drop, |this, handler| {
343 this.on_drop(move |data, window, cx| (handler)(data, window, cx))
344 })
345 .children(self.children)
346 }
347}