Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Commit

Permalink
Add dialog to save before closing an unsaved file
Browse files Browse the repository at this point in the history
  • Loading branch information
maze-n committed May 3, 2020
1 parent 1f70ce5 commit 97d0c22
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 27 deletions.
53 changes: 34 additions & 19 deletions src/components/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use super::file_operations::*;
use super::misc::*;
use super::{Content, Header, SearchBox};
use super::{Content, Header, SearchBox, UnsavedDialog};
use crate::state::ActiveMetadata;
use gio::SettingsExt;
use gtk::SettingsExt as GTKSettingsExt;
Expand Down Expand Up @@ -83,14 +83,6 @@ impl App {
window.set_default_size(800, 600);
window.add(&window_box);

let cloned_window = window.clone();

window.connect_delete_event(move |_, _| {
before_quit(&cloned_window);
main_quit();
Inhibit(false)
});

App {
window,
header,
Expand All @@ -104,6 +96,7 @@ impl App {
let current_file = Arc::new(RwLock::new(None));
{
let save = &self.header.save;
self.window_quit(&self.window, current_file.clone());
self.theme_changed(&self.header.theme_switch);
self.editor_changed(current_file.clone(), &self.header.save.clone());
self.open_file(current_file.clone());
Expand All @@ -115,6 +108,37 @@ impl App {
ConnectedApp(self)
}

fn window_quit(&self, window: &Window, current_file: Arc<RwLock<Option<ActiveMetadata>>>) {
let window_clone = window.clone();
let save_button = self.header.save.clone();
let editor = self.content.buff.clone();
let headerbar = self.header.container.clone();

window.connect_delete_event(move |window, _| {
before_quit(&window_clone);
if save_button.get_sensitive() {
let dialog = UnsavedDialog::new(&window);
let result = dialog.run();
if result == ResponseType::Yes.into() {
if save_before_close(&editor, &headerbar, &save_button, &current_file) {
main_quit();
Inhibit(false)
} else {
Inhibit(true)
}
} else if result == ResponseType::No.into() {
main_quit();
Inhibit(false)
} else {
Inhibit(true)
}
} else {
main_quit();
Inhibit(false)
}
});
}

fn theme_changed(&self, theme_switch: &Switch) {
let settings = gio::Settings::new("com.github.maze-n.eddit");
let source_style_manager = self.content.style_manager.clone();
Expand Down Expand Up @@ -190,7 +214,6 @@ impl App {

fn find_replace(&self, find_button: &ToggleButton, revealer: &Revealer, search_entry: &SearchEntry) {
let revealer = revealer.clone();
//let case_sens_button = self.search_bar.case_sens_button.clone ();
let search_entry = search_entry.clone();
let up = self.search_bar.up.clone();
let down = self.search_bar.down.clone();
Expand All @@ -212,13 +235,6 @@ impl App {
search_settings_clone.set_search_text(Some(""));
});

//let search_settings_clone = search_settings.clone ();
//let case_sens_clone = case_sens_button.clone ();
//let search_flag_clone = search_flag.clone ();
//case_sens_clone.connect_toggled (move |case_sens_clone| {
// search_settings_clone.set_case_sensitive (case_sens_clone.get_active ());
//});

let up_clone = up.clone();
let down_clone = down.clone();
let search_settings_clone = search_settings.clone();
Expand Down Expand Up @@ -332,7 +348,6 @@ impl App {
let headerbar = self.header.container.clone();
let save_button = self.header.save.clone();
let find_button = self.header.find_button.clone();
let search_entry = self.search_bar.search_entry.clone();

self.window.connect_key_press_event(move |_, gdk| {
match gdk.get_keyval() {
Expand Down Expand Up @@ -367,4 +382,4 @@ impl ConnectedApp {
self.0.window.show_all();
gtk::main();
}
}
}
2 changes: 1 addition & 1 deletion src/components/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Content {
if let Some(font) = settings.get_string("font") {
config_sourceview(&view, font.as_str().to_string());
}
if let Some(gtk_settings) = Settings::get_default() {
if let Some(_) = Settings::get_default() {
let is_dark = settings.get_boolean("is-dark");
if is_dark {
style_manager
Expand Down
55 changes: 55 additions & 0 deletions src/components/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct OpenDialog(FileChooserDialog);

pub struct SaveDialog(FileChooserDialog);

pub struct UnsavedDialog(Dialog);

impl OpenDialog {
pub fn new(path: Option<PathBuf>) -> OpenDialog {
let open_dialog = FileChooserDialog::new(
Expand Down Expand Up @@ -77,6 +79,53 @@ impl SaveDialog {
}
}

impl UnsavedDialog {
pub fn new(window: &Window) -> UnsavedDialog {
let unsaved_dialog = Dialog::new_with_buttons(
Some("Warning"),
Some(window),
DialogFlags::DESTROY_WITH_PARENT,
&[],
);

let dialog_box = unsaved_dialog.get_content_area();

let dialog_grid = Box::new(Orientation::Horizontal, 20);
dialog_grid.set_border_width(20);

let warning_image = Image::new_from_icon_name(Some("dialog-warning"), IconSize::Dialog);
let head_label = Label::new(Some("Save the file before closing?"));
head_label.set_markup("<big><b>Save the file before closing?</b></big>");
let sub_label = Label::new(Some("Your changes will be lost if you don't save them."));

let label_box = Box::new(Orientation::Vertical, 4);
label_box.add(&head_label);
label_box.add(&sub_label);

dialog_grid.add(&warning_image);
dialog_grid.add(&label_box);

dialog_box.add(&dialog_grid);

let save_button = Button::new_with_label("Save");
save_button.get_style_context().add_class("suggested-action");

let unsave_button = Button::new_with_label("Don't Save");

unsaved_dialog.add_action_widget(&unsave_button, ResponseType::No);
unsaved_dialog.add_action_widget(&save_button, ResponseType::Yes);

unsaved_dialog.set_resizable(false);
unsaved_dialog.show_all();

UnsavedDialog(unsaved_dialog)
}

pub fn run(&self) -> ResponseType {
self.0.run()
}
}

impl Drop for OpenDialog {
fn drop(&mut self) {
self.0.destroy();
Expand All @@ -88,3 +137,9 @@ impl Drop for SaveDialog {
self.0.destroy();
}
}

impl Drop for UnsavedDialog {
fn drop(&mut self) {
self.0.destroy();
}
}
39 changes: 39 additions & 0 deletions src/components/file_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,45 @@ fn write_data(path: Option<&ActiveMetadata>, data: &[u8]) -> io::Result<SaveActi
}
}

pub fn save_before_close(
editor: &Buffer,
headerbar: &HeaderBar,
save: &Button,
current_file: &RwLock<Option<ActiveMetadata>>,
) -> bool
{
let mut is_saved = false;
if let Some(text) = get_buffer(editor) {
let result = write_data(current_file.read().unwrap().as_ref(), text.as_bytes());

match result {
Ok(SaveAction::New(file)) => {
set_title(headerbar, file.get_path());
if let Some(parent) = file.get_dir() {
let subtitle: &str = &parent.to_string_lossy();
headerbar.set_subtitle(Some(subtitle));
}

let mut current_file = current_file.write().unwrap();
*current_file = Some(file);
save.set_sensitive(false);
is_saved = true;
}

Ok(SaveAction::Saved) => {
if let Some(ref mut current_file) = *current_file.write().unwrap() {
current_file.set_sum(&text.as_bytes());
save.set_sensitive(false);
}
is_saved = true;
}

_ => (),
}
}
is_saved
}

pub fn open(editor: &Buffer, headerbar: &HeaderBar, current_file: &RwLock<Option<ActiveMetadata>>) {
let open_dialog = OpenDialog::new({
let lock = current_file.read().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ mod searchbox;

pub use self::app::App;
pub use self::content::Content;
pub use self::dialog::{OpenDialog, SaveDialog};
pub use self::dialog::{OpenDialog, SaveDialog, UnsavedDialog};
pub use self::header::Header;
pub use self::searchbox::SearchBox;
6 changes: 0 additions & 6 deletions src/components/searchbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub struct SearchBox {
pub replace_all_button: Button,
pub up: Button,
pub down: Button,
//pub case_sens_button: ToggleButton,
}

impl SearchBox {
Expand All @@ -49,11 +48,7 @@ impl SearchBox {
let down = Button::new_from_icon_name(Some("go-down-symbolic"), IconSize::SmallToolbar);
down.set_tooltip_text(Some("Next Item"));
down.set_sensitive(false);
//let case_sens_button = ToggleButton::new ();
//case_sens_button.set_image (Some (&Image::new_from_icon_name (Some ("font-x-generic-symbolic"), IconSize::SmallToolbar)));
//case_sens_button.set_tooltip_text (Some ("Set case sensitive searching"));

//search_grid.add (&case_sens_button);
search_grid.add(&search_entry);
search_grid.add(&down);
search_grid.add(&up);
Expand Down Expand Up @@ -86,7 +81,6 @@ impl SearchBox {
replace_all_button,
up,
down,
//case_sens_button,
}
}
}

0 comments on commit 97d0c22

Please sign in to comment.