diff --git a/crates/egui/src/ui.rs b/crates/egui/src/ui.rs index 715f0780..4e545e9a 100644 --- a/crates/egui/src/ui.rs +++ b/crates/egui/src/ui.rs @@ -2814,7 +2814,14 @@ impl Ui { /// /// Returns the dropped item, if it was released this frame. /// - /// The given frame is used for its margins, but the color is ignored. + /// The margins, corner radius and shadow of the given frame are always used. + /// + /// When nothing is being dragged, the frame's own fill and stroke are kept, + /// unless the frame has no styling of its own (transparent fill and no visible stroke, + /// e.g. [`Frame::default`]), in which case the inactive widget style is used, + /// so the drop zone is still visible. + /// + /// During a drag, the fill and stroke are always replaced with the drop target visuals. #[doc(alias = "drag and drop")] pub fn dnd_drop_zone( &mut self, @@ -2832,29 +2839,31 @@ impl Ui { let inner = add_contents(&mut frame.content_ui); let response = frame.allocate_space(self); - // 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() - { - self.visuals().widgets.active - } else { - self.visuals().widgets.inactive - }; + let has_own_styling = + frame.frame.fill != Color32::TRANSPARENT || !frame.frame.stroke.is_empty(); - let mut fill = style.bg_fill; - let mut stroke = style.bg_stroke; + if is_anything_being_dragged || !has_own_styling { + // NOTE: we use `response.contains_pointer` here instead of `hovered`, because + // `hovered` is always false when another widget is being dragged. + let style = if can_accept_what_is_being_dragged && response.contains_pointer() { + self.visuals().widgets.active + } else { + self.visuals().widgets.inactive + }; - if is_anything_being_dragged && !can_accept_what_is_being_dragged { - // When dragging something else, show that it can't be dropped here: - fill = self.visuals().disable(fill); - stroke.color = self.visuals().disable(stroke.color); + 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 = self.visuals().disable(fill); + stroke.color = self.visuals().disable(stroke.color); + } + + frame.frame.fill = fill; + frame.frame.stroke = stroke; } - frame.frame.fill = fill; - frame.frame.stroke = stroke; - frame.paint(self); let payload = response.dnd_release_payload::(); diff --git a/crates/egui/tests/drop_zone.rs b/crates/egui/tests/drop_zone.rs new file mode 100644 index 00000000..85f586c0 --- /dev/null +++ b/crates/egui/tests/drop_zone.rs @@ -0,0 +1,150 @@ +use egui::{ + Button, Color32, Context, DragAndDrop, Event, Frame, Modifiers, PointerButton, RawInput, Rect, + Stroke, Ui, epaint::ClippedShape, pos2, vec2, +}; + +#[derive(Debug, PartialEq)] +struct Output { + shapes: Vec, + content: Rect, + zone: Rect, + sibling: Rect, + payload: Option, +} + +fn show(ctx: &Context, frame: Frame, drop_zone: bool, events: Vec) -> Output { + let mut result = Output { + shapes: Vec::new(), + content: Rect::NOTHING, + zone: Rect::NOTHING, + sibling: Rect::NOTHING, + payload: None, + }; + let output = ctx.run_ui( + RawInput { + screen_rect: Some(Rect::from_min_size(pos2(0.0, 0.0), vec2(400.0, 300.0))), + events, + ..Default::default() + }, + |ui| { + let contents = |ui: &mut Ui| { + ui.add_sized(vec2(40.0, 20.0), Button::new("")); + ui.min_rect() + }; + let response = if drop_zone { + let (response, payload) = ui.dnd_drop_zone::(frame, contents); + result.payload = payload.as_deref().copied(); + response + } else { + frame.show(ui, contents) + }; + result.content = response.inner; + result.zone = response.response.rect; + result.sibling = ui.add_sized(vec2(40.0, 20.0), Button::new("")).rect; + }, + ); + result.shapes = output.shapes.clone(); + output.drop_without_applying_deltas(); + result +} + +fn custom_frame() -> Frame { + Frame::NONE + .inner_margin(8) + .outer_margin(3) + .fill(Color32::RED) + .stroke(Stroke::new(3.0, Color32::GREEN)) + .corner_radius(7) + .shadow(egui::epaint::Shadow { + offset: [2, 3], + blur: 4, + spread: 1, + color: Color32::BLACK, + }) +} + +#[test] +fn idle_drop_zone_preserves_frame_and_contents() { + let frame = custom_frame(); + let plain = Context::default(); + let drop_zone = Context::default(); + for _ in 0..3 { + assert_eq!( + show(&plain, frame, false, vec![]), + show(&drop_zone, frame, true, vec![]), + ); + } +} + +#[test] +fn idle_drop_zone_without_styling_uses_inactive_style() { + for frame in [Frame::NONE, Frame::default().inner_margin(4.0)] { + let ctx = Context::default(); + for _ in 0..3 { + let output = show(&ctx, frame, true, vec![]); + let inactive = ctx.style_of(ctx.theme()).visuals.widgets.inactive; + let expected = frame.fill(inactive.bg_fill).stroke(inactive.bg_stroke); + assert!( + output + .shapes + .iter() + .any(|s| s.shape == expected.paint(output.content)) + ); + } + } +} + +#[test] +fn drop_zone_drag_feedback_and_release() { + for accepted in [true, false] { + let ctx = Context::default(); + let frame = custom_frame(); + let idle = show(&ctx, frame, true, vec![]); + let inside = idle.zone.min + vec2(4.0, 4.0); + let outside = pos2(300.0, 200.0); + if accepted { + DragAndDrop::set_payload(&ctx, 42_u32); + } else { + DragAndDrop::set_payload(&ctx, "wrong type"); + } + for pointer in [outside, inside, outside, inside] { + let output = show(&ctx, frame, true, vec![Event::PointerMoved(pointer)]); + let visuals = ctx.style_of(ctx.theme()).visuals.clone(); + let style = if accepted && pointer == inside { + visuals.widgets.active + } else { + visuals.widgets.inactive + }; + let mut expected = frame.fill(style.bg_fill).stroke(style.bg_stroke); + if !accepted { + expected.fill = visuals.disable(expected.fill); + expected.stroke.color = visuals.disable(expected.stroke.color); + } + assert!( + output + .shapes + .iter() + .any(|s| s.shape == expected.paint(output.content)) + ); + assert_eq!(output.content, idle.content); + assert_eq!(output.zone, idle.zone); + assert_eq!(output.sibling, idle.sibling); + assert_eq!(output.payload, None); + } + let release = show( + &ctx, + frame, + true, + vec![Event::PointerButton { + pos: inside, + button: PointerButton::Primary, + pressed: false, + modifiers: Modifiers::NONE, + }], + ); + assert_eq!(release.payload, accepted.then_some(42)); + assert!(!DragAndDrop::has_any_payload(&ctx)); + let restored = show(&ctx, frame, true, vec![Event::PointerGone]); + assert_eq!(restored, idle); + } +} diff --git a/tests/egui_tests/tests/snapshots/drop_zone_styling_dragging.png b/tests/egui_tests/tests/snapshots/drop_zone_styling_dragging.png new file mode 100644 index 00000000..ffd37ba7 --- /dev/null +++ b/tests/egui_tests/tests/snapshots/drop_zone_styling_dragging.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3de40d92a38bd18fba4d0cd5982e94287f840c52132dd99825c5f736e27f556c +size 5820 diff --git a/tests/egui_tests/tests/snapshots/drop_zone_styling_idle.png b/tests/egui_tests/tests/snapshots/drop_zone_styling_idle.png new file mode 100644 index 00000000..b694591f --- /dev/null +++ b/tests/egui_tests/tests/snapshots/drop_zone_styling_idle.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:86ef099b218f6f36fc508cb3e4e9a51646243b4fa5b10123664a6c20f0233fc8 +size 5174 diff --git a/tests/egui_tests/tests/test_drop_zone.rs b/tests/egui_tests/tests/test_drop_zone.rs new file mode 100644 index 00000000..6b01afed --- /dev/null +++ b/tests/egui_tests/tests/test_drop_zone.rs @@ -0,0 +1,53 @@ +use egui::{Color32, Frame, Id, Stroke, Vec2}; +use egui_kittest::{Harness, SnapshotResults, kittest::Queryable as _}; + +/// Shows how [`egui::Ui::dnd_drop_zone`] paints its frame: +/// +/// * An unstyled frame (transparent fill, no stroke) falls back to the inactive widget style. +/// * A styled frame keeps its own fill and stroke while idle. +/// * During a drag, the zone under the pointer is highlighted and the others use the inactive style. +#[test] +fn drop_zone_styling() { + let styled_frame = Frame::NONE + .inner_margin(8) + .fill(Color32::from_rgb(40, 60, 110)) + .stroke(Stroke::new(2.0, Color32::from_rgb(240, 170, 40))) + .corner_radius(8); + + let mut harness = Harness::builder().build_ui(|ui| { + ui.dnd_drag_source(Id::unique("drag_source"), 42_u32, |ui| { + ui.label("Drag me"); + }); + ui.horizontal(|ui| { + ui.dnd_drop_zone::(Frame::default().inner_margin(4.0), |ui| { + ui.set_min_size(Vec2::new(100.0, 50.0)); + ui.label("Unstyled frame"); + }); + ui.dnd_drop_zone::(styled_frame, |ui| { + ui.set_min_size(Vec2::new(100.0, 50.0)); + ui.label("Styled frame"); + }); + }); + }); + + let mut results = SnapshotResults::new(); + + harness.fit_contents(); + harness.run(); + results.add(harness.try_snapshot("drop_zone_styling_idle")); + + // Grab the drag source at its left edge, and hover the upper part of the styled zone, + // so the dragged label doesn't cover the zone's own label: + let source = harness.get_by_label("Drag me").rect(); + let from = source.left_center() + Vec2::new(2.0, 0.0); + let zone_label = harness.get_by_label("Styled frame").rect(); + let to = zone_label.center_top() - Vec2::new(0.0, 17.0); + harness.drag_at(from); + harness.run_steps(2); + harness.hover_at(to); + harness.run_steps(2); + results.add(harness.try_snapshot("drop_zone_styling_dragging")); + + harness.drop_at(to); + harness.run(); +}