use crate::drawings::DrawingToolType;
use egui::{Context, Key, KeyboardShortcut, Modifiers};
pub struct DrawingToolShortcuts {
shortcuts: Vec<(KeyboardShortcut, DrawingToolType)>,
}
impl Default for DrawingToolShortcuts {
fn default() -> Self {
Self {
shortcuts: vec![
(
KeyboardShortcut::new(Modifiers::ALT, Key::T),
DrawingToolType::TrendLine,
),
(
KeyboardShortcut::new(Modifiers::ALT, Key::H),
DrawingToolType::HorizontalLine,
),
(
KeyboardShortcut::new(Modifiers::ALT, Key::V),
DrawingToolType::VerticalLine,
),
(
KeyboardShortcut::new(Modifiers::ALT, Key::F),
DrawingToolType::FibonacciRetracement,
),
(
KeyboardShortcut::new(Modifiers::ALT, Key::R),
DrawingToolType::Rect,
),
(
KeyboardShortcut::new(Modifiers::ALT, Key::L),
DrawingToolType::TextLabel,
),
(
KeyboardShortcut::new(Modifiers::ALT, Key::P),
DrawingToolType::Pitchfork,
),
],
}
}
}
impl DrawingToolShortcuts {
pub fn new() -> Self {
Self::default()
}
pub fn check(&self, ctx: &Context) -> Option<DrawingToolType> {
for (shortcut, tool) in &self.shortcuts {
if ctx.input_mut(|i| i.consume_shortcut(shortcut)) {
return Some(*tool);
}
}
None
}
pub fn add_shortcut(&mut self, shortcut: KeyboardShortcut, tool: DrawingToolType) {
self.shortcuts.push((shortcut, tool));
}
pub fn help_text(&self) -> Vec<(String, String)> {
self.shortcuts
.iter()
.map(|(shortcut, tool)| {
let key_text = format!("{shortcut:?}");
(key_text, tool.as_str().to_string())
})
.collect()
}
}