use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc};
use sui_core::{
DragDropScope, DragEvent, DragEventKind, DragPayload, DragPreview, DragSessionId, DropEffect,
Event, Point, PointerButton, PointerEventKind, Rect, Size, Vector, WindowEvent,
};
use sui_layout::Constraints;
use sui_runtime::{
ArrangeCtx, EventCtx, LayerOptions, MeasureCtx, OverlayDismissPolicy, OverlayFocusBehavior,
OverlayKind, OverlayOptions, PaintBoundaryMode, PaintCtx, SemanticsCtx, SingleChild,
StackSurfaceOptions, Widget, WidgetPod, WidgetPodMutVisitor, WidgetPodVisitor,
};
use sui_scene::{Border, LayerCompositionMode};
use crate::DefaultTheme;
const DEFAULT_DRAG_THRESHOLD: f32 = 4.0;
type PayloadFactory = Box<dyn FnMut() -> DragPayload>;
type DragStartCallback = Box<dyn FnMut(&mut EventCtx, &DragPreview)>;
type DragEndCallback = Box<dyn FnMut(&mut EventCtx, &DragEvent)>;
type DropAcceptCallback = Box<dyn FnMut(&DragEvent) -> DropEffect>;
type DropCallback = Box<dyn FnMut(&mut EventCtx, &DragEvent)>;
type HoverCallback = Box<dyn FnMut(bool)>;
type ThemeReader = Rc<dyn Fn() -> DefaultTheme>;
#[derive(Default)]
struct DragPreviewThemeSource {
theme: DefaultTheme,
reader: Option<ThemeReader>,
}
impl DragPreviewThemeSource {
fn resolve(&self) -> DefaultTheme {
self.reader
.as_ref()
.map(|reader| reader())
.unwrap_or(self.theme)
}
}
pub struct DragDropHost {
scope: DragDropScope,
child: SingleChild,
overlay: SingleChild,
preview_theme: Rc<RefCell<DragPreviewThemeSource>>,
external_hovered: Vec<PathBuf>,
on_external_hover: Option<Box<dyn FnMut(&mut EventCtx, &[PathBuf])>>,
on_external_drop: Option<Box<dyn FnMut(&mut EventCtx, PathBuf)>>,
on_external_cancel: Option<Box<dyn FnMut(&mut EventCtx)>>,
}
impl DragDropHost {
pub fn new<W>(scope: DragDropScope, child: W) -> Self
where
W: Widget + 'static,
{
let preview_theme = Rc::new(RefCell::new(DragPreviewThemeSource::default()));
Self {
overlay: SingleChild::new(DragPreviewOverlay::new(
scope.clone(),
Rc::clone(&preview_theme),
)),
scope,
child: SingleChild::new(child),
preview_theme,
external_hovered: Vec::new(),
on_external_hover: None,
on_external_drop: None,
on_external_cancel: None,
}
}
pub fn scope(&self) -> &DragDropScope {
&self.scope
}
pub fn child(&self) -> &WidgetPod {
self.child.child()
}
pub fn child_mut(&mut self) -> &mut WidgetPod {
self.child.child_mut()
}
pub fn external_hovered_files(&self) -> &[PathBuf] {
&self.external_hovered
}
pub fn theme(self, theme: DefaultTheme) -> Self {
{
let mut source = self.preview_theme.borrow_mut();
source.theme = theme;
source.reader = None;
}
self
}
pub fn theme_when<F>(self, reader: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.preview_theme.borrow_mut().reader = Some(Rc::new(reader));
self
}
pub fn on_external_file_hover<F>(mut self, callback: F) -> Self
where
F: FnMut(&mut EventCtx, &[PathBuf]) + 'static,
{
self.on_external_hover = Some(Box::new(callback));
self
}
pub fn on_external_file_drop<F>(mut self, callback: F) -> Self
where
F: FnMut(&mut EventCtx, PathBuf) + 'static,
{
self.on_external_drop = Some(Box::new(callback));
self
}
pub fn on_external_file_hover_cancelled<F>(mut self, callback: F) -> Self
where
F: FnMut(&mut EventCtx) + 'static,
{
self.on_external_cancel = Some(Box::new(callback));
self
}
}
impl Widget for DragDropHost {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
match event {
Event::Window(WindowEvent::ExternalFileHovered(path)) => {
if !self.external_hovered.contains(path) {
self.external_hovered.push(path.clone());
}
if let Some(callback) = &mut self.on_external_hover {
callback(ctx, &self.external_hovered);
}
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
Event::Window(WindowEvent::ExternalFileHoverCancelled) => {
self.external_hovered.clear();
if let Some(callback) = &mut self.on_external_cancel {
callback(ctx);
}
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
Event::Window(WindowEvent::ExternalFileDropped(path)) => {
self.external_hovered.retain(|hovered| hovered != path);
if let Some(callback) = &mut self.on_external_drop {
callback(ctx, path.clone());
}
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
_ => {}
}
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let size = self.child.measure(ctx, constraints);
self.overlay.measure(ctx, Constraints::tight(size));
size
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.child.arrange(ctx, bounds);
self.overlay.arrange(ctx, bounds);
}
fn paint(&self, ctx: &mut PaintCtx) {
self.child.paint(ctx);
self.overlay.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.child.semantics(ctx);
}
fn overlay_options(&self) -> Option<OverlayOptions> {
self.scope.active_drag().is_some().then_some(
OverlayOptions::new(OverlayKind::DragPreview)
.dismiss(OverlayDismissPolicy::NONE)
.focus(OverlayFocusBehavior::NONE),
)
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
self.overlay.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
self.overlay.visit_children_mut(visitor);
}
}
struct DragPreviewOverlay {
scope: DragDropScope,
theme: Rc<RefCell<DragPreviewThemeSource>>,
}
impl DragPreviewOverlay {
fn new(scope: DragDropScope, theme: Rc<RefCell<DragPreviewThemeSource>>) -> Self {
Self { scope, theme }
}
fn resolved_theme(&self) -> DefaultTheme {
self.theme.borrow().resolve()
}
}
impl Widget for DragPreviewOverlay {
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
constraints.clamp(Size::new(
constraints.max.width.max(0.0),
constraints.max.height.max(0.0),
))
}
fn paint(&self, ctx: &mut PaintCtx) {
let Some(active) = self.scope.active_drag() else {
return;
};
let label = drag_preview_label(&active);
let theme = self.resolved_theme();
let text_style = drag_preview_text_style(theme);
let estimated_character_width = text_style.font_size * 0.54;
let width =
((label.chars().count() as f32 * estimated_character_width) + 24.0).clamp(48.0, 260.0);
let height = text_style.line_height + 12.0;
let offset = Vector::new(12.0, 12.0);
let bounds = ctx.bounds();
let x = (active.position.x + offset.x).min((bounds.max_x() - width).max(bounds.x()));
let y = (active.position.y + offset.y).min((bounds.max_y() - height).max(bounds.y()));
let rect = Rect::new(x.max(bounds.x()), y.max(bounds.y()), width, height);
ctx.fill_rrect_bordered(
rect,
[7.0; 4],
theme.palette.surface_raised.with_alpha(0.96),
Border {
width: 1.0,
color: theme.palette.border.with_alpha(0.72),
},
);
let text_rect = Rect::new(
rect.x() + 12.0,
rect.y() + 6.0,
rect.width() - 24.0,
text_style.line_height,
);
ctx.push_clip_rect(text_rect);
ctx.draw_text(text_rect, label, text_style);
ctx.pop_clip();
}
fn layer_options(&self) -> LayerOptions {
LayerOptions {
paint_boundary: PaintBoundaryMode::Explicit,
composition_mode: LayerCompositionMode::Overlay,
}
}
fn stack_surface_options(&self) -> Option<StackSurfaceOptions> {
Some(StackSurfaceOptions {
transient: true,
hit_test: false,
..StackSurfaceOptions::default()
})
}
}
fn drag_preview_text_style(theme: DefaultTheme) -> sui_text::TextStyle {
let mut style = theme.text_style(theme.palette.text);
style.font_size = theme.text.sm.size.max(1.0);
style.line_height = theme.text.sm.line_height.max(1.0);
style
}
pub struct Draggable {
scope: DragDropScope,
child: SingleChild,
payload: PayloadFactory,
allowed_effect: DropEffect,
preview_label: Option<String>,
threshold: f32,
press: Option<DragPress>,
active_session: Option<DragSessionId>,
on_drag_start: Option<DragStartCallback>,
on_drag_end: Option<DragEndCallback>,
}
#[derive(Debug, Clone, Copy)]
struct DragPress {
pointer_id: u64,
start_position: Point,
}
impl Draggable {
pub fn new<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self {
scope: DragDropScope::new(),
child: SingleChild::new(child),
payload: Box::new(|| DragPayload::text("")),
allowed_effect: DropEffect::Move,
preview_label: None,
threshold: DEFAULT_DRAG_THRESHOLD,
press: None,
active_session: None,
on_drag_start: None,
on_drag_end: None,
}
}
pub fn scope(mut self, scope: DragDropScope) -> Self {
self.scope = scope;
self
}
pub fn payload<F>(mut self, payload: F) -> Self
where
F: FnMut() -> DragPayload + 'static,
{
self.payload = Box::new(payload);
self
}
pub fn effect(mut self, effect: DropEffect) -> Self {
self.allowed_effect = effect;
self
}
pub fn preview_label(mut self, label: impl Into<String>) -> Self {
self.preview_label = Some(label.into());
self
}
pub fn threshold(mut self, threshold: f32) -> Self {
self.threshold = threshold.max(0.0);
self
}
pub fn on_drag_start<F>(mut self, callback: F) -> Self
where
F: FnMut(&mut EventCtx, &DragPreview) + 'static,
{
self.on_drag_start = Some(Box::new(callback));
self
}
pub fn on_drag_end<F>(mut self, callback: F) -> Self
where
F: FnMut(&mut EventCtx, &DragEvent) + 'static,
{
self.on_drag_end = Some(Box::new(callback));
self
}
pub fn scope_ref(&self) -> &DragDropScope {
&self.scope
}
pub fn child(&self) -> &WidgetPod {
self.child.child()
}
pub fn child_mut(&mut self) -> &mut WidgetPod {
self.child.child_mut()
}
fn start_drag(&mut self, ctx: &mut EventCtx, press: DragPress, position: Point) {
let payload = (self.payload)();
let preview_label = self.preview_label.clone();
let session_id = ctx.begin_drag(
self.scope.id(),
press.pointer_id,
press.start_position,
payload.clone(),
self.allowed_effect,
preview_label.clone(),
);
let preview = DragPreview {
session_id,
scope_id: self.scope.id(),
pointer_id: press.pointer_id,
source: ctx.widget_id(),
position,
start_position: press.start_position,
payload,
allowed_effect: self.allowed_effect,
preview_label: preview_label.map(Arc::from),
};
self.scope.set_active_drag(preview.clone());
self.active_session = Some(session_id);
if let Some(callback) = &mut self.on_drag_start {
callback(ctx, &preview);
}
ctx.request_paint();
}
}
impl Widget for Draggable {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
match event {
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Down
&& pointer.button == Some(PointerButton::Primary)
&& ctx.bounds().contains(pointer.position)
&& ctx.phase() != sui_runtime::EventPhase::Capture =>
{
self.press = Some(DragPress {
pointer_id: pointer.pointer_id,
start_position: pointer.position,
});
self.active_session = None;
ctx.request_pointer_capture(pointer.pointer_id);
ctx.set_handled();
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Move
&& self
.press
.is_some_and(|press| press.pointer_id == pointer.pointer_id) =>
{
if let Some(session_id) = self.active_session {
self.scope
.update_drag_position(session_id, pointer.position);
ctx.request_paint();
ctx.set_handled();
return;
}
let press = self.press.unwrap();
let delta = pointer.position - press.start_position;
let distance_sq = (delta.x * delta.x) + (delta.y * delta.y);
if distance_sq >= self.threshold * self.threshold {
self.start_drag(ctx, press, pointer.position);
ctx.set_handled();
}
}
Event::Pointer(pointer)
if matches!(
pointer.kind,
PointerEventKind::Up | PointerEventKind::Cancel
) && self
.press
.is_some_and(|press| press.pointer_id == pointer.pointer_id) =>
{
if self.active_session.is_none() {
self.press = None;
}
ctx.release_pointer_capture(pointer.pointer_id);
ctx.set_handled();
}
Event::Drag(drag)
if drag.kind == DragEventKind::End
&& self.active_session == Some(drag.session_id) =>
{
self.scope.finish_drag(drag.session_id);
self.press = None;
self.active_session = None;
if let Some(callback) = &mut self.on_drag_end {
callback(ctx, drag);
}
ctx.request_paint();
ctx.set_handled();
}
_ => {}
}
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.child.measure(ctx, constraints)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.child.arrange(ctx, bounds);
}
fn paint(&self, ctx: &mut PaintCtx) {
self.child.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.child.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
}
}
pub struct DropTarget {
scope: DragDropScope,
child: SingleChild,
accept: DropAcceptCallback,
hovered: bool,
on_drop: Option<DropCallback>,
on_hover_change: Option<HoverCallback>,
}
impl DropTarget {
pub fn new<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self {
scope: DragDropScope::new(),
child: SingleChild::new(child),
accept: Box::new(|_| DropEffect::Copy),
hovered: false,
on_drop: None,
on_hover_change: None,
}
}
pub fn scope(mut self, scope: DragDropScope) -> Self {
self.scope = scope;
self
}
pub fn accept<F>(mut self, accept: F) -> Self
where
F: FnMut(&DragEvent) -> DropEffect + 'static,
{
self.accept = Box::new(accept);
self
}
pub fn on_drop<F>(mut self, callback: F) -> Self
where
F: FnMut(&mut EventCtx, &DragEvent) + 'static,
{
self.on_drop = Some(Box::new(callback));
self
}
pub fn on_hover_change<F>(mut self, callback: F) -> Self
where
F: FnMut(bool) + 'static,
{
self.on_hover_change = Some(Box::new(callback));
self
}
pub fn is_hovered(&self) -> bool {
self.hovered
}
pub fn scope_ref(&self) -> &DragDropScope {
&self.scope
}
pub fn child(&self) -> &WidgetPod {
self.child.child()
}
pub fn child_mut(&mut self) -> &mut WidgetPod {
self.child.child_mut()
}
fn set_hovered(&mut self, ctx: &mut EventCtx, hovered: bool) {
if self.hovered == hovered {
return;
}
self.hovered = hovered;
if let Some(callback) = &mut self.on_hover_change {
callback(hovered);
}
ctx.request_paint();
ctx.request_semantics();
}
}
impl Widget for DropTarget {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
let Event::Drag(drag) = event else {
return;
};
if drag.scope_id != self.scope.id() {
return;
}
match drag.kind {
DragEventKind::Enter | DragEventKind::Over => {
let effect = (self.accept)(drag);
if effect.is_some() {
ctx.accept_drop(effect);
self.set_hovered(ctx, true);
} else {
self.set_hovered(ctx, false);
}
}
DragEventKind::Leave => {
self.set_hovered(ctx, false);
}
DragEventKind::Drop if drag.target == Some(ctx.widget_id()) => {
if let Some(callback) = &mut self.on_drop {
callback(ctx, drag);
}
self.set_hovered(ctx, false);
ctx.set_handled();
}
_ => {}
}
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.child.measure(ctx, constraints)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.child.arrange(ctx, bounds);
}
fn paint(&self, ctx: &mut PaintCtx) {
self.child.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.child.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
}
}
fn drag_preview_label(active: &DragPreview) -> String {
if let Some(label) = &active.preview_label {
return label.to_string();
}
match &active.payload {
DragPayload::Text(text) if !text.is_empty() => text.clone(),
DragPayload::Image { .. } => "Image".to_string(),
DragPayload::Custom { kind, .. } => kind.to_string(),
DragPayload::Text(_) => "Dragging".to_string(),
}
}
#[cfg(test)]
mod tests {
use std::{cell::RefCell, rc::Rc};
use super::*;
use crate::{Label, SizedBox, Stack, Surface, ThemeTextToken};
use sui_core::{
DragOutcome, Modifiers, PointerButtons, PointerEvent, PointerKind, Result, WindowId,
};
use sui_runtime::{Application, Runtime, WindowBuilder};
use sui_scene::{Brush, SceneCommand};
fn primary_pointer(kind: PointerEventKind, position: Point, pressed: bool) -> Event {
let mut buttons = PointerButtons::NONE;
if pressed {
buttons.insert(PointerButton::Primary);
}
Event::Pointer(PointerEvent {
pointer_id: 1,
kind,
position,
delta: Vector::ZERO,
scroll_delta: None,
button: Some(PointerButton::Primary),
buttons,
modifiers: Modifiers::NONE,
pointer_kind: PointerKind::Mouse,
is_primary: true,
})
}
fn build_runtime<W>(root: W) -> (Runtime, WindowId)
where
W: Widget + 'static,
{
let runtime = Application::new()
.window(WindowBuilder::new().title("Drag Drop").root(root))
.build()
.unwrap();
let window_id = runtime.window_ids()[0];
(runtime, window_id)
}
fn has_fill_color(output: &sui_runtime::RenderOutput, color: sui_core::Color) -> bool {
let mut found = false;
output
.frame
.scene
.visit_commands(&mut |command| match command {
SceneCommand::FillRect {
brush: Brush::Solid(fill),
..
}
| SceneCommand::FillPath {
brush: Brush::Solid(fill),
..
}
| SceneCommand::FillRoundedRect {
brush: Brush::Solid(fill),
..
} if *fill == color => found = true,
_ => {}
});
found
}
fn has_stroke_color(output: &sui_runtime::RenderOutput, color: sui_core::Color) -> bool {
let mut found = false;
output
.frame
.scene
.visit_commands(&mut |command| match command {
SceneCommand::StrokeRect {
brush: Brush::Solid(stroke),
..
}
| SceneCommand::StrokePath {
brush: Brush::Solid(stroke),
..
} if *stroke == color => found = true,
_ => {}
});
found
}
fn feedback_fill_precedes_text(
output: &sui_runtime::RenderOutput,
color: sui_core::Color,
) -> bool {
let mut command_index = 0;
let mut fill_index = None;
let mut text_index = None;
output.frame.scene.visit_commands(&mut |command| {
match command {
SceneCommand::FillRect {
brush: Brush::Solid(fill),
..
}
| SceneCommand::FillPath {
brush: Brush::Solid(fill),
..
}
| SceneCommand::FillRoundedRect {
brush: Brush::Solid(fill),
..
} if *fill == color => {
fill_index.get_or_insert(command_index);
}
SceneCommand::DrawText(_)
| SceneCommand::DrawShapedText(_)
| SceneCommand::DrawShapedTextWindow(_) => {
text_index.get_or_insert(command_index);
}
_ => {}
};
command_index += 1;
});
matches!((fill_index, text_index), (Some(fill), Some(text)) if fill < text)
}
#[test]
fn drag_preview_theme_tracks_static_and_dynamic_small_tokens() {
let mut static_theme = DefaultTheme::default();
static_theme.text.sm = ThemeTextToken {
size: 17.0,
line_height: 24.0,
};
let host = DragDropHost::new(
DragDropScope::new(),
SizedBox::new().width(80.0).height(40.0),
)
.theme(static_theme);
let resolved = host.preview_theme.borrow().resolve();
let style = drag_preview_text_style(resolved);
assert_eq!(style.font_size, static_theme.text.sm.size);
assert_eq!(style.line_height, static_theme.text.sm.line_height);
assert_eq!(style.color, static_theme.palette.text);
let current = Rc::new(RefCell::new(DefaultTheme::default()));
let dynamic_host = DragDropHost::new(
DragDropScope::new(),
SizedBox::new().width(80.0).height(40.0),
)
.theme_when({
let current = Rc::clone(¤t);
move || *current.borrow()
});
current.borrow_mut().text.sm = ThemeTextToken {
size: 19.0,
line_height: 27.0,
};
let resolved = dynamic_host.preview_theme.borrow().resolve();
let style = drag_preview_text_style(resolved);
assert_eq!(style.font_size, 19.0);
assert_eq!(style.line_height, 27.0);
}
#[test]
fn draggable_starts_after_threshold_and_drops_typed_payload() -> Result<()> {
let scope = DragDropScope::new();
let starts = Rc::new(RefCell::new(0));
let ends = Rc::new(RefCell::new(Vec::new()));
let drops = Rc::new(RefCell::new(Vec::new()));
let hover_changes = Rc::new(RefCell::new(Vec::new()));
let source = Draggable::new(SizedBox::new().width(80.0).height(40.0))
.scope(scope.clone())
.payload(|| DragPayload::custom("asset", 42_u32))
.effect(DropEffect::Move)
.preview_label("Asset")
.on_drag_start({
let starts = Rc::clone(&starts);
move |_, _| *starts.borrow_mut() += 1
})
.on_drag_end({
let ends = Rc::clone(&ends);
move |_, drag| ends.borrow_mut().push(drag.outcome)
});
let target = DropTarget::new(SizedBox::new().width(80.0).height(40.0))
.scope(scope.clone())
.accept(|drag| {
if drag.payload.custom_data::<u32>() == Some(&42) {
DropEffect::Move
} else {
DropEffect::None
}
})
.on_drop({
let drops = Rc::clone(&drops);
move |_, drag| {
drops
.borrow_mut()
.push(*drag.payload.custom_data::<u32>().unwrap());
}
})
.on_hover_change({
let hover_changes = Rc::clone(&hover_changes);
move |hovered| hover_changes.borrow_mut().push(hovered)
});
let root = DragDropHost::new(
scope.clone(),
Stack::horizontal().with_child(source).with_child(target),
);
let (mut runtime, window_id) = build_runtime(root);
let _ = runtime.render(window_id)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, Point::new(10.0, 20.0), true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, Point::new(12.0, 20.0), true),
)?;
assert_eq!(*starts.borrow(), 0);
assert!(scope.active_drag().is_none());
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, Point::new(100.0, 20.0), true),
)?;
assert_eq!(*starts.borrow(), 1);
assert!(scope.active_drag().is_some());
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Up, Point::new(100.0, 20.0), false),
)?;
assert_eq!(&*drops.borrow(), &[42]);
assert!(matches!(
ends.borrow().as_slice(),
[Some(DragOutcome::Dropped {
effect: DropEffect::Move,
..
})]
));
assert_eq!(&*hover_changes.borrow(), &[true, false]);
assert!(scope.active_drag().is_none());
Ok(())
}
#[test]
fn accepting_drop_target_has_no_default_hover_feedback() -> Result<()> {
let scope = DragDropScope::new();
let theme = DefaultTheme::default();
let source = Draggable::new(SizedBox::new().width(80.0).height(40.0))
.scope(scope.clone())
.payload(|| DragPayload::text("item"))
.effect(DropEffect::Copy)
.preview_label("Item");
let target = DropTarget::new(SizedBox::new().width(80.0).height(40.0))
.scope(scope.clone())
.accept(|_| DropEffect::Copy);
let root = DragDropHost::new(
scope,
Stack::horizontal().with_child(source).with_child(target),
);
let (mut runtime, window_id) = build_runtime(root);
let idle = runtime.render(window_id)?;
assert!(!has_fill_color(&idle, theme.palette.accent_soft));
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, Point::new(10.0, 20.0), true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, Point::new(100.0, 20.0), true),
)?;
let hovering = runtime.render(window_id)?;
assert!(!has_fill_color(&hovering, theme.palette.accent_soft));
Ok(())
}
#[test]
fn drop_target_hover_callback_can_drive_child_feedback_behind_content() -> Result<()> {
let scope = DragDropScope::new();
let theme = DefaultTheme::default();
let hovered = Rc::new(RefCell::new(false));
let source = Draggable::new(SizedBox::new().width(80.0).height(40.0))
.scope(scope.clone())
.payload(|| DragPayload::text("item"))
.effect(DropEffect::Copy)
.preview_label("Item");
let surface_hovered = Rc::clone(&hovered);
let target = DropTarget::new(
Surface::panel(
SizedBox::new()
.width(80.0)
.height(40.0)
.with_child(Label::new("Drop here")),
)
.theme_when(move || {
let mut resolved = theme;
if *surface_hovered.borrow() {
resolved.surfaces.panel = resolved.palette.accent_soft;
resolved.surfaces.border = resolved.palette.accent_border_focus;
}
resolved
}),
)
.scope(scope.clone())
.accept(|_| DropEffect::Copy)
.on_hover_change(move |value| *hovered.borrow_mut() = value);
let root = DragDropHost::new(
scope,
Stack::horizontal().with_child(source).with_child(target),
);
let (mut runtime, window_id) = build_runtime(root);
let idle = runtime.render(window_id)?;
assert!(!has_fill_color(&idle, theme.palette.accent_soft));
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, Point::new(10.0, 20.0), true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, Point::new(100.0, 20.0), true),
)?;
let hovering = runtime.render(window_id)?;
assert!(has_fill_color(&hovering, theme.palette.accent_soft));
assert!(has_stroke_color(
&hovering,
theme.palette.accent_border_focus
));
assert!(feedback_fill_precedes_text(
&hovering,
theme.palette.accent_soft
));
Ok(())
}
#[test]
fn nearest_accepting_drop_target_receives_drop() -> Result<()> {
let scope = DragDropScope::new();
let inner_drops = Rc::new(RefCell::new(0));
let outer_drops = Rc::new(RefCell::new(0));
let source = Draggable::new(SizedBox::new().width(80.0).height(40.0))
.scope(scope.clone())
.payload(|| DragPayload::text("item"));
let inner = DropTarget::new(SizedBox::new().width(80.0).height(40.0))
.scope(scope.clone())
.accept(|_| DropEffect::Copy)
.on_drop({
let inner_drops = Rc::clone(&inner_drops);
move |_, _| *inner_drops.borrow_mut() += 1
});
let outer = DropTarget::new(inner)
.scope(scope.clone())
.accept(|_| DropEffect::Copy)
.on_drop({
let outer_drops = Rc::clone(&outer_drops);
move |_, _| *outer_drops.borrow_mut() += 1
});
let root = DragDropHost::new(
scope,
Stack::horizontal().with_child(source).with_child(outer),
);
let (mut runtime, window_id) = build_runtime(root);
let _ = runtime.render(window_id)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, Point::new(10.0, 20.0), true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, Point::new(100.0, 20.0), true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Up, Point::new(100.0, 20.0), false),
)?;
assert_eq!(*inner_drops.borrow(), 1);
assert_eq!(*outer_drops.borrow(), 0);
Ok(())
}
#[test]
fn cross_scope_target_ignores_drag_session() -> Result<()> {
let source_scope = DragDropScope::new();
let target_scope = DragDropScope::new();
let ends = Rc::new(RefCell::new(Vec::new()));
let drops = Rc::new(RefCell::new(0));
let source = Draggable::new(SizedBox::new().width(80.0).height(40.0))
.scope(source_scope.clone())
.payload(|| DragPayload::text("item"))
.on_drag_end({
let ends = Rc::clone(&ends);
move |_, drag| ends.borrow_mut().push(drag.outcome)
});
let target = DropTarget::new(SizedBox::new().width(80.0).height(40.0))
.scope(target_scope)
.accept(|_| DropEffect::Copy)
.on_drop({
let drops = Rc::clone(&drops);
move |_, _| *drops.borrow_mut() += 1
});
let root = DragDropHost::new(
source_scope,
Stack::horizontal().with_child(source).with_child(target),
);
let (mut runtime, window_id) = build_runtime(root);
let _ = runtime.render(window_id)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, Point::new(10.0, 20.0), true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, Point::new(100.0, 20.0), true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Up, Point::new(100.0, 20.0), false),
)?;
assert_eq!(*drops.borrow(), 0);
assert_eq!(&*ends.borrow(), &[Some(DragOutcome::Cancelled)]);
Ok(())
}
#[test]
fn drag_drop_host_preview_layer_is_not_hit_tested() -> Result<()> {
let scope = DragDropScope::new();
let starts = Rc::new(RefCell::new(0));
let source = Draggable::new(SizedBox::new().width(80.0).height(40.0))
.scope(scope.clone())
.payload(|| DragPayload::text("item"))
.on_drag_start({
let starts = Rc::clone(&starts);
move |_, _| *starts.borrow_mut() += 1
});
let root = DragDropHost::new(scope, source);
let (mut runtime, window_id) = build_runtime(root);
let output = runtime.render(window_id)?;
let mut preview_layer = None;
output.frame.scene.visit_layers(&mut |layer| {
if layer.descriptor.composition_mode == LayerCompositionMode::Overlay {
preview_layer = Some((layer.widget_id(), layer.descriptor.hit_test));
}
});
assert!(matches!(preview_layer, Some((_, false))));
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Down, Point::new(10.0, 20.0), true),
)?;
runtime.handle_event(
window_id,
primary_pointer(PointerEventKind::Move, Point::new(24.0, 20.0), true),
)?;
assert_eq!(*starts.borrow(), 1);
Ok(())
}
#[test]
fn drag_drop_host_routes_platform_file_hover_drop_and_cancel() -> Result<()> {
let hovered = Rc::new(RefCell::new(Vec::<Vec<PathBuf>>::new()));
let dropped = Rc::new(RefCell::new(Vec::<PathBuf>::new()));
let cancelled = Rc::new(RefCell::new(0));
let root = DragDropHost::new(
DragDropScope::new(),
SizedBox::new().width(160.0).height(80.0),
)
.on_external_file_hover({
let hovered = Rc::clone(&hovered);
move |_, paths| hovered.borrow_mut().push(paths.to_vec())
})
.on_external_file_drop({
let dropped = Rc::clone(&dropped);
move |_, path| dropped.borrow_mut().push(path)
})
.on_external_file_hover_cancelled({
let cancelled = Rc::clone(&cancelled);
move |_| *cancelled.borrow_mut() += 1
});
let (mut runtime, window_id) = build_runtime(root);
let _ = runtime.render(window_id)?;
let first = PathBuf::from("/tmp/first.txt");
let second = PathBuf::from("/tmp/second.png");
runtime.handle_event(
window_id,
Event::Window(WindowEvent::ExternalFileHovered(first.clone())),
)?;
runtime.handle_event(
window_id,
Event::Window(WindowEvent::ExternalFileHovered(second.clone())),
)?;
runtime.handle_event(
window_id,
Event::Window(WindowEvent::ExternalFileDropped(first.clone())),
)?;
runtime.handle_event(
window_id,
Event::Window(WindowEvent::ExternalFileHoverCancelled),
)?;
assert_eq!(
hovered.borrow().as_slice(),
&[vec![first.clone()], vec![first.clone(), second]]
);
assert_eq!(dropped.borrow().as_slice(), &[first]);
assert_eq!(*cancelled.borrow(), 1);
Ok(())
}
#[test]
fn drag_payload_custom_equality_uses_stable_kind() {
let left = DragPayload::custom("asset", 1_u32);
let right = DragPayload::custom("asset", 2_u32);
let other = DragPayload::custom("other", 1_u32);
assert_eq!(left, right);
assert_ne!(left, other);
assert_eq!(left.custom_data::<u32>(), Some(&1));
assert_eq!(right.custom_data::<u32>(), Some(&2));
}
}