Skip to content

Commit

Permalink
Move demo code around a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 25, 2024
1 parent 17877fa commit e1a7b32
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions crates/egui_demo_lib/src/demo/drag_and_drop.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,5 @@
use egui::*;

fn drop_zone<R>(
ui: &mut Ui,
can_accept_what_is_being_dragged: bool,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
let is_anything_being_dragged = DragAndDrop::has_any_payload(ui.ctx());

let frame = Frame::default().inner_margin(4.0);
let mut frame = frame.begin(ui);
let ret = add_contents(&mut frame.content_ui);
let response = frame.allocate_space(ui);

// NOTE: we use `response.contains_pointer` here instead of `hovered`, because
// `hovered` is always false when another widget is being dragged.
let style = if is_anything_being_dragged
&& can_accept_what_is_being_dragged
&& response.contains_pointer()
{
ui.visuals().widgets.active
} else {
ui.visuals().widgets.inactive
};

let mut fill = style.bg_fill;
let mut stroke = style.bg_stroke;

if is_anything_being_dragged && !can_accept_what_is_being_dragged {
// When dragging something else, show that it can't be dropped here.
fill = ui.visuals().gray_out(fill);
stroke.color = ui.visuals().gray_out(stroke.color);
}

frame.frame.fill = fill;
frame.frame.stroke = stroke;

frame.paint(ui);

InnerResponse::new(ret, response)
}

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct DragAndDropDemo {
Expand Down Expand Up @@ -120,3 +80,44 @@ impl super::View for DragAndDropDemo {
});
}
}

/// Paint a drop-zone which changes colors when you can drop something onto it.
fn drop_zone<R>(
ui: &mut Ui,
can_accept_what_is_being_dragged: bool,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
let is_anything_being_dragged = DragAndDrop::has_any_payload(ui.ctx());

let frame = Frame::default().inner_margin(4.0);
let mut frame = frame.begin(ui);
let ret = add_contents(&mut frame.content_ui);
let response = frame.allocate_space(ui);

// NOTE: we use `response.contains_pointer` here instead of `hovered`, because
// `hovered` is always false when another widget is being dragged.
let style = if is_anything_being_dragged
&& can_accept_what_is_being_dragged
&& response.contains_pointer()
{
ui.visuals().widgets.active
} else {
ui.visuals().widgets.inactive
};

let mut fill = style.bg_fill;
let mut stroke = style.bg_stroke;

if is_anything_being_dragged && !can_accept_what_is_being_dragged {
// When dragging something else, show that it can't be dropped here.
fill = ui.visuals().gray_out(fill);
stroke.color = ui.visuals().gray_out(stroke.color);
}

frame.frame.fill = fill;
frame.frame.stroke = stroke;

frame.paint(ui);

InnerResponse::new(ret, response)
}

0 comments on commit e1a7b32

Please sign in to comment.