Write.as GTK desktop app https://write.as/apps/desktop
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

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