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
//! Helpers for drag and drop support for reordering hierarchical lists.
//!
//! Works well in combination with [`crate::list_item::ListItem`].

pub enum ItemKind<ItemId: Copy> {
    /// Root container item.
    ///
    /// Root container don't have a parent and are restricted when hovered as drop target (the dragged item can only go
    /// _in_ the root container, not before or after).
    RootContainer,

    /// Container item.
    Container {
        parent_id: ItemId,
        position_index_in_parent: usize,
    },

    /// Leaf item.
    Leaf {
        parent_id: ItemId,
        position_index_in_parent: usize,
    },
}

/// Context information about the hovered item.
///
/// This is used by [`find_drop_target`] to compute the [`DropTarget`], if any.
pub struct ItemContext<ItemId: Copy> {
    /// ID of the item being hovered during drag
    pub id: ItemId,

    /// What kind of item is this?
    pub item_kind: ItemKind<ItemId>,

    /// ID of the container just before this item within the parent, if such a container exists.
    pub previous_container_id: Option<ItemId>,
}

/// Drop target information, including where to draw the drop indicator and where to insert the dragged item.
#[derive(Clone, Debug)]
pub struct DropTarget<ItemId: Copy> {
    /// Range of X coordinates for the drag target indicator
    pub indicator_span_x: egui::Rangef,

    /// Y coordinate for drag target indicator
    pub indicator_position_y: f32,

    /// Destination container ID
    pub target_parent_id: ItemId,

    /// Destination position within the container
    pub target_position_index: usize,
}

impl<ItemId: Copy> DropTarget<ItemId> {
    pub fn new(
        indicator_span_x: egui::Rangef,
        indicator_position_y: f32,
        target_parent_id: ItemId,
        target_position_index: usize,
    ) -> Self {
        Self {
            indicator_span_x,
            indicator_position_y,
            target_parent_id,
            target_position_index,
        }
    }
}

