Write.as GTK desktop app https://write.as/apps/desktop
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

285 lignes
10 KiB

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