Skip to content

Commit

Permalink
Clean up and add visualize parameter for drag and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
guusw committed Nov 23, 2023
1 parent 58c79ae commit 4e3f8bb
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions shards/egui/src/dnd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub fn drop_target<R>(
ui: &mut egui::Ui,
ctx: &UIContext,
can_accept_what_is_being_dragged: bool,
visualize: bool,
body: impl FnOnce(&mut egui::Ui) -> R,
) -> InnerResponse<R> {
let is_being_dragged = ui.memory(|mem| mem.is_anything_being_dragged());
Expand All @@ -86,7 +87,7 @@ pub fn drop_target<R>(

let outer_rect_bounds = ui.available_rect_before_wrap();
let inner_rect = outer_rect_bounds.shrink2(margin);
// let where_to_put_background = ui.painter().add(Shape::Noop);
let where_to_put_background = ui.painter().add(Shape::Noop);
let mut content_ui = ui.child_ui(inner_rect, *ui.layout());
let ret = body(&mut content_ui);
let outer_rect = Rect::from_min_max(outer_rect_bounds.min, content_ui.min_rect().max + margin);
Expand Down Expand Up @@ -117,20 +118,22 @@ pub fn drop_target<R>(
};
}

// let mut stroke = Stroke {
// color: stroke_color.into(),
// ..style.bg_stroke
// };

// ui.painter().set(
// where_to_put_background,
// epaint::RectShape {
// rect,
// rounding: style.rounding,
// stroke,
// fill: fill.into(),
// },
// );
if visualize {
let mut stroke = Stroke {
color: stroke_color.into(),
..style.bg_stroke
};

ui.painter().set(
where_to_put_background,
epaint::RectShape {
rect,
rounding: style.rounding,
stroke,
fill: fill.into(),
},
);
}
}

InnerResponse::new(ret, response)
Expand Down Expand Up @@ -163,6 +166,8 @@ struct DragDrop {
STRING_VAR_OR_NONE_SLICE
)]
id: ParamVar,
#[shard_param("Visualize", "Visualize valid drop targets", BOOL_OR_NONE_SLICE)]
visualize: ClonedVar,
#[shard_warmup]
contexts: ParamVar,
#[shard_warmup]
Expand All @@ -186,6 +191,7 @@ impl Default for DragDrop {
parents: ParamVar::new_named(PARENTS_UI_NAME),
required: ExposedTypes::new(),
inner_exposed: ExposedTypes::new(),
visualize: ClonedVar::default(),
payload: None,
prev_size: Vec2::ZERO,
}
Expand Down Expand Up @@ -284,7 +290,9 @@ impl Shard for DragDrop {
true
};

let inner = drop_target(ui, ui_ctx, accepts_value, |ui| {
let visualize = TryInto::<bool>::try_into(&self.visualize.0).unwrap_or(true);

let inner = drop_target(ui, ui_ctx, accepts_value, visualize, |ui| {
util::activate_ui_contents(context, input, ui, &mut self.parents, &mut self.contents)
});

Expand All @@ -308,8 +316,9 @@ impl Shard for DragDrop {
inner.inner?
} else {
let id = ui.id().with("dragdrop");
let inner = drag_source(ui, ui_ctx,&input, self.prev_size, id, |ui| {
let r = util::activate_ui_contents(context, input, ui, &mut self.parents, &mut self.contents);
let inner = drag_source(ui, ui_ctx, &input, self.prev_size, id, |ui| {
let r =
util::activate_ui_contents(context, input, ui, &mut self.parents, &mut self.contents);
self.prev_size = ui.min_size();
r
});
Expand Down

0 comments on commit 4e3f8bb

Please sign in to comment.