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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use std::path::Path;

use tauri::{
    api::dialog::{FileDialogBuilder, MessageDialogButtons, MessageDialogKind},
    async_runtime::block_on,
    CustomMenuItem,
    Manager,
    Menu,
    Submenu,
    Window,
    WindowMenuEvent,
};

use super::display_dialog;
use crate::{
    dprintln,
    interface::storage::FileShareStatus::{Client, Server},
    io::file_io,
    modules::riscv::basic::interface::{
        assembler::RiscVAssembler,
        parser::{RISCVExtension, RISCVParser},
    },
    simulator::simulator::RISCVSimulator,
    storage::rope_store,
    types::{
        menu_types,
        middleware_types::{Tab, TabMap},
        rpc_types::RpcState,
        ResultVoid,
    },
    utility::{
        ptr::Ptr,
        state_helper::event::{get_current_tab_name, set_current_tab_name},
    },
    APP_HANDLE,
};

pub fn new() -> Submenu {
    Submenu::new(
        "File",
        Menu::with_items([
            CustomMenuItem::new("file_open", "Open").into(),
            CustomMenuItem::new("file_save", "Save").into(),
            CustomMenuItem::new("file_save_as", "Save As...").into(),
            CustomMenuItem::new("file_share", "Share").into(),
            CustomMenuItem::new("file_close", "Close Tab").into(),
            CustomMenuItem::new("file_exit", "Exit").into(),
        ]),
    )
}

pub fn event_handler(event: WindowMenuEvent) {
    match event.menu_item_id().strip_prefix("file_").unwrap() {
        "open" => {
            open_handler(event);
        }
        "save" => {
            save_handler(&event);
        }
        "save_as" => {
            save_as_handler(event);
        }
        "share" => {
            share_handler(&event);
        }
        "close" => {
            close_handler(&event);
        }
        "exit" => {
            exit_handler(&event);
        }
        _ => {
            println!("Unknown file menu item {}", event.menu_item_id());
        }
    }
}

/// event emit: front_file_open
/// payload: OpenFile { file_path: String, content: String }
fn open_handler(event: WindowMenuEvent) {
    let picker = FileDialogBuilder::new();
    picker.pick_file(move |file_path| match file_path {
        Some(file_path) => {
            if let Some(_) = event
                .window()
                .state::<TabMap>()
                .tabs
                .lock()
                .unwrap()
                .get(file_path.to_str().unwrap())
            {
                display_dialog(
                    MessageDialogKind::Info,
                    MessageDialogButtons::Ok,
                    format!(
                        "{} has already opened",
                        file_path.file_name().unwrap().to_str().unwrap()
                    )
                    .as_str(),
                    format!("Full path: {}", file_path.to_str().unwrap()).as_str(),
                    |_| {},
                );
                return;
            }
            match new_tab(&event, file_path.as_path()) {
                Ok(_) => {
                    let name = get_current_tab_name(&event);
                    let tab_map = event.window().state::<TabMap>();
                    let lock = tab_map.tabs.lock().unwrap();
                    let tab = lock.get(&name).unwrap();
                    let content = tab.text.to_string();
                    event
                        .window()
                        .emit(
                            "front_file_open",
                            menu_types::OpenFile {
                                file_path: file_path.to_str().unwrap().to_owned(),
                                content,
                            },
                        )
                        .unwrap();
                }
                Err(e) => display_dialog(
                    MessageDialogKind::Info,
                    MessageDialogButtons::Ok,
                    format!("Failed to open {}", file_path.to_str().unwrap()).as_str(),
                    &e.to_string(),
                    |_| {},
                ),
            }
        }
        _ => {}
    });
}

/// event emit: front_file_save
/// payload: String fn save_handler(event: &WindowMenuEvent) { let name =
/// get_current_tab_name(&event); let tab_map =
/// event.window().state::<TabMap>(); let mut lock =
/// tab_map.tabs.lock().unwrap();
fn save_handler(event: &WindowMenuEvent) {
    let tab_map = event.window().state::<TabMap>();
    if tab_map.tabs.lock().unwrap().len() == 0 {
        return;
    }
    let name = get_current_tab_name(&event);
    let mut lock = tab_map.tabs.lock().unwrap();
    let tab = lock.get_mut(&name).unwrap();
    match tab.text.save() {
        Ok(_) => {
            let _ = event
                .window()
                .emit("front_file_save", get_current_tab_name(&event));
        }
        Err(e) => {
            display_dialog(
                MessageDialogKind::Info,
                MessageDialogButtons::Ok,
                "Failed to save file",
                &e.to_string(),
                |_| {},
            );
        }
    }
}

