use std::{
path::{Path, PathBuf},
sync::Arc,
};
use super::EventControl;
use crate::core::NodeId;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DroppedPath {
path: PathBuf,
is_folder: bool,
}
impl DroppedPath {
pub fn new(path: PathBuf) -> Self {
let is_folder = path.is_dir();
Self { path, is_folder }
}
pub fn with_kind(path: PathBuf, is_folder: bool) -> Self {
Self { path, is_folder }
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn into_path(self) -> PathBuf {
self.path
}
pub fn is_folder(&self) -> bool {
self.is_folder
}
pub fn is_file(&self) -> bool {
!self.is_folder
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DraggedPaths {
items: Arc<[DroppedPath]>,
}
impl DraggedPaths {
pub fn new(items: impl IntoIterator<Item = DroppedPath>) -> Self {
Self {
items: items.into_iter().collect(),
}
}
pub fn from_paths(paths: impl IntoIterator<Item = PathBuf>) -> Self {
Self::new(paths.into_iter().map(DroppedPath::new))
}
pub fn items(&self) -> &[DroppedPath] {
&self.items
}
pub fn paths(&self) -> impl Iterator<Item = &Path> {
self.items.iter().map(DroppedPath::path)
}
pub fn files(&self) -> impl Iterator<Item = &Path> {
self.items.iter().filter(|item| item.is_file()).map(DroppedPath::path)
}
pub fn folders(&self) -> impl Iterator<Item = &Path> {
self.items.iter().filter(|item| item.is_folder()).map(DroppedPath::path)
}
pub fn has_folders(&self) -> bool {
self.items.iter().any(DroppedPath::is_folder)
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
}
#[derive(Clone, Debug)]
pub struct FileDragEvent {
pub x: f32,
pub y: f32,
pub paths: DraggedPaths,
pub target_id: NodeId,
pub(crate) control: EventControl,
pub(crate) accepted: Arc<std::sync::atomic::AtomicBool>,
}
impl FileDragEvent {
pub(crate) fn new(x: f32, y: f32, paths: DraggedPaths, target_id: NodeId) -> Self {
Self {
x,
y,
paths,
target_id,
control: EventControl::new(),
accepted: Arc::new(std::sync::atomic::AtomicBool::new(true)),
}
}
pub(crate) fn at(&self, target_id: NodeId) -> Self {
Self {
target_id,
paths: self.paths.clone(),
control: self.control.clone(),
accepted: Arc::new(std::sync::atomic::AtomicBool::new(true)),
..*self
}
}
pub fn refuse(&self) {
self.accepted.store(false, std::sync::atomic::Ordering::Relaxed);
self.control.prevent_default();
}
pub fn prevent_default(&self) {
self.refuse();
}
pub fn default_prevented(&self) -> bool {
self.control.default_prevented()
}
pub fn is_accepted(&self) -> bool {
self.accepted.load(std::sync::atomic::Ordering::Relaxed)
}
pub fn stop_propagation(&self) {
self.control.stop_propagation();
}
pub fn propagation_stopped(&self) -> bool {
self.control.propagation_stopped()
}
pub fn stop_immediate_propagation(&self) {
self.control.stop_immediate_propagation();
}
pub fn immediate_propagation_stopped(&self) -> bool {
self.control.immediate_propagation_stopped()
}
}
#[derive(Clone, Debug)]
pub struct FileDropEvent {
pub x: f32,
pub y: f32,
pub paths: DraggedPaths,
pub target_id: NodeId,
pub(crate) control: EventControl,
}
impl FileDropEvent {
pub(crate) fn new(x: f32, y: f32, paths: DraggedPaths, target_id: NodeId) -> Self {
Self {
x,
y,
paths,
target_id,
control: EventControl::new(),
}
}
pub(crate) fn at(&self, target_id: NodeId) -> Self {
Self {
target_id,
paths: self.paths.clone(),
control: self.control.clone(),
..*self
}
}
pub fn prevent_default(&self) {
self.control.prevent_default();
}
pub fn default_prevented(&self) -> bool {
self.control.default_prevented()
}
pub fn stop_propagation(&self) {
self.control.stop_propagation();
}
pub fn propagation_stopped(&self) -> bool {
self.control.propagation_stopped()
}
pub fn stop_immediate_propagation(&self) {
self.control.stop_immediate_propagation();
}
pub fn immediate_propagation_stopped(&self) -> bool {
self.control.immediate_propagation_stopped()
}
}