use super::{
indicator::{self, Indicator},
style::DragStyle,
DragEffect, DropZones, Dropped, Insertion,
};
use crate::{
context::{
drag::state::{Hover, TargetInfo},
file_drop::FileTarget,
HitAction, HitRegion,
},
files::{FileFilter, PickedFile},
Id, Rect, Response, Ui, Vec2,
};
use std::{any::Any, convert::Infallible, marker::PhantomData, sync::Arc};
struct FileAccept {
filter: Arc<FileFilter>,
max: usize,
}
pub struct DropTarget<P, F> {
key: Id,
accept: F,
enabled: bool,
zones: Option<DropZones>,
passthrough: bool,
effect: Option<DragEffect>,
indent: f32,
indicator: bool,
style: DragStyle,
files: Option<FileAccept>,
payload: PhantomData<fn(&P)>,
}
#[derive(Debug)]
pub struct DropOutput<R, P> {
pub inner: R,
pub response: Response,
pub id: Id,
pub hovering: bool,
pub acceptable: bool,
pub rejected: bool,
pub position: Option<Vec2>,
pub insertion: Option<Insertion>,
pub dropped: Option<Dropped<P>>,
pub files_hovering: bool,
pub files_acceptable: bool,
pub dropped_files: Vec<PickedFile>,
}
impl<P: Any, F: Fn(&P) -> bool> DropTarget<P, F> {
pub fn new(id: Id, accept: F) -> Self {
Self {
key: id,
accept,
enabled: true,
zones: None,
passthrough: false,
effect: None,
indent: 0.0,
indicator: true,
style: DragStyle::default(),
files: None,
payload: PhantomData,
}
}
pub fn accepts_files(mut self, filter: impl Into<Arc<FileFilter>>) -> Self {
self.files = Some(FileAccept {
filter: filter.into(),
max: usize::MAX,
});
self
}
pub fn max_files(mut self, max: usize) -> Self {
if let Some(files) = &mut self.files {
files.max = max;
}
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn zones(mut self, zones: DropZones) -> Self {
self.zones = Some(zones);
self
}
pub fn passthrough_rejected(mut self, passthrough: bool) -> Self {
self.passthrough = passthrough;
self
}
pub fn effect(mut self, effect: DragEffect) -> Self {
self.effect = Some(effect);
self
}
#[track_caller]
pub fn line_indent(mut self, indent: f32) -> Self {
self.indent = crate::components::sanitize::non_negative("DropTarget::line_indent", indent)
.unwrap_or(self.indent);
self
}
pub fn indicator(mut self, draw: bool) -> Self {
self.indicator = draw;
self
}
pub fn style(mut self, style: DragStyle) -> Self {
self.style.merge(&style);
self
}
pub fn show<R>(
self,
ui: &mut Ui<'_>,
build: impl FnOnce(&mut Ui<'_>) -> R,
) -> DropOutput<R, P> {
if ui.flow.is_some() {
return ui.layout_item(|ui| self.show(ui, build));
}
let id = ui.scope.with(("drop-target", self.key));
let active = self.enabled && ui.enabled && ui.context.drag_running();
let nests = active || (self.files.is_some() && self.enabled && ui.enabled);
let depth = if nests { ui.context.drag_enter() } else { 0 };
let (inner, size, placement) = ui.measure_effect(id.with("content"), true, build);
if nests {
ui.context.drag_exit();
}
let rect = Rect::from_min_size(ui.layout.cursor, size);
let state = self.resolve(ui, id, rect, depth, active);
let clip = ui.clip_rect();
ui.context.place(placement, Vec2::ZERO, clip);
ui.allocate_space(size);
let response = ui.response(id, rect, self.enabled);
self.finish(ui, rect, inner, response, state)
}
pub fn attach(self, ui: &mut Ui<'_>, response: Response) -> DropOutput<(), P> {
let id = ui.scope.with(("drop-target", self.key));
let active = self.enabled && ui.enabled && response.enabled && ui.context.drag_running();
let depth = ui.context.drag.depth;
let state = self.resolve(ui, id, response.rect, depth, active);
self.finish(ui, response.rect, (), response, state)
}
fn resolve(&self, ui: &mut Ui<'_>, id: Id, rect: Rect, depth: u16, active: bool) -> State<P> {
let mut state = State {
hover: None,
dropped: None,
file_hover: None,
files: Vec::new(),
id,
};
if let (Some(accept), true) = (&self.files, self.enabled && ui.enabled) {
let slot = ui.context.file_add_target(FileTarget {
id,
key: self.key,
depth,
filter: Arc::clone(&accept.filter),
max: accept.max,
passthrough: self.passthrough,
});
ui.context.register_hit(HitRegion {
id: id.with("files"),
window: ui.window,
rect,
clip: ui.clip,
action: HitAction::FileDrop { slot },
});
state.file_hover = ui.context.files.hover_target.filter(|h| h.id == id);
state.files = ui.context.file_claim(id).unwrap_or_default();
if !state.files.is_empty() {
ui.context.request_repaint();
}
}
if active {
let accepts = ui
.context
.drag
.session
.as_ref()
.and_then(|session| session.payload.as_ref())
.and_then(|payload| payload.get::<P>())
.is_some_and(|payload| (self.accept)(payload));
let slot = ui.context.drag_add_target(TargetInfo {
id,
key: self.key,
depth,
accepts,
passthrough: self.passthrough,
zones: self.zones,
effect: self.effect,
});
ui.context.register_hit(HitRegion {
id,
window: ui.window,
rect,
clip: ui.clip,
action: HitAction::DropTarget { slot },
});
state.hover = ui
.context
.drag
.session
.as_ref()
.and_then(|session| session.hover)
.filter(|hover| hover.id == id);
}
if self.enabled && ui.enabled {
state.dropped = self.claim(ui, id);
}
state
}
fn claim(&self, ui: &mut Ui<'_>, id: Id) -> Option<Dropped<P>> {
let ended = ui
.context
.drag
.ended
.as_mut()
.filter(|ended| ended.target.id == id && !ended.claimed)?;
if !ended
.payload
.as_ref()
.and_then(|payload| payload.get::<P>())
.is_some_and(|payload| (self.accept)(payload))
{
return None;
}
let payload = ended.payload.take()?.take::<P>().ok()?;
ended.claimed = true;
let dropped = Dropped {
source: ended.info.key,
target: self.key,
position: ended.position,
local: ended.target.local,
insertion: ended.target.insertion,
effect: ended.target.effect.unwrap_or(ended.info.effect),
payload,
};
ui.context.request_repaint();
Some(dropped)
}
fn finish<R>(
self,
ui: &mut Ui<'_>,
rect: Rect,
inner: R,
response: Response,
state: State<P>,
) -> DropOutput<R, P> {
let hover: Option<Hover> = state.hover;
let file_hover = state.file_hover;
let mut response = response;
response.files_hovering = file_hover.is_some();
response.files_dropped = !state.files.is_empty();
if let (Some(hover), true) = (hover.or(file_hover), self.indicator) {
indicator::paint(
ui,
&self.style,
Indicator {
id: state.id,
rect,
hover,
zones: self.zones,
indent: self.indent,
},
);
}
DropOutput {
inner,
response,
id: self.key,
hovering: hover.is_some(),
acceptable: hover.is_some_and(|h| h.accepts),
rejected: hover.is_some_and(|h| !h.accepts),
position: hover.map(|h| h.local),
insertion: hover.and_then(|h| h.insertion),
dropped: state.dropped,
files_hovering: file_hover.is_some(),
files_acceptable: file_hover.is_some_and(|h| h.accepts),
dropped_files: state.files,
}
}
}
impl DropTarget<Infallible, fn(&Infallible) -> bool> {
pub fn files(id: Id) -> Self {
fn never(_: &Infallible) -> bool {
false
}
Self::new(id, never as fn(&Infallible) -> bool)
.accepts_files(FileFilter::any().directories(true))
}
}
struct State<P> {
hover: Option<Hover>,
dropped: Option<Dropped<P>>,
file_hover: Option<Hover>,
files: Vec<PickedFile>,
id: Id,
}