egui_charts/ui/drawing_toolbar/
state.rs1use super::DrawingTemplate;
6use super::categories::cursors::CursorType;
7use super::data;
8use crate::drawings::DrawingToolType;
9use std::collections::{HashMap, VecDeque};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum MagnetType {
14 Weak,
16 Strong,
18}
19
20#[derive(Clone)]
22pub struct ToolbarState {
23 pub sel_tool: Option<DrawingToolType>,
25 pub expanded: bool,
27 pub expanded_category: Option<String>,
29 pub favorites: Vec<DrawingToolType>,
31 pub floating_visible: bool,
33 pub floating_pos: Option<egui::Pos2>,
36 pub recent: VecDeque<DrawingToolType>,
38 pub magnet_mode: bool,
40 pub magnet_type: MagnetType,
42 pub snap_to_indicators: bool,
44 pub magnet_hint_dismissed: bool,
49 pub stay_in_drawing_mode: bool,
51 pub drawing_color: [u8; 4],
53 pub show_color_picker: bool,
55 pub category_last_tool: HashMap<String, DrawingToolType>,
57 pub search_open: bool,
59 pub search_query: String,
61 pub glyph_picker: super::GlyphPicker,
63 pub values_tooltip_on_long_press: bool,
65 pub eraser_mode_enabled: bool,
67 pub lock_all: bool,
69 pub hide_drawings: bool,
71 pub hide_indicators: bool,
73 pub hide_positions: bool,
75 pub hide_orders: bool,
77 pub has_zoom_history: bool,
79 pub zoom_mode_active: bool,
81 pub current_cursor_type: CursorType,
83 pub templates: Vec<DrawingTemplate>,
85 pub show_template_name_input: bool,
87 pub template_name_input: String,
89}
90
91impl Default for ToolbarState {
92 fn default() -> Self {
93 let mut category_last_tool = HashMap::new();
94 category_last_tool.insert("Lines".to_string(), DrawingToolType::TrendLine);
96 category_last_tool.insert(
97 "Fibonacci".to_string(),
98 DrawingToolType::FibonacciRetracement,
99 );
100 category_last_tool.insert("Patterns".to_string(), DrawingToolType::XABCDPattern);
101 category_last_tool.insert("Projection".to_string(), DrawingToolType::LongPos);
102 category_last_tool.insert("Brushes/Shapes".to_string(), DrawingToolType::Brush);
103 category_last_tool.insert("Text/Annotations".to_string(), DrawingToolType::TextLabel);
104
105 Self {
106 sel_tool: None,
107 expanded: true,
108 expanded_category: None,
109 favorites: Vec::new(),
110 floating_visible: true,
111 floating_pos: None,
112 recent: VecDeque::with_capacity(5),
113 magnet_mode: false,
114 magnet_type: MagnetType::Strong, snap_to_indicators: true,
116 magnet_hint_dismissed: false,
117 stay_in_drawing_mode: false,
118 drawing_color: [242, 54, 69, 255], show_color_picker: false,
120 category_last_tool,
121 search_open: false,
122 search_query: String::new(),
123 glyph_picker: Default::default(),
124 values_tooltip_on_long_press: true, eraser_mode_enabled: false,
126 lock_all: false,
127 hide_drawings: false,
128 hide_indicators: false,
129 hide_positions: false,
130 hide_orders: false,
131 has_zoom_history: false,
132 zoom_mode_active: false,
133 current_cursor_type: CursorType::Arrow, templates: Vec::new(),
135 show_template_name_input: false,
136 template_name_input: String::new(),
137 }
138 }
139}
140
141impl ToolbarState {
142 pub fn new() -> Self {
144 Self::default()
145 }
146
147 pub fn select_tool(&mut self, tool: DrawingToolType) {
149 self.sel_tool = Some(tool);
150 self.eraser_mode_enabled = false;
151 self.zoom_mode_active = false;
152 self.add_to_recent(tool);
153
154 if let Some(category) = data::get_tool_category(tool) {
156 self.category_last_tool.insert(category.to_string(), tool);
157 }
158 }
159
160 pub fn add_to_recent(&mut self, tool: DrawingToolType) {
162 self.recent.retain(|t| *t != tool);
164 self.recent.push_front(tool);
166 while self.recent.len() > 5 {
168 self.recent.pop_back();
169 }
170 }
171
172 pub fn clear_selection(&mut self) {
174 self.sel_tool = None;
175 self.zoom_mode_active = false;
176 }
177
178 pub fn toggle_favorite(&mut self, tool: DrawingToolType) {
180 if self.favorites.contains(&tool) {
181 self.favorites.retain(|t| *t != tool);
182 } else {
183 self.favorites.push(tool);
184 }
185 }
186
187 pub fn is_favorite(&self, tool: &DrawingToolType) -> bool {
189 self.favorites.contains(tool)
190 }
191
192 pub fn toggle_magnet(&mut self) {
194 self.magnet_mode = !self.magnet_mode;
195 }
196
197 pub fn toggle_stay_in_drawing(&mut self) {
199 self.stay_in_drawing_mode = !self.stay_in_drawing_mode;
200 }
201
202 pub fn sync_from(
204 &mut self,
205 active_tool: Option<DrawingToolType>,
206 magnet_mode: bool,
207 stay_in_drawing_mode: bool,
208 eraser_mode: bool,
209 drawing_color: [u8; 4],
210 ) {
211 self.sel_tool = active_tool;
212 self.magnet_mode = magnet_mode;
213 self.stay_in_drawing_mode = stay_in_drawing_mode;
214 self.eraser_mode_enabled = eraser_mode;
215 self.drawing_color = drawing_color;
216 }
217}