use std::collections::BTreeMap;
use egui::{Pos2, Rect, ViewportId};
use super::drag_and_drop::TreeComponent;
use crate::layout::ZoneName;
use crate::{NodePath, TabDestination, TabPath};
pub(crate) const RELEASED_PASSES: u8 = 12;
#[derive(Clone, Debug)]
pub(crate) struct DragSession {
pub(crate) src: TreeComponent,
pub(crate) rect: Rect,
pub(crate) source: ViewportId,
pub(crate) local: Pos2,
pub(crate) desktop: Option<Pos2>,
pub(crate) outside: bool,
pub(crate) pass: u64,
pub(crate) cancelled: bool,
pub(crate) released: Option<u8>,
pub(crate) reports: BTreeMap<ViewportId, Report>,
}
#[derive(Clone, Debug)]
pub(crate) struct Report {
pub(crate) stamp: u64,
pub(crate) rank: u32,
pub(crate) outcome: Outcome,
pub(crate) unfold: Option<ZoneName>,
}
#[derive(Clone, Debug)]
pub(crate) enum Outcome {
Drop(TabDestination),
Refused,
Free,
}
impl DragSession {
pub(crate) fn source_in<Tab>(&self, state: &crate::DockState<Tab>) -> bool {
match self.src {
TreeComponent::Surface(surface) => state.get_surface(surface).is_some(),
TreeComponent::Node(path) => state.leaf(path).is_ok(),
TreeComponent::Tab(path) => state
.leaf(path.node_path())
.is_ok_and(|leaf| path.tab.0 < leaf.tabs.len()),
}
}
pub(crate) fn new(src: TreeComponent, rect: Rect, source: ViewportId, local: Pos2) -> Self {
Self {
src,
rect,
source,
local,
desktop: None,
outside: false,
pass: 0,
cancelled: false,
released: None,
reports: BTreeMap::new(),
}
}
pub(crate) fn remote(&self, source_rank: u32) -> Option<&Report> {
self.reports
.iter()
.filter(|(viewport, report)| {
**viewport != self.source
&& report.stamp + 1 >= self.pass
&& (self.outside || (source_rank == 0 && report.rank > 0))
})
.map(|(_, report)| report)
.max_by_key(|report| report.rank)
}
pub(crate) fn dragged(&self) -> Option<SpaceDrag> {
let payload = match self.src {
TreeComponent::Tab(path) => DragPayload::Tab(path),
TreeComponent::Node(path) => DragPayload::Group(path),
TreeComponent::Surface(_) => return None,
};
let over = self
.reports
.iter()
.filter(|(_, report)| report.stamp + 1 >= self.pass)
.max_by_key(|(_, report)| report.rank)
.map(|(viewport, _)| *viewport)
.filter(|_| self.outside || self.remote(0).is_some());
Some(SpaceDrag {
payload,
source: self.source,
over,
released: self.released.is_some(),
cancelled: self.cancelled,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DragPayload {
Tab(TabPath),
Group(NodePath),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SpaceDrag {
pub payload: DragPayload,
pub source: ViewportId,
pub over: Option<ViewportId>,
pub released: bool,
pub cancelled: bool,
}