66public class AppWindow : Gtk .Window {
77 public File file { get ; set ; }
88 private Gtk . TextBuffer buf;
9+ private Gtk . HeaderBar header;
910 public string file_name { get ; set ; }
1011
1112 // Add a debounce so we aren't writing the entire buffer every character input
@@ -44,9 +45,9 @@ public class AppWindow : Gtk.Window {
4445 actions_box.append (open_button );
4546 actions_box.append (save_as_button );
4647
47-
4848 var header = new Gtk .HeaderBar () {
49- show_title_buttons = true
49+ show_title_buttons = true ,
50+ tooltip_text = " "
5051 };
5152 header.add_css_class (Granite .STYLE_CLASS_FLAT );
5253 header.pack_start (actions_box );
@@ -76,41 +77,43 @@ public class AppWindow : Gtk.Window {
7677 default_width = 300 ;
7778 titlebar = header;
7879
79- debug ("Connecting signals ");
80+ debug ("Binding window title to file_name ");
81+ bind_property ("file_name ", this , "title ");
82+ debug ("Success !");
83+
84+ open_file (file );
8085
86+ debug ("Connecting signals ");
8187 // Signal callbacks are heavily derived from similar operations in
8288 // elementary/code
8389 save_as_button.clicked.connect (on_save_as );
8490 this.close_request.connect (on_close );
8591 buf.changed.connect (on_buffer_changed );
8692
87- debug ("Binding window title to file_name ");
88-
89- bind_property ("file_name ", this , "title ");
90-
91- debug ("Success !");
92-
93- open_file (file );
9493 }
9594
9695
9796 /* ---------------- FILE OPERATIONS ---------------- */
9897 public void open_file (File file = this .file ) {
99- this . file = file;
10098 debug (" Attempting to open file %s " , file. get_basename ());
99+
101100 try {
102- this . file_name = file. get_basename ();
103101 var distream = new DataInputStream (file. read (null ));
104102 var contents = distream. read_upto (" " , - 1 , null );
105- buf. set_text (contents);
103+ buf. set_text (contents ?? " " );
104+
105+ this . file = file;
106+ this . file_name = file. get_basename ();
107+ this . tooltip_text = file. get_path ();
108+
106109 } catch (Error err) {
107110 warning (" Couldn't open file: %s " , err. message);
108111 }
109112 }
110113
111114 public void save_file (File file = this .file ) {
112115 if (Environment . get_user_data_dir () in this . file. get_path ()) {
113- Application . check_if_datadir ();
116+ Utils . check_if_datadir ();
114117 }
115118
116119 try {
@@ -126,27 +129,34 @@ public class AppWindow : Gtk.Window {
126129
127130 var contents = buf. text;
128131 dostream. put_string (contents);
132+
129133 } catch (Error err) {
130134 warning (" Couldn't save file: %s " , err. message);
131135 }
132136 }
133137
134-
135138 /* ---------------- HANDLERS ---------------- */
136139 public void on_save_as () {
137140 debug (" Save event!" );
138- var save_dialog = new Gtk . FileDialog () { initial_name = file_name };
141+
139142 File oldfile = this . file;
140- bool delete_after = (Environment . get_user_data_dir () in this . file. get_path ());
143+ bool is_unsaved_doc = (Environment . get_user_data_dir () in this . file. get_path ());
144+
145+ var save_dialog = new Gtk .FileDialog () {
146+ initial_name = (is_unsaved_doc ? file_name + " .txt" : file_name)
147+ };
141148
142149 save_dialog. save. begin (this , null , (obj, res) = > {
143150 try {
144151
145152 file = save_dialog. save. end (res);
146- file_name = file. get_basename ();
147153 save_file (file);
148154
149- if ((delete_after) && (oldfile != file)) {
155+ this . file = file;
156+ file_name = file. get_basename ();
157+ this . tooltip_text = file. get_path ();
158+
159+ if ((is_unsaved_doc) && (oldfile != file)) {
150160 oldfile. delete ();
151161 }
152162
@@ -184,6 +194,7 @@ public class AppWindow : Gtk.Window {
184194
185195 try {
186196 this . file. delete ();
197+
187198 } catch (Error err) {
188199 warning (" Failed to delete empty temp file: %s " , err. message);
189200 }
0 commit comments