Write.as GTK desktop app https://write.as/apps/desktop
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

300 строки
11 KiB

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