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.
 
 
 

408 lines
15 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 Granite.ModeSwitch darkmode_switch;
  19. private Gtk.RadioMenuItem font_serif_option;
  20. private Gtk.RadioMenuItem font_sans_option;
  21. private Gtk.RadioMenuItem font_wrap_option;
  22. private static string data_dir = ".writeas";
  23. private static string version = "1.0.1";
  24. private int font_size = 16;
  25. private bool dark_mode = false;
  26. private string font = "Lora, 'Palatino Linotype',"
  27. + "'Book Antiqua', 'New York', 'DejaVu serif', serif";
  28. private string fontstyle = "serif";
  29. private bool text_changed = false;
  30. private bool is_initializing = true;
  31. construct {
  32. header = new Gtk.HeaderBar();
  33. header.title = "Write.as";
  34. construct_toolbar();
  35. build_keyboard_shortcuts();
  36. var scrolled = new Gtk.ScrolledWindow(null, null);
  37. canvas = new Gtk.SourceView();
  38. canvas.wrap_mode = Gtk.WrapMode.WORD_CHAR;
  39. scrolled.add(canvas);
  40. add(scrolled);
  41. size_allocate.connect((_) => {adjust_text_style();});
  42. canvas.event_after.connect((evt) => {
  43. // TODO This word count algorithm may be quite naive
  44. // and could do improvement.
  45. var word_count = canvas.buffer.text.split(" ").length;
  46. header.subtitle = ngettext("%i word","%i words",word_count).printf(word_count);
  47. text_changed = true;
  48. });
  49. Timeout.add_full(Priority.DEFAULT_IDLE, 100/*ms*/, () => {
  50. if (!text_changed) return Source.CONTINUE;
  51. var text = canvas.buffer.text;
  52. // This happens sometimes for some reason, but it's difficult to debug.
  53. if (text == "") return Source.CONTINUE;
  54. try {
  55. draft_file().replace_contents(text.data, null, false,
  56. FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
  57. null);
  58. text_changed = false;
  59. } catch (Error err) {/* We'll try again anyways. */}
  60. return Source.CONTINUE;
  61. });
  62. adjust_text_style(false);
  63. }
  64. public MainWindow(Gtk.Application app) {
  65. stdout.printf("writeas-gtk v%s\n", version);
  66. set_application(app);
  67. icon_name = "write-as";
  68. init_folder();
  69. try {
  70. open_file(draft_file());
  71. } catch (Error err) {}
  72. restore_styles();
  73. set_default_size(800, 600);
  74. is_initializing = false;
  75. }
  76. private static void init_folder() {
  77. var home = File.new_for_path(get_data_dir());
  78. try {
  79. home.make_directory();
  80. } catch (Error e) {
  81. stderr.printf("Create data dir: %s\n", e.message);
  82. }
  83. }
  84. private static string get_data_dir() {
  85. return Environment.get_home_dir() + "/" + data_dir;
  86. }
  87. private static File draft_file() {
  88. var home = File.new_for_path(get_data_dir());
  89. return home.get_child("draft.txt");
  90. }
  91. private static bool supports_dark_theme() {
  92. var theme = Gtk.Settings.get_default().gtk_theme_name;
  93. foreach (var datapath in Environment.get_system_data_dirs()) {
  94. var path = File.new_for_path(Path.build_filename(datapath, "themes", theme));
  95. if (path.get_child("gtk-dark.css").query_exists()) return true;
  96. try {
  97. var enumerator = path.enumerate_children("standard::*", 0);
  98. FileInfo info = null;
  99. while ((info = enumerator.next_file()) != null) {
  100. var fullpath = path.get_child(info.get_name()).get_child("gtk-dark.css");
  101. if (fullpath.query_exists()) return true;
  102. }
  103. } catch (Error err) {/* Might be missing something, but no biggy. */}
  104. }
  105. return false;
  106. }
  107. private void construct_toolbar() {
  108. header.show_close_button = true;
  109. set_titlebar(header);
  110. var publish_button = new Gtk.Button.from_icon_name("document-send",
  111. Gtk.IconSize.SMALL_TOOLBAR);
  112. publish_button.tooltip_text = _("Publish to Write.as on the web");
  113. publish_button.clicked.connect(() => {
  114. canvas.buffer.text += "\n\n" + publish();
  115. canvas.grab_focus();
  116. });
  117. header.pack_end(publish_button);
  118. darkmode_switch = new Granite.ModeSwitch.from_icon_name ("display-brightness-symbolic", "weather-clear-night-symbolic");
  119. darkmode_switch.primary_icon_tooltip_text = ("Light theme");
  120. darkmode_switch.secondary_icon_tooltip_text = ("Dark theme");
  121. darkmode_switch.valign = Gtk.Align.CENTER;
  122. var settings = Gtk.Settings.get_default();
  123. darkmode_switch.notify["active"].connect(() => {
  124. settings.gtk_application_prefer_dark_theme = darkmode_switch.active;
  125. dark_mode = darkmode_switch.active;
  126. if (!is_initializing) theme_save();
  127. canvas.grab_focus();
  128. });
  129. if (supports_dark_theme()) header.pack_end(darkmode_switch);
  130. var fonts = new Gtk.MenuButton();
  131. fonts.tooltip_text = _("Change document font");
  132. fonts.image = new Gtk.Image.from_icon_name("font-x-generic", Gtk.IconSize.SMALL_TOOLBAR);
  133. fonts.popup = new Gtk.Menu();
  134. header.pack_start(fonts);
  135. font_serif_option = build_fontoption(fonts.popup, _("Serif"), "serif", font);
  136. font_sans_option = build_fontoption(fonts.popup, _("Sans-serif"), "sans",
  137. "'Open Sans', 'Segoe UI', Tahoma, Arial, sans-serif");
  138. font_wrap_option = build_fontoption(fonts.popup, _("Monospace"), "wrap", "Hack, consolas," +
  139. "Menlo-Regular, Menlo, Monaco, 'ubuntu mono', monospace");
  140. fonts.popup.show_all();
  141. }
  142. private unowned SList<Gtk.RadioMenuItem>? font_options = null;
  143. private Gtk.RadioMenuItem build_fontoption(Gtk.Menu menu,
  144. string label, string fontstyle, string families) {
  145. var option = new Gtk.RadioMenuItem.with_label(font_options, label);
  146. font_options = option.get_group();
  147. option.activate.connect(() => {
  148. this.font = families;
  149. this.fontstyle = fontstyle;
  150. adjust_text_style(!is_initializing);
  151. canvas.grab_focus();
  152. });
  153. var styles = option.get_style_context();
  154. var provider = new Gtk.CssProvider();
  155. try {
  156. provider.load_from_data("* {font-family: %s;}".printf(families));
  157. styles.add_provider(provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
  158. } catch (Error e) {
  159. warning(e.message);
  160. }
  161. menu.add(option);
  162. return option;
  163. }
  164. public override void grab_focus() {
  165. canvas.grab_focus();
  166. }
  167. private KeyFile theme = new KeyFile();
  168. private void restore_styles() {
  169. try {
  170. loaded_theme = true;
  171. theme.load_from_file(get_data_dir() + "/prefs.ini", KeyFileFlags.NONE);
  172. dark_mode = theme.get_boolean("Theme", "darkmode");
  173. darkmode_switch.active = dark_mode;
  174. Gtk.Settings.get_default().gtk_application_prefer_dark_theme = dark_mode;
  175. font_size = theme.get_integer("Theme", "fontsize");
  176. font = theme.get_string("Post", "font");
  177. fontstyle = theme.get_string("Post", "fontstyle");
  178. // Select the current font in the menu
  179. if (fontstyle == "serif") {
  180. font_serif_option.set_active(true);
  181. } else if (fontstyle == "sans") {
  182. font_sans_option.set_active(true);
  183. } else if (fontstyle == "wrap") {
  184. font_wrap_option.set_active(true);
  185. }
  186. adjust_text_style(false);
  187. } catch (Error err) {/* No biggy... */}
  188. }
  189. private Gtk.CssProvider cur_styles = null;
  190. // So the theme isn't read before it's saved.
  191. private bool loaded_theme = false;
  192. private void adjust_text_style(bool save_theme = true) {
  193. try {
  194. if (cur_styles != null)
  195. Gtk.StyleContext.remove_provider_for_screen(Gdk.Screen.get_default(), cur_styles);
  196. var padding = canvas.get_allocated_width()*0.10;
  197. var css = ("textview {font-family: %s; font-size: %dpx; padding: 20px 0;" +
  198. " caret-color: #5ac4ee;}").printf(font, font_size);
  199. cur_styles = new Gtk.CssProvider();
  200. cur_styles.load_from_data(css);
  201. Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
  202. cur_styles, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
  203. canvas.left_margin = canvas.right_margin = (int) padding;
  204. if (save_theme) theme_save();
  205. } catch (Error e) {
  206. warning(e.message);
  207. }
  208. }
  209. private void theme_save() {
  210. if (!loaded_theme) return;
  211. theme.set_boolean("Theme", "darkmode", dark_mode);
  212. theme.set_integer("Theme", "fontsize", font_size);
  213. theme.set_string("Post", "font", font);
  214. theme.set_string("Post", "fontstyle", fontstyle);
  215. try {
  216. theme.save_to_file(get_data_dir() + "/prefs.ini");
  217. } catch (FileError err) {/* Oh well. */}
  218. }
  219. private string publish() {
  220. try {
  221. if (text_changed) {;
  222. draft_file().replace_contents(canvas.buffer.text.data, null, false,
  223. FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
  224. null);
  225. text_changed = false;
  226. }
  227. var cmd = "sh -c 'cat ~/" + data_dir + "/draft.txt | writeas --md --font %s --user-agent \"writeas-gtk v" + version + "\"'";
  228. cmd = cmd.printf(fontstyle);
  229. string stdout, stderr;
  230. int status;
  231. Process.spawn_command_line_sync(cmd,
  232. out stdout, out stderr, out status);
  233. // Open it in the browser
  234. if (status == 0) {
  235. var browser = AppInfo.get_default_for_uri_scheme("https");
  236. var urls = new List<string>();
  237. urls.append(stdout.strip());
  238. browser.launch_uris(urls, null);
  239. }
  240. return stderr.strip();
  241. } catch (Error err) {
  242. return err.message;
  243. }
  244. }
  245. /* --- */
  246. private void build_keyboard_shortcuts() {
  247. /* These operations are not exposed to the UI as buttons,
  248. as most people are very familiar with them and they are not the
  249. focus of this app. */
  250. var accels = new Gtk.AccelGroup();
  251. // App operations
  252. accels.connect(Gdk.Key.W, Gdk.ModifierType.CONTROL_MASK,
  253. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  254. (g,a,k,m) => quit());
  255. accels.connect(Gdk.Key.Q, Gdk.ModifierType.CONTROL_MASK,
  256. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  257. (g,a,k,m) => quit());
  258. // File operations
  259. accels.connect(Gdk.Key.S, Gdk.ModifierType.CONTROL_MASK,
  260. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  261. (g,a,k,m) => save_as());
  262. accels.connect(Gdk.Key.S,
  263. Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK,
  264. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  265. (g,a,k,m) => save_as());
  266. // Adjust text size
  267. accels.connect(Gdk.Key.minus, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  268. if (font_size < 3) {
  269. return false;
  270. }
  271. if (font_size <= 10) {
  272. font_size -= 1;
  273. } else {
  274. font_size -= 2;
  275. }
  276. adjust_text_style(true);
  277. return true;
  278. });
  279. accels.connect(Gdk.Key.equal, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  280. if (font_size < 10) {
  281. font_size += 1;
  282. } else {
  283. font_size += 2;
  284. }
  285. adjust_text_style(true);
  286. return true;
  287. });
  288. // Toggle theme with Ctrl+T
  289. accels.connect(Gdk.Key.T, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  290. darkmode_switch.active = !darkmode_switch.active;
  291. return true;
  292. });
  293. // Publish with Ctrl+Enter
  294. accels.connect(Gdk.Key.Return, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
  295. canvas.buffer.text += "\n\n" + publish();
  296. return true;
  297. });
  298. add_accel_group(accels);
  299. }
  300. private bool save_as() {
  301. try {
  302. var file = prompt_file(Gtk.FileChooserAction.SAVE, _("Save as"));
  303. file.replace_contents(canvas.buffer.text.data, null, false,
  304. FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
  305. null);
  306. } catch (Error e) {
  307. // It's fine...
  308. }
  309. return true;
  310. }
  311. private File prompt_file(Gtk.FileChooserAction mode, string action)
  312. throws UserCancellable {
  313. var file_chooser = new Gtk.FileChooserDialog(action, this, mode,
  314. _("Cancel"), Gtk.ResponseType.CANCEL,
  315. action, Gtk.ResponseType.ACCEPT);
  316. file_chooser.select_multiple = false;
  317. var filter = new Gtk.FileFilter();
  318. filter.add_mime_type("text/plain");
  319. file_chooser.set_filter(filter);
  320. var resp = file_chooser.run();
  321. file_chooser.close();
  322. if (resp == Gtk.ResponseType.ACCEPT) {
  323. return file_chooser.get_file();
  324. } else {
  325. throw new UserCancellable.USER_CANCELLED("FileChooserDialog");
  326. }
  327. }
  328. public void open_file(File file) throws Error {
  329. uint8[] text;
  330. file.load_contents(null, out text, null);
  331. canvas.buffer.text = (string) text;
  332. }
  333. private bool quit() {
  334. this.close();
  335. return true;
  336. }
  337. }
  338. errordomain WriteAs.UserCancellable {USER_CANCELLED}