Skip to content

Commit 0e11f2c

Browse files
committed
Add managing several unsaved documents
1 parent d1a3871 commit 0e11f2c

File tree

4 files changed

+104
-44
lines changed

4 files changed

+104
-44
lines changed

src/Application.vala

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const string APP_ID = "io.github.wpkelso.slate";
88

99
public class Application : Gtk.Application {
1010

11-
uint created_documents = 1;
11+
public static uint created_documents = 1;
1212

1313
public Application () {
1414
Object (
@@ -43,6 +43,7 @@ public class Application : Gtk.Application {
4343
"new-document",
4444
null
4545
);
46+
4647
new_document_action.activate.connect (() => {
4748
var name = get_new_document_name ();
4849
var path = Path.build_filename (Environment.get_user_data_dir (), name);
@@ -86,21 +87,37 @@ public class Application : Gtk.Application {
8687
}
8788

8889
protected override void activate () {
89-
var name = get_new_document_name ();
90-
var path = Path.build_filename (Environment.get_user_data_dir (), name);
91-
var file = File.new_for_path (path);
92-
debug (
93-
"Document is unsaved, creating an save location at: %s",
94-
path
95-
);
9690

97-
var new_window = new AppWindow () {
98-
is_new = true,
99-
};
100-
new_window.open_file (file);
91+
Slate.Utils.check_if_datadir ();
92+
saved_unsaved_documents = Environment.get_user_data_dir ();
93+
pile_unsaved_documents = saved_unsaved_documents.open ();
94+
95+
// Conveniently, if there is no unsaved document, we just get NULL
96+
// Which AppWindow will process as a new unsaved doc
97+
foreach (unsaved_document in pile_unsaved_documents.read_name() ) {
98+
99+
debug (
100+
"Document is unsaved, creating an save location at: %s",
101+
path
102+
);
103+
104+
var new_window = new AppWindow (unsaved_document) {
105+
is_new = true,
106+
};
107+
108+
add_window (new_window);
109+
new_window.present ();
110+
}
101111

102-
add_window (new_window);
103-
new_window.present ();
112+
113+
if (args[1] != null) {
114+
115+
open_at = File.new_for_path(args[1]);
116+
var new_window = new AppWindow (open_at);
117+
118+
add_window (new_window);
119+
new_window.present ();
120+
}
104121
}
105122

106123
protected override void open (File[] files, string hint) {
@@ -115,22 +132,6 @@ public class Application : Gtk.Application {
115132
}
116133
}
117134

118-
string get_new_document_name () {
119-
var name = "New Document";
120-
if (created_documents > 1) {
121-
name = name + " " + created_documents.to_string ();
122-
}
123-
124-
debug (
125-
"New document name is: %s",
126-
name
127-
);
128-
129-
this.created_documents++;
130-
131-
return name;
132-
}
133-
134135
public static int main (string[] args) {
135136
return new Application ().run (args);
136137
}

src/Utils.vala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* SPDX-License-Identifier: GPL-3.0-or-later
3+
* SPDX-FileCopyrightText: 2025 William Kelso <[email protected]>
4+
*/
5+
6+
7+
namespace Slate.Utils {
8+
9+
public string get_new_document_name () {
10+
var name = _"New Document";
11+
12+
if (created_documents > 1) {
13+
name = name + " " + created_documents.to_string ();
14+
}
15+
16+
debug (
17+
"New document name is: %s",
18+
name
19+
);
20+
21+
created_documents++;
22+
23+
return name;
24+
}
25+
26+
public void check_if_datadir () {
27+
debug ("do we have a data directory?");
28+
var data_directory = File.new_for_path (Environment.get_user_data_dir ());
29+
try {
30+
if (!data_directory.query_exists ()) {
31+
data_directory.make_directory ();
32+
print("Prepared target data directory");
33+
}
34+
} catch (Error e) {
35+
warning ("Failed to prepare target data directory %s\n", e.message);
36+
}
37+
}
38+
39+
}

src/Window.vala

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
public class AppWindow : Gtk.Window {
77
public File file { get; set; }
88
private Gtk.TextBuffer buf;
9-
public string file_name { get; set; default = "unknown"; }
10-
public bool is_new { get; set; default = false; }
9+
public string? file_name { get; set; default = null; }
10+
public bool is_new;
1111

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

16-
public AppWindow () {
16+
public AppWindow (File? document) {
1717
debug ("Constructing GUI");
1818

1919
Intl.setlocale ();
@@ -80,7 +80,7 @@ public class AppWindow : Gtk.Window {
8080

8181
// Signal callbacks are heavily derived from similar operations in
8282
// elementary/code
83-
save_as_button.clicked.connect (on_save);
83+
save_as_button.clicked.connect (on_save_as);
8484
this.close_request.connect (on_close);
8585
buf.changed.connect (on_buffer_changed);
8686

@@ -89,22 +89,35 @@ public class AppWindow : Gtk.Window {
8989
bind_property ("file_name", this, "title");
9090

9191
debug ("Success!");
92+
93+
94+
if (document == null) {
95+
new_empty_doc ();
96+
}
97+
98+
open_file (document);
9299
}
93100

94101

95102
/* ---------------- FILE OPERATIONS ---------------- */
96-
public void open_file (File file = this.file) {
97-
this.file = file;
103+
public void open_file (File? file = this.file) {
104+
debug ("Attempting to open file %s", file.get_basename ());
105+
106+
if (file = null) {
107+
is_new = true;
108+
109+
this.file_name = Slate.Utils.get_new_document_name ();
110+
this.file = File.new_for_path (Environment.get_user_data_dir () + '/' + name);
98111

99-
if (this.is_new) {
100112
try {
101-
file.create (GLib.FileCreateFlags.REPLACE_DESTINATION);
102-
this.file_name = file.get_basename ();
113+
this.file.create (GLib.FileCreateFlags.REPLACE_DESTINATION);
103114
} catch (Error err) {
104-
warning ("Couldn't create file: %s", err.message);
115+
warning ("Couldn't create file: %s", err.message);
105116
}
117+
106118
} else {
107-
debug ("Attempting to open file %s", file.get_basename ());
119+
this.file = file;
120+
108121
try {
109122
this.file_name = file.get_basename ();
110123
var distream = new DataInputStream (file.read (null));
@@ -117,6 +130,13 @@ public class AppWindow : Gtk.Window {
117130
}
118131

119132
public void save_file (File file = this.file) {
133+
134+
// We have to always check if nothing happened to datadir
135+
// This way if the user deleted in the meantime everything, we still can save unsaved docs
136+
if (is_new) {
137+
Slate.Utils.check_if_datadir;
138+
}
139+
120140
try {
121141
debug ("Attempting to save the buffer to disk..");
122142
DataOutputStream dostream;
@@ -135,9 +155,8 @@ public class AppWindow : Gtk.Window {
135155
}
136156
}
137157

138-
139158
/* ---------------- HANDLERS ---------------- */
140-
public void on_save () {
159+
public void on_save_as () {
141160
debug ("Save event!");
142161
var save_dialog = new Gtk.FileDialog () { initial_name = file_name };
143162

@@ -147,7 +166,7 @@ public class AppWindow : Gtk.Window {
147166
file_name = file.get_basename ();
148167
if (is_new) { is_new = false; }
149168
save_file (file);
150-
169+
151170
} catch (Error err) {
152171
warning ("Failed to save file: %s", err.message);
153172
}

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
sources += files(
22
'Window.vala',
33
'Application.vala',
4+
'Utils.vala',
45
)

0 commit comments

Comments
 (0)