1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Drag to reorder: rows you can pick up with the pointer and drop elsewhere in their group,
//! with an insertion line and the other rows sliding into place.
use std::collections::HashMap;
use std::rc::Rc;
use std::time::Instant;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, Bounds, ElementId, FontWeight, IntoElement, MouseButton, MouseDownEvent,
MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, RenderOnce, SharedString, Styled, Window,
canvas, div, px,
};
use super::{RoundedExt, all, shadow_pop};
use crate::motion::{MotionSettings, Spring, elapsed, key};
use crate::theme::{Theme, font_family};
/// A finished drag: the row at `from` now goes to index `to` (both count rows only, not
/// headers). Apply it with [`apply_reorder`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ReorderEvent {
pub from: usize,
pub to: usize,
}
/// Moves `items[from]` to index `to`, as reported by a [`ReorderEvent`].
pub fn apply_reorder<T>(items: &mut Vec<T>, event: ReorderEvent) {
if event.from < items.len() && event.from != event.to {
let item = items.remove(event.from);
items.insert(event.to.min(items.len()), item);
}
}
/// Final index of the row at `from` dropped before (`after = false`) or after the row at `over`.
pub(crate) fn drop_index(from: usize, over: usize, after: bool) -> usize {
let over_in_rest = if over > from { over - 1 } else { over };
over_in_rest + after as usize
}
/// Pointer distance before a press becomes a drag (so clicks still work).
const DRAG_THRESHOLD: f32 = 5.;
struct Drag {
from: usize,
y0: Pixels,
moved: bool,
/// Row under the pointer and whether the drop goes after it.
target: Option<(usize, bool)>,
}
struct Slide {
offset: f32,
start: Instant,
}
#[derive(Default)]
struct ReorderState {
drag: Option<Drag>,
/// Layout bounds of each row in the last frame (without the slide offset), by key.
bounds: HashMap<SharedString, Bounds<Pixels>>,
slides: HashMap<SharedString, Slide>,
/// Generation of the last [`ReorderList::moved`] request that was applied.
applied: u64,
}
enum Entry {
Header(AnyElement),
Row {
key: SharedString,
group: usize,
content: AnyElement,
},
}
type ReorderHandler = Rc<dyn Fn(ReorderEvent, &mut Window, &mut App)>;
/// A list whose rows can be reordered by dragging, within their group (favorites stay with
/// favorites). Each row needs a stable key; the order is yours: apply the [`ReorderEvent`] from
/// [`ReorderList::on_reorder`] to your data (see [`apply_reorder`]) and render the new order.
///
/// While dragging, the picked row lifts onto a tonal pill with a shadow and a 2 px line in the
/// primary color marks where it will land; on drop, the rows slide to their new places with the
/// spatial spring. A press that moves less than 5 px is still a click.
#[derive(IntoElement)]
pub struct ReorderList {
id: ElementId,
entries: Vec<Entry>,
gap: Pixels,
grab: Option<Pixels>,
moved: Option<(ReorderEvent, u64)>,
held: Option<(usize, Option<(usize, bool)>)>,
on_reorder: Option<ReorderHandler>,
}
impl ReorderList {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
entries: Vec::new(),
gap: px(2.),
grab: None,
moved: None,
held: None,
on_reorder: None,
}
}
/// A draggable row. `key` must be stable across renders (a path, an id); rows only move
/// within the same `group`.
pub fn row(
mut self,
key: impl Into<SharedString>,
group: usize,
content: impl IntoElement,
) -> Self {
self.entries.push(Entry::Row {
key: key.into(),
group,
content: content.into_any_element(),
});
self
}
/// A static group title ("Favorites", "Projects"); it does not count as a row.
pub fn header(mut self, title: impl Into<SharedString>) -> Self {
self.entries.push(Entry::Header(
ReorderHeader(title.into()).into_any_element(),
));
self
}
/// Any static element between rows (it does not count as a row).
pub fn item(mut self, element: impl IntoElement) -> Self {
self.entries.push(Entry::Header(element.into_any_element()));
self
}
/// Space between rows (2 px by default).
pub fn gap(mut self, gap: Pixels) -> Self {
self.gap = gap;
self
}
/// Only the top `height` of each row starts a drag (a project header with its
/// conversations below): the rest of the row keeps its own clicks and drags.
pub fn grab_height(mut self, height: Pixels) -> Self {
self.grab = Some(height);
self
}
/// Slides the rows into place after you moved one yourself (a "move up" button, a script)
/// instead of by dragging: pass the event you applied to your data, with a `generation`
/// that changes for each move. Render the new order together with it.
pub fn moved(mut self, event: ReorderEvent, generation: u64) -> Self {
self.moved = Some((event, generation));
self
}
/// Draws row `from` as lifted, with the insertion line before or after row `over` (`after`
/// true), as if it were being dragged. For a scripted or remote drag: you keep calling it
/// while it lasts, then apply the move with [`ReorderList::moved`].
pub fn held(mut self, from: usize, target: Option<(usize, bool)>) -> Self {
self.held = Some((from, target));
self
}
pub fn on_reorder(mut self, f: impl Fn(ReorderEvent, &mut Window, &mut App) + 'static) -> Self {
self.on_reorder = Some(Rc::new(f));
self
}
}
#[derive(IntoElement)]
struct ReorderHeader(SharedString);
impl RenderOnce for ReorderHeader {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
div()
.pt(px(8.))
.pb(px(4.))
.px(px(12.))
.text_size(px(12.))
.font_weight(FontWeight(700.))
.text_color(Theme::of(cx).primary)
.child(self.0)
}
}
impl RenderOnce for ReorderList {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let state =
window.use_keyed_state(key(&self.id, "reorder"), cx, |_, _| ReorderState::default());
let reduced = MotionSettings::get(cx).reduced;
// Rows as (key, group), in order, for the pointer listeners.
let rows: Rc<Vec<(SharedString, usize)>> = Rc::new(
self.entries
.iter()
.filter_map(|e| match e {
Entry::Row { key, group, .. } => Some((key.clone(), *group)),
Entry::Header(_) => None,
})
.collect(),
);
let (dragging_row, target) = match self.held {
Some((from, target)) => (Some(from), target),
None => {
let st = state.read(cx);
match &st.drag {
Some(d) if d.moved => (Some(d.from), d.target),
_ => (None, None),
}
}
};
// A move made by the host: `rows` already has the new order, the slides start from the old.
if let Some((event, generation)) = self.moved
&& state.read(cx).applied != generation
{
let mut before = (*rows).clone();
apply_reorder(
&mut before,
ReorderEvent {
from: event.to,
to: event.from,
},
);
state.update(cx, |st, _| {
st.applied = generation;
start_slides(st, &before, event.from, event.to);
});
}
// Slide offsets after a drop.
let mut moving = false;
let offsets: HashMap<SharedString, f32> = {
let st = state.read(cx);
st.slides
.iter()
.filter_map(|(k, slide)| {
let t = elapsed(slide.start, cx);
let spring = Spring::SPATIAL;
if reduced || t >= spring.settle_time().as_secs_f32() {
return None;
}
moving = true;
Some((k.clone(), slide.offset * (1. - spring.step(t))))
})
.collect()
};
if moving {
crate::motion::request_frame(window);
} else if !state.read(cx).slides.is_empty() {
state.update(cx, |st, _| st.slides.clear());
}
let mut list = div()
.relative()
.flex()
.flex_col()
.gap(self.gap)
.font_family(font_family())
.when(dragging_row.is_some(), |d| d.cursor_grabbing());
let mut row_ix = 0;
for entry in self.entries {
match entry {
Entry::Header(el) => list = list.child(el),
Entry::Row {
key: k, content, ..
} => {
let ix = row_ix;
row_ix += 1;
let offset = offsets.get(&k).copied().unwrap_or(0.);
let lifted = dragging_row == Some(ix);
let record = {
let state = state.clone();
let k = k.clone();
canvas(
|_, _, _| {},
move |bounds, _, _, cx| {
let mut logical = bounds;
logical.origin.y -= px(offset);
state.update(cx, |st, _| {
st.bounds.insert(k, logical);
});
},
)
.absolute()
.size_full()
};
let line = |top: bool| {
div()
.absolute()
.left(px(8.))
.right(px(8.))
.h(px(2.))
.rounded(px(1.))
.bg(s.primary)
.when(top, |d| d.top(px(-2.)))
.when(!top, |d| d.bottom(px(-2.)))
};
let row = div()
.relative()
.top(px(offset))
.when(lifted, |d| {
d.bg(s.secondary_container)
.corners(all(px(16.)))
.shadow(shadow_pop(&s))
})
.child(content)
.child(record)
.when(target == Some((ix, false)), |d| d.child(line(true)))
.when(target == Some((ix, true)), |d| d.child(line(false)));
list = list.child(row);
}
}
}
let on_reorder = self.on_reorder;
let grab = self.grab;
let listener = {
let state = state.clone();
canvas(
|_, _, _| {},
move |_, _, window, _| {
let st = state.clone();
let rows_down = rows.clone();
window.on_mouse_event(move |e: &MouseDownEvent, phase, _, cx| {
if !phase.bubble() || e.button != MouseButton::Left {
return;
}
let hit = {
let s = st.read(cx);
rows_down.iter().position(|(k, _)| {
s.bounds.get(k).is_some_and(|b| {
b.contains(&e.position)
&& grab.is_none_or(|h| e.position.y < b.origin.y + h)
})
})
};
if let Some(from) = hit {
st.update(cx, |s, _| {
s.drag = Some(Drag {
from,
y0: e.position.y,
moved: false,
target: None,
})
});
}
});
let st = state.clone();
let rows_move = rows.clone();
window.on_mouse_event(move |e: &MouseMoveEvent, phase, _, cx| {
if !phase.bubble() {
return;
}
let Some((from, y0, moved)) =
st.read(cx).drag.as_ref().map(|d| (d.from, d.y0, d.moved))
else {
return;
};
if e.pressed_button != Some(MouseButton::Left) {
st.update(cx, |s, cx| {
s.drag = None;
cx.notify();
});
return;
}
if !moved && (e.position.y - y0).abs() < px(DRAG_THRESHOLD) {
return;
}
let group = rows_move[from].1;
let target = {
let s = st.read(cx);
rows_move.iter().enumerate().find_map(|(i, (k, g))| {
let b = s.bounds.get(k)?;
let inside = e.position.y >= b.top() && e.position.y < b.bottom();
(inside && i != from && *g == group)
.then(|| (i, e.position.y > b.center().y))
})
};
st.update(cx, |s, cx| {
if let Some(d) = s.drag.as_mut() {
d.moved = true;
d.target = target;
}
cx.notify();
});
});
let st = state.clone();
let rows_up = rows.clone();
let on_reorder = on_reorder.clone();
window.on_mouse_event(move |_: &MouseUpEvent, phase, window, cx| {
if !phase.bubble() {
return;
}
let Some(drag) = st.update(cx, |s, cx| {
cx.notify();
s.drag.take()
}) else {
return;
};
if !drag.moved {
return;
}
// The release ends a drag: it must not click the row under it.
cx.stop_propagation();
let Some((over, after)) = drag.target else {
return;
};
let to = drop_index(drag.from, over, after);
if to == drag.from {
return;
}
st.update(cx, |s, _| {
start_slides(s, &rows_up, drag.from, to);
});
if let Some(f) = &on_reorder {
f(
ReorderEvent {
from: drag.from,
to,
},
window,
cx,
);
}
});
},
)
.absolute()
.size_full()
};
list.child(listener)
}
}
/// Predicts where every row lands after moving `from` to `to` and starts sliding each one
/// from its old place.
fn start_slides(st: &mut ReorderState, rows: &[(SharedString, usize)], from: usize, to: usize) {
let tops: Vec<Option<Bounds<Pixels>>> = rows
.iter()
.map(|(k, _)| st.bounds.get(k).copied())
.collect();
let Some(Some(first)) = tops.first().copied() else {
return;
};
let mut order: Vec<usize> = (0..rows.len()).collect();
let moved = order.remove(from);
order.insert(to.min(order.len()), moved);
// Rows keep their heights and the spacing between consecutive rows (headers included)
// stays with the slot, so the new top of slot `i` is the old top of slot `i` shifted by
// the difference in heights of the rows above it.
let mut y = f32::from(first.top());
let now = Instant::now();
for (slot, &row) in order.iter().enumerate() {
let Some(old) = tops[row] else {
return;
};
if slot > 0
&& let (Some(Some(prev_slot)), Some(Some(this_slot))) =
(tops.get(slot - 1).copied(), tops.get(slot).copied())
{
// The gap that followed the previous slot (it includes any header in between).
let gap = f32::from(this_slot.top()) - f32::from(prev_slot.bottom());
y += gap;
}
let offset = f32::from(old.top()) - y;
if offset.abs() > 0.5 {
st.slides
.insert(rows[row].0.clone(), Slide { offset, start: now });
}
y += f32::from(old.size.height);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn drop_index_accounts_for_the_removed_row() {
// Drop row 0 after row 2: it ends at index 2.
assert_eq!(drop_index(0, 2, true), 2);
// Drop row 3 before row 1: index 1.
assert_eq!(drop_index(3, 1, false), 1);
// Drop row 1 before row 2: no change.
assert_eq!(drop_index(1, 2, false), 1);
let mut v = vec!["a", "b", "c", "d"];
apply_reorder(
&mut v,
ReorderEvent {
from: 0,
to: drop_index(0, 2, true),
},
);
assert_eq!(v, ["b", "c", "a", "d"]);
apply_reorder(
&mut v,
ReorderEvent {
from: 3,
to: drop_index(3, 0, false),
},
);
assert_eq!(v, ["d", "b", "c", "a"]);
}
#[test]
fn slides_start_from_the_old_places() {
let mut st = ReorderState::default();
let rows: Vec<(SharedString, usize)> = ["a", "b", "c"]
.iter()
.map(|k| (SharedString::from(*k), 0))
.collect();
for (i, (k, _)) in rows.iter().enumerate() {
let top = px(i as f32 * 32.);
st.bounds.insert(
k.clone(),
Bounds::new(gpui::point(px(0.), top), gpui::size(px(100.), px(30.))),
);
}
// "a" goes to the end: it slides up from 0 to 64 (offset -64); b and c move up 32.
start_slides(&mut st, &rows, 0, 2);
assert_eq!(st.slides["a"].offset, -64.);
assert_eq!(st.slides["b"].offset, 32.);
assert_eq!(st.slides["c"].offset, 32.);
}
}