Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 76 additions & 14 deletions src/Window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
public class AppWindow : Gtk.Window {
public File file { get; set; }
private Gtk.TextBuffer buf;
public string file_name { get; set; }
private Gtk.EditableLabel header_label;
public string file_name;

// Add a debounce so we aren't writing the entire buffer every character input
public int interval = 500; // ms
public uint debounce_timer_id = 0;

// We need to track this for specific cases
public bool is_unsaved_doc = false;


public AppWindow (File file) {
debug ("Constructing GUI");

Expand Down Expand Up @@ -51,6 +56,13 @@ public class AppWindow : Gtk.Window {
header.add_css_class (Granite.STYLE_CLASS_FLAT);
header.pack_start (actions_box);

header_label = new Gtk.EditableLabel ("") {
xalign = 0.5f,
text = ""
};

header.title_widget = header_label;

var text_view = new Gtk.TextView () {
cursor_visible = true,
editable = true,
Expand Down Expand Up @@ -86,30 +98,64 @@ public class AppWindow : Gtk.Window {

debug ("Binding window title to file_name");

bind_property ("file_name", this, "title");
// Window title, and header label, are file name
// We dont want to rename files with no save location yet
//
file.bind_property ("file_name", this, "title");
file.bind_property ("file_name", header_label, "text");
bind_property ("is_unsaved_doc", header_label, "sensitive", BindingFlags.INVERT_BOOLEANS);

debug ("Success!");

// We want to open the file in a new window when people drop files on this
var drop_target = new Gtk.DropTarget (typeof (Gdk.FileList), Gdk.DragAction.COPY);
add_controller (drop_target);

//TODO: Proper handler
drop_target.drop.connect ((target, value, x, y) => {
debug ("Drop event!");
if (value.type () == typeof (Gdk.FileList)) {
var list = (Gdk.FileList)value;

File[] file_array = {};
foreach (unowned var files in list.get_files ()) {
file_array += files;
}

Application.open (file_array);
return true;
}
return false;
});

open_file (file);
}


/* ---------------- FILE OPERATIONS ---------------- */
public void open_file (File file = this.file) {
this.file = file;

debug ("Attempting to open file %s", file.get_basename ());
try {
this.file_name = file.get_basename ();
var distream = new DataInputStream (file.read (null));
var contents = distream.read_upto ("", -1, null);

buf.set_text (contents);
this.file = file;
this.is_unsaved_doc = (Environment.get_user_data_dir () in this.file.get_path ());
this.header_label.tooltip_markup = this.file.get_path ();
this.file_name = file.get_basename ();

} catch (Error err) {
warning ("Couldn't open file: %s", err.message);
}
}

public void save_file (File file = this.file) {
if (Environment.get_user_data_dir () in this.file.get_path ()) {

// Check if datadir still there, recreate it if not
// So we prevent data loss in the event the user deletes everything during use
if (is_unsaved_doc) {
Application.check_if_datadir ();
}

Expand All @@ -126,38 +172,43 @@ public class AppWindow : Gtk.Window {

var contents = buf.text;
dostream.put_string (contents);

} catch (Error err) {
warning ("Couldn't save file: %s", err.message);
}
}


/* ---------------- HANDLERS ---------------- */
public void on_save_as () {
debug ("Save event!");
var save_dialog = new Gtk.FileDialog () { initial_name = file_name };
var save_dialog = new Gtk.FileDialog () { initial_name = this.file.get_basename () };
File oldfile = this.file;
bool delete_after = (Environment.get_user_data_dir () in this.file.get_path ());

save_dialog.save.begin (this, null, (obj, res) => {
try {

file = save_dialog.save.end (res);
file_name = file.get_basename ();
save_file (file);

if ((delete_after) && (oldfile != file)) {
// Only do after the operation, so we do not set this.file to something fucky
this.file = file;
this.file_name = file.get_basename ();

if ((this.unsaved_doc) && (oldfile != file)) {
oldfile.delete ();
}
this.is_unsaved_doc = (Environment.get_user_data_dir () in this.file.get_path ());


} catch (Error err) {
warning ("Failed to save file: %s", err.message);
}
});


}

public void on_buffer_changed () {
private void on_buffer_changed () {
debug ("The buffer has been modified, starting the debounce timer");

if (debounce_timer_id != 0) {
Expand All @@ -174,11 +225,9 @@ public class AppWindow : Gtk.Window {

}

public bool on_close () {
private bool on_close () {
debug ("Close event!");

bool is_unsaved_doc = (Environment.get_user_data_dir () in this.file.get_path ());

// We want to delete empty unsaved documents
if ((is_unsaved_doc) && (buf.text == "")) {

Expand All @@ -194,4 +243,17 @@ public class AppWindow : Gtk.Window {

return false;
}

private void on_title_changed () {
debug ("Renaming event!");

try {
this.file.move (File.new_for_path (header_label.text), File.CopyFlags.None);
this.file_name = this.file.get_basename ();

} catch (Error err) {
warning ("Failed to rename: %s", err.message);
}

}
}
Loading