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.
 
 
 

368 lines
13 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.TextView();
  34. canvas.wrap_mode = Gtk.WrapMode.WORD_CHAR;
  35. scrolled.add(canvas);
  36. add(scrolled);
  37. canvas.event_after.connect((evt) => {
  38. // TODO This word count algorithm may be quite naive
  39. // and could do improvement.
  40. var word_count = canvas.buffer.text.split(" ").length;
  41. header.subtitle = ngettext("%i word","%i words",word_count).printf(word_count);
  42. text_changed = true;
  43. });
  44. Timeout.add_full(Priority.DEFAULT_IDLE, 100/*ms*/, () => {
  45. if (!text_changed) return Source.CONTINUE;
  46. try {
  47. draft_file().replace_contents(canvas.buffer.text.data, null, false,
  48. FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
  49. null);
  50. text_changed = false;
  51. } catch (Error err) {/* We'll try again anyways. */}
  52. return Source.CONTINUE;
  53. });
  54. adjust_text_style(false);
  55. }
  56. public MainWindow(Gtk.Application app) {
  57. set_application(app);
  58. icon_name = "write-as";
  59. init_folder();
  60. try {
  61. open_file(draft_file());
  62. } catch (Error err) {/* It's fine... */}
  63. restore_styles();
  64. set_default_size(800, 600);
  65. is_initializing = false;
  66. }
  67. private static void init_folder() {
  68. var home = File.new_for_path(get_data_dir());
  69. try {
  70. home.make_directory();
  71. } catch (Error e) {
  72. stderr.printf("Create data dir: %s\n", e.message);
  73. }
  74. }
  75. private static string get_data_dir() {
  76. return Environment.get_home_dir() + "/" + data_dir;
  77. }
  78. private static File draft_file() {
  79. var home = File.new_for_path(get_data_dir());
  80. return home.get_child("draft.txt");
  81. }
  82. private void construct_toolbar() {
  83. header.show_close_button = true;
  84. set_titlebar(header);
  85. var publish_button = new Gtk.Button.from_icon_name("document-send",
  86. Gtk.IconSize.SMALL_TOOLBAR);
  87. publish_button.clicked.connect(() => {
  88. canvas.buffer.text += "\n\n" + publish();
  89. });
  90. header.pack_end(publish_button);
  91. darkmode_button = new Gtk.ToggleButton();
  92. darkmode_button.tooltip_text = _("Toggle dark theme");
  93. // NOTE the fallback icon is a bit of a meaning stretch, but it works.
  94. var icon_theme = Gtk.IconTheme.get_default();
  95. darkmode_button.image = new Gtk.Image.from_icon_name(
  96. icon_theme.has_icon("writeas-bright-dark") ?
  97. "writeas-bright-dark" : "weather-clear-night",
  98. Gtk.IconSize.SMALL_TOOLBAR);
  99. darkmode_button.draw_indicator = false;
  100. var settings = Gtk.Settings.get_default();
  101. darkmode_button.toggled.connect(() => {
  102. settings.gtk_application_prefer_dark_theme = darkmode_button.active;
  103. dark_mode = darkmode_button.active;
  104. adjust_text_style(!is_initializing);
  105. });
  106. header.pack_end(darkmode_button);
  107. var fonts = new Gtk.MenuButton();
  108. fonts.tooltip_text = _("Change document font");
  109. fonts.image = new Gtk.Image.from_icon_name("font-x-generic", Gtk.IconSize.SMALL_TOOLBAR);
  110. fonts.popup = new Gtk.Menu();
  111. header.pack_start(fonts);
  112. build_fontoption(fonts.popup, _("Serif"), "serif", font);
  113. build_fontoption(fonts.popup, _("Sans-serif"), "sans",
  114. "'Open Sans', 'Segoe UI', Tahoma, Arial, sans-serif");
  115. build_fontoption(fonts.popup, _("Monospace"), "wrap", "Hack, consolas," +
  116. "Menlo-Regular, Menlo, Monaco, 'ubuntu mono', monospace");
  117. fonts.popup.show_all();
  118. }
  119. private unowned SList<Gtk.RadioMenuItem>? font_options = null;
  120. private void build_fontoption(Gtk.Menu menu,
  121. string label, string fontstyle, string families) {
  122. var option = new Gtk.RadioMenuItem.with_label(font_options, label);
  123. font_options = option.get_group();
  124. option.activate.connect(() => {
  125. this.font = families;
  126. this.fontstyle = fontstyle;
  127. adjust_text_style(!is_initializing);
  128. });
  129. var styles = option.get_style_context();
  130. var provider = new Gtk.CssProvider();
  131. try {
  132. provider.load_from_data("* {font: %s;}".printf(families));
  133. styles.add_provider(provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
  134. } catch (Error e) {
  135. warning(e.message);
  136. }
  137. menu.add(option);
  138. }
  139. private KeyFile theme = new KeyFile();
  140. private void restore_styles() {
  141. try {
  142. loaded_theme = true;
  143. theme.load_from_file(get_data_dir() + "/prefs.ini", KeyFileFlags.NONE);
  144. dark_mode = theme.get_boolean("Theme", "darkmode");
  145. darkmode_button.set_active(dark_mode);
  146. Gtk.Settings.get_default().gtk_application_prefer_dark_theme = dark_mode;
  147. font_size = theme.get_integer("Theme", "fontsize");
  148. font = theme.get_string("Post", "font");
  149. fontstyle = theme.get_string("Post", "fontstyle");
  150. adjust_text_style(false);
  151. } catch (Error err) {/* No biggy... */}
  152. }
  153. private Gtk.CssProvider cur_styles = null;
  154. // So the theme isn't read before it's saved.
  155. private bool loaded_theme = false;
  156. private void adjust_text_style(bool save_theme = true) {
  157. try {
  158. var styles = canvas.get_style_context();
  159. if (cur_styles != null) styles.remove_provider(cur_styles);
  160. var css = "* {font: %s; font-size: %dpt; padding: 20px;}".printf(font, font_size);
  161. if (dark_mode) {
  162. // Try to detect whether the system provided a better dark mode.
  163. var text_color = styles.get_color(Gtk.StateFlags.ACTIVE);
  164. double h, s, v;
  165. Gtk.rgb_to_hsv(text_color.red, text_color.green, text_color.blue,
  166. out h, out s, out v);
  167. if (v < 0.5) css += "* {background: #222; color: white;}";
  168. }
  169. cur_styles = new Gtk.CssProvider();
  170. cur_styles.load_from_data(css);
  171. styles.add_provider(cur_styles, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
  172. if (save_theme && loaded_theme) {
  173. theme.set_boolean("Theme", "darkmode", dark_mode);
  174. theme.set_integer("Theme", "fontsize", font_size);
  175. theme.set_string("Post", "font", font);
  176. theme.set_string("Post", "fontstyle", fontstyle);
  177. theme.save_to_file(get_data_dir() + "/prefs.ini");
  178. }
  179. } catch (Error e) {
  180. warning(e.message);
  181. }
  182. }
  183. private string publish() {
  184. try {
  185. if (text_changed) {;
  186. draft_file().replace_contents(canvas.buffer.text.data, null, false,
  187. FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
  188. null);
  189. text_changed = false;
  190. }
  191. var cmd = "sh -c 'cat ~/" + data_dir + "/draft.txt | writeas --font %s'";
  192. cmd = cmd.printf(fontstyle);
  193. string stdout, stderr;
  194. int status;
  195. Process.spawn_command_line_sync(cmd,
  196. out stdout, out stderr, out status);
  197. // Open it in the browser
  198. if (status == 0) {
  199. var browser = AppInfo.get_default_for_uri_scheme("https");
  200. var urls = new List<string>();
  201. urls.append(stdout.strip());
  202. browser.launch_uris(urls, null);
  203. }
  204. return stderr.strip();
  205. } catch (Error err) {
  206. return err.message;
  207. }
  208. }
  209. /* --- */
  210. private void build_keyboard_shortcuts() {
  211. /* These operations are not exposed to the UI as buttons,
  212. as most people are very familiar with them and they are not the
  213. focus of this app. */
  214. var accels = new Gtk.AccelGroup();
  215. // App operations
  216. accels.connect(Gdk.Key.W, Gdk.ModifierType.CONTROL_MASK,
  217. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  218. (g,a,k,m) => quit());
  219. accels.connect(Gdk.Key.Q, Gdk.ModifierType.CONTROL_MASK,
  220. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  221. (g,a,k,m) => quit());
  222. // File operations
  223. accels.connect(Gdk.Key.S, Gdk.ModifierType.CONTROL_MASK,
  224. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  225. (g,a,k,m) => save_as());
  226. accels.connect(Gdk.Key.S,
  227. Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK,
  228. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  229. (g,a,k,m) => save_as());
  230. accels.connect(Gdk.Key.O, Gdk.ModifierType.CONTROL_MASK,
  231. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g, a, k, m) => {
  232. try {
  233. open_file(prompt_file(Gtk.FileChooserAction.OPEN, _("_Open")));
  234. } catch (Error e) {
  235. // It's fine...
  236. }
  237. return true;
  238. });
  239. // Adjust text size
  240. accels.connect(Gdk.Key.minus, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  241. if (font_size < 3) {
  242. return false;
  243. }
  244. if (font_size <= 10) {
  245. font_size -= 1;
  246. } else {
  247. font_size -= 2;
  248. }
  249. adjust_text_style(true);
  250. return true;
  251. });
  252. accels.connect(Gdk.Key.equal, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  253. if (font_size < 10) {
  254. font_size += 1;
  255. } else {
  256. font_size += 2;
  257. }
  258. adjust_text_style(true);
  259. return true;
  260. });
  261. // Toggle theme with Ctrl+T
  262. accels.connect(Gdk.Key.T, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  263. darkmode_button.set_active(!darkmode_button.get_active());
  264. return true;
  265. });
  266. // Publish with Ctrl+Enter
  267. accels.connect(Gdk.Key.Return, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  268. canvas.buffer.text += "\n\n" + publish();
  269. return true;
  270. });
  271. add_accel_group(accels);
  272. }
  273. private bool save_as() {
  274. try {
  275. var file = prompt_file(Gtk.FileChooserAction.SAVE, _("_Save as"));
  276. file.replace_contents(canvas.buffer.text.data, null, false,
  277. FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
  278. null);
  279. } catch (Error e) {
  280. // It's fine...
  281. }
  282. return true;
  283. }
  284. private File prompt_file(Gtk.FileChooserAction mode, string action)
  285. throws UserCancellable {
  286. var file_chooser = new Gtk.FileChooserDialog(action, this, mode,
  287. _("_Cancel"), Gtk.ResponseType.CANCEL,
  288. action, Gtk.ResponseType.ACCEPT);
  289. file_chooser.select_multiple = false;
  290. var filter = new Gtk.FileFilter();
  291. filter.add_mime_type("text/plain");
  292. file_chooser.set_filter(filter);
  293. var resp = file_chooser.run();
  294. file_chooser.close();
  295. if (resp == Gtk.ResponseType.ACCEPT) {
  296. return file_chooser.get_file();
  297. } else {
  298. throw new UserCancellable.USER_CANCELLED("FileChooserDialog");
  299. }
  300. }
  301. public void open_file(File file) throws Error {
  302. uint8[] text;
  303. file.load_contents(null, out text, null);
  304. canvas.buffer.text = (string) text;
  305. }
  306. private bool quit() {
  307. this.close();
  308. return true;
  309. }
  310. }
  311. errordomain WriteAs.UserCancellable {USER_CANCELLED}