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.
 
 
 

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