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.
 
 
 

317 lines
11 KiB

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