use std::{any::Any, fmt, rc::Rc, sync::Arc};
use gpui_kit::base::AutoScroll;
use gpui_kit::component::ActiveTheme as _;
use gpui_kit::{
AnyElement, App, AppContext as _, Bounds, Context, CursorStyle, Div, DragMoveEvent, EntityId,
Hsla, InteractiveElement as _, IntoElement, Modifiers, ParentElement as _, Pixels, Point,
Render, SharedString, Stateful, StatefulInteractiveElement as _, Styled as _, Window, div,
point, prelude::FluentBuilder as _, px, relative, size,
};
use rust_i18n::t;
use super::{
CELL_PADDING, KeyOf, LiveGridDelegate, LiveGridEvent, LiveGridState,
frame::PaintedLine,
ghost::GhostTextStyle,
pinned::BodyLine,
selection::LineTarget,
stats::{profile_elapsed, profile_start},
};
use crate::{
engine::GroupPath,
model::GridRow,
policy::{
DropLine, NoRowDrag, NoRowDrop, RowDragGesture, RowDragPressContext, RowDropContext,
RowDropPolicy, SelectableLine,
},
store::StoreSnapshot,
};
#[derive(Clone)]
pub struct RowDrag<K> {
grid: EntityId,
keys: Arc<[K]>,
payload: DragPayload,
}
impl<K> RowDrag<K> {
pub fn grid(&self) -> EntityId {
self.grid
}
pub fn keys(&self) -> &Arc<[K]> {
&self.keys
}
pub fn payload(&self) -> &DragPayload {
&self.payload
}
}
impl<K> fmt::Debug for RowDrag<K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RowDrag")
.field("grid", &self.grid)
.field("keys", &self.keys.len())
.field("payload", &self.payload)
.finish()
}
}
#[derive(Clone, Default)]
pub struct DragPayload {
data: Option<Arc<dyn Any>>,
label: Option<SharedString>,
}
impl DragPayload {
pub fn new() -> Self {
Self::default()
}
pub fn with_data<T: 'static>(mut self, data: T) -> Self {
self.data = Some(Arc::new(data));
self
}
pub fn data<T: 'static>(&self) -> Option<&T> {
self.data.as_deref()?.downcast_ref()
}
pub fn with_label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn label(&self) -> Option<&SharedString> {
self.label.as_ref()
}
}
impl fmt::Debug for DragPayload {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DragPayload")
.field("label", &self.label)
.field("has_data", &self.data.is_some())
.finish()
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum DropSource<K> {
Rows(RowDrag<K>),
Payload(DragPayload),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DropTarget<K> {
Row {
key: K,
place: DropPlace,
},
Group(GroupPath),
Empty,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DropPlace {
Before,
After,
Onto,
}
pub struct RowDragContext<'a, R: GridRow> {
keys: &'a [R::Key],
pressed: &'a R::Key,
snapshot: &'a StoreSnapshot<R>,
modifiers: Modifiers,
}
impl<'a, R: GridRow> RowDragContext<'a, R> {
pub fn keys(&self) -> &'a [R::Key] {
self.keys
}
pub fn pressed(&self) -> &'a R::Key {
self.pressed
}
pub fn row(&self, key: &R::Key) -> Option<&'a Arc<R>> {
self.snapshot.row(self.snapshot.row_id(key)?)
}
pub fn modifiers(&self) -> Modifiers {
self.modifiers
}
}
type DropRegistrar<D> = Rc<dyn Fn(Stateful<Div>, &mut Context<LiveGridState<D>>) -> Stateful<Div>>;
pub(super) struct DragSlot<D: LiveGridDelegate> {
gesture: Box<dyn RowDragGesture<D::Row>>,
armed: Option<RowDrag<KeyOf<D>>>,
drop_policy: Box<dyn RowDropPolicy<D::Row>>,
payloads: Vec<DropRegistrar<D>>,
hover: Option<DropHover<KeyOf<D>>>,
retarget: bool,
auto_scroll: AutoScroll,
}
impl<D: LiveGridDelegate> Default for DragSlot<D> {
fn default() -> Self {
Self {
gesture: Box::new(NoRowDrag),
armed: None,
drop_policy: Box::new(NoRowDrop),
payloads: Vec::new(),
hover: None,
retarget: false,
auto_scroll: AutoScroll::default(),
}
}
}
impl<D: LiveGridDelegate> DragSlot<D> {
pub(super) fn is_enabled(&self) -> bool {
self.gesture.is_enabled()
}
pub(super) fn is_drag_press(&self, press: &RowDragPressContext<'_, D::Row>) -> bool {
self.gesture.is_enabled() && self.gesture.is_drag_press(press)
}
pub(super) fn threshold(&self) -> Pixels {
self.gesture.threshold()
}
pub(super) fn unarm(&mut self) {
self.armed = None;
}
pub(super) fn is_over(&self) -> bool {
self.hover.is_some()
}
}
#[derive(Clone)]
enum Dragged<K> {
Rows(RowDrag<K>),
Value(Rc<dyn Any>),
}
impl<K> Dragged<K> {
fn as_ref(&self) -> DraggedRef<'_, K> {
match self {
Self::Rows(drag) => DraggedRef::Rows(drag),
Self::Value(value) => DraggedRef::Value(value.as_ref()),
}
}
}
#[derive(Clone, Copy)]
enum DraggedRef<'a, K> {
Rows(&'a RowDrag<K>),
Value(&'a dyn Any),
}
struct DropHover<K> {
dragged: Dragged<K>,
own: bool,
position: Point<Pixels>,
modifiers: Modifiers,
target: Option<DropTarget<K>>,
}
struct RowDragGhost {
label: SharedString,
height: Pixels,
offset: Point<Pixels>,
text: GhostTextStyle,
}
impl Render for RowDragGhost {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();
self.text
.root()
.pl(self.offset.x + px(12.))
.pt(self.offset.y + px(12.))
.child(
div()
.h(self.height)
.max_w(px(320.))
.px(CELL_PADDING)
.flex()
.items_center()
.overflow_hidden()
.bg(theme.table_head)
.text_color(theme.table_head_foreground)
.border_1()
.border_color(theme.border)
.shadow_md()
.opacity(0.9)
.child(div().min_w(px(0.)).truncate().child(self.label.clone())),
)
}
}
fn ghost_label<K>(drag: &RowDrag<K>) -> SharedString {
if let Some(label) = drag.payload().label() {
return label.clone();
}
match drag.keys().len() {
1 => t!("live_grid.drag.row").into(),
count => t!("live_grid.drag.rows", count = count).into(),
}
}
impl<D: LiveGridDelegate> LiveGridState<D> {
pub fn with_row_drag_gesture(mut self, gesture: impl RowDragGesture<D::Row>) -> Self {
self.drag.gesture = Box::new(gesture);
self
}
pub fn with_row_drop_policy(mut self, policy: impl RowDropPolicy<D::Row>) -> Self {
self.drag.drop_policy = Box::new(policy);
self
}
pub fn with_drop_payload<T: Clone + 'static>(mut self) -> Self {
self.drag.payloads.push(Rc::new(|this, cx| {
this.on_drag_move(cx.listener(Self::on_payload_drag_move::<T>))
.on_drop(cx.listener(Self::on_payload_drop::<T>))
}));
self
}
pub fn is_dragging_rows(&self) -> bool {
self.drag.armed.is_some()
}
#[cfg(any(test, feature = "test-support"))]
pub fn row_drag(&self) -> Option<&RowDrag<KeyOf<D>>> {
self.drag.armed.as_ref()
}
pub(super) fn is_row_drag_press(
&self,
key: &KeyOf<D>,
column: super::ColumnOf<D>,
was_selected: bool,
modifiers: Modifiers,
click_count: usize,
) -> bool {
if !self.drag.is_enabled() {
return false;
}
let Some(id) = self.snapshot.row_id(key) else {
return false;
};
let Some(row) = self.snapshot.row(id) else {
return false;
};
let line = SelectableLine::Row {
row: &**row,
branch: self.projection.branch(id),
};
self.drag.is_drag_press(&RowDragPressContext::new(
line,
column,
was_selected,
modifiers,
self.platform,
self.selection.mode(),
click_count,
))
}
pub(super) fn arm_row_drag(
&mut self,
pressed: &KeyOf<D>,
modifiers: Modifiers,
cx: &mut Context<Self>,
) -> bool {
if self.snapshot.row_id(pressed).is_none() {
return false;
}
let keys = self
.command_scope(Some(&LineTarget::Row(pressed.clone())))
.shared_keys();
let payload = self.delegate.drag_payload(
&RowDragContext {
keys: &keys,
pressed,
snapshot: &self.snapshot,
modifiers,
},
cx,
);
let Some(payload) = payload else {
return false;
};
self.drag.armed = Some(RowDrag {
grid: cx.entity_id(),
keys,
payload,
});
cx.notify();
true
}
pub(super) fn unarm_row_drag(&mut self) {
self.drag.unarm();
}
pub(super) fn settle_row_drag(&mut self, cx: &mut Context<Self>) {
if self.drag.armed.is_some() && self.press.is_none() && !cx.has_active_drag() {
self.drag.unarm();
}
if self.drag.hover.is_some() && !cx.has_active_drag() {
self.end_drop_hover(cx);
}
}
pub(super) fn render_rows(
&self,
body: impl IntoElement,
ghost_text: GhostTextStyle,
cx: &mut Context<Self>,
) -> AnyElement {
let row_height = self.row_height;
div()
.id("live-grid-rows")
.flex()
.flex_col()
.flex_grow(1.)
.flex_shrink(1.)
.flex_basis(relative(0.))
.w_full()
.when(self.drag.is_enabled(), |this| this.on_click(|_, _, _| {}))
.on_drag_move(cx.listener(Self::on_row_drag_move))
.when(self.drag.drop_policy.is_enabled(), |this| {
this.on_drop(cx.listener(Self::on_rows_drop))
})
.map(|this| {
self.drag
.payloads
.iter()
.fold(this, |this, register| register(this, cx))
})
.when_some(self.drag.armed.clone(), |this, drag| {
this.cursor(CursorStyle::ClosedHand).on_drag(
drag,
move |drag: &RowDrag<KeyOf<D>>, offset, _: &mut Window, cx: &mut App| {
let label = ghost_label(drag);
cx.new(|_| RowDragGhost {
label,
height: row_height,
offset,
text: ghost_text.clone(),
})
},
)
})
.child(body)
.into_any_element()
}
fn on_row_drag_move(
&mut self,
event: &DragMoveEvent<RowDrag<KeyOf<D>>>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let (position, modifiers) = (event.event.position, event.event.modifiers);
let inside = event.bounds.contains(&position);
if !inside && self.drag.hover.is_none() {
return;
}
let drag = event.drag(cx).clone();
let own = drag.grid() == cx.entity_id();
self.follow_drag(
Dragged::Rows(drag),
own,
position,
modifiers,
inside,
window,
cx,
);
}
fn on_payload_drag_move<T: Clone + 'static>(
&mut self,
event: &DragMoveEvent<T>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let (position, modifiers) = (event.event.position, event.event.modifiers);
let inside = event.bounds.contains(&position);
if !inside && self.drag.hover.is_none() {
return;
}
let value = match &self.drag.hover {
Some(DropHover {
dragged: Dragged::Value(value),
..
}) if value.is::<T>() => value.clone(),
_ => Rc::new(event.drag(cx).clone()) as Rc<dyn Any>,
};
self.follow_drag(
Dragged::Value(value),
false,
position,
modifiers,
inside,
window,
cx,
);
}
#[allow(clippy::too_many_arguments)]
fn follow_drag(
&mut self,
dragged: Dragged<KeyOf<D>>,
own: bool,
position: Point<Pixels>,
modifiers: Modifiers,
inside: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
if !inside {
self.end_drop_hover(cx);
cx.set_active_drag_cursor_style(CursorStyle::ClosedHand, window);
return;
}
let started = profile_start();
let target = self.row_drop_target_at(dragged.as_ref(), own, position, modifiers);
self.stats.last_drop_target = profile_elapsed(started);
let cursor = match &target {
Some(_) if self.drag.drop_policy.is_copy(modifiers) => CursorStyle::DragCopy,
Some(_) => CursorStyle::ClosedHand,
None if own => CursorStyle::ClosedHand,
None => CursorStyle::OperationNotAllowed,
};
window.defer(cx, move |window, cx| {
if cx.active_drag_cursor_style() != Some(cursor) {
cx.set_active_drag_cursor_style(cursor, window);
}
});
let changed = self.drag.hover.as_ref().map(|hover| &hover.target) != Some(&target);
let entered = self.drag.hover.is_none();
self.drag.hover = Some(DropHover {
dragged,
own,
position,
modifiers,
target,
});
if self.drag.drop_policy.is_enabled() {
let delta = AutoScroll::compute_delta(position.y, self.rows_bounds());
self.drag.auto_scroll.last_drag_position = Some(position);
self.drag.auto_scroll.set(delta, cx, |delta, state, cx| {
state.auto_scroll_drop(delta, cx)
});
}
if changed || entered {
cx.notify();
}
}
fn auto_scroll_drop(&mut self, delta: Pixels, cx: &mut Context<Self>) {
if self.drag.hover.is_none() {
self.drag.auto_scroll.stop();
return;
}
let before = (self.scroll_row, self.scroll_offset);
self.scroll_rows_by(delta);
if (self.scroll_row, self.scroll_offset) != before {
self.drag.retarget = true;
cx.notify();
}
}
pub(super) fn retarget_drop(&mut self) {
if !std::mem::take(&mut self.drag.retarget) {
return;
}
let Some(hover) = &self.drag.hover else {
return;
};
let target = self.row_drop_target_at(
hover.dragged.as_ref(),
hover.own,
hover.position,
hover.modifiers,
);
if let Some(hover) = &mut self.drag.hover {
hover.target = target;
}
}
pub(crate) fn end_drop_hover(&mut self, cx: &mut Context<Self>) {
self.drag.auto_scroll.stop();
self.drag.retarget = false;
if self.drag.hover.take().is_some() {
self.reconsider_held(cx);
cx.notify();
}
}
fn row_drop_target_at(
&self,
dragged: DraggedRef<'_, KeyOf<D>>,
own: bool,
position: Point<Pixels>,
modifiers: Modifiers,
) -> Option<DropTarget<KeyOf<D>>> {
let policy = &self.drag.drop_policy;
if !policy.is_enabled() {
return None;
}
let BodyLine::Data(line) = self.body_line_at(position)? else {
return None;
};
let rows = self.rows_bounds();
let y = position.y - rows.origin.y + self.scroll_offset;
let offset = ((y - self.row_height * line as f32) / self.row_height).clamp(0., 1.);
let (snapshot, projection) = (&self.snapshot, &self.projection);
let drop_line = match self.painted.get(line) {
Some(PaintedLine::Row(id)) => match snapshot.row(*id) {
Some(row) => DropLine::Row {
key: row.key(),
branch: projection.branch(*id),
},
None => DropLine::Empty,
},
Some(PaintedLine::Caption(path)) => DropLine::Caption(projection.groups().find(path)?),
Some(PaintedLine::Footer(_)) => return None,
Some(PaintedLine::Ghost(_)) | None => DropLine::Empty,
};
let last_row = matches!(drop_line, DropLine::Empty)
.then(|| {
(0..projection.len())
.rev()
.filter_map(|ix| projection.row(ix))
.find_map(|id| snapshot.row(id))
.map(|row| row.key())
})
.flatten();
let (rows_drag, value) = match dragged {
DraggedRef::Rows(drag) => (Some(drag), None),
DraggedRef::Value(value) => (None, Some(value)),
};
policy.place(&RowDropContext::new(
drop_line,
offset,
rows_drag,
value,
own,
modifiers,
(
!self.view.sort().is_empty(),
projection.is_grouped(),
projection.is_tree(),
),
last_row,
snapshot,
))
}
fn on_rows_drop(
&mut self,
drag: &RowDrag<KeyOf<D>>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let own = drag.grid() == cx.entity_id();
let source = DropSource::Rows(drag.clone());
self.drop_on_rows(DraggedRef::Rows(drag), own, source, window, cx);
}
fn on_payload_drop<T: Clone + 'static>(
&mut self,
value: &T,
window: &mut Window,
cx: &mut Context<Self>,
) {
let source = DropSource::Payload(DragPayload::new().with_data(value.clone()));
self.drop_on_rows(DraggedRef::Value(value), false, source, window, cx);
}
fn drop_on_rows(
&mut self,
dragged: DraggedRef<'_, KeyOf<D>>,
own: bool,
source: DropSource<KeyOf<D>>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let (position, modifiers) = (window.mouse_position(), window.modifiers());
let target = self.row_drop_target_at(dragged, own, position, modifiers);
self.end_drop_hover(cx);
if let Some(target) = target {
cx.emit(LiveGridEvent::Drop {
source,
target,
modifiers,
});
}
}
pub(super) fn drop_indicator(
&self,
rows: Bounds<Pixels>,
fill_color: Hsla,
border: Hsla,
) -> Vec<(Bounds<Pixels>, Hsla)> {
let Some(target) = self
.drag
.hover
.as_ref()
.and_then(|hover| hover.target.as_ref())
else {
return Vec::new();
};
let top = |line: usize| rows.origin.y + self.row_height * line as f32 - self.scroll_offset;
let line_of =
|painted: &dyn Fn(&PaintedLine) -> bool| self.painted.iter().position(painted);
let mut quads = Vec::new();
let bar = |y: Pixels, quads: &mut Vec<_>| {
quads.push((
Bounds::new(
point(rows.origin.x, y - px(1.)),
size(rows.size.width, px(2.)),
),
border,
));
};
let frame = |area: Bounds<Pixels>, fill: Option<Hsla>, quads: &mut Vec<_>| {
if let Some(color) = fill {
quads.push((area, color));
}
let (w, h, b) = (area.size.width, area.size.height, px(2.));
let origin = area.origin;
quads.extend([
(Bounds::new(origin, size(w, b)), border),
(
Bounds::new(point(origin.x, origin.y + h - b), size(w, b)),
border,
),
(Bounds::new(origin, size(b, h)), border),
(
Bounds::new(point(origin.x + w - b, origin.y), size(b, h)),
border,
),
]);
};
let line_area = |line: usize| {
Bounds::new(
point(rows.origin.x, top(line)),
size(rows.size.width, self.row_height),
)
};
match target {
DropTarget::Row { key, place } => {
let Some(id) = self.snapshot.row_id(key) else {
return quads;
};
let Some(line) = line_of(&|painted| painted.row() == Some(id)) else {
return quads;
};
match place {
DropPlace::Before => bar(top(line), &mut quads),
DropPlace::After => bar(top(line + 1), &mut quads),
_ => frame(line_area(line), Some(fill_color), &mut quads),
}
}
DropTarget::Group(path) => {
if let Some(line) = line_of(&|painted| painted.group() == Some(path)) {
frame(line_area(line), Some(fill_color), &mut quads);
}
}
_ => {
let shown = self
.painted
.iter()
.rposition(|painted| !matches!(painted, PaintedLine::Ghost(_)));
match shown {
Some(last) => bar(top(last + 1), &mut quads),
None => frame(rows, None, &mut quads),
}
}
}
quads
.into_iter()
.filter_map(|(bounds, color)| {
let bounds = bounds.intersect(&rows);
(bounds.size.height > px(0.) && bounds.size.width > px(0.))
.then_some((bounds, color))
})
.collect()
}
pub(super) fn cancel_row_drag(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
if self.drag.armed.is_none() {
return false;
}
self.drag.unarm();
cx.stop_active_drag(window);
cx.notify();
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_drag_payload() {
let payload = DragPayload::new();
assert!(payload.label().is_none() && payload.data::<u32>().is_none());
let payload = payload.with_data(7u32).with_label("3 orders");
assert_eq!(payload.data::<u32>(), Some(&7));
assert_eq!(payload.data::<i64>(), None);
assert_eq!(payload.label().map(AsRef::as_ref), Some("3 orders"));
let debug = format!("{payload:?}");
assert!(debug.contains("3 orders") && debug.contains("has_data: true"));
assert_ne!(
DropTarget::Row {
key: 1,
place: DropPlace::Before
},
DropTarget::Empty
);
}
}