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
//! Draggable module.
//!
//! This public module implements the Liora drag state and reorder helpers shared by draggable list surfaces. It keeps the reusable
//! component logic inside `liora-components` rather than Gallery or Docs so
//! downstream GPUI applications can compose the same behavior with their own
//! app state, assets, and release policy.
//!
//! ## Usage model
//!
//! Components in this module render native GPUI element trees. Stateless builder
//! values can be constructed inline, while controls with focus, selection,
//! popup, drag, or editing state should be stored as `gpui::Entity<T>` fields in
//! the parent view so state survives GPUI render passes.
//!
//! ## Design contract
//!
//! The implementation should use Liora theme tokens from `liora-core` and
//! `liora-theme`, keep accessibility-oriented keyboard/pointer behavior close to
//! the component, and avoid app-specific Gallery/Docs resources in this SDK
//! crate.
use gpui::{AnyElement, Bounds, Div, Hsla, Pixels, Point, div, point, prelude::*, px};
use liora_icons::Icon;
use liora_icons_lucide::IconName;
/// Axis used by Liora's native drag helpers.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DragAxis {
/// Lays out content in the horizontal direction.
Horizontal,
/// Lays out content in the vertical direction.
Vertical,
/// Uses the `Free` option for `DragAxis`.
Free,
}
/// Runtime pointer state for handle-based dragging.
///
/// The helper intentionally stores only coordinates and item positions, never
/// rendered GPUI elements. This mirrors the safe part of drag-rs' model: a drag
/// operation has a start point, a current cursor point and a result callback,
/// while each UI renders its own preview/handle natively.
#[derive(Clone, Debug, Default)]
pub struct DragState {
origin_index: Option<usize>,
active_index: Option<usize>,
over_index: Option<usize>,
start_position: Option<Point<Pixels>>,
current_position: Option<Point<Pixels>>,
grab_offset: Option<Point<Pixels>>,
}
impl DragState {
/// Performs the start operation used by this component.
pub fn start(&mut self, index: usize, position: Point<Pixels>) {
self.start_at(index, position, None);
}
/// Performs the start at operation used by this component.
pub fn start_at(
&mut self,
index: usize,
position: Point<Pixels>,
bounds: Option<Bounds<Pixels>>,
) {
self.origin_index = Some(index);
self.active_index = Some(index);
self.over_index = Some(index);
self.start_position = Some(position);
self.current_position = Some(position);
self.grab_offset =
bounds.map(|bounds| point(position.x - bounds.origin.x, position.y - bounds.origin.y));
}
/// Performs the update position operation used by this component.
pub fn update_position(&mut self, position: Point<Pixels>) {
if self.active_index.is_some() {
self.current_position = Some(position);
}
}
/// Updates the stored over value and keeps the existing component identity.
pub fn set_over(&mut self, index: usize) {
if self.active_index.is_some() {
self.over_index = Some(index);
}
}
/// Performs the move active to operation used by this component.
pub fn move_active_to(&mut self, index: usize) {
self.active_index = Some(index);
self.over_index = Some(index);
self.start_position = self.current_position;
}
/// Performs the finish operation used by this component.
pub fn finish(&mut self) -> Option<(usize, usize)> {
let origin = self.origin_index.take()?;
let active = self.active_index.take()?;
let target = self.over_index.take().unwrap_or(active);
self.start_position = None;
self.current_position = None;
self.grab_offset = None;
Some((origin, target))
}
/// Performs the cancel operation used by this component.
pub fn cancel(&mut self) {
self.origin_index = None;
self.active_index = None;
self.over_index = None;
self.start_position = None;
self.current_position = None;
self.grab_offset = None;
}
/// Performs the active index operation used by this component.
pub fn active_index(&self) -> Option<usize> {
self.active_index
}
/// Performs the origin index operation used by this component.
pub fn origin_index(&self) -> Option<usize> {
self.origin_index
}
/// Performs the over index operation used by this component.
pub fn over_index(&self) -> Option<usize> {
self.over_index
}
/// Returns whether active is currently true for this value.
pub fn is_active(&self, index: usize) -> bool {
self.active_index == Some(index)
}
/// Returns whether over is currently true for this value.
pub fn is_over(&self, index: usize) -> bool {
self.over_index == Some(index) && self.active_index != Some(index)
}
/// Performs the offset operation used by this component.
pub fn offset(&self, axis: DragAxis) -> (Pixels, Pixels) {
let Some(start) = self.start_position else {
return (px(0.0), px(0.0));
};
let Some(current) = self.current_position else {
return (px(0.0), px(0.0));
};
let dx = current.x - start.x;
let dy = current.y - start.y;
match axis {
DragAxis::Horizontal => (dx, px(0.0)),
DragAxis::Vertical => (px(0.0), dy),
DragAxis::Free => (dx, dy),
}
}
/// Offset the active item from its current layout slot so the original
/// grabbed point remains under the pointer.
///
/// Reorderable lists may move the active item to a new slot while the
/// pointer is still down. Using only `current - start` makes the item jump
/// when that layout slot changes. When the caller can provide the active
/// slot's latest bounds, this method compensates by anchoring the visual
/// preview to the grab offset captured on mouse down.
pub fn offset_from_bounds(
&self,
axis: DragAxis,
bounds: Option<Bounds<Pixels>>,
) -> (Pixels, Pixels) {
let Some(bounds) = bounds else {
return self.offset(axis);
};
let Some(current) = self.current_position else {
return (px(0.0), px(0.0));
};
let grab_offset = self.grab_offset.unwrap_or_else(|| point(px(0.0), px(0.0)));
let dx = current.x - grab_offset.x - bounds.origin.x;
let dy = current.y - grab_offset.y - bounds.origin.y;
match axis {
DragAxis::Horizontal => (dx, px(0.0)),
DragAxis::Vertical => (px(0.0), dy),
DragAxis::Free => (dx, dy),
}
}
}
/// Move an item inside a vector, returning whether a move happened.
pub fn reorder_indices<T>(items: &mut Vec<T>, from: usize, to: usize) -> bool {
if from >= items.len() || to >= items.len() || from == to {
return false;
}
let item = items.remove(from);
items.insert(to, item);
true
}
/// Default front-side drag handle used by reorderable components.
pub fn drag_handle(color: Hsla, active: bool, width: Pixels) -> Div {
div()
.flex()
.flex_none()
.w(width)
.items_start()
.items_center()
.justify_center()
.cursor_pointer()
.rounded_md()
.when(!active, |s| {
s.hover(|s| s.cursor_pointer().bg(gpui::black().opacity(0.018)))
})
.when(active, |s| s.cursor_pointer())
.child(
Icon::new(IconName::GripVertical)
.size(px(16.0))
.color(color),
)
}
/// Convenience wrapper for callers that need a boxed element handle.
pub fn drag_handle_element(color: Hsla, active: bool, width: Pixels) -> AnyElement {
drag_handle(color, active, width).into_any_element()
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::{Bounds, point, px, size};
#[test]
fn reorder_indices_moves_items() {
let mut items = vec![0, 1, 2, 3];
assert!(reorder_indices(&mut items, 0, 2));
assert_eq!(items, vec![1, 2, 0, 3]);
assert!(reorder_indices(&mut items, 3, 1));
assert_eq!(items, vec![1, 3, 2, 0]);
}
#[test]
fn drag_state_tracks_axis_offsets() {
let mut state = DragState::default();
state.start(2, point(px(10.0), px(20.0)));
state.update_position(point(px(42.0), px(12.0)));
assert_eq!(state.offset(DragAxis::Horizontal), (px(32.0), px(0.0)));
assert_eq!(state.offset(DragAxis::Vertical), (px(0.0), px(-8.0)));
assert_eq!(state.offset(DragAxis::Free), (px(32.0), px(-8.0)));
}
#[test]
fn drag_state_finishes_with_last_over_index() {
let mut state = DragState::default();
state.start(1, point(px(0.0), px(0.0)));
state.update_position(point(px(20.0), px(0.0)));
state.move_active_to(3);
assert_eq!(state.finish(), Some((1, 3)));
assert_eq!(state.origin_index(), None);
assert_eq!(state.active_index(), None);
assert_eq!(state.over_index(), None);
}
#[test]
fn drag_state_keeps_grab_offset_when_slot_moves_backward() {
let mut state = DragState::default();
state.start_at(
3,
point(px(310.0), px(10.0)),
Some(Bounds::new(
point(px(300.0), px(0.0)),
size(px(100.0), px(40.0)),
)),
);
state.update_position(point(px(250.0), px(10.0)));
state.move_active_to(2);
assert_eq!(
state.offset_from_bounds(
DragAxis::Horizontal,
Some(Bounds::new(
point(px(200.0), px(0.0)),
size(px(100.0), px(40.0)),
)),
),
(px(40.0), px(0.0))
);
}
}