1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
mod update_files_screen;
mod view_files_screen;
use std::{fmt::Debug, path::PathBuf};
use crate::gui::components::{navigation, Navigation, Panel};
pub struct FilesScreen {
pub options: Navigation<Panel<OptionId>>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum OptionId {
NewFile,
PickFile,
ImportFromActualbudget,
OpenFile(std::path::PathBuf),
}
#[derive(Clone, Debug, PartialEq)]
pub enum Message {
NavigationMessage(navigation::Message<Panel<OptionId>>),
ReportFailedToCreateFile(String),
ReportFailedToImportFromActualbudget(String),
ReportFailedToPickFile(String),
ReportFailedToOpenFile(PathBuf, String),
ActivateSelectedOption,
NextOption,
PrevOption,
}
#[derive(Debug)]
pub enum Action {
ImportFromActualbudget,
NewFile,
None,
OpenFile(PathBuf),
PickFile,
}
impl<'a> FilesScreen {
pub fn new(file_paths: &'a [PathBuf]) -> Self {
let mut options = vec![
Panel::new(OptionId::NewFile, "New file", None, None),
Panel::new(OptionId::PickFile, "Open file...", None, None),
Panel::new(
OptionId::ImportFromActualbudget,
"Import from Actualbudget",
None,
None,
),
];
options.extend(
file_paths
.iter()
.map(|file_path| file_panel(file_path, None)),
);
Self {
options: Navigation::new(None, options),
}
}
}
fn file_panel<'a>(file_path: &'a PathBuf, err: Option<String>) -> Panel<OptionId> {
let file_name = file_path
.file_name()
.map(|os_str| os_str.to_str().unwrap_or("[invalid Unicode]"))
.unwrap_or("[invalid filename]");
Panel::new(
OptionId::OpenFile(file_path.clone()),
file_name,
file_path.to_str().map(String::from),
err.map(|err| format!("Failed to open new file {:#?}: {:#?}", file_path, err)),
)
}
|