@@ -33,6 +33,9 @@ enum Item {
33
33
34
34
#[ derive( Debug ) ]
35
35
enum Command {
36
+ /// Set the selected item
37
+ SetSelectedItem ( Option < ItemId > ) ,
38
+
36
39
/// Move the currently dragged item to the given container and position.
37
40
MoveItem {
38
41
moved_item_id : ItemId ,
@@ -51,6 +54,9 @@ pub struct HierarchicalDragAndDrop {
51
54
/// Id of the root item (not displayed in the UI)
52
55
root_id : ItemId ,
53
56
57
+ /// Selected item, if any
58
+ selected_item : Option < ItemId > ,
59
+
54
60
/// If a drag is ongoing, this is the id of the destination container (if any was identified)
55
61
///
56
62
/// This is used to highlight the target container.
@@ -73,6 +79,7 @@ impl Default for HierarchicalDragAndDrop {
73
79
let mut res = Self {
74
80
items : std:: iter:: once ( ( root_id, root_item) ) . collect ( ) ,
75
81
root_id,
82
+ selected_item : None ,
76
83
target_container : None ,
77
84
command_receiver,
78
85
command_sender,
@@ -235,12 +242,25 @@ impl HierarchicalDragAndDrop {
235
242
self . container_children_ui ( ui, top_level_items) ;
236
243
}
237
244
245
+ // deselect by clicking in the empty space
246
+ if ui
247
+ . interact (
248
+ ui. available_rect_before_wrap ( ) ,
249
+ "empty_space" . into ( ) ,
250
+ egui:: Sense :: click ( ) ,
251
+ )
252
+ . clicked ( )
253
+ {
254
+ self . send_command ( Command :: SetSelectedItem ( None ) ) ;
255
+ }
256
+
238
257
// always reset the target container
239
258
self . target_container = None ;
240
259
241
260
while let Ok ( command) = self . command_receiver . try_recv ( ) {
242
- // println!("Received command: {command:?}");
261
+ println ! ( "Received command: {command:?}" ) ;
243
262
match command {
263
+ Command :: SetSelectedItem ( item_id) => self . selected_item = item_id,
244
264
Command :: MoveItem {
245
265
moved_item_id,
246
266
target_container_id,
@@ -264,13 +284,25 @@ impl HierarchicalDragAndDrop {
264
284
ui. add (
265
285
egui:: Label :: new ( format ! ( "Container {item_id:?}" ) )
266
286
. selectable ( false )
267
- . sense ( egui:: Sense :: drag ( ) ) ,
287
+ . sense ( egui:: Sense :: click_and_drag ( ) ) ,
268
288
)
269
289
} )
270
290
. body ( |ui| {
271
291
self . container_children_ui ( ui, children) ;
272
292
} ) ;
273
293
294
+ if head_response. inner . clicked ( ) {
295
+ self . send_command ( Command :: SetSelectedItem ( Some ( item_id) ) ) ;
296
+ }
297
+
298
+ if self . target_container == Some ( item_id) {
299
+ ui. painter ( ) . rect_stroke (
300
+ head_response. inner . rect ,
301
+ 2.0 ,
302
+ ( 1.0 , ui. visuals ( ) . selection . bg_fill ) ,
303
+ ) ;
304
+ }
305
+
274
306
self . handle_drag_and_drop_interaction (
275
307
ui,
276
308
item_id,
@@ -282,6 +314,13 @@ impl HierarchicalDragAndDrop {
282
314
283
315
fn container_children_ui ( & self , ui : & mut egui:: Ui , children : & Vec < ItemId > ) {
284
316
for child_id in children {
317
+ // check if the item is selected
318
+ ui. visuals_mut ( ) . override_text_color = if Some ( * child_id) == self . selected_item {
319
+ Some ( ui. visuals ( ) . selection . bg_fill )
320
+ } else {
321
+ None
322
+ } ;
323
+
285
324
match self . items . get ( child_id) {
286
325
Some ( Item :: Container ( children) ) => {
287
326
self . container_ui ( ui, * child_id, children) ;
@@ -298,9 +337,13 @@ impl HierarchicalDragAndDrop {
298
337
let response = ui. add (
299
338
egui:: Label :: new ( label)
300
339
. selectable ( false )
301
- . sense ( egui:: Sense :: drag ( ) ) ,
340
+ . sense ( egui:: Sense :: click_and_drag ( ) ) ,
302
341
) ;
303
342
343
+ if response. clicked ( ) {
344
+ self . send_command ( Command :: SetSelectedItem ( Some ( item_id) ) ) ;
345
+ }
346
+
304
347
self . handle_drag_and_drop_interaction ( ui, item_id, false , & response, None ) ;
305
348
}
306
349
@@ -318,6 +361,9 @@ impl HierarchicalDragAndDrop {
318
361
319
362
if response. drag_started ( ) {
320
363
egui:: DragAndDrop :: set_payload ( ui. ctx ( ) , item_id) ;
364
+
365
+ // force selection to the dragged item
366
+ self . send_command ( Command :: SetSelectedItem ( Some ( item_id) ) ) ;
321
367
}
322
368
323
369
//
0 commit comments