/// Compute the geometry of the drag cursor and where the dragged item should be inserted.
///
/// This function performs the following tasks:
/// - based on `item_rect` and `body_rect`, establish the geometry of actual drop zones (see below)
/// - test the mouse cursor against these zones
/// - if one is a match:
///   - compute the geometry of a drop insertion indicator
///   - use the context provided in `item_context` to return the "logical" drop target (ie. the target container and
///     position within it)
///
/// This function implements the following logic:
/// ```text
///
///                        insert         insert last in container before me
///                      before me           (if any) or insert before me
///                          │                             │
///                      ╔═══▼═════════════════════════════▼══════════════════╗
///                      ║      │                                             ║
///         leaf item    ║ ─────┴──────────────────────────────────────────── ║
///                      ║                                                    ║
///                      ╚═════════════════════▲══════════════════════════════╝
///                                            │
///                                     insert after me
///
///
///                        insert         insert last in container before me
///                      before me           (if any) or insert before me
///                          │                             │
///                      ╔═══▼═════════════════════════════▼══════════════════╗
///         leaf item    ║      │                                             ║
///         with body    ║ ─────┴──────────────────────────────────────────── ║
///                      ║                                                    ║
///                      ╚══════╦══════════════════════════════════════▲══════╣ ─┐
///                      │      ║                                      │      ║  │
///                      │      ║                                   insert    ║  │
///                      │      ║                                  after me   ║  │
///                      │      ╠══                                         ══╣  │
///                      │      ║             no insertion possible           ║  │
///                      │      ║             here by definition of           ║  │ body
///                      │      ║              parent being a leaf            ║  │
///                      │      ╠══                                         ══╣  │
///                      │      ║                                             ║  │
///                      │      ║                                             ║  │
///                      │      ║                                             ║  │
///                      └──▲── ╚══════════════════════════▲══════════════════╝ ─┘
///                         │                              │
///                      insert                         insert
///                     after me                       after me
///
///
///                        insert         insert last in container before me
///                      before me           (if any) or insert before me
///                          │                             │
///                      ╔═══▼═════════════════════════════▼══════════════════╗
///    container item    ║      │                                             ║
///  (empty/collapsed    ║ ─────┼──────────────────────────────────────────── ║
///             body)    ║      │                                             ║
///                      ╚═══▲═════════════════════════════▲══════════════════╝
///                          │                             │
///                       insert                   insert inside me
///                      after me                     at pos = 0
///
///
///                        insert         insert last in container before me
///                      before me           (if any) or insert before me
///                          │                             │
///                      ╔═══▼═════════════════════════════▼══════════════════╗
///    container item    ║      │                                             ║
///         with body    ║ ─────┴──────────────────────────────────────────── ║
///                      ║                                                    ║
///                      ╚═▲════╦═════════════════════════════════════════════╣ ─┐
///                        │    ║                                             ║  │
///                     insert  ║                                             ║  │
///                  inside me  ║                                             ║  │
///                 at pos = 0  ╠══                                         ══╣  │
///                             ║                  same logic                 ║  │
///                             ║                 recursively                 ║  │ body
///                     insert  ║                 applied here                ║  │
///                   after me  ╠══                                         ══╣  │
///                        │    ║                                             ║  │
///                      ┌─▼─── ║                                             ║  │
///                      │      ║                                             ║  │
///                      └───── ╚═════════════════════════════════════════════╝ ─┘
///
///                                              not a valid drop zone
///                                                        │
///                      ╔═════════════════════════════════▼══════════════════╗
///    root container    ║                                                    ║
///              item    ║ ────────────────────────────────────────────────── ║
///                      ║                                                    ║
///                      ╚════════════════════════▲═══════════════════════════╝
///                                               │
///                                       insert inside me
///                                          at pos = 0
/// ```
///
/// Here are a few observations of the above that help navigate the "if-statement-of-death"
/// in the implementation:
/// - The top parts of the item are treated the same in most cases (root container is an
///   exception).
/// - Handling of the body can be simplified by making the sensitive area either a small
///   corner (container case), or the entire body (leaf case). Then, that area always maps
///   to "insert after me".
/// - The bottom parts have the most difference between cases and need case-by-case handling.
///   In both leaf item cases, the entire bottom part maps to "insert after me", though.
///
/// **Note**: in debug builds, press `Alt` to visualize the drag zones while dragging.
pub fn find_drop_target<ItemId: Copy>(
    ui: &egui::Ui,
    item_context: &ItemContext<ItemId>,
    item_rect: egui::Rect,
    body_rect: Option<egui::Rect>,
    item_height: f32,
) -> Option<DropTarget<ItemId>> {
    let indent = ui.spacing().indent;
    let item_id = item_context.id;
    let item_kind = &item_context.item_kind;
    let is_non_root_container = matches!(item_kind, ItemKind::Container { .. });

    // For both leaf and containers we have two drag zones on the upper half of the item.
    let (mut top, mut bottom) = item_rect.split_top_bottom_at_fraction(0.5);

    let mut left_top = egui::Rect::NOTHING;
    if matches!(item_kind, ItemKind::RootContainer) {
        top = egui::Rect::NOTHING;
    } else {
        (left_top, top) = top.split_left_right_at_x(top.left() + indent);
    }

    // For the lower part of the item, the story is more complicated:
    // - for leaf and root container items, we have a single drag zone on the entire lower half
    // - for container item, we must distinguish between the indent part and the rest, plus check some area in the
    //   body
    let mut left_bottom = egui::Rect::NOTHING;
    if is_non_root_container {
        (left_bottom, bottom) = bottom.split_left_right_at_x(bottom.left() + indent);
    }

    // For the body area we have two cases:
    // - container item: it's handled recursively by the nested items, so we only need to check a small area down
    //   left, which maps to "insert after me"
    // - leaf item: the entire body area, if any, cannot receive a drag (by definition) and thus homogeneously maps
    //   to "insert after me"
    let body_insert_after_me_area = if let Some(body_rect) = body_rect {
        if is_non_root_container {
            egui::Rect::from_two_pos(
                body_rect.left_bottom() + egui::vec2(indent, -item_height / 2.0),
                body_rect.left_bottom(),
            )
        } else {
            body_rect
        }
    } else {
        egui::Rect::NOTHING
    };

    // body rect, if any AND it actually contains something
    let non_empty_body_rect = body_rect.filter(|r| r.height() > 0.0);

    // visualize the drag zones in debug builds, when the `Alt` key is pressed during drag
    #[cfg(debug_assertions)]
    {
        // Visualize the drag zones
        if ui.input(|i| i.modifiers.alt) {
            ui.ctx()
                .debug_painter()
                .debug_rect(top, egui::Color32::RED, "t");
            ui.ctx()
                .debug_painter()
                .debug_rect(bottom, egui::Color32::GREEN, "b");

            ui.ctx().debug_painter().debug_rect(
                left_top,
                egui::Color32::RED.gamma_multiply(0.5),
                "lt",
            );
            ui.ctx().debug_painter().debug_rect(
                left_bottom,
                egui::Color32::GREEN.gamma_multiply(0.5),
                "lb",
            );
            ui.ctx().debug_painter().debug_rect(
                body_insert_after_me_area,
                egui::Color32::YELLOW,
                "bdy",
            );
        }
    }

    match *item_kind {
        ItemKind::RootContainer => {
            // we just need to test the bottom section in this case.
            if ui.rect_contains_pointer(bottom) {
                Some(DropTarget::new(
                    item_rect.x_range(),
                    bottom.bottom(),
                    item_id,
                    0,
                ))
            } else {
                None
            }
        }

        ItemKind::Container {
            parent_id,
            position_index_in_parent,
        }
        | ItemKind::Leaf {
            parent_id,
            position_index_in_parent,
        } => {
            /* ===== TOP SECTIONS (same leaf/container items) ==== */
            if ui.rect_contains_pointer(left_top) {
                // insert before me
                Some(DropTarget::new(
                    item_rect.x_range(),
                    top.top(),
                    parent_id,
                    position_index_in_parent,
                ))
            } else if ui.rect_contains_pointer(top) {
                // insert last in the previous container if any, else insert before me
                if let Some(previous_container_id) = item_context.previous_container_id {
                    Some(DropTarget::new(
                        (item_rect.left() + indent..=item_rect.right()).into(),
                        top.top(),
                        previous_container_id,
                        usize::MAX,
                    ))
                } else {
                    Some(DropTarget::new(
                        item_rect.x_range(),
                        top.top(),
                        parent_id,
                        position_index_in_parent,
                    ))
                }
            }
            /* ==== BODY SENSE AREA ==== */
            else if ui.rect_contains_pointer(body_insert_after_me_area) {
                // insert after me in my parent
                Some(DropTarget::new(
                    item_rect.x_range(),
                    body_insert_after_me_area.bottom(),
                    parent_id,
                    position_index_in_parent + 1,
                ))
            }
            /* ==== BOTTOM SECTIONS (leaf item) ==== */
            else if !is_non_root_container {
                if ui.rect_contains_pointer(bottom) {
                    let position_y = if let Some(body_rect) = non_empty_body_rect {
                        body_rect.bottom()
                    } else {
                        bottom.bottom()
                    };

                    // insert after me
                    Some(DropTarget::new(
                        item_rect.x_range(),
                        position_y,
                        parent_id,
                        position_index_in_parent + 1,
                    ))
                } else {
                    None
                }
            }
            /* ==== BOTTOM SECTIONS (container item) ==== */
            else if let Some(body_rect) = non_empty_body_rect {
                if ui.rect_contains_pointer(left_bottom) || ui.rect_contains_pointer(bottom) {
                    // insert at pos = 0 inside me
                    Some(DropTarget::new(
                        (body_rect.left() + indent..=body_rect.right()).into(),
                        left_bottom.bottom(),
                        item_id,
                        0,
                    ))
                } else {
                    None
                }
            } else if ui.rect_contains_pointer(left_bottom) {
                // insert after me in my parent
                Some(DropTarget::new(
                    item_rect.x_range(),
                    left_bottom.bottom(),
                    parent_id,
                    position_index_in_parent + 1,
                ))
            } else if ui.rect_contains_pointer(bottom) {
                // insert at pos = 0 inside me
                Some(DropTarget::new(
                    (item_rect.left() + indent..=item_rect.right()).into(),
                    bottom.bottom(),
                    item_id,
                    0,
                ))
            }
            /* ==== Who knows where else the mouse cursor might wander… ¯\_(ツ)_/¯ ==== */
            else {
                None
            }
        }
    }
}