dioxus_swdir_tree_core/event.rs
1//! Events produced by state-machine inspectors (keyboard, drag) and
2//! consumed by both the core test suite and the view component.
3//!
4//! Placing `DirectoryTreeEvent` in the core crate lets
5//! [`crate::keyboard::handle_key`] return it without a circular dependency,
6//! and lets application code depend only on `dioxus-swdir-tree-core` when
7//! integrating the widget without a Dioxus renderer.
8
9use std::path::PathBuf;
10
11use crate::drag::DragMsg;
12use crate::selection::SelectionMode;
13
14/// An event emitted by the keyboard handler, the view component, or the drag
15/// state machine.
16///
17/// The host handles each variant by calling the corresponding method on
18/// `DirectoryTree`:
19///
20/// ```no_run
21/// # use dioxus_swdir_tree_core::{DirectoryTreeEvent, DirectoryTree, SelectionMode};
22/// # use dioxus_swdir_tree_core::drag::DragOutcome;
23/// # use std::path::Path;
24/// fn handle(tree: &mut DirectoryTree, ev: DirectoryTreeEvent) {
25/// match ev {
26/// DirectoryTreeEvent::Toggled(path) => {
27/// let _req = tree.on_toggled(&path);
28/// }
29/// DirectoryTreeEvent::Selected { path, is_dir, mode } => {
30/// tree.on_selected(&path, is_dir, mode);
31/// }
32/// DirectoryTreeEvent::Drag(msg) => {
33/// match tree.on_drag_msg(msg) {
34/// DragOutcome::Clicked { path, is_dir } => {
35/// tree.on_selected(&path, is_dir, SelectionMode::Replace);
36/// }
37/// DragOutcome::Completed { sources, destination } => {
38/// // application performs the filesystem operation
39/// let _ = (sources, destination);
40/// }
41/// DragOutcome::None => {}
42/// }
43/// }
44/// }
45/// }
46/// ```
47#[derive(Debug, Clone, PartialEq)]
48pub enum DirectoryTreeEvent {
49 /// Expand or collapse the directory at `path`.
50 Toggled(PathBuf),
51
52 /// Change the selection to include `path`.
53 Selected {
54 /// Target path.
55 path: PathBuf,
56 /// Whether the target is a directory (used by the view layer for
57 /// icon/styling; the core's `on_selected` ignores it).
58 is_dir: bool,
59 /// How the selection should change.
60 mode: SelectionMode,
61 },
62
63 /// A drag-and-drop gesture. The host calls
64 /// [`crate::DirectoryTree::on_drag_msg`] and acts on the returned
65 /// [`crate::drag::DragOutcome`].
66 Drag(DragMsg),
67}