Skip to content

Commit

Permalink
1.0.0 - Improve New Document button handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Lains committed Jun 3, 2017
1 parent 78b45a1 commit 4befcca
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ namespace Quilter {
settings.window_x = x;
settings.window_y = y;

Utils.FileUtils.save_tmp_file ();
return false;
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Utils/DialogUtils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,19 @@ namespace Quilter.Utils.DialogUtils {
chooser.destroy();
return file;
}

private int display_save_confirm () {
var dialog = new Gtk.MessageDialog (null, Gtk.DialogFlags.MODAL,
Gtk.MessageType.WARNING, Gtk.ButtonsType.NONE, "<b>" +
_("There are unsaved changes. Do you want to save?") + "</b>" +
"\n\n" + _("If you don't save, changes will be lost forever."));
dialog.use_markup = true;
dialog.type_hint = Gdk.WindowTypeHint.DIALOG;
dialog.add_button (Gtk.Stock.CANCEL, Gtk.ResponseType.CANCEL);
dialog.add_button (Gtk.Stock.SAVE, Gtk.ResponseType.YES);
dialog.set_default_response (Gtk.ResponseType.ACCEPT);
int response = dialog.run ();
dialog.destroy();
return response;
}
}
48 changes: 45 additions & 3 deletions src/Widgets/Toolbar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Quilter.Widgets {
private Widgets.Preferences preferences_dialog;

public File file;
public Quilter.MainWindow win;

public Toolbar() {
var header_context = this.get_style_context ();
Expand All @@ -41,8 +42,7 @@ namespace Quilter.Widgets {
new_button.tooltip_text = (_("New file"));

new_button.clicked.connect(() => {
Widgets.SourceView.buffer.set_text ("");
Utils.FileUtils.save_tmp_file ();
new_button_pressed ();
});

save_button = new Gtk.Button ();
Expand Down Expand Up @@ -92,6 +92,22 @@ namespace Quilter.Widgets {
this.show_all ();
}

public void new_button_pressed () {


if (Widgets.SourceView.is_modified = true) {
try {
debug ("Opening file...");
new_document ();
} catch (Error e) {
error ("Unexpected error during open: " + e.message);
}
}

file = null;
Widgets.SourceView.is_modified = false;
}

public void open_button_pressed () {
debug ("Open button pressed.");

Expand Down Expand Up @@ -124,6 +140,33 @@ namespace Quilter.Widgets {
Widgets.SourceView.is_modified = false;
}

public bool new_document() throws Error {
if (Widgets.SourceView.is_modified) {
// Ask the user for save before loading a new file.
debug ("Buffer was modified. Asking user to save first.");
int wanna_save = Utils.DialogUtils.display_save_confirm ();
if (wanna_save == Gtk.ResponseType.CANCEL ||
wanna_save == Gtk.ResponseType.DELETE_EVENT) {
debug ("User canceled save confirm. Aborting operation.");
}

if (wanna_save == Gtk.ResponseType.YES) {
debug ("Saving file before loading new file.");
try {
bool was_saved = save_document ();
if (!was_saved) {
debug ("Cancelling open document too.");
Widgets.SourceView.buffer.text = "";
}
} catch (Error e) {
error ("Unexpected error during save: " + e.message);
}
}
}
Utils.FileUtils.save_tmp_file ();
return true;
}

public bool open_document () throws Error {
// If it's a file, ask the user for a valid location.
if (file == null) {
Expand All @@ -138,7 +181,6 @@ namespace Quilter.Widgets {

string text;
FileUtils.get_contents (file.get_path(), out text);

Widgets.SourceView.buffer.text = text;
return true;
}
Expand Down

0 comments on commit 4befcca

Please sign in to comment.