Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions data/io.github.wpkelso.slate.desktop.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Terminal=false
Type=Application
StartupNotify=true
Categories=Utility;GTK;Office;Development;TextTools;TextEditor;
MimeType=text/plain;
1 change: 1 addition & 0 deletions po/POTFILES
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
src/Application.vala
src/Window.vala
src/Utils.vala
4 changes: 2 additions & 2 deletions po/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgid ""
msgstr ""
"Project-Id-Version: io.github.wpkelso.slate\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-06-12 19:53+0200\n"
"POT-Creation-Date: 2025-06-15 14:50+0200\n"
"PO-Revision-Date: 2025-06-09 HO:MI+ZONE\n"
"Last-Translator: Teamcons - teamcons.carrd.co\n"
"Language-Team: DE <[email protected]>\n"
Expand All @@ -12,7 +12,7 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: src/Application.vala:107 src/Window.vala:24
#: src/Window.vala:24 src/Utils.vala:9
msgid "New Document"
msgstr "Neues Dokument"

Expand Down
4 changes: 2 additions & 2 deletions po/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: io.github.wpkelso.slate\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-06-12 19:53+0200\n"
"POT-Creation-Date: 2025-06-15 14:50+0200\n"
"PO-Revision-Date: 2025-06-11 HO:MI+ZONE\n"
"Last-Translator: Teamcons - teamcons.carrd.co\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -17,7 +17,7 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: src/Application.vala:107 src/Window.vala:24
#: src/Window.vala:24 src/Utils.vala:9
msgid "New Document"
msgstr "Nuevo Documento"

Expand Down
4 changes: 2 additions & 2 deletions po/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: io.github.wpkelso.slate\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-06-12 19:53+0200\n"
"POT-Creation-Date: 2025-06-15 14:50+0200\n"
"PO-Revision-Date: 2025-06-11 HO:MI+ZONE\n"
"Last-Translator: Teamcons - teamcons.carrd.co\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -17,7 +17,7 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: src/Application.vala:107 src/Window.vala:24
#: src/Window.vala:24 src/Utils.vala:9
msgid "New Document"
msgstr "Nouveau Document"

Expand Down
4 changes: 2 additions & 2 deletions po/io.github.wpkelso.slate.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: io.github.wpkelso.slate\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-06-12 19:53+0200\n"
"POT-Creation-Date: 2025-06-15 14:50+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -17,7 +17,7 @@ msgstr ""
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: src/Application.vala:107 src/Window.vala:24
#: src/Window.vala:24 src/Utils.vala:9
msgid "New Document"
msgstr ""

Expand Down
41 changes: 9 additions & 32 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class Application : Gtk.Application {
protected override void activate () {

// Reopen all the unsaved documents we have in datadir
check_if_datadir ();
Utils.check_if_datadir ();
var datadir = Environment.get_user_data_dir ();
try {
var pile_unsaved_documents = Dir.open (datadir);
Expand Down Expand Up @@ -107,43 +107,26 @@ public class Application : Gtk.Application {
}
}

string get_new_document_name () {
var name = _("New Document");
if (created_documents > 1) {
name = name + " " + created_documents.to_string ();
}

debug ("New document name is: %s", name);

created_documents++;

return name;
}

public static void check_if_datadir () {
debug ("do we have a data directory?");
var data_directory = File.new_for_path (Environment.get_user_data_dir ());
try {
if (!data_directory.query_exists ()) {
data_directory.make_directory ();
}
} catch (Error e) {
warning ("Failed to prepare target data directory %s\n", e.message);
}
public void open_file (File file) {
var new_window = new AppWindow (file);
add_window (new_window);
new_window.present ();
}

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

/* ---------------- HANDLERS ---------------- */
public void on_new_document () {
var name = get_new_document_name ();
var name = Utils.get_new_document_name ();
var path = Path.build_filename (Environment.get_user_data_dir (), name);
var file = File.new_for_path (path);

check_if_datadir ();
Utils.check_if_datadir ();
try {
file.create_readwrite (GLib.FileCreateFlags.REPLACE_DESTINATION);

} catch (Error e) {
warning ("Failed to prepare target file %s\n", e.message);
}
Expand All @@ -152,12 +135,6 @@ public class Application : Gtk.Application {

}

public void open_file (File file) {
var new_window = new AppWindow (file);
add_window (new_window);
new_window.present ();
}

public void on_open_document () {
var open_dialog = new Gtk.FileDialog ();
open_dialog.open.begin (this.active_window, null, (obj, res) => {
Expand Down
34 changes: 34 additions & 0 deletions src/Utils.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2025 William Kelso <[email protected]>
*/

namespace Utils {

public string get_new_document_name () {
var name = _("New Document");

if (Application.created_documents > 1) {
name = name + " " + Application.created_documents.to_string ();
}

debug ("New document name is: %s", name);

Application.created_documents++;

return name;
}

public static void check_if_datadir () {
debug ("do we have a data directory?");

var data_directory = File.new_for_path (Environment.get_user_data_dir ());
try {
if (!data_directory.query_exists ()) {
data_directory.make_directory ();
}
} catch (Error e) {
warning ("Failed to prepare target data directory %s\n", e.message);
}
}
}
49 changes: 30 additions & 19 deletions src/Window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class AppWindow : Gtk.Window {
public File file { get; set; }
private Gtk.TextBuffer buf;
private Gtk.HeaderBar header;
public string file_name { get; set; }

// Add a debounce so we aren't writing the entire buffer every character input
Expand Down Expand Up @@ -44,9 +45,9 @@ public class AppWindow : Gtk.Window {
actions_box.append (open_button);
actions_box.append (save_as_button);


var header = new Gtk.HeaderBar () {
show_title_buttons = true
show_title_buttons = true,
tooltip_text = ""
};
header.add_css_class (Granite.STYLE_CLASS_FLAT);
header.pack_start (actions_box);
Expand Down Expand Up @@ -76,41 +77,43 @@ public class AppWindow : Gtk.Window {
default_width = 300;
titlebar = header;

debug ("Connecting signals");
debug ("Binding window title to file_name");
bind_property ("file_name", this, "title");
debug ("Success!");

open_file (file);

debug ("Connecting signals");
// Signal callbacks are heavily derived from similar operations in
// elementary/code
save_as_button.clicked.connect (on_save_as);
this.close_request.connect (on_close);
buf.changed.connect (on_buffer_changed);

debug ("Binding window title to file_name");

bind_property ("file_name", this, "title");

debug ("Success!");

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);
buf.set_text (contents ?? "");

this.file = file;
this.file_name = file.get_basename ();
this.tooltip_text = file.get_path ();

} 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 ()) {
Application.check_if_datadir ();
Utils.check_if_datadir ();
}

try {
Expand All @@ -126,27 +129,34 @@ 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 };

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

var save_dialog = new Gtk.FileDialog () {
initial_name = (is_unsaved_doc ? file_name + ".txt" : file_name)
};

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)) {
this.file = file;
file_name = file.get_basename ();
this.tooltip_text = file.get_path ();

if ((is_unsaved_doc) && (oldfile != file)) {
oldfile.delete ();
}

Expand Down Expand Up @@ -184,6 +194,7 @@ public class AppWindow : Gtk.Window {

try {
this.file.delete ();

} catch (Error err) {
warning ("Failed to delete empty temp file: %s", err.message);
}
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
sources += files(
'Window.vala',
'Application.vala',
'Utils.vala',
)