Skip to main content

egui_charts/ui/widget_bar/panels/object_tree/
actions.rs

1//! Actions emitted by the Object Tree panel
2//!
3//! Apply chart operations with `ObjectTreePanel::apply_to_chart`; the host handles dialog intents.
4
5/// Actions that can be performed on objects in the tree
6#[derive(Clone, Debug, PartialEq, Default)]
7#[non_exhaustive]
8pub enum ObjectTreeAction {
9    /// No action
10    #[default]
11    None,
12
13    // === Selection ===
14    /// Select a single object (clears other selections)
15    Select(usize),
16    /// Add to selection (Ctrl+click)
17    AddToSelection(usize),
18    /// Range select (Shift+click)
19    RangeSelect(usize),
20    /// Clear all selections
21    ClearSelection,
22    /// Select all objects
23    SelectAll,
24
25    // === Visibility ===
26    /// Toggle visibility of an object
27    ToggleVisibility(usize),
28    /// Show all objects
29    ShowAll,
30    /// Hide all objects
31    HideAll,
32    /// Hide selected objects
33    HideSelected,
34
35    // === Locking (drawings only) ===
36    /// Toggle lock state of an object
37    ToggleLock(usize),
38    /// Lock all drawings
39    LockAll,
40    /// Unlock all drawings
41    UnlockAll,
42
43    // === Object Manipulation ===
44    /// Delete an object
45    Delete(usize),
46    /// Delete all selected objects
47    DeleteSelected,
48    /// Duplicate an object
49    Duplicate(usize),
50    /// Duplicate all selected objects
51    DuplicateSelected,
52
53    // === Properties ===
54    /// Open properties dialog for object
55    OpenProperties(usize),
56    /// Rename an object (id, new_name)
57    Rename(usize, String),
58    /// Change object color
59    ChangeColor(usize, egui::Color32),
60
61    // === Z-ordering ===
62    /// Bring object to front
63    BringToFront(usize),
64    /// Send object to back
65    SendToBack(usize),
66    /// Move object up one level
67    MoveUp(usize),
68    /// Move object down one level
69    MoveDown(usize),
70    /// Move a drawing to a pane
71    MoveToPane(usize, crate::widget::PaneId),
72
73    // === Folders ===
74    /// Create a named folder under a parent path and move selected drawings into it.
75    CreateFolder(Vec<String>, String),
76    /// Move drawing ids into an existing folder, or an empty path for the root.
77    MoveToFolder(Vec<usize>, Vec<String>),
78    /// Set visibility of every drawing in a folder and its descendants.
79    SetFolderVisible(Vec<String>, bool),
80    /// Set locking of every drawing in a folder and its descendants.
81    SetFolderLocked(Vec<String>, bool),
82
83    // === Navigation ===
84    /// Zoom to object on chart (focus view)
85    ZoomTo(usize),
86
87    // === Indicators ===
88    /// Open indicator settings dialog
89    OpenIndicatorSettings(usize),
90    /// Remove indicator
91    RemoveIndicator(usize),
92
93    // === Bulk Operations ===
94    /// Remove all drawings
95    RemoveAllDrawings,
96    /// Remove all indicators
97    RemoveAllIndicators,
98}
99
100impl ObjectTreeAction {
101    /// Check if this is a no-op action
102    pub fn is_none(&self) -> bool {
103        matches!(self, Self::None)
104    }
105
106    /// Check if this action modifies selection
107    pub fn is_selection_action(&self) -> bool {
108        matches!(
109            self,
110            Self::Select(_)
111                | Self::AddToSelection(_)
112                | Self::RangeSelect(_)
113                | Self::ClearSelection
114                | Self::SelectAll
115        )
116    }
117
118    /// Check if this action modifies objects
119    pub fn is_modification_action(&self) -> bool {
120        matches!(
121            self,
122            Self::Delete(_)
123                | Self::DeleteSelected
124                | Self::Duplicate(_)
125                | Self::DuplicateSelected
126                | Self::Rename(_, _)
127                | Self::ChangeColor(_, _)
128                | Self::BringToFront(_)
129                | Self::SendToBack(_)
130                | Self::MoveUp(_)
131                | Self::MoveDown(_)
132                | Self::MoveToPane(_, _)
133                | Self::CreateFolder(_, _)
134                | Self::MoveToFolder(_, _)
135                | Self::SetFolderVisible(_, _)
136                | Self::SetFolderLocked(_, _)
137                | Self::RemoveIndicator(_)
138                | Self::RemoveAllDrawings
139                | Self::RemoveAllIndicators
140        )
141    }
142
143    /// Check if this action requires confirmation
144    pub fn requires_confirmation(&self) -> bool {
145        matches!(
146            self,
147            Self::DeleteSelected | Self::RemoveAllDrawings | Self::RemoveAllIndicators
148        )
149    }
150
151    /// Get a human-readable description of the action
152    pub fn description(&self) -> String {
153        match self {
154            Self::None => "No action".to_string(),
155            Self::Select(id) => format!("Select object {id}"),
156            Self::Delete(id) => format!("Delete object {id}"),
157            Self::DeleteSelected => "Delete selected objects".to_string(),
158            Self::Duplicate(id) => format!("Duplicate object {id}"),
159            Self::ToggleVisibility(id) => format!("Toggle visibility for {id}"),
160            Self::ToggleLock(id) => format!("Toggle lock for {id}"),
161            Self::Rename(id, name) => format!("Rename {id} to '{name}'"),
162            Self::BringToFront(id) => format!("Bring {id} to front"),
163            Self::SendToBack(id) => format!("Send {id} to back"),
164            Self::ZoomTo(id) => format!("Zoom to object {id}"),
165            Self::RemoveAllDrawings => "Remove all drawings".to_string(),
166            Self::RemoveAllIndicators => "Remove all indicators".to_string(),
167            _ => format!("{self:?}"),
168        }
169    }
170}