Skip to content

Commit feff84a

Browse files
authored
Merge branch 'main' into autosave-dir
2 parents 2137445 + 6d69187 commit feff84a

File tree

10 files changed

+87
-64
lines changed

10 files changed

+87
-64
lines changed

data/io.github.wpkelso.slate.desktop.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Terminal=false
88
Type=Application
99
StartupNotify=true
1010
Categories=Utility;GTK;Office;Development;TextTools;TextEditor;
11+
MimeType=text/plain;

po/POTFILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
src/Application.vala
22
src/Window.vala
3+
src/Utils.vala

po/de.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ msgid ""
33
msgstr ""
44
"Project-Id-Version: io.github.wpkelso.slate\n"
55
"Report-Msgid-Bugs-To: \n"
6-
"POT-Creation-Date: 2025-06-12 19:53+0200\n"
6+
"POT-Creation-Date: 2025-06-15 14:50+0200\n"
77
"PO-Revision-Date: 2025-06-09 HO:MI+ZONE\n"
88
"Last-Translator: Teamcons - teamcons.carrd.co\n"
99
"Language-Team: DE <[email protected]>\n"
@@ -12,7 +12,7 @@ msgstr ""
1212
"Content-Type: text/plain; charset=UTF-8\n"
1313
"Content-Transfer-Encoding: 8bit\n"
1414

15-
#: src/Application.vala:107 src/Window.vala:24
15+
#: src/Window.vala:24 src/Utils.vala:9
1616
msgid "New Document"
1717
msgstr "Neues Dokument"
1818

po/es.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: io.github.wpkelso.slate\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2025-06-12 19:53+0200\n"
11+
"POT-Creation-Date: 2025-06-15 14:50+0200\n"
1212
"PO-Revision-Date: 2025-06-11 HO:MI+ZONE\n"
1313
"Last-Translator: Teamcons - teamcons.carrd.co\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -17,7 +17,7 @@ msgstr ""
1717
"Content-Type: text/plain; charset=UTF-8\n"
1818
"Content-Transfer-Encoding: 8bit\n"
1919

20-
#: src/Application.vala:107 src/Window.vala:24
20+
#: src/Window.vala:24 src/Utils.vala:9
2121
msgid "New Document"
2222
msgstr "Nuevo Documento"
2323

po/fr.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: io.github.wpkelso.slate\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2025-06-12 19:53+0200\n"
11+
"POT-Creation-Date: 2025-06-15 14:50+0200\n"
1212
"PO-Revision-Date: 2025-06-11 HO:MI+ZONE\n"
1313
"Last-Translator: Teamcons - teamcons.carrd.co\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -17,7 +17,7 @@ msgstr ""
1717
"Content-Type: text/plain; charset=UTF-8\n"
1818
"Content-Transfer-Encoding: 8bit\n"
1919

20-
#: src/Application.vala:107 src/Window.vala:24
20+
#: src/Window.vala:24 src/Utils.vala:9
2121
msgid "New Document"
2222
msgstr "Nouveau Document"
2323

po/io.github.wpkelso.slate.pot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: io.github.wpkelso.slate\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2025-06-12 19:53+0200\n"
11+
"POT-Creation-Date: 2025-06-15 14:50+0200\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -17,7 +17,7 @@ msgstr ""
1717
"Content-Type: text/plain; charset=CHARSET\n"
1818
"Content-Transfer-Encoding: 8bit\n"
1919

20-
#: src/Application.vala:107 src/Window.vala:24
20+
#: src/Window.vala:24 src/Utils.vala:9
2121
msgid "New Document"
2222
msgstr ""
2323

src/Application.vala

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class Application : Gtk.Application {
7979
protected override void activate () {
8080

8181
// Reopen all the unsaved documents we have in datadir
82-
check_if_data_dir ();
82+
Utils.check_if_data_dir ();
8383

8484
try {
8585
var pile_unsaved_documents = Dir.open (data_dir_path);
@@ -112,51 +112,26 @@ public class Application : Gtk.Application {
112112
protected override void open (File[] files, string hint) {
113113
foreach (var file in files) {
114114
debug ("Creating window with file: %s", file.get_basename ());
115-
open_file (file);
116-
}
117-
}
118-
119-
string get_new_document_name () {
120-
var name = _("New Document");
121-
if (created_documents > 1) {
122-
name = name + " " + created_documents.to_string ();
123-
}
124-
125-
debug ("New document name is: %s", name);
126-
127-
created_documents++;
128-
129-
return name;
130-
}
131-
132-
public static void check_if_data_dir () {
133-
debug ("Do we have a data directory?");
134-
var data_directory = File.new_for_path (data_dir_path);
135-
try {
136-
if (!data_directory.query_exists ()) {
137-
data_directory.make_directory ();
138-
debug ("No, creating data directory");
139-
} else {
140-
debug ("Yes, data directory exists!");
141-
}
142-
} catch (Error e) {
143-
warning ("Failed to prepare target data directory %s\n", e.message);
115+
open_window_with_file (file);
144116
}
145117
}
146118

147119
public static int main (string[] args) {
148120
return new Application ().run (args);
149121
}
150122

123+
/* ---------------- HANDLERS ---------------- */
151124
public void on_new_document () {
152-
var name = get_new_document_name ();
153-
var path = Path.build_filename (data_dir_path, name);
154-
var file = File.new_for_path (path);
155125

156-
check_if_data_dir ();
126+
var name = Utils.get_new_document_name ();
127+
var path = Path.build_filename (Environment.get_user_data_dir (), name);
128+
var file = File.new_for_path (path);
157129

130+
Utils.check_if_data_dir ();
131+
158132
try {
159133
file.create_readwrite (GLib.FileCreateFlags.REPLACE_DESTINATION);
134+
160135
} catch (Error e) {
161136
warning ("Failed to prepare target file %s\n", e.message);
162137
}
@@ -165,7 +140,7 @@ public class Application : Gtk.Application {
165140

166141
}
167142

168-
public bool open_file (File file) {
143+
public bool open_window_with_file (File file) {
169144
if (file.query_file_type (FileQueryInfoFlags.NONE) != FileType.REGULAR) {
170145
warning ("Couldn't open, not a regular file.");
171146
return false;
@@ -176,13 +151,13 @@ public class Application : Gtk.Application {
176151
new_window.present ();
177152
return true;
178153
}
179-
154+
180155
public void on_open_document () {
181156
var open_dialog = new Gtk.FileDialog ();
182157
open_dialog.open.begin (this.active_window, null, (obj, res) => {
183158
try {
184159
var file = open_dialog.open.end (res);
185-
open_file (file);
160+
open_window_with_file (file);
186161
} catch (Error err) {
187162
warning ("Failed to select file to open: %s", err.message);
188163
}

src/Utils.vala

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

src/Window.vala

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public 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 (Application.data_dir_path in this.file.get_path ()) {
113-
Application.check_if_data_dir ();
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 = (Application.data_dir_path in this.file.get_path ());
143+
bool is_unsaved_doc = (Application.data_dir_path 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
}

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)