Write.as GTK desktop app https://write.as/apps/desktop
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

390 lines
14 KiB

  1. /*
  2. Copyright © 2018 Write.as
  3. This file is part of the Write.as GTK desktop app.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. public class WriteAs.MainWindow : Gtk.ApplicationWindow {
  16. private Gtk.TextView canvas;
  17. private Gtk.HeaderBar header;
  18. private Gtk.ToggleButton darkmode_button;
  19. private static string data_dir = ".writeas";
  20. private int font_size = 12;
  21. private bool dark_mode = false;
  22. private string font = "Lora, 'Palatino Linotype',"
  23. + "'Book Antiqua', 'New York', 'DejaVu serif', serif";
  24. private string fontstyle = "serif";
  25. private bool text_changed = false;
  26. private bool is_initializing = true;
  27. construct {
  28. header = new Gtk.HeaderBar();
  29. header.title = "Write.as";
  30. construct_toolbar();
  31. build_keyboard_shortcuts();
  32. var scrolled = new Gtk.ScrolledWindow(null, null);
  33. canvas = new Gtk.SourceView();
  34. canvas.wrap_mode = Gtk.WrapMode.WORD_CHAR;
  35. scrolled.add(canvas);
  36. add(scrolled);
  37. size_allocate.connect((_) => {adjust_text_style();});
  38. canvas.event_after.connect((evt) => {
  39. // TODO This word count algorithm may be quite naive
  40. // and could do improvement.
  41. var word_count = canvas.buffer.text.split(" ").length;
  42. header.subtitle = ngettext("%i word","%i words",word_count).printf(word_count);
  43. text_changed = true;
  44. });
  45. Timeout.add_full(Priority.DEFAULT_IDLE, 100/*ms*/, () => {
  46. if (!text_changed) return Source.CONTINUE;
  47. var text = canvas.buffer.text;
  48. // This happens sometimes for some reason, but it's difficult to debug.
  49. if (text == "") return Source.CONTINUE;
  50. try {
  51. draft_file().replace_contents(text.data, null, false,
  52. FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
  53. null);
  54. text_changed = false;
  55. } catch (Error err) {/* We'll try again anyways. */}
  56. return Source.CONTINUE;
  57. });
  58. adjust_text_style(false);
  59. }
  60. public MainWindow(Gtk.Application app) {
  61. set_application(app);
  62. icon_name = "write-as";
  63. init_folder();
  64. try {
  65. open_file(draft_file());
  66. } catch (Error err) {canvas.buffer.text = err.message;}
  67. restore_styles();
  68. set_default_size(800, 600);
  69. is_initializing = false;
  70. }
  71. private static void init_folder() {
  72. var home = File.new_for_path(get_data_dir());
  73. try {
  74. home.make_directory();
  75. } catch (Error e) {
  76. stderr.printf("Create data dir: %s\n", e.message);
  77. }
  78. }
  79. private static string get_data_dir() {
  80. return Environment.get_home_dir() + "/" + data_dir;
  81. }
  82. private static File draft_file() {
  83. var home = File.new_for_path(get_data_dir());
  84. return home.get_child("draft.txt");
  85. }
  86. private static bool supports_dark_theme() {
  87. var theme = Gtk.Settings.get_default().gtk_theme_name;
  88. return Gtk.CssProvider.get_named(theme, null).to_string() !=
  89. Gtk.CssProvider.get_named(theme, "dark").to_string();
  90. }
  91. private void construct_toolbar() {
  92. header.show_close_button = true;
  93. set_titlebar(header);
  94. var publish_button = new Gtk.Button.from_icon_name("document-send",
  95. Gtk.IconSize.SMALL_TOOLBAR);
  96. publish_button.clicked.connect(() => {
  97. canvas.buffer.text += "\n\n" + publish();
  98. canvas.grab_focus();
  99. });
  100. header.pack_end(publish_button);
  101. darkmode_button = new Gtk.ToggleButton();
  102. darkmode_button.tooltip_text = _("Toggle dark theme");
  103. // NOTE the fallback icon is a bit of a meaning stretch, but it works.
  104. var icon_theme = Gtk.IconTheme.get_default();
  105. darkmode_button.image = new Gtk.Image.from_icon_name(
  106. icon_theme.has_icon("writeas-bright-dark") ?
  107. "writeas-bright-dark" : "weather-clear-night",
  108. Gtk.IconSize.SMALL_TOOLBAR);
  109. darkmode_button.draw_indicator = false;
  110. var settings = Gtk.Settings.get_default();
  111. darkmode_button.toggled.connect(() => {
  112. settings.gtk_application_prefer_dark_theme = darkmode_button.active;
  113. dark_mode = darkmode_button.active;
  114. if (!is_initializing) theme_save();
  115. canvas.grab_focus();
  116. });
  117. if (supports_dark_theme()) header.pack_end(darkmode_button);
  118. var fonts = new Gtk.MenuButton();
  119. fonts.tooltip_text = _("Change document font");
  120. fonts.image = new Gtk.Image.from_icon_name("font-x-generic", Gtk.IconSize.SMALL_TOOLBAR);
  121. fonts.popup = new Gtk.Menu();
  122. header.pack_start(fonts);
  123. build_fontoption(fonts.popup, _("Serif"), "serif", font);
  124. build_fontoption(fonts.popup, _("Sans-serif"), "sans",
  125. "'Open Sans', 'Segoe UI', Tahoma, Arial, sans-serif");
  126. build_fontoption(fonts.popup, _("Monospace"), "wrap", "Hack, consolas," +
  127. "Menlo-Regular, Menlo, Monaco, 'ubuntu mono', monospace");
  128. fonts.popup.show_all();
  129. }
  130. private unowned SList<Gtk.RadioMenuItem>? font_options = null;
  131. private void build_fontoption(Gtk.Menu menu,
  132. string label, string fontstyle, string families) {
  133. var option = new Gtk.RadioMenuItem.with_label(font_options, label);
  134. font_options = option.get_group();
  135. option.activate.connect(() => {
  136. this.font = families;
  137. this.fontstyle = fontstyle;
  138. adjust_text_style(!is_initializing);
  139. canvas.grab_focus();
  140. });
  141. var styles = option.get_style_context();
  142. var provider = new Gtk.CssProvider();
  143. try {
  144. provider.load_from_data("* {font: %s;}".printf(families));
  145. styles.add_provider(provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
  146. } catch (Error e) {
  147. warning(e.message);
  148. }
  149. menu.add(option);
  150. }
  151. public override void grab_focus() {
  152. canvas.grab_focus();
  153. }
  154. private KeyFile theme = new KeyFile();
  155. private void restore_styles() {
  156. try {
  157. loaded_theme = true;
  158. theme.load_from_file(get_data_dir() + "/prefs.ini", KeyFileFlags.NONE);
  159. dark_mode = theme.get_boolean("Theme", "darkmode");
  160. darkmode_button.set_active(dark_mode);
  161. Gtk.Settings.get_default().gtk_application_prefer_dark_theme = dark_mode;
  162. font_size = theme.get_integer("Theme", "fontsize");
  163. font = theme.get_string("Post", "font");
  164. fontstyle = theme.get_string("Post", "fontstyle");
  165. adjust_text_style(false);
  166. } catch (Error err) {/* No biggy... */}
  167. }
  168. private Gtk.CssProvider cur_styles = null;
  169. // So the theme isn't read before it's saved.
  170. private bool loaded_theme = false;
  171. private void adjust_text_style(bool save_theme = true) {
  172. try {
  173. if (cur_styles != null)
  174. Gtk.StyleContext.remove_provider_for_screen(Gdk.Screen.get_default(), cur_styles);
  175. var padding = canvas.get_allocated_width()*0.10;
  176. var css = ("GtkTextView {font: %s; font-size: %dpx; padding: 20px;" +
  177. " padding-left: %ipx; padding-right: %ipx;" +
  178. " -GtkWidget-cursor-color: #5ac4ee;}").printf(font, font_size,
  179. (int) padding, (int) padding);
  180. cur_styles = new Gtk.CssProvider();
  181. cur_styles.load_from_data(css);
  182. Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
  183. cur_styles, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
  184. if (save_theme) theme_save();
  185. } catch (Error e) {
  186. warning(e.message);
  187. }
  188. }
  189. private void theme_save() {
  190. if (!loaded_theme) return;
  191. theme.set_boolean("Theme", "darkmode", dark_mode);
  192. theme.set_integer("Theme", "fontsize", font_size);
  193. theme.set_string("Post", "font", font);
  194. theme.set_string("Post", "fontstyle", fontstyle);
  195. try {
  196. theme.save_to_file(get_data_dir() + "/prefs.ini");
  197. } catch (FileError err) {/* Oh well. */}
  198. }
  199. private string publish() {
  200. try {
  201. if (text_changed) {;
  202. draft_file().replace_contents(canvas.buffer.text.data, null, false,
  203. FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
  204. null);
  205. text_changed = false;
  206. }
  207. var cmd = "sh -c 'cat ~/" + data_dir + "/draft.txt | writeas --font %s'";
  208. cmd = cmd.printf(fontstyle);
  209. string stdout, stderr;
  210. int status;
  211. Process.spawn_command_line_sync(cmd,
  212. out stdout, out stderr, out status);
  213. // Open it in the browser
  214. if (status == 0) {
  215. var browser = AppInfo.get_default_for_uri_scheme("https");
  216. var urls = new List<string>();
  217. urls.append(stdout.strip());
  218. browser.launch_uris(urls, null);
  219. }
  220. return stderr.strip();
  221. } catch (Error err) {
  222. return err.message;
  223. }
  224. }
  225. /* --- */
  226. private void build_keyboard_shortcuts() {
  227. /* These operations are not exposed to the UI as buttons,
  228. as most people are very familiar with them and they are not the
  229. focus of this app. */
  230. var accels = new Gtk.AccelGroup();
  231. // App operations
  232. accels.connect(Gdk.Key.W, Gdk.ModifierType.CONTROL_MASK,
  233. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  234. (g,a,k,m) => quit());
  235. accels.connect(Gdk.Key.Q, Gdk.ModifierType.CONTROL_MASK,
  236. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  237. (g,a,k,m) => quit());
  238. // File operations
  239. accels.connect(Gdk.Key.S, Gdk.ModifierType.CONTROL_MASK,
  240. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  241. (g,a,k,m) => save_as());
  242. accels.connect(Gdk.Key.S,
  243. Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK,
  244. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  245. (g,a,k,m) => save_as());
  246. accels.connect(Gdk.Key.O, Gdk.ModifierType.CONTROL_MASK,
  247. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g, a, k, m) => {
  248. try {
  249. open_file(prompt_file(Gtk.FileChooserAction.OPEN, _("_Open")));
  250. } catch (Error e) {
  251. // It's fine...
  252. }
  253. return true;
  254. });
  255. // Adjust text size
  256. accels.connect(Gdk.Key.minus, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  257. if (font_size < 3) {
  258. return false;
  259. }
  260. if (font_size <= 10) {
  261. font_size -= 1;
  262. } else {
  263. font_size -= 2;
  264. }
  265. adjust_text_style(true);
  266. return true;
  267. });
  268. accels.connect(Gdk.Key.equal, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  269. if (font_size < 10) {
  270. font_size += 1;
  271. } else {
  272. font_size += 2;
  273. }
  274. adjust_text_style(true);
  275. return true;
  276. });
  277. // Toggle theme with Ctrl+T
  278. accels.connect(Gdk.Key.T, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  279. darkmode_button.set_active(!darkmode_button.get_active());
  280. return true;
  281. });
  282. // Publish with Ctrl+Enter
  283. accels.connect(Gdk.Key.Return, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  284. canvas.buffer.text += "\n\n" + publish();
  285. return true;
  286. });
  287. add_accel_group(accels);
  288. }
  289. private bool save_as() {
  290. try {
  291. var file = prompt_file(Gtk.FileChooserAction.SAVE, _("_Save as"));
  292. file.replace_contents(canvas.buffer.text.data, null, false,
  293. FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
  294. null);
  295. } catch (Error e) {
  296. // It's fine...
  297. }
  298. return true;
  299. }
  300. private File prompt_file(Gtk.FileChooserAction mode, string action)
  301. throws UserCancellable {
  302. var file_chooser = new Gtk.FileChooserDialog(action, this, mode,
  303. _("_Cancel"), Gtk.ResponseType.CANCEL,
  304. action, Gtk.ResponseType.ACCEPT);
  305. file_chooser.select_multiple = false;
  306. var filter = new Gtk.FileFilter();
  307. filter.add_mime_type("text/plain");
  308. file_chooser.set_filter(filter);
  309. var resp = file_chooser.run();
  310. file_chooser.close();
  311. if (resp == Gtk.ResponseType.ACCEPT) {
  312. return file_chooser.get_file();
  313. } else {
  314. throw new UserCancellable.USER_CANCELLED("FileChooserDialog");
  315. }
  316. }
  317. public void open_file(File file) throws Error {
  318. uint8[] text;
  319. file.load_contents(null, out text, null);
  320. canvas.buffer.text = (string) text;
  321. }
  322. private bool quit() {
  323. this.close();
  324. return true;
  325. }
  326. }
  327. errordomain WriteAs.UserCancellable {USER_CANCELLED}