Skip to main content

DropTarget

Trait DropTarget 

Source
pub trait DropTarget {
    // Required methods
    fn can_accept(&self, drag_type: &str) -> bool;
    fn drop_position(
        &self,
        pos: Position,
        payload: &DragPayload,
    ) -> DropPosition;
    fn on_drop(
        &mut self,
        payload: DragPayload,
        position: DropPosition,
    ) -> DropResult;

    // Provided methods
    fn on_drag_enter(&mut self) { ... }
    fn on_drag_leave(&mut self) { ... }
    fn accepted_types(&self) -> &[&str] { ... }
}
Expand description

Trait for widgets that can accept drops.

Implement this trait to allow a widget to be a drop target. The drag manager queries these methods during hover and drop to determine acceptance and placement.

§Example

use ftui_widgets::drag::{DropTarget, DragPayload, DropPosition, DropResult};

struct FileList { files: Vec<String> }

impl DropTarget for FileList {
    fn can_accept(&self, drag_type: &str) -> bool {
        drag_type == "application/file-path" || drag_type == "text/plain"
    }

    fn drop_position(&self, pos: Position, _payload: &DragPayload) -> DropPosition {
        DropPosition::from_list(pos.y, 1, self.files.len())
    }

    fn on_drop(&mut self, payload: DragPayload, position: DropPosition) -> DropResult {
        if let Some(text) = payload.as_text() {
            let idx = match position {
                DropPosition::Before(i) => i,
                DropPosition::After(i) => i + 1,
                DropPosition::Append => self.files.len(),
                _ => return DropResult::rejected("unsupported position"),
            };
            self.files.insert(idx, text.to_string());
            DropResult::Accepted
        } else {
            DropResult::rejected("expected text payload")
        }
    }
}

Required Methods§

Source

fn can_accept(&self, drag_type: &str) -> bool

Check if this target accepts the given drag type.

Called during hover to provide visual feedback (valid vs. invalid target). Must be a cheap check — called on every mouse move during a drag.

Source

fn drop_position(&self, pos: Position, payload: &DragPayload) -> DropPosition

Calculate the drop position within this widget.

pos is the cursor position relative to the widget’s area origin. payload provides access to the drag data for type-aware positioning.

Source

fn on_drop( &mut self, payload: DragPayload, position: DropPosition, ) -> DropResult

Handle the actual drop.

Called when the user releases the mouse button over a valid target. Returns DropResult::Accepted if the drop was handled, or DropResult::Rejected with a reason if it cannot be applied.

Provided Methods§

Source

fn on_drag_enter(&mut self)

Called when a drag enters this target’s area.

Use for hover-enter visual feedback.

Source

fn on_drag_leave(&mut self)

Called when a drag leaves this target’s area.

Use to clear hover visual feedback.

Source

fn accepted_types(&self) -> &[&str]

Accepted drag types as a list of MIME-like patterns.

Override to provide a static list for documentation or introspection. Defaults to an empty slice (use can_accept for runtime checks).

Implementors§