use super::super::{HitRegion, Id, Paint};
use crate::components::drag_drop::{
style::Resolved, DragEffect, DragEnd, DropZones, Insertion, PreviewKind,
};
use crate::time::Instant;
use crate::{Rect, Vec2};
use std::any::{Any, TypeId};
use winit::window::CursorIcon;
pub struct Payload {
pub type_id: TypeId,
pub value: Box<dyn Any>,
}
impl Payload {
pub fn new<P: Any>(value: P) -> Self {
Self {
type_id: TypeId::of::<P>(),
value: Box::new(value),
}
}
pub fn get<P: Any>(&self) -> Option<&P> {
self.value.downcast_ref()
}
pub fn take<P: Any>(self) -> Result<P, Self> {
if self.type_id != TypeId::of::<P>() {
return Err(self);
}
match self.value.downcast::<P>() {
Ok(value) => Ok(*value),
Err(value) => Err(Self {
type_id: self.type_id,
value,
}),
}
}
}
#[derive(Clone, Copy)]
pub struct SourceInfo {
pub id: Id,
pub key: Id,
pub depth: u16,
pub focus: Option<Id>,
pub own_focus: bool,
pub effect: DragEffect,
pub preview: PreviewKind,
pub resolved: Resolved,
}
#[derive(Clone, Copy)]
pub struct TargetInfo {
pub id: Id,
pub key: Id,
pub depth: u16,
pub accepts: bool,
pub passthrough: bool,
pub zones: Option<DropZones>,
pub effect: Option<DragEffect>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Hover {
pub id: Id,
pub key: Id,
pub accepts: bool,
pub insertion: Option<Insertion>,
pub local: Vec2,
pub rect: Rect,
pub effect: Option<DragEffect>,
}
#[derive(Clone)]
pub struct Snapshot {
pub rect: Rect,
pub items: Vec<(Id, Vec<Paint>)>,
}
pub struct Pending {
pub hit: HitRegion,
pub info: SourceInfo,
pub press: Vec2,
pub time: Instant,
pub passive: bool,
}
pub struct Session {
pub info: SourceInfo,
pub window: Id,
pub rect: Rect,
pub grab: Vec2,
pub pointer: Vec2,
pub keyboard: bool,
pub outside: bool,
pub payload: Option<Payload>,
pub started: bool,
pub seen: bool,
pub hover: Option<Hover>,
pub cursor: CursorIcon,
pub snapshot: Option<Snapshot>,
pub last_tick: Option<Instant>,
}
pub struct Ended {
pub info: SourceInfo,
pub target: Hover,
pub position: Vec2,
pub payload: Option<Payload>,
pub claimed: bool,
}
pub struct Returning {
pub snapshot: Snapshot,
pub from: Vec2,
pub to: Vec2,
pub scale: f32,
pub opacity: f32,
pub tween: crate::TweenOptions,
pub source: Id,
}
#[derive(Default)]
pub struct DragRuntime {
pub pending: Option<Pending>,
pub session: Option<Session>,
pub ended: Option<Ended>,
pub result: Option<(DragEnd, u64)>,
pub sources: Vec<SourceInfo>,
pub targets: Vec<TargetInfo>,
pub last_sources: Vec<SourceInfo>,
pub last_targets: Vec<TargetInfo>,
pub depth: u16,
pub returning: Option<Returning>,
pub custom_preview: u64,
pub deadline: Option<Instant>,
pub scrolling: bool,
}
impl DragRuntime {
pub(crate) fn active(&self) -> bool {
self.session.as_ref().is_some_and(|s| s.started)
}
}