Implement the 'always-saved' feature both write.as and elementary pride themselves on.

8min.
This commit is contained in:
Adrian Cochrane 2018-04-18 13:09:14 +12:00
parent e91fb6601b
commit 150f20035d

View File

@ -10,12 +10,17 @@ public class WriteAs.MainWindow : Gtk.ApplicationWindow {
build_keyboard_shortcuts();
canvas = new Gtk.TextView();
canvas.wrap_mode = Gtk.WrapMode.WORD_CHAR;
add(canvas);
canvas.event_after.connect((evt) => {
// TODO This word count algorithm may be quite naive
// and could do improvement.
var word_count = canvas.buffer.text.split(" ").length;
title = ngettext("%i word","%i words",word_count).printf(word_count);
draft_file().replace_contents(canvas.buffer.text.data, null, false,
FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
null);
});
adjust_text_style();
@ -23,10 +28,18 @@ public class WriteAs.MainWindow : Gtk.ApplicationWindow {
public MainWindow(Gtk.Application app) {
set_application(app);
try {
open_file(draft_file());
} catch (Error err) {/* It's fine... */}
set_default_size(800, 600);
}
private static File draft_file() {
var home = File.new_for_path(Environment.get_home_dir());
return home.get_child(".writeas-draft.txt");
}
private void construct_toolbar() {
var header = new Gtk.HeaderBar();
header.show_close_button = true;