Write.as GTK desktop app https://write.as/apps/desktop
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

272 rader
9.9 KiB

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