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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use logisheets_base::async_func::{AsyncCalcResult, Task};
use logisheets_base::{CellId, SheetId};
use logisheets_workbook::prelude::{read, SerdeErr};
pub mod display;
pub mod edit_action;
pub mod status;
mod transaction;
mod viewer;
use crate::file_loader2::load;
use crate::payloads::sheet_shift::{SheetShiftPayload, SheetShiftType};
use crate::payloads::Process;
use crate::settings::Settings;
use edit_action::{ActionEffect, Converter};
use status::Status;
use transaction::{Transaction, TransactionContext};
use viewer::SheetViewer;
use self::display::{DisplayRequest, DisplayResponse};
use crate::async_func_manager::AsyncFuncManager;
use edit_action::EditAction;
pub struct Controller {
pub status: Status,
pub async_func_manager: AsyncFuncManager,
pub curr_book_name: String,
pub settings: Settings,
pub undo_stack: Vec<Status>,
pub redo_stack: Vec<Status>,
}
impl Default for Controller {
fn default() -> Self {
let mut empty = Controller {
status: Status::default(),
curr_book_name: String::from("Book1"),
settings: Settings::default(),
undo_stack: vec![],
redo_stack: vec![],
async_func_manager: AsyncFuncManager::default(),
};
let add_sheet = Process::SheetShift(SheetShiftPayload {
idx: 0,
ty: SheetShiftType::Insert,
});
empty.handle_process(vec![add_sheet], false);
empty
}
}
impl Controller {
pub fn save(&self) -> Vec<u8> {
vec![]
}
pub fn from(status: Status, book_name: String, settings: Settings) -> Self {
Controller {
curr_book_name: book_name,
redo_stack: Vec::new(),
settings,
status,
undo_stack: Vec::new(),
async_func_manager: AsyncFuncManager::default(),
}
}
pub fn from_file(name: String, f: &[u8]) -> Result<Self, SerdeErr> {
let res = read(f);
match res {
Ok(ss) => Ok(load(ss, name)),
Err(e) => Err(e),
}
}
pub fn get_sheet_id_by_idx(&self, idx: usize) -> Option<SheetId> {
self.status.sheet_pos_manager.get_sheet_id(idx)
}
pub fn get_sheet_id_by_name(&self, name: &str) -> Option<SheetId> {
self.status.sheet_id_manager.has(name)
}
pub fn handle_action(&mut self, action: EditAction, undoable: bool) -> Option<ActionEffect> {
match action {
EditAction::Undo => match self.undo() {
true => Some(ActionEffect::default()),
false => None,
},
EditAction::Redo => match self.redo() {
true => Some(ActionEffect::default()),
false => None,
},
EditAction::Payloads(payloads) => {
let mut c = Converter {
sheet_pos_manager: &self.status.sheet_pos_manager,
navigator: &mut self.status.navigator,
container: &mut self.status.container,
text_id_manager: &mut self.status.text_id_manager,
};
let proc = c.convert_edit_payloads(payloads);
self.handle_process(proc, undoable);
let (tasks, dirties) = self.async_func_manager.get_calc_tasks();
Some(ActionEffect {
sheets: vec![],
async_tasks: tasks,
dirtys: dirties,
})
}
}
}
pub fn handle_async_calc_results(
&mut self,
tasks: Vec<Task>,
res: Vec<AsyncCalcResult>,
dirtys: Vec<(SheetId, CellId)>,
) -> Option<ActionEffect> {
tasks.into_iter().zip(res.into_iter()).for_each(|(t, r)| {
self.async_func_manager.add_value(t, r);
});
self.handle_process(vec![Process::Recalc(dirtys)], false);
Some(ActionEffect::default())
}
fn handle_process(&mut self, proc: Vec<Process>, undoable: bool) {
let context = TransactionContext {
book_name: &self.curr_book_name,
calc_config: self.settings.calc_config.clone(),
async_funcs: &self.settings.async_funcs,
};
let transcation = Transaction {
async_func_manager: &mut self.async_func_manager,
status: self.status.clone(),
context,
proc,
};
let mut new_status = transcation.start();
std::mem::swap(&mut new_status, &mut self.status);
if undoable {
self.undo_stack.push(new_status);
}
}
pub fn get_display_response(&mut self, req: DisplayRequest) -> DisplayResponse {
let viewer = SheetViewer::default();
let response = viewer.display(self, req.sheet_idx);
response
}
pub fn undo(&mut self) -> bool {
match self.undo_stack.pop() {
Some(mut last_status) => {
std::mem::swap(&mut self.status, &mut last_status);
self.redo_stack.push(last_status);
true
}
None => false,
}
}
pub fn redo(&mut self) -> bool {
match self.redo_stack.pop() {
Some(mut next_status) => {
std::mem::swap(&mut self.status, &mut next_status);
self.undo_stack.push(next_status);
true
}
None => false,
}
}
}
#[cfg(test)]
mod tests {
use super::{
edit_action::{CellInput, EditAction, EditPayload},
Controller,
};
#[test]
fn controller_default_test() {
let wb = Controller::default();
println!("{:?}", wb.status);
}
#[test]
fn controller_input_formula() {
let mut wb = Controller::default();
let action = EditAction::Payloads(vec![EditPayload::CellInput(CellInput {
sheet_idx: 0,
row: 0,
col: 0,
content: String::from("=ABS(1)"),
})]);
wb.handle_action(action, true);
let len = wb.status.vertex_manager.status.formulas.len();
assert_eq!(len, 1);
}
#[test]
fn from_file_test() {
use std::fs;
let buf = fs::read("../../tests/6.xlsx").unwrap();
let controller = Controller::from_file(String::from("6"), &buf);
match controller {
Ok(_) => {}
Err(_) => {
panic!()
}
}
}
}