2018-04-16 22:42:30 +00:00
|
|
|
public class WriteAs.MainWindow : Gtk.ApplicationWindow {
|
|
|
|
private Gtk.TextView canvas;
|
|
|
|
|
2018-04-17 00:21:28 +00:00
|
|
|
private bool dark_mode = false;
|
2018-04-17 01:18:29 +00:00
|
|
|
private string font = "Lora, 'Palatino Linotype',"
|
|
|
|
+ "'Book Antiqua', 'New York', 'DejaVu serif', serif";
|
2018-04-18 01:14:44 +00:00
|
|
|
private string fontstyle = "serif";
|
2018-04-18 22:01:48 +00:00
|
|
|
private bool text_changed = false;
|
2018-04-17 00:21:28 +00:00
|
|
|
|
2018-04-16 22:42:30 +00:00
|
|
|
construct {
|
2018-04-16 23:49:33 +00:00
|
|
|
construct_toolbar();
|
2018-04-17 03:17:55 +00:00
|
|
|
build_keyboard_shortcuts();
|
2018-04-16 22:42:30 +00:00
|
|
|
|
2018-04-18 21:02:34 +00:00
|
|
|
var scrolled = new Gtk.ScrolledWindow(null, null);
|
2018-04-16 22:42:30 +00:00
|
|
|
canvas = new Gtk.TextView();
|
2018-04-18 01:09:14 +00:00
|
|
|
canvas.wrap_mode = Gtk.WrapMode.WORD_CHAR;
|
2018-04-18 21:02:34 +00:00
|
|
|
scrolled.add(canvas);
|
|
|
|
add(scrolled);
|
2018-04-18 01:21:10 +00:00
|
|
|
|
2018-04-17 04:05:37 +00:00
|
|
|
canvas.event_after.connect((evt) => {
|
|
|
|
// TODO This word count algorithm may be quite naive
|
|
|
|
// and could do improvement.
|
|
|
|
var word_count = canvas.buffer.text.split(" ").length;
|
|
|
|
title = ngettext("%i word","%i words",word_count).printf(word_count);
|
2018-04-18 01:09:14 +00:00
|
|
|
|
2018-04-18 01:21:10 +00:00
|
|
|
text_changed = true;
|
|
|
|
});
|
|
|
|
Timeout.add_full(Priority.DEFAULT_IDLE, 100/*ms*/, () => {
|
|
|
|
if (!text_changed) return Source.CONTINUE;
|
|
|
|
|
|
|
|
try {
|
2018-04-18 01:09:14 +00:00
|
|
|
draft_file().replace_contents(canvas.buffer.text.data, null, false,
|
|
|
|
FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
|
|
|
|
null);
|
2018-04-18 01:21:10 +00:00
|
|
|
text_changed = false;
|
|
|
|
} catch (Error err) {/* We'll try again anyways. */}
|
|
|
|
|
|
|
|
return Source.CONTINUE;
|
2018-04-17 04:05:37 +00:00
|
|
|
});
|
2018-04-17 01:18:29 +00:00
|
|
|
|
|
|
|
adjust_text_style();
|
2018-04-16 22:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public MainWindow(Gtk.Application app) {
|
|
|
|
set_application(app);
|
2018-04-18 23:05:02 +00:00
|
|
|
icon_name = "write-as-gtk";
|
2018-04-18 01:09:14 +00:00
|
|
|
try {
|
|
|
|
open_file(draft_file());
|
|
|
|
} catch (Error err) {/* It's fine... */}
|
2018-04-16 22:48:06 +00:00
|
|
|
|
|
|
|
set_default_size(800, 600);
|
2018-04-16 22:42:30 +00:00
|
|
|
}
|
2018-04-16 23:49:33 +00:00
|
|
|
|
2018-04-18 01:09:14 +00:00
|
|
|
private static File draft_file() {
|
|
|
|
var home = File.new_for_path(Environment.get_home_dir());
|
|
|
|
return home.get_child(".writeas-draft.txt");
|
|
|
|
}
|
|
|
|
|
2018-04-16 23:49:33 +00:00
|
|
|
private void construct_toolbar() {
|
|
|
|
var header = new Gtk.HeaderBar();
|
|
|
|
header.show_close_button = true;
|
|
|
|
set_titlebar(header);
|
|
|
|
|
2018-04-17 23:59:36 +00:00
|
|
|
var publish_button = new Gtk.Button.from_icon_name("document-send",
|
|
|
|
Gtk.IconSize.SMALL_TOOLBAR);
|
|
|
|
publish_button.clicked.connect(() => {
|
2018-04-18 22:32:38 +00:00
|
|
|
canvas.buffer.text += "\n\n" + publish();
|
2018-04-17 23:59:36 +00:00
|
|
|
});
|
|
|
|
header.pack_end(publish_button);
|
|
|
|
|
2018-04-16 23:49:33 +00:00
|
|
|
var darkmode_button = new Gtk.ToggleButton();
|
2018-04-17 01:49:19 +00:00
|
|
|
darkmode_button.tooltip_text = _("Toggle dark theme");
|
2018-04-16 23:49:33 +00:00
|
|
|
// NOTE the fallback icon is a bit of a meaning stretch, but it works.
|
|
|
|
var icon_theme = Gtk.IconTheme.get_default();
|
|
|
|
darkmode_button.image = new Gtk.Image.from_icon_name(
|
|
|
|
icon_theme.has_icon("writeas-bright-dark") ?
|
|
|
|
"writeas-bright-dark" : "weather-clear-night",
|
|
|
|
Gtk.IconSize.SMALL_TOOLBAR);
|
|
|
|
darkmode_button.draw_indicator = false;
|
|
|
|
var settings = Gtk.Settings.get_default();
|
|
|
|
darkmode_button.toggled.connect(() => {
|
|
|
|
settings.gtk_application_prefer_dark_theme = darkmode_button.active;
|
2018-04-17 00:21:28 +00:00
|
|
|
dark_mode = darkmode_button.active;
|
|
|
|
adjust_text_style();
|
2018-04-16 23:49:33 +00:00
|
|
|
});
|
|
|
|
header.pack_end(darkmode_button);
|
2018-04-17 01:18:29 +00:00
|
|
|
|
|
|
|
var fonts = new Gtk.MenuButton();
|
2018-04-17 01:49:19 +00:00
|
|
|
fonts.tooltip_text = _("Change document font");
|
2018-04-17 01:18:29 +00:00
|
|
|
fonts.image = new Gtk.Image.from_icon_name("font-x-generic", Gtk.IconSize.SMALL_TOOLBAR);
|
|
|
|
fonts.popup = new Gtk.Menu();
|
|
|
|
header.pack_start(fonts);
|
|
|
|
|
2018-04-18 01:14:44 +00:00
|
|
|
build_fontoption(fonts.popup, _("Serif"), "serif", font);
|
|
|
|
build_fontoption(fonts.popup, _("Sans-serif"), "sans",
|
|
|
|
"'Open Sans', 'Segoe UI', Tahoma, Arial, sans-serif");
|
2018-04-18 13:48:46 +00:00
|
|
|
build_fontoption(fonts.popup, _("Monospace"), "wrap", "Hack, consolas," +
|
2018-04-17 01:18:29 +00:00
|
|
|
"Menlo-Regular, Menlo, Monaco, 'ubuntu mono', monospace");
|
|
|
|
fonts.popup.show_all();
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:14:44 +00:00
|
|
|
private void build_fontoption(Gtk.Menu menu,
|
|
|
|
string label, string fontstyle, string families) {
|
2018-04-17 01:18:29 +00:00
|
|
|
var option = new Gtk.MenuItem.with_label(label);
|
|
|
|
option.activate.connect(() => {
|
|
|
|
this.font = families;
|
2018-04-18 01:14:44 +00:00
|
|
|
this.fontstyle = fontstyle;
|
2018-04-17 01:18:29 +00:00
|
|
|
adjust_text_style();
|
|
|
|
});
|
|
|
|
|
|
|
|
var styles = option.get_style_context();
|
|
|
|
var provider = new Gtk.CssProvider();
|
2018-04-17 03:17:55 +00:00
|
|
|
try {
|
|
|
|
provider.load_from_data("* {font: %s;}".printf(families));
|
|
|
|
styles.add_provider(provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
|
|
} catch (Error e) {
|
|
|
|
warning(e.message);
|
|
|
|
}
|
2018-04-17 01:18:29 +00:00
|
|
|
|
|
|
|
menu.add(option);
|
2018-04-16 23:49:33 +00:00
|
|
|
}
|
2018-04-17 00:21:28 +00:00
|
|
|
|
|
|
|
private Gtk.CssProvider cur_styles = null;
|
|
|
|
private void adjust_text_style() {
|
|
|
|
try {
|
|
|
|
var styles = canvas.get_style_context();
|
|
|
|
if (cur_styles != null) styles.remove_provider(cur_styles);
|
|
|
|
|
2018-04-17 01:18:29 +00:00
|
|
|
var css = "* {font: %s; padding: 20px;}".printf(font);
|
2018-04-17 00:21:28 +00:00
|
|
|
if (dark_mode) {
|
2018-04-17 00:36:00 +00:00
|
|
|
// Try to detect whether the system provided a better dark mode.
|
|
|
|
var text_color = styles.get_color(Gtk.StateFlags.ACTIVE);
|
|
|
|
double h, s, v;
|
|
|
|
Gtk.rgb_to_hsv(text_color.red, text_color.green, text_color.blue,
|
|
|
|
out h, out s, out v);
|
|
|
|
|
2018-04-17 01:31:56 +00:00
|
|
|
if (v < 0.5) css += "* {background: black; color: white;}";
|
2018-04-17 00:21:28 +00:00
|
|
|
}
|
|
|
|
cur_styles = new Gtk.CssProvider();
|
|
|
|
cur_styles.load_from_data(css);
|
|
|
|
|
|
|
|
styles.add_provider(cur_styles, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
|
|
} catch (Error e) {
|
|
|
|
warning(e.message);
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 03:17:55 +00:00
|
|
|
|
2018-04-18 22:32:38 +00:00
|
|
|
private string publish() {
|
2018-04-18 00:57:12 +00:00
|
|
|
try {
|
2018-04-18 22:01:48 +00:00
|
|
|
if (text_changed) {;
|
|
|
|
draft_file().replace_contents(canvas.buffer.text.data, null, false,
|
|
|
|
FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
|
|
|
|
null);
|
|
|
|
text_changed = false;
|
|
|
|
}
|
2018-04-18 00:57:12 +00:00
|
|
|
|
2018-04-18 22:32:38 +00:00
|
|
|
var cmd = "sh -c 'cat ~/.writeas-draft.txt | writeas --font %s'";
|
|
|
|
cmd = cmd.printf(fontstyle);
|
2018-04-18 22:01:48 +00:00
|
|
|
string stdout, stderr;
|
|
|
|
int status;
|
|
|
|
Process.spawn_command_line_sync(cmd,
|
|
|
|
out stdout, out stderr, out status);
|
2018-04-18 00:57:12 +00:00
|
|
|
|
|
|
|
// Open it in the browser
|
|
|
|
var browser = AppInfo.get_default_for_uri_scheme("https");
|
|
|
|
var urls = new List<string>();
|
2018-04-18 22:01:48 +00:00
|
|
|
urls.append(stdout.strip());
|
2018-04-18 00:57:12 +00:00
|
|
|
browser.launch_uris(urls, null);
|
|
|
|
|
2018-04-18 22:01:48 +00:00
|
|
|
return stderr.strip();
|
2018-04-18 00:57:12 +00:00
|
|
|
} catch (Error err) {
|
2018-04-18 22:01:48 +00:00
|
|
|
return err.message;
|
2018-04-18 00:57:12 +00:00
|
|
|
}
|
2018-04-17 05:08:11 +00:00
|
|
|
}
|
2018-04-17 03:17:55 +00:00
|
|
|
/* --- */
|
|
|
|
|
|
|
|
private void build_keyboard_shortcuts() {
|
|
|
|
/* These operations are not exposed to the UI as buttons,
|
|
|
|
as most people are very familiar with them and they are not the
|
|
|
|
focus of this app. */
|
|
|
|
var accels = new Gtk.AccelGroup();
|
|
|
|
|
|
|
|
accels.connect(Gdk.Key.S, Gdk.ModifierType.CONTROL_MASK,
|
|
|
|
Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
|
|
|
|
(g,a,k,m) => save_as());
|
|
|
|
accels.connect(Gdk.Key.S,
|
|
|
|
Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK,
|
|
|
|
Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
|
|
|
|
(g,a,k,m) => save_as());
|
|
|
|
accels.connect(Gdk.Key.O, Gdk.ModifierType.CONTROL_MASK,
|
|
|
|
Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g, a, k, m) => {
|
|
|
|
try {
|
2018-04-17 03:46:38 +00:00
|
|
|
open_file(prompt_file(Gtk.FileChooserAction.OPEN, _("_Open")));
|
2018-04-17 03:17:55 +00:00
|
|
|
} catch (Error e) {
|
|
|
|
// It's fine...
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
add_accel_group(accels);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool save_as() {
|
2018-04-17 03:27:58 +00:00
|
|
|
try {
|
|
|
|
var file = prompt_file(Gtk.FileChooserAction.SAVE, _("_Save as"));
|
|
|
|
file.replace_contents(canvas.buffer.text.data, null, false,
|
|
|
|
FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
|
|
|
|
null);
|
|
|
|
} catch (Error e) {
|
|
|
|
// It's fine...
|
|
|
|
}
|
2018-04-17 03:17:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private File prompt_file(Gtk.FileChooserAction mode, string action)
|
|
|
|
throws UserCancellable {
|
|
|
|
var file_chooser = new Gtk.FileChooserDialog(action, this, mode,
|
|
|
|
_("_Cancel"), Gtk.ResponseType.CANCEL,
|
|
|
|
action, Gtk.ResponseType.ACCEPT);
|
|
|
|
|
|
|
|
file_chooser.select_multiple = false;
|
|
|
|
var filter = new Gtk.FileFilter();
|
|
|
|
filter.add_mime_type("text/plain");
|
|
|
|
file_chooser.set_filter(filter);
|
|
|
|
|
|
|
|
var resp = file_chooser.run();
|
|
|
|
file_chooser.close();
|
|
|
|
if (resp == Gtk.ResponseType.ACCEPT) return file_chooser.get_file();
|
|
|
|
else throw new UserCancellable.USER_CANCELLED("FileChooserDialog");
|
|
|
|
}
|
2018-04-17 03:46:38 +00:00
|
|
|
|
|
|
|
public void open_file(File file) throws Error {
|
|
|
|
uint8[] text;
|
|
|
|
file.load_contents(null, out text, null);
|
|
|
|
canvas.buffer.text = (string) text;
|
|
|
|
}
|
2018-04-16 22:42:30 +00:00
|
|
|
}
|
2018-04-17 03:17:55 +00:00
|
|
|
|
|
|
|
errordomain WriteAs.UserCancellable {USER_CANCELLED}
|