confirm exit if unsaved

This commit is contained in:
Penwing 2024-01-25 13:11:49 +01:00
parent fee92f29a8
commit e7dae3f939
3 changed files with 46 additions and 12 deletions

View file

@ -30,11 +30,13 @@ impl Calcifer {
ui.separator(); ui.separator();
self.terminal_visible = self.toggle(ui, self.terminal_visible, "🖵"); self.terminal_visible = self.toggle(ui, self.terminal_visible, "🖵");
ui.separator(); ui.separator();
self.search_menu.visible = self.toggle(ui, self.search_menu.visible, "🔍");
ui.separator();
self.settings_menu.visible = self.toggle(ui, self.settings_menu.visible, ""); self.settings_menu.visible = self.toggle(ui, self.settings_menu.visible, "");
ui.separator(); ui.separator();
self.shortcuts_menu.visible = self.toggle(ui, self.shortcuts_menu.visible, ""); self.shortcuts_menu.visible = self.toggle(ui, self.shortcuts_menu.visible, "");
ui.separator(); ui.separator();
self.profiler_visible = self.toggle(ui, self.profiler_visible, "🗠"); self.profiler_visible = self.toggle(ui, self.profiler_visible, "");
}); });
}); });
} }
@ -79,6 +81,9 @@ impl Calcifer {
ui.label(""); ui.label("");
ui.horizontal(|ui| { ui.horizontal(|ui| {
if ui.add(egui::Button::new("")).clicked() {
self.command_history = vec![];
}
ui.style_mut().visuals.extreme_bg_color = bg_color; ui.style_mut().visuals.extreme_bg_color = bg_color;
let Self { command, .. } = self; let Self { command, .. } = self;
ui.colored_label(command_color.clone(), tools::format_path(&env::current_dir().expect("Could not find Shell Environnment"))); ui.colored_label(command_color.clone(), tools::format_path(&env::current_dir().expect("Could not find Shell Environnment")));
@ -182,12 +187,12 @@ impl Calcifer {
let lines = current_tab.code.chars().filter(|&c| c == '\n').count() + 1; let lines = current_tab.code.chars().filter(|&c| c == '\n').count() + 1;
let mut override_cursor : Option<CCursorRange> = None; let mut override_cursor : Option<CCursorRange> = None;
if !self.search.result_selected { if !self.search_menu.result_selected {
override_cursor = Some(CCursorRange::two( override_cursor = Some(CCursorRange::two(
CCursor::new(self.search.get_cursor_start()), CCursor::new(self.search_menu.get_cursor_start()),
CCursor::new(self.search.get_cursor_end()), CCursor::new(self.search_menu.get_cursor_end()),
)); ));
self.search.result_selected = true; self.search_menu.result_selected = true;
} }
CodeEditor::default().id_source("code editor") CodeEditor::default().id_source("code editor")
@ -200,8 +205,8 @@ impl Calcifer {
} }
pub fn draw_windows(&mut self, ctx: &egui::Context) { pub fn draw_windows(&mut self, ctx: &egui::Context) {
if self.search.visible { if self.search_menu.visible {
self.search.show(ctx, &mut self.tabs, &mut self.selected_tab); self.search_menu.show(ctx, &mut self.tabs, &mut self.selected_tab);
} }
if self.close_tab_confirm.visible { if self.close_tab_confirm.visible {
self.close_tab_confirm.show(ctx); self.close_tab_confirm.show(ctx);
@ -209,6 +214,15 @@ impl Calcifer {
if self.refresh_confirm.visible { if self.refresh_confirm.visible {
self.refresh_confirm.show(ctx); self.refresh_confirm.show(ctx);
} }
if self.exit_confirm.visible {
self.exit_confirm.show(ctx);
}
if self.exit_confirm.proceed {
for tab in self.tabs.iter_mut() {
tab.saved = true;
}
egui::Context::send_viewport_cmd(ctx, egui::ViewportCommand::Close);
}
if self.shortcuts_menu.visible { if self.shortcuts_menu.visible {
self.shortcuts_menu.show(ctx); self.shortcuts_menu.show(ctx);
} }

View file

@ -80,8 +80,9 @@ struct Calcifer {
close_tab_confirm: tools::confirm::ConfirmWindow, close_tab_confirm: tools::confirm::ConfirmWindow,
tab_to_close: usize, tab_to_close: usize,
refresh_confirm: tools::confirm::ConfirmWindow, refresh_confirm: tools::confirm::ConfirmWindow,
exit_confirm: tools::confirm::ConfirmWindow,
search: tools::search::SearchWindow, search_menu: tools::search::SearchWindow,
settings_menu: tools::settings::SettingsWindow, settings_menu: tools::settings::SettingsWindow,
shortcuts_menu: tools::shortcuts::ShortcutsWindow, shortcuts_menu: tools::shortcuts::ShortcutsWindow,
@ -109,8 +110,9 @@ impl Default for Calcifer {
close_tab_confirm: tools::confirm::ConfirmWindow::new("You have some unsaved changes, Do you still want to close this document ?", "Confirm Close"), close_tab_confirm: tools::confirm::ConfirmWindow::new("You have some unsaved changes, Do you still want to close this document ?", "Confirm Close"),
tab_to_close: 0, tab_to_close: 0,
refresh_confirm: tools::confirm::ConfirmWindow::new("You have some unsaved changes, Do you still want to refresh this document ?", "Confirm Refresh"), refresh_confirm: tools::confirm::ConfirmWindow::new("You have some unsaved changes, Do you still want to refresh this document ?", "Confirm Refresh"),
exit_confirm: tools::confirm::ConfirmWindow::new("", "Confirm Exit"),
search: tools::search::SearchWindow::default(), search_menu: tools::search::SearchWindow::default(),
settings_menu: tools::settings::SettingsWindow::new(DEFAULT_THEMES[0]), settings_menu: tools::settings::SettingsWindow::new(DEFAULT_THEMES[0]),
shortcuts_menu: tools::shortcuts::ShortcutsWindow::new(), shortcuts_menu: tools::shortcuts::ShortcutsWindow::new(),
@ -172,8 +174,26 @@ impl eframe::App for Calcifer {
} }
if ctx.input( |i| i.key_pressed(egui::Key::F) && i.modifiers.ctrl) { if ctx.input( |i| i.key_pressed(egui::Key::F) && i.modifiers.ctrl) {
self.search.visible = !self.search.visible.clone(); self.search_menu.visible = !self.search_menu.visible.clone();
self.search.initialized = !self.search.visible.clone(); self.search_menu.initialized = !self.search_menu.visible.clone();
}
if ctx.input(|i| i.viewport().close_requested()) {
let mut unsaved_tabs : Vec<usize> = vec![];
for (index, tab) in self.tabs.iter().enumerate() {
if !tab.saved {
unsaved_tabs.push(index);
}
}
if unsaved_tabs.len() > 0 {
let mut unsaved_tabs_names : String = "".to_string();
for index in unsaved_tabs.iter() {
unsaved_tabs_names.push_str(&self.tabs[*index].get_name());
}
egui::Context::send_viewport_cmd(ctx, egui::ViewportCommand::CancelClose);
self.exit_confirm.prompt = format!("You have some unsaved changes :\n{}\nDo you still want to exit ?", unsaved_tabs_names);
self.exit_confirm.ask();
}
} }
self.time_watch[0] = watch.elapsed().as_micros() as f32 / 1000.0; self.time_watch[0] = watch.elapsed().as_micros() as f32 / 1000.0;

View file

@ -4,7 +4,7 @@ use eframe::egui;
pub struct ConfirmWindow { pub struct ConfirmWindow {
pub visible: bool, pub visible: bool,
pub proceed: bool, pub proceed: bool,
prompt: String, pub prompt: String,
id: String, id: String,
} }