Skip to content

Commit 4037299

Browse files
committed
Add support for selection and target container highlight
1 parent 9689d71 commit 4037299

File tree

1 file changed

+49
-3
lines changed
  • examples/hierachical_list_drag_and_drop/src

1 file changed

+49
-3
lines changed

examples/hierachical_list_drag_and_drop/src/app.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ enum Item {
3333

3434
#[derive(Debug)]
3535
enum Command {
36+
/// Set the selected item
37+
SetSelectedItem(Option<ItemId>),
38+
3639
/// Move the currently dragged item to the given container and position.
3740
MoveItem {
3841
moved_item_id: ItemId,
@@ -51,6 +54,9 @@ pub struct HierarchicalDragAndDrop {
5154
/// Id of the root item (not displayed in the UI)
5255
root_id: ItemId,
5356

57+
/// Selected item, if any
58+
selected_item: Option<ItemId>,
59+
5460
/// If a drag is ongoing, this is the id of the destination container (if any was identified)
5561
///
5662
/// This is used to highlight the target container.
@@ -73,6 +79,7 @@ impl Default for HierarchicalDragAndDrop {
7379
let mut res = Self {
7480
items: std::iter::once((root_id, root_item)).collect(),
7581
root_id,
82+
selected_item: None,
7683
target_container: None,
7784
command_receiver,
7885
command_sender,
@@ -235,12 +242,25 @@ impl HierarchicalDragAndDrop {
235242
self.container_children_ui(ui, top_level_items);
236243
}
237244

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+
238257
// always reset the target container
239258
self.target_container = None;
240259

241260
while let Ok(command) = self.command_receiver.try_recv() {
242-
//println!("Received command: {command:?}");
261+
println!("Received command: {command:?}");
243262
match command {
263+
Command::SetSelectedItem(item_id) => self.selected_item = item_id,
244264
Command::MoveItem {
245265
moved_item_id,
246266
target_container_id,
@@ -264,13 +284,25 @@ impl HierarchicalDragAndDrop {
264284
ui.add(
265285
egui::Label::new(format!("Container {item_id:?}"))
266286
.selectable(false)
267-
.sense(egui::Sense::drag()),
287+
.sense(egui::Sense::click_and_drag()),
268288
)
269289
})
270290
.body(|ui| {
271291
self.container_children_ui(ui, children);
272292
});
273293

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+
274306
self.handle_drag_and_drop_interaction(
275307
ui,
276308
item_id,
@@ -282,6 +314,13 @@ impl HierarchicalDragAndDrop {
282314

283315
fn container_children_ui(&self, ui: &mut egui::Ui, children: &Vec<ItemId>) {
284316
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+
285324
match self.items.get(child_id) {
286325
Some(Item::Container(children)) => {
287326
self.container_ui(ui, *child_id, children);
@@ -298,9 +337,13 @@ impl HierarchicalDragAndDrop {
298337
let response = ui.add(
299338
egui::Label::new(label)
300339
.selectable(false)
301-
.sense(egui::Sense::drag()),
340+
.sense(egui::Sense::click_and_drag()),
302341
);
303342

343+
if response.clicked() {
344+
self.send_command(Command::SetSelectedItem(Some(item_id)));
345+
}
346+
304347
self.handle_drag_and_drop_interaction(ui, item_id, false, &response, None);
305348
}
306349

@@ -318,6 +361,9 @@ impl HierarchicalDragAndDrop {
318361

319362
if response.drag_started() {
320363
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)));
321367
}
322368

323369
//

0 commit comments

Comments
 (0)