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

249 рядки
9.2 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. construct {
  8. construct_toolbar();
  9. build_keyboard_shortcuts();
  10. canvas = new Gtk.TextView();
  11. canvas.wrap_mode = Gtk.WrapMode.WORD_CHAR;
  12. add(canvas);
  13. var text_changed = false;
  14. canvas.event_after.connect((evt) => {
  15. // TODO This word count algorithm may be quite naive
  16. // and could do improvement.
  17. var word_count = canvas.buffer.text.split(" ").length;
  18. title = ngettext("%i word","%i words",word_count).printf(word_count);
  19. text_changed = true;
  20. });
  21. Timeout.add_full(Priority.DEFAULT_IDLE, 100/*ms*/, () => {
  22. if (!text_changed) return Source.CONTINUE;
  23. try {
  24. draft_file().replace_contents(canvas.buffer.text.data, null, false,
  25. FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
  26. null);
  27. text_changed = false;
  28. } catch (Error err) {/* We'll try again anyways. */}
  29. return Source.CONTINUE;
  30. });
  31. adjust_text_style();
  32. }
  33. public MainWindow(Gtk.Application app) {
  34. set_application(app);
  35. try {
  36. open_file(draft_file());
  37. } catch (Error err) {/* It's fine... */}
  38. set_default_size(800, 600);
  39. }
  40. private static File draft_file() {
  41. var home = File.new_for_path(Environment.get_home_dir());
  42. return home.get_child(".writeas-draft.txt");
  43. }
  44. private void construct_toolbar() {
  45. var header = new Gtk.HeaderBar();
  46. header.show_close_button = true;
  47. set_titlebar(header);
  48. var publish_button = new Gtk.Button.from_icon_name("document-send",
  49. Gtk.IconSize.SMALL_TOOLBAR);
  50. publish_button.clicked.connect(() => {
  51. title = _("Publishing post…");
  52. canvas.sensitive = false;
  53. publish.begin((obj, res) => {
  54. canvas.buffer.text += "\n\n" + publish.end(res);
  55. canvas.sensitive = true;
  56. });
  57. });
  58. header.pack_end(publish_button);
  59. var darkmode_button = new Gtk.ToggleButton();
  60. darkmode_button.tooltip_text = _("Toggle dark theme");
  61. // NOTE the fallback icon is a bit of a meaning stretch, but it works.
  62. var icon_theme = Gtk.IconTheme.get_default();
  63. darkmode_button.image = new Gtk.Image.from_icon_name(
  64. icon_theme.has_icon("writeas-bright-dark") ?
  65. "writeas-bright-dark" : "weather-clear-night",
  66. Gtk.IconSize.SMALL_TOOLBAR);
  67. darkmode_button.draw_indicator = false;
  68. var settings = Gtk.Settings.get_default();
  69. darkmode_button.toggled.connect(() => {
  70. settings.gtk_application_prefer_dark_theme = darkmode_button.active;
  71. dark_mode = darkmode_button.active;
  72. adjust_text_style();
  73. });
  74. header.pack_end(darkmode_button);
  75. var fonts = new Gtk.MenuButton();
  76. fonts.tooltip_text = _("Change document font");
  77. fonts.image = new Gtk.Image.from_icon_name("font-x-generic", Gtk.IconSize.SMALL_TOOLBAR);
  78. fonts.popup = new Gtk.Menu();
  79. header.pack_start(fonts);
  80. build_fontoption(fonts.popup, _("Serif"), "serif", font);
  81. build_fontoption(fonts.popup, _("Sans-serif"), "sans",
  82. "'Open Sans', 'Segoe UI', Tahoma, Arial, sans-serif");
  83. build_fontoption(fonts.popup, _("Monospace"), "mono", "Hack, consolas," +
  84. "Menlo-Regular, Menlo, Monaco, 'ubuntu mono', monospace");
  85. fonts.popup.show_all();
  86. }
  87. private void build_fontoption(Gtk.Menu menu,
  88. string label, string fontstyle, string families) {
  89. var option = new Gtk.MenuItem.with_label(label);
  90. option.activate.connect(() => {
  91. this.font = families;
  92. this.fontstyle = fontstyle;
  93. adjust_text_style();
  94. });
  95. var styles = option.get_style_context();
  96. var provider = new Gtk.CssProvider();
  97. try {
  98. provider.load_from_data("* {font: %s;}".printf(families));
  99. styles.add_provider(provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
  100. } catch (Error e) {
  101. warning(e.message);
  102. }
  103. menu.add(option);
  104. }
  105. private Gtk.CssProvider cur_styles = null;
  106. private void adjust_text_style() {
  107. try {
  108. var styles = canvas.get_style_context();
  109. if (cur_styles != null) styles.remove_provider(cur_styles);
  110. var css = "* {font: %s; padding: 20px;}".printf(font);
  111. if (dark_mode) {
  112. // Try to detect whether the system provided a better dark mode.
  113. var text_color = styles.get_color(Gtk.StateFlags.ACTIVE);
  114. double h, s, v;
  115. Gtk.rgb_to_hsv(text_color.red, text_color.green, text_color.blue,
  116. out h, out s, out v);
  117. if (v < 0.5) css += "* {background: black; color: white;}";
  118. }
  119. cur_styles = new Gtk.CssProvider();
  120. cur_styles.load_from_data(css);
  121. styles.add_provider(cur_styles, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
  122. } catch (Error e) {
  123. warning(e.message);
  124. }
  125. }
  126. private async string publish() {
  127. var session = new Soup.Session();
  128. // Send the request
  129. var req = new Soup.Message("POST", "https://write.as/api/posts");
  130. // TODO specify font.
  131. var req_body = "{\"body\": \"%s\", \"font\": \"%s\"}".printf(
  132. canvas.buffer.text, fontstyle);
  133. req.set_request("application/json", Soup.MemoryUse.COPY, req_body.data);
  134. try {
  135. var resp = yield session.send_async(req);
  136. // Handle the response
  137. if (req.status_code != 201)
  138. return _("Error code: HTTP %u").printf(req.status_code);
  139. var json = new Json.Parser();
  140. json.load_from_stream(resp);
  141. var data = json.get_root().get_object().get_object_member("data");
  142. var url = "https://write.as/" + data.get_string_member("id");
  143. Gtk.Clipboard.get_default(get_display()).set_text(url, -1);
  144. // Open it in the browser
  145. var browser = AppInfo.get_default_for_uri_scheme("https");
  146. var urls = new List<string>();
  147. urls.append(url);
  148. browser.launch_uris(urls, null);
  149. return _("The link to your published article has been copied into your clipboard for you.");
  150. } catch (Error err) {
  151. return _("Failed to upload post! Are you connected to the Internet?")
  152. + "\n\n" + err.message;
  153. }
  154. }
  155. /* --- */
  156. private void build_keyboard_shortcuts() {
  157. /* These operations are not exposed to the UI as buttons,
  158. as most people are very familiar with them and they are not the
  159. focus of this app. */
  160. var accels = new Gtk.AccelGroup();
  161. accels.connect(Gdk.Key.S, Gdk.ModifierType.CONTROL_MASK,
  162. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  163. (g,a,k,m) => save_as());
  164. accels.connect(Gdk.Key.S,
  165. Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK,
  166. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED,
  167. (g,a,k,m) => save_as());
  168. accels.connect(Gdk.Key.O, Gdk.ModifierType.CONTROL_MASK,
  169. Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g, a, k, m) => {
  170. try {
  171. open_file(prompt_file(Gtk.FileChooserAction.OPEN, _("_Open")));
  172. } catch (Error e) {
  173. // It's fine...
  174. }
  175. return true;
  176. });
  177. add_accel_group(accels);
  178. }
  179. private bool save_as() {
  180. try {
  181. var file = prompt_file(Gtk.FileChooserAction.SAVE, _("_Save as"));
  182. file.replace_contents(canvas.buffer.text.data, null, false,
  183. FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION,
  184. null);
  185. } catch (Error e) {
  186. // It's fine...
  187. }
  188. return true;
  189. }
  190. private File prompt_file(Gtk.FileChooserAction mode, string action)
  191. throws UserCancellable {
  192. var file_chooser = new Gtk.FileChooserDialog(action, this, mode,
  193. _("_Cancel"), Gtk.ResponseType.CANCEL,
  194. action, Gtk.ResponseType.ACCEPT);
  195. file_chooser.select_multiple = false;
  196. var filter = new Gtk.FileFilter();
  197. filter.add_mime_type("text/plain");
  198. file_chooser.set_filter(filter);
  199. var resp = file_chooser.run();
  200. file_chooser.close();
  201. if (resp == Gtk.ResponseType.ACCEPT) return file_chooser.get_file();
  202. else throw new UserCancellable.USER_CANCELLED("FileChooserDialog");
  203. }
  204. public void open_file(File file) throws Error {
  205. uint8[] text;
  206. file.load_contents(null, out text, null);
  207. canvas.buffer.text = (string) text;
  208. }
  209. }
  210. errordomain WriteAs.UserCancellable {USER_CANCELLED}