Skip to main content

datui_lib/
chart_export_modal.rs

1//! Chart export modal: format (PNG/EPS), optional chart title, and file path. Used from Chart view only.
2
3use crate::chart_export::ChartExportFormat;
4use crate::widgets::text_input::TextInput;
5use std::path::Path;
6
7#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
8pub enum ChartExportFocus {
9    #[default]
10    FormatSelector,
11    TitleInput,
12    PathInput,
13    ExportButton,
14    CancelButton,
15}
16
17pub struct ChartExportModal {
18    pub active: bool,
19    pub focus: ChartExportFocus,
20    pub selected_format: ChartExportFormat,
21    pub title_input: TextInput,
22    pub path_input: TextInput,
23}
24
25impl ChartExportModal {
26    pub fn new() -> Self {
27        Self::default()
28    }
29
30    pub fn open(&mut self, theme: &crate::config::Theme, history_limit: usize) {
31        self.active = true;
32        self.focus = ChartExportFocus::PathInput;
33        self.title_input = TextInput::new().with_theme(theme);
34        self.title_input.clear();
35        self.path_input = TextInput::new()
36            .with_history_limit(history_limit)
37            .with_theme(theme);
38        self.path_input.clear();
39    }
40
41    /// Reopen the modal with path pre-filled (e.g. after cancel overwrite or export error). Focus is PathInput.
42    pub fn reopen_with_path(&mut self, path: &Path, format: ChartExportFormat) {
43        self.active = true;
44        self.focus = ChartExportFocus::PathInput;
45        self.selected_format = format;
46        self.title_input.clear();
47        self.path_input.set_value(path.display().to_string());
48    }
49
50    pub fn close(&mut self) {
51        self.active = false;
52        self.focus = ChartExportFocus::FormatSelector;
53        self.title_input.clear();
54        self.path_input.clear();
55    }
56
57    pub fn next_focus(&mut self) {
58        self.focus = match self.focus {
59            ChartExportFocus::FormatSelector => ChartExportFocus::TitleInput,
60            ChartExportFocus::TitleInput => ChartExportFocus::PathInput,
61            ChartExportFocus::PathInput => ChartExportFocus::ExportButton,
62            ChartExportFocus::ExportButton => ChartExportFocus::CancelButton,
63            ChartExportFocus::CancelButton => ChartExportFocus::FormatSelector,
64        };
65    }
66
67    pub fn prev_focus(&mut self) {
68        self.focus = match self.focus {
69            ChartExportFocus::FormatSelector => ChartExportFocus::CancelButton,
70            ChartExportFocus::TitleInput => ChartExportFocus::FormatSelector,
71            ChartExportFocus::PathInput => ChartExportFocus::TitleInput,
72            ChartExportFocus::ExportButton => ChartExportFocus::PathInput,
73            ChartExportFocus::CancelButton => ChartExportFocus::ExportButton,
74        };
75    }
76}
77
78impl Default for ChartExportModal {
79    fn default() -> Self {
80        Self {
81            active: false,
82            focus: ChartExportFocus::FormatSelector,
83            selected_format: ChartExportFormat::Png,
84            title_input: TextInput::new(),
85            path_input: TextInput::new(),
86        }
87    }
88}