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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::RawModuleLocation;
use super::{Details, Payload, Task};
/// A helper struct for sending progress updates to the Tauri frontend.
///
/// This struct is only available when the `tauri` feature is enabled.
#[cfg(feature = "tauri")]
#[allow(clippy::module_name_repetitions)]
pub struct ProgressHelper {
/// The estimated total number of steps that will be taken.
estimated_total: usize,
/// The current step index.
current_index: usize,
/// The Tauri window to send updates to.
tauri_window: tauri::Window,
/// The progress cache.
progress_cache: Payload,
}
#[cfg(feature = "tauri")]
impl ProgressHelper {
/// Create a new `ProgressHelper` with the given Tauri window.
///
/// # Parameters
///
/// * `window` - The Tauri window to send updates to.
pub fn with_tauri_window(window: tauri::Window) -> Self {
Self {
estimated_total: 1,
current_index: 0,
tauri_window: window,
progress_cache: Payload::default(),
}
}
/// Add the given number of steps to the estimated total.
///
/// # Parameters
///
/// * `amount` - The number of steps to add.
pub fn add_steps(&mut self, amount: usize) {
self.estimated_total += amount;
}
/// Reset the details of the progress.
///
/// This will reset the module, file, file location, and module location.
pub fn reset_details(&mut self) {
self.progress_cache.details = Details::default();
}
/// Set the current task.
///
/// # Parameters
///
/// * `task` - The current task.
pub fn set_task(&mut self, task: Task) {
self.progress_cache.current_task = task;
}
/// Set the module location being processed.
///
/// # Parameters
///
/// * `location` - The module location within the Dwarf Fortress directory.
pub fn set_location(&mut self, location: RawModuleLocation) {
self.progress_cache.details.location = location;
}
/// Set the name of the module currently being processed.
///
/// # Parameters
///
/// * `module` - The name of the module currently being processed.
pub fn set_module(&mut self, module: &str) {
self.progress_cache.details.module = Some(String::from(module));
}
/// Set the name of the file currently being processed.
///
/// # Parameters
///
/// * `file` - The name of the file currently being processed.
pub fn set_file(&mut self, file: &str) {
self.progress_cache.details.raw_file = Some(String::from(file));
}
/// Set the file location of the file currently being processed.
///
/// # Parameters
///
/// * `file_location` - The file location of the file currently being processed.
pub fn set_file_location(&mut self, file_location: &str) {
self.progress_cache.details.file_location = Some(String::from(file_location));
}
/// Add some amount of progress to the running total.
///
/// This should be tracking the estimated number of raws and then this helps calculate the percentage.
///
/// # Parameters
///
/// * `amount` - The amount of progress to add to the running total.
pub fn add_to_running_total(&mut self, amount: usize) {
self.progress_cache.running_total += amount;
}
/// Advance the current step index.
///
/// This can be called after each step for convenience. It adds one to the current step index and updates the percentage.
#[allow(clippy::cast_precision_loss)]
fn step_advance(&mut self) {
self.current_index += 1;
self.progress_cache.percentage = self.current_index as f64 / self.estimated_total as f64;
}
/// Advance the current step index and send an update to the Tauri frontend.
pub fn advance_and_send_update(&mut self) {
self.step_advance();
self.send_update();
}
/// Send an update to the Tauri frontend with the current progress.
pub fn send_update(&mut self) {
use tauri::Manager;
self.tauri_window
.emit("progress", self.progress_cache.clone())
.unwrap_or_else(|e| tracing::debug!("Tauri window emit error {:?}", e));
}
/// Update the current task/details/index/percentage to values that would be expected for the end of the process.
///
/// This should be called when the process is complete. It updates all detail values to `None` and sets the current task to `Task::Idle`.
///
/// This also sets the percentage to 1.0 and the current index to the estimated total.
pub fn finalize(&mut self) {
self.progress_cache.details.raw_file = None;
self.progress_cache.details.file_location = None;
self.progress_cache.details.module = None;
self.progress_cache.details.location = RawModuleLocation::Unknown;
self.progress_cache.current_task = Task::Idle;
self.progress_cache.percentage = 1.0;
self.estimated_total = self.current_index;
}
/// Calls `finalize` and then sends the final update to the Tauri frontend.
pub fn finalize_and_send(&mut self) {
self.finalize();
self.send_update();
}
/// Send a summary of parsed objects to the Tauri frontend.
///
/// # Parameters
///
/// * `summary` - A map of object types to the number of objects parsed.
pub(crate) fn send_summary(
&self,
summary: &std::collections::HashMap<crate::ObjectType, usize>,
) {
use tauri::Manager;
self.tauri_window
.emit("SUMMARY", summary.clone())
.unwrap_or_else(|e| tracing::debug!("Tauri window emit error {:?}", e));
}
}