/// event emit: front_file_save_as
/// payload: String
/// FIXME: the emit event maybe unused?
fn save_as_handler(event: WindowMenuEvent) {
    let content = {
        let name = get_current_tab_name(&event);
        let tab_map = event.window().state::<TabMap>();
        let lock = tab_map.tabs.lock().unwrap();
        let tab = lock.get(&name).unwrap();
        tab.text.to_string()
    };
    let picker = FileDialogBuilder::new();
    picker.save_file(move |file_path| match file_path {
        Some(file_path) => match file_io::write_file(file_path.as_path(), &content) {
            Ok(_) => {
                event.window().emit("front_file_save_as", true).unwrap();
            }
            Err(e) => {
                display_dialog(
                    MessageDialogKind::Info,
                    MessageDialogButtons::Ok,
                    "Failed to save file",
                    &e.to_string(),
                    |_| {},
                );
            }
        },
        _ => {}
    });
}

fn share_handler(event: &WindowMenuEvent) {
    let handle_lock = APP_HANDLE.lock().unwrap();
    let app_handle = handle_lock.as_ref().unwrap();
    let _window = tauri::WindowBuilder::new(
        app_handle,
        "live_share",
        tauri::WindowUrl::App("/share".parse().unwrap()),
    )
    .title("Live Share")
    .menu(Menu::new())
    .build();
}

fn close_handler(event: &WindowMenuEvent) {
    let window = event.window();
    let tab_map = window.state::<TabMap>();
    let name = get_current_tab_name(event);
    let mut lock = tab_map.tabs.lock().unwrap();
    match lock.get_mut(&name) {
        Some(tab) => {
            close_checker(event.window(), &name, tab);
        }
        None => {}
    }
}

/// Iterate all tabs, check if each tab is dirty, if so, display a dialog to ask
/// user to save the file or not. If user choose to save, save the file and then
/// close the tab. If user choose not to save, close the tab directly.
///
/// This function will emit a `front_close_tab` event to the window, and
/// may emit multiple times if there are multiple dirty tabs need to be closed.
fn exit_handler(event: &WindowMenuEvent) {
    let window = event.window();
    let tab_map = window.state::<TabMap>();
    let mut lock = tab_map.tabs.lock().unwrap();
    for (name, tab) in lock.iter_mut() {
        close_checker(event.window(), name, tab);
    }
    window.app_handle().exit(0);
}

/// Create a new tab with the file in provided file path
///
/// Return None if success, Some(err) if failed.
/// If success, will set the current tab name to the opened file path.
fn new_tab(event: &WindowMenuEvent, file_path: &Path) -> ResultVoid {
    let content = rope_store::Text::from_path(file_path)?;
    let tab_map = event.window().state::<TabMap>();
    let tab = Tab {
        text: Box::new(content),
        parser: Box::new(RISCVParser::new(&vec![RISCVExtension::RV32I])),
        assembler: Box::new(RiscVAssembler::new()),
        simulator: Box::new(RISCVSimulator::new(file_path.to_str().unwrap())),
        assembly_cache: Default::default(),
    };
    tab_map
        .tabs
        .lock()
        .unwrap()
        .insert(file_path.to_str().unwrap().to_string(), tab);
    set_current_tab_name(&event, file_path.to_str().unwrap());
    Ok(())
}

/// Check if the file is dirty, if so, display a dialog to ask user to save the
/// file or not. If user choose to save, save the file and then close the tab.
/// If user choose not to save, close the tab directly.
///
/// This function will emit a `front_close_tab` event to the window.
pub fn close_checker(window: &Window, name: &str, tab: &mut Tab) {
    let tab_ptr = Ptr::new(tab);
    let window_ptr = Ptr::new(window);
    let mut text = &mut tab.text;
    let share_status = text.get_share_status();
    if share_status == Server {
        window
            .state::<RpcState>()
            .rpc_server
            .lock()
            .unwrap()
            .stop_server();
    } else if share_status == Client {
        if let Err(e) = block_on(
            window
                .state::<RpcState>()
                .rpc_client
                .lock()
                .unwrap()
                .send_disconnect(),
        ) {
            dprintln!("{}", e.to_string());
        }
        window
            .state::<RpcState>()
            .rpc_client
            .lock()
            .unwrap()
            .stop()
            .unwrap();
        return;
    }
    if text.is_dirty() {
        display_dialog(
            MessageDialogKind::Warning,
            MessageDialogButtons::YesNo,
            "Warning",
            format!(
                "File {} is modified but not save, are you sure to exit",
                name
            )
            .as_str(),
            move |save| {
                if save {
                    let tab = tab_ptr.as_mut();
                    match tab.text.save() {
                        Ok(_) => {}
                        Err(e) => {
                            display_dialog(
                                MessageDialogKind::Info,
                                MessageDialogButtons::Ok,
                                "Failed to save file",
                                &e.to_string(),
                                |_| {},
                            );
                        }
                    }
                }
                let _ = window_ptr.as_mut().emit("front_close_tab", true);
            },
        )
    }
}