From 3cc6cdc96663704c44bf8afe422ca7cbb9b62241 Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Thu, 11 Jul 2024 19:33:24 +0200 Subject: [PATCH] Fix egui_dnd in centered / justified layouts --- .../examples/vertical_centered_justified.rs | 23 +++++++++++++++++++ crates/egui_dnd/src/item.rs | 10 +++++--- 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 crates/egui_dnd/examples/vertical_centered_justified.rs diff --git a/crates/egui_dnd/examples/vertical_centered_justified.rs b/crates/egui_dnd/examples/vertical_centered_justified.rs new file mode 100644 index 0000000..d69afc4 --- /dev/null +++ b/crates/egui_dnd/examples/vertical_centered_justified.rs @@ -0,0 +1,23 @@ +use eframe::egui; +use egui::{Button, CentralPanel, Widget}; +use egui_dnd::dnd; + +pub fn main() -> eframe::Result<()> { + let mut items = vec!["alfred", "bernhard", "christian"]; + + eframe::run_simple_native( + "DnD Simple Example", + Default::default(), + move |ctx, _frame| { + CentralPanel::default().show(ctx, |ui| { + ui.vertical_centered_justified(|ui| { + dnd(ui, "dnd_example").show_vec(&mut items, |ui, item, handle, state| { + handle.ui(ui, |ui| { + Button::new(&**item).selected(state.dragged).ui(ui); + }); + }); + }); + }); + }, + ) +} diff --git a/crates/egui_dnd/src/item.rs b/crates/egui_dnd/src/item.rs index a181dd2..b489936 100644 --- a/crates/egui_dnd/src/item.rs +++ b/crates/egui_dnd/src/item.rs @@ -1,4 +1,4 @@ -use egui::{CursorIcon, Id, InnerResponse, LayerId, Order, Pos2, Rect, Sense, Ui, Vec2}; +use egui::{CursorIcon, Id, InnerResponse, LayerId, Layout, Order, Pos2, Rect, Sense, Ui, Vec2}; use egui_animation::animate_position; use crate::state::DragDetectionState; @@ -92,6 +92,7 @@ impl<'a> Item<'a> { position, hovering_over_any_handle, size, + *ui.layout(), drag_body, ); @@ -130,6 +131,7 @@ impl<'a> Item<'a> { position, hovering_over_any_handle, size, + *ui.layout(), drag_body, ); @@ -189,7 +191,8 @@ impl<'a> Item<'a> { rect } else { - let position = ui.next_widget_position(); + let position = ui.cursor().min; + let animated_position = animate_position( ui, id, @@ -247,6 +250,7 @@ impl<'a> Item<'a> { pos: Pos2, hovering_over_any_handle: &mut bool, size: Option, + layout: Layout, body: impl FnOnce(&mut Ui, Handle, ItemState), ) -> InnerResponse { egui::Area::new(Id::new("draggable_item")) @@ -254,7 +258,7 @@ impl<'a> Item<'a> { .fixed_pos(pos) .order(Order::Tooltip) .show(ui.ctx(), |ui| { - ui.scope(|ui| { + ui.with_layout(layout, |ui| { if let Some(size) = size.or(dnd_state.detection_state.dragged_item_size()) { ui.set_max_size(size); }