Skip to content

Commit 6d69187

Browse files
authored
Cleanup and enhancements (#16)
* set file as working file only if reading it succeeds * Introduce Utils * Consider target as target only if saving succeeds * Silence the GTK CRITICAL and clean spacing while at it * Add tooiltip on header showing where file * Add a file extension if saving an unsaved document * remove space * Officially mimetype * Move the signals a bit later Attempt at fixing: #18
1 parent 59f6685 commit 6d69187

File tree

10 files changed

+84
-59
lines changed

10 files changed

+84
-59
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: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class Application : Gtk.Application {
7575
protected override void activate () {
7676

7777
// Reopen all the unsaved documents we have in datadir
78-
check_if_datadir ();
78+
Utils.check_if_datadir ();
7979
var datadir = Environment.get_user_data_dir ();
8080
try {
8181
var pile_unsaved_documents = Dir.open (datadir);
@@ -107,43 +107,26 @@ public class Application : Gtk.Application {
107107
}
108108
}
109109

110-
string get_new_document_name () {
111-
var name = _("New Document");
112-
if (created_documents > 1) {
113-
name = name + " " + created_documents.to_string ();
114-
}
115-
116-
debug ("New document name is: %s", name);
117-
118-
created_documents++;
119-
120-
return name;
121-
}
122-
123-
public static void check_if_datadir () {
124-
debug ("do we have a data directory?");
125-
var data_directory = File.new_for_path (Environment.get_user_data_dir ());
126-
try {
127-
if (!data_directory.query_exists ()) {
128-
data_directory.make_directory ();
129-
}
130-
} catch (Error e) {
131-
warning ("Failed to prepare target data directory %s\n", e.message);
132-
}
110+
public void open_file (File file) {
111+
var new_window = new AppWindow (file);
112+
add_window (new_window);
113+
new_window.present ();
133114
}
134115

135116
public static int main (string[] args) {
136117
return new Application ().run (args);
137118
}
138119

120+
/* ---------------- HANDLERS ---------------- */
139121
public void on_new_document () {
140-
var name = get_new_document_name ();
122+
var name = Utils.get_new_document_name ();
141123
var path = Path.build_filename (Environment.get_user_data_dir (), name);
142124
var file = File.new_for_path (path);
143125

144-
check_if_datadir ();
126+
Utils.check_if_datadir ();
145127
try {
146128
file.create_readwrite (GLib.FileCreateFlags.REPLACE_DESTINATION);
129+
147130
} catch (Error e) {
148131
warning ("Failed to prepare target file %s\n", e.message);
149132
}
@@ -152,12 +135,6 @@ public class Application : Gtk.Application {
152135

153136
}
154137

155-
public void open_file (File file) {
156-
var new_window = new AppWindow (file);
157-
add_window (new_window);
158-
new_window.present ();
159-
}
160-
161138
public void on_open_document () {
162139
var open_dialog = new Gtk.FileDialog ();
163140
open_dialog.open.begin (this.active_window, null, (obj, res) => {

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 (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
}

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)