ask_close_file_dialog.rs - icy_draw - icy_draw is the successor to mystic draw. fork / mirror
(HTM) git clone https://git.drkhsh.at/icy_draw.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
ask_close_file_dialog.rs (3097B)
---
1 use eframe::egui;
2 use egui_modal::Modal;
3 use egui_tiles::TileId;
4 use i18n_embed_fl::fl;
5
6 use crate::{util::autosave::remove_autosave, MainWindow, Message, SaveFileDialog, TerminalResult};
7
8 pub struct AskCloseFileDialog {
9 do_commit: bool,
10 save: bool,
11
12 id: TileId,
13 path: Option<std::path::PathBuf>,
14 }
15
16 impl AskCloseFileDialog {
17 pub(crate) fn new(path: Option<std::path::PathBuf>, id: TileId) -> Self {
18 Self {
19 save: false,
20 do_commit: false,
21 path,
22 id,
23 }
24 }
25 }
26
27 impl crate::ModalDialog for AskCloseFileDialog {
28 fn show(&mut self, ctx: &egui::Context) -> bool {
29 let mut result = false;
30 let modal = Modal::new(ctx, "ask_close_file_dialog");
31
32 modal.show(|ui| {
33 let file_name = if let Some(file_name) = &self.path {
34 file_name.file_name().unwrap().to_string_lossy().to_string()
35 } else {
36 fl!(crate::LANGUAGE_LOADER, "unsaved-title")
37 };
38
39 modal.frame(ui, |ui| {
40 ui.strong(fl!(crate::LANGUAGE_LOADER, "ask_close_file_dialog-description", filename = file_name));
41 ui.small(fl!(crate::LANGUAGE_LOADER, "ask_close_file_dialog-subdescription"));
42 });
43
44 modal.buttons(ui, |ui| {
45 if ui.button(fl!(crate::LANGUAGE_LOADER, "ask_close_file_dialog-save_button")).clicked() {
46 self.save = true;
47 self.do_commit = true;
48 result = true;
49 }
50 if ui.button(fl!(crate::LANGUAGE_LOADER, "new-file-cancel")).clicked() {
51 result = true;
52 }
53 if ui.button(fl!(crate::LANGUAGE_LOADER, "ask_close_file_dialog-dont_save_button")).clicked() {
54 self.save = false;
55 self.do_commit = true;
56 result = true;
57 }
58 });
59 });
60 modal.open();
61 result
62 }
63
64 fn should_commit(&self) -> bool {
65 self.do_commit
66 }
67
68 fn commit_self(&self, window: &mut MainWindow<'_>) -> TerminalResult<Option<Message>> {
69 let mut msg = None;
70 if self.save {
71 if self.path.is_none() {
72 window.open_dialog(SaveFileDialog::new(None));
73 return Ok(None);
74 }
75 if let Some(egui_tiles::Tile::Pane(pane)) = window.document_tree.tiles.get_mut(self.id) {
76 // TODO: potential message clash. Maybe we should return a Vec<Message> instead? OR a Vec MessageType?
77 msg = pane.save();
78 let msg2 = pane.destroy(&window.gl);
79 if msg2.is_some() {
80 msg = msg2;
81 }
82 }
83 } else if let Some(egui_tiles::Tile::Pane(pane)) = window.document_tree.tiles.get_mut(self.id) {
84 msg = pane.destroy(&window.gl);
85 }
86 window.document_tree.tiles.remove(self.id);
87 if let Some(path) = &self.path {
88 remove_autosave(path);
89 }
90 Ok(msg)
91 }
92 }