-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for drag-and-drop in blueprint tree #4910
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
990e421
WIP
abey79 b73a8cb
First working drag and drop!! :tada:
abey79 73efd74
`ListItem`: don't show icons when a drag is ongoing
abey79 3b6597b
Highlight receiving container
abey79 b763458
Lint
abey79 cfb13a2
Merge branch 'main' into antoine/blueprint_dnd
abey79 d37f60b
Update egui_tile commit ref
abey79 e8cfa30
Fixed self-drag bug + cargo bug from main
abey79 747d218
Merge branch 'main' into antoine/blueprint_dnd
abey79 9a967a2
Back-port `re_ui::drag_and_drop::find_drop_target` to `re_ui_example`
abey79 9146345
Updated to last egui_tile + minor code improvement
abey79 dea19c9
Accept drag in the empty space bellow the tree
abey79 88c36e0
Merge branch 'main' into antoine/blueprint_dnd
abey79 492b7c5
Post merge fixes
abey79 59489b1
WIP
abey79 128add0
Fix stuff due to egui bump
abey79 39adb17
Update the dnd demo to the new APIs
abey79 12ccfb1
Update the dnd demo to the new APIs
abey79 82c187c
Merge branch 'main' into antoine/dnd_better_egui_api
abey79 21073db
Remove useless code
abey79 6478006
Improved docstring
abey79 4536b47
Unneeded whitespace
abey79 75749e0
Bump egui commit to today's master
abey79 de957fb
Merge branch 'main' into antoine/blueprint_dnd
abey79 cb3a755
Fix lint
abey79 628b0c9
Update crates/re_ui/src/drag_and_drop.rs
abey79 8c0d55a
Clarified `find_drop_target` docstring and improved its signature
abey79 406e5d7
Merge branch 'main' into antoine/blueprint_dnd
abey79 85d31a9
Review comments: `ListItem` code clean-up
abey79 8dc3aca
Review comments: highlighted container clarifications
abey79 e4b47c0
Update crates/re_viewport/src/viewport.rs
abey79 957f8e8
Merge branch 'main' into antoine/blueprint_dnd
abey79 2040ab0
Post merge
abey79 3abcb57
Post merge proper
abey79 7f57f40
Merge branch 'main' into antoine/blueprint_dnd
abey79 b4084fe
Rolled back some `ListItem` changes that triggered unwanted interactions
abey79 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,320 @@ | ||
//! Helpers for drag and drop support for reordering hierarchical lists. | ||
//! | ||
//! Works well in combination with [`crate::list_item::ListItem`]. | ||
|
||
/// 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, | ||
|
||
/// Can this item "contain" the currently dragged item? | ||
pub is_container: bool, | ||
|
||
/// ID of the parent of this item. | ||
pub parent_id: ItemId, | ||
|
||
/// Position of this item within its parent. | ||
pub position_index_in_parent: usize, | ||
|
||
/// 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 ╠══ ══╣ │ | ||
/// │ ║ ║ │ | ||
/// ┌─▼─── ║ ║ │ | ||
/// │ ║ ║ │ | ||
/// └───── ╚═════════════════════════════════════════════╝ ─┘ | ||
/// ``` | ||
/// | ||
/// 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 all four cases. | ||
/// - 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 is_container = item_context.is_container; | ||
let parent_id = item_context.parent_id; | ||
let pos_in_parent = item_context.position_index_in_parent; | ||
|
||
// For both leaf and containers we have two drag zones on the upper half of the item. | ||
let (top, mut bottom) = item_rect.split_top_bottom_at_fraction(0.5); | ||
let (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 item, 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_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 item_context.is_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", | ||
); | ||
} | ||
} | ||
|
||
/* ===== 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, | ||
pos_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, | ||
pos_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, | ||
pos_in_parent + 1, | ||
)) | ||
} | ||
/* ==== BOTTOM SECTIONS (leaf item) ==== */ | ||
else if !is_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, | ||
pos_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, | ||
pos_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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like that this is a completely separate module, with no deps. We could consider moving it to
egui
; then it could be used for emilk/egui#3903 as wellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's kind of what I had in mind. I would wait a little bit though and see how it crystallise in the short/mid term.