Skip to content

Commit

Permalink
Fix egui_dnd in centered / justified layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Jul 11, 2024
1 parent 0d642ee commit 3cc6cdc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
23 changes: 23 additions & 0 deletions crates/egui_dnd/examples/vertical_centered_justified.rs
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
});
},
)
}
10 changes: 7 additions & 3 deletions crates/egui_dnd/src/item.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -92,6 +92,7 @@ impl<'a> Item<'a> {
position,
hovering_over_any_handle,
size,
*ui.layout(),
drag_body,
);

Expand Down Expand Up @@ -130,6 +131,7 @@ impl<'a> Item<'a> {
position,
hovering_over_any_handle,
size,
*ui.layout(),
drag_body,
);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -247,14 +250,15 @@ impl<'a> Item<'a> {
pos: Pos2,
hovering_over_any_handle: &mut bool,
size: Option<Vec2>,
layout: Layout,
body: impl FnOnce(&mut Ui, Handle, ItemState),
) -> InnerResponse<Rect> {
egui::Area::new(Id::new("draggable_item"))
.interactable(false)
.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);
}
Expand Down

0 comments on commit 3cc6cdc

Please sign in to comment.