diff --git a/src/Granite/Accels.vala b/src/Granite/Accels.vala new file mode 100644 index 0000000..33eb62a --- /dev/null +++ b/src/Granite/Accels.vala @@ -0,0 +1,136 @@ +// From https://github.com/elementary/granite/blob/621f2669f6c8940fe32ac9817ddca92e97d27ae0/lib/Widgets/Utils.vala#L79-L192 + +/* + * Copyright (C) 2012-2017 Granite Developers + * + * This program or library is free software; you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + */ + +namespace Granite { + +/** + * Converts a {@link Gtk.accelerator_parse} style accel string to a human-readable string. + * + * @param accel an accelerator label like “a” or “Right” + * + * @return a human-readable string like "Ctrl + A" or "⌘ + →" + */ +public static string accel_to_string (string accel) { + uint accel_key; + Gdk.ModifierType accel_mods; + Gtk.accelerator_parse (accel, out accel_key, out accel_mods); + + string[] arr = {}; + if (Gdk.ModifierType.SUPER_MASK in accel_mods) { + arr += "⌘"; + } + + if (Gdk.ModifierType.SHIFT_MASK in accel_mods) { + arr += _("Shift"); + } + + if (Gdk.ModifierType.CONTROL_MASK in accel_mods) { + arr += _("Ctrl"); + } + + if (Gdk.ModifierType.MOD1_MASK in accel_mods) { + arr += _("Alt"); + } + + switch (accel_key) { + case Gdk.Key.Up: + arr += "↑"; + break; + case Gdk.Key.Down: + arr += "↓"; + break; + case Gdk.Key.Left: + arr += "←"; + break; + case Gdk.Key.Right: + arr += "→"; + break; + case Gdk.Key.minus: + case Gdk.Key.KP_Subtract: + ///TRANSLATORS: This is a non-symbol representation of the "-" key + arr += _("Minus"); + break; + case Gdk.Key.KP_Add: + case Gdk.Key.plus: + ///TRANSLATORS: This is a non-symbol representation of the "+" key + arr += _("Plus"); + break; + case Gdk.Key.KP_Enter: + case Gdk.Key.Return: + arr += _("Enter"); + break; + default: + arr += Gtk.accelerator_get_label (accel_key, 0); + break; + } + + return string.joinv (" + ", arr); +} + +/** + * Takes a description and an array of accels and returns {@link Pango} markup for use in a {@link Gtk.Tooltip}. This method uses {@link Granite.accel_to_string}. + * + * Example: + * + * Description + * Shortcut 1, Shortcut 2 + * + * @param a string array of accelerator labels like {"a", "Right"} + * + * @param description a standard tooltip text string + * + * @return {@link Pango} markup with the description label on one line and a list of human-readable accels on a new line + */ +public static string markup_accel_tooltip (string[]? accels, string? description = null) { + string[] parts = {}; + if (description != null && description != "") { + parts += description; + } + + if (accels != null && accels.length > 0) { + string[] unique_accels = {}; + + for (int i = 0; i < accels.length; i++) { + if (accels[i] == "") { + continue; + } + + var accel_string = accel_to_string (accels[i]); + if (!(accel_string in unique_accels)) { + unique_accels += accel_string; + } + } + + if (unique_accels.length > 0) { + ///TRANSLATORS: This is a delimiter that separates two keyboard shortcut labels like "⌘ + →, Control + A" + var accel_label = string.joinv (_(", "), unique_accels); + + var accel_markup = """%s""".printf (accel_label); + + parts += accel_markup; + } + } + + return string.joinv ("\n", parts); +} + +} + diff --git a/src/Widgets/ModeSwitch.vala b/src/Granite/ModeSwitch.vala similarity index 100% rename from src/Widgets/ModeSwitch.vala rename to src/Granite/ModeSwitch.vala index 98d99f2..bb1c720 100644 --- a/src/Widgets/ModeSwitch.vala +++ b/src/Granite/ModeSwitch.vala @@ -1,157 +1,157 @@ +// From https://github.com/elementary/granite/blob/2066b377226cf327cb2d5399b6b40a2d36d47b11/lib/Widgets/ModeSwitch.vala + /* * Copyright (c) 2018 elementary, Inc. (https://elementary.io) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA */ -// From https://github.com/elementary/granite/blob/2066b377226cf327cb2d5399b6b40a2d36d47b11/lib/Widgets/ModeSwitch.vala - /** * ModeSwitch is a selection control for choosing between two options that can be described with an icon. * * ''Example''<
> * {{{ * var gtk_settings = Gtk.Settings.get_default (); * * var mode_switch = new ModeSwitch.from_icon_name ("display-brightness-symbolic", "weather-clear-night-symbolic"); * mode_switch.primary_icon_tooltip_text = _("Light background"); * mode_switch.secondary_icon_tooltip_text = _("Dark background"); * mode_switch.bind_property ("active", gtk_settings, "gtk_application_prefer_dark_theme"); * }}} */ public class Granite.ModeSwitch : Gtk.Grid { /** * Whether the {@link Gtk.Switch} widget is pointing to the secondary icon or not. */ public bool active { get; set; } /** * The {@link GLib.Icon} to use for the primary icon for the switch. */ public GLib.Icon primary_icon_gicon { get; construct set; } /** * The icon name to use for the primary icon for the switch. */ public string primary_icon_name { get; construct set; } /** * The contents of the tooltip on the primary icon. */ public string primary_icon_tooltip_text { get; set; } /** * The {@link GLib.Icon} to use for the secondary icon for the switch. */ public GLib.Icon secondary_icon_gicon { get; construct set; } /** * The icon name to use for the secondary icon for the switch. */ public string secondary_icon_name { get; construct set; } /** * The contents of the tooltip on the secondary icon. */ public string secondary_icon_tooltip_text { get; set; } /** * Constructs a new {@link Granite.ModeSwitch} using {@link GLib.Icon}s. * * @param primary_icon_gicon The {@link GLib.Icon} to use for the primary icon for the switch. * @param secondary_icon_gicon The {@link GLib.Icon} to use for the secondary icon for the switch. */ public ModeSwitch (GLib.Icon primary_icon_gicon, GLib.Icon secondary_icon_gicon) { Object ( primary_icon_gicon: primary_icon_gicon, secondary_icon_gicon: secondary_icon_gicon ); } /** * Constructs a new {@link Granite.ModeSwitch} from icon names. * * @param primary_icon_name The icon name to use for the primary icon for the switch. * @param secondary_icon_name The icon name to use for the secondary icon for the switch. */ public ModeSwitch.from_icon_name (string primary_icon_name, string secondary_icon_name) { Object ( primary_icon_gicon: new ThemedIcon (primary_icon_name), secondary_icon_gicon: new ThemedIcon (secondary_icon_name), primary_icon_name: primary_icon_name, secondary_icon_name: secondary_icon_name ); } construct { var primary_image = new Gtk.Image (); primary_image.pixel_size = 16; var primary_icon_box = new Gtk.EventBox (); primary_icon_box.add_events (Gdk.EventMask.BUTTON_RELEASE_MASK); primary_icon_box.add (primary_image); var mode_switch = new Gtk.Switch (); mode_switch.valign = Gtk.Align.CENTER; mode_switch.get_style_context ().add_class ("mode-switch"); var secondary_icon = new Gtk.Image (); secondary_icon.pixel_size = 16; var secondary_icon_box = new Gtk.EventBox (); secondary_icon_box.add_events (Gdk.EventMask.BUTTON_RELEASE_MASK); secondary_icon_box.add (secondary_icon); column_spacing = 6; add (primary_icon_box); add (mode_switch); add (secondary_icon_box); bind_property ("primary-icon-gicon", primary_image, "gicon", GLib.BindingFlags.SYNC_CREATE); bind_property ("primary-icon-name", primary_image, "icon-name", GLib.BindingFlags.SYNC_CREATE); bind_property ("primary-icon-tooltip-text", primary_image, "tooltip-text"); bind_property ("secondary-icon-gicon", secondary_icon, "gicon", GLib.BindingFlags.SYNC_CREATE); bind_property ("secondary-icon-name", secondary_icon, "icon_name", GLib.BindingFlags.SYNC_CREATE); bind_property ("secondary-icon-tooltip-text", secondary_icon, "tooltip-text"); this.notify["active"].connect (() => { if (Gtk.StateFlags.DIR_RTL in get_state_flags ()) { mode_switch.active = !active; } else { mode_switch.active = active; } }); mode_switch.notify["active"].connect (() => { if (Gtk.StateFlags.DIR_RTL in get_state_flags ()) { active = !mode_switch.active; } else { active = mode_switch.active; } }); primary_icon_box.button_release_event.connect (() => { active = false; return Gdk.EVENT_STOP; }); secondary_icon_box.button_release_event.connect (() => { active = true; return Gdk.EVENT_STOP; }); } } diff --git a/src/meson.build b/src/meson.build index 991bce1..761c5c9 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,10 +1,11 @@ executable('com.github.writeas.writeas-gtk', 'application.vala', 'window.vala', - 'Widgets/ModeSwitch.vala', + 'Granite/Accels.vala', + 'Granite/ModeSwitch.vala', c_args: ['-include', 'config.h'], link_args: '-lm', dependencies: [dependency('gtk+-3.0'), dependency('gtksourceview-3.0')], install: true ) diff --git a/src/window.vala b/src/window.vala index bbfd2c9..8df938f 100644 --- a/src/window.vala +++ b/src/window.vala @@ -1,407 +1,414 @@ /* Copyright © 2018 Write.as This file is part of the Write.as GTK desktop app. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ public class WriteAs.MainWindow : Gtk.ApplicationWindow { private Gtk.TextView canvas; private Gtk.HeaderBar header; private Granite.ModeSwitch darkmode_switch; private Gtk.RadioMenuItem font_serif_option; private Gtk.RadioMenuItem font_sans_option; private Gtk.RadioMenuItem font_wrap_option; private static string data_dir = ".writeas"; private static string version = "1.0.2"; private int font_size = 16; private bool dark_mode = false; private string font = "Lora, 'Palatino Linotype'," + "'Book Antiqua', 'New York', 'DejaVu serif', serif"; private string fontstyle = "serif"; private bool text_changed = false; private bool is_initializing = true; construct { header = new Gtk.HeaderBar(); header.title = "Write.as"; construct_toolbar(); build_keyboard_shortcuts(); var scrolled = new Gtk.ScrolledWindow(null, null); canvas = new Gtk.SourceView(); canvas.wrap_mode = Gtk.WrapMode.WORD_CHAR; scrolled.add(canvas); add(scrolled); size_allocate.connect((_) => {adjust_text_style();}); canvas.event_after.connect((evt) => { // TODO This word count algorithm may be quite naive // and could do improvement. var word_count = canvas.buffer.text.split(" ").length; header.subtitle = ngettext("%i word","%i words",word_count).printf(word_count); text_changed = true; }); Timeout.add_full(Priority.DEFAULT_IDLE, 100/*ms*/, () => { if (!text_changed) return Source.CONTINUE; var text = canvas.buffer.text; // This happens sometimes for some reason, but it's difficult to debug. if (text == "") return Source.CONTINUE; try { draft_file().replace_contents(text.data, null, false, FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION, null); text_changed = false; } catch (Error err) {/* We'll try again anyways. */} return Source.CONTINUE; }); adjust_text_style(false); } public MainWindow(Gtk.Application app) { stdout.printf("writeas-gtk v%s\n", version); set_application(app); icon_name = "com.github.writeas.writeas-gtk"; init_folder(); try { open_file(draft_file()); } catch (Error err) {} restore_styles(); set_default_size(800, 600); is_initializing = false; } private static void init_folder() { var home = File.new_for_path(get_data_dir()); try { home.make_directory(); } catch (Error e) { stderr.printf("Create data dir: %s\n", e.message); } } private static string get_data_dir() { return Environment.get_home_dir() + "/" + data_dir; } private static File draft_file() { var home = File.new_for_path(get_data_dir()); return home.get_child("draft.txt"); } private static bool supports_dark_theme() { var theme = Gtk.Settings.get_default().gtk_theme_name; foreach (var datapath in Environment.get_system_data_dirs()) { var path = File.new_for_path(Path.build_filename(datapath, "themes", theme)); if (path.get_child("gtk-dark.css").query_exists()) return true; try { var enumerator = path.enumerate_children("standard::*", 0); FileInfo info = null; while ((info = enumerator.next_file()) != null) { var fullpath = path.get_child(info.get_name()).get_child("gtk-dark.css"); if (fullpath.query_exists()) return true; } } catch (Error err) {/* Might be missing something, but no biggy. */} } return false; } private void construct_toolbar() { header.show_close_button = true; set_titlebar(header); var publish_button = new Gtk.Button.from_icon_name("document-send", Gtk.IconSize.LARGE_TOOLBAR); - publish_button.tooltip_text = _("Publish to Write.as on the web"); + publish_button.tooltip_markup = Granite.markup_accel_tooltip ( + {"Return"}, + _("Publish to Write.as on the web") + ); publish_button.clicked.connect(() => { canvas.buffer.text += "\n\n" + publish(); canvas.grab_focus(); }); header.pack_end(publish_button); darkmode_switch = new Granite.ModeSwitch.from_icon_name ("display-brightness-symbolic", "weather-clear-night-symbolic"); darkmode_switch.primary_icon_tooltip_text = ("Light theme"); darkmode_switch.secondary_icon_tooltip_text = ("Dark theme"); + darkmode_switch.tooltip_markup = Granite.markup_accel_tooltip ( + {"T"}, + _("Toggle light/dark theme") + ); darkmode_switch.valign = Gtk.Align.CENTER; var settings = Gtk.Settings.get_default(); darkmode_switch.notify["active"].connect(() => { settings.gtk_application_prefer_dark_theme = darkmode_switch.active; dark_mode = darkmode_switch.active; if (!is_initializing) theme_save(); canvas.grab_focus(); }); if (supports_dark_theme()) header.pack_end(darkmode_switch); var fonts = new Gtk.MenuButton(); fonts.tooltip_text = _("Change document font"); fonts.image = new Gtk.Image.from_icon_name("font-x-generic", Gtk.IconSize.LARGE_TOOLBAR); fonts.popup = new Gtk.Menu(); header.pack_start(fonts); font_serif_option = build_fontoption(fonts.popup, _("Serif"), "serif", font); font_sans_option = build_fontoption(fonts.popup, _("Sans-serif"), "sans", "'Open Sans', 'Segoe UI', Tahoma, Arial, sans-serif"); font_wrap_option = build_fontoption(fonts.popup, _("Monospace"), "wrap", "Hack, consolas," + "Menlo-Regular, Menlo, Monaco, 'ubuntu mono', monospace"); fonts.popup.show_all(); } private unowned SList? font_options = null; private Gtk.RadioMenuItem build_fontoption(Gtk.Menu menu, string label, string fontstyle, string families) { var option = new Gtk.RadioMenuItem.with_label(font_options, label); font_options = option.get_group(); option.activate.connect(() => { this.font = families; this.fontstyle = fontstyle; adjust_text_style(!is_initializing); canvas.grab_focus(); }); var styles = option.get_style_context(); var provider = new Gtk.CssProvider(); try { provider.load_from_data("* {font-family: %s;}".printf(families)); styles.add_provider(provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION); } catch (Error e) { warning(e.message); } menu.add(option); return option; } public override void grab_focus() { canvas.grab_focus(); } private KeyFile theme = new KeyFile(); private void restore_styles() { try { loaded_theme = true; theme.load_from_file(get_data_dir() + "/prefs.ini", KeyFileFlags.NONE); dark_mode = theme.get_boolean("Theme", "darkmode"); darkmode_switch.active = dark_mode; Gtk.Settings.get_default().gtk_application_prefer_dark_theme = dark_mode; font_size = theme.get_integer("Theme", "fontsize"); font = theme.get_string("Post", "font"); fontstyle = theme.get_string("Post", "fontstyle"); // Select the current font in the menu if (fontstyle == "serif") { font_serif_option.set_active(true); } else if (fontstyle == "sans") { font_sans_option.set_active(true); } else if (fontstyle == "wrap") { font_wrap_option.set_active(true); } adjust_text_style(false); } catch (Error err) {/* No biggy... */} } private Gtk.CssProvider cur_styles = null; // So the theme isn't read before it's saved. private bool loaded_theme = false; private void adjust_text_style(bool save_theme = true) { try { if (cur_styles != null) Gtk.StyleContext.remove_provider_for_screen(Gdk.Screen.get_default(), cur_styles); var padding = canvas.get_allocated_width()*0.10; var css = ("textview {font-family: %s; font-size: %dpx; padding: 20px 0;" + " caret-color: #5ac4ee;}").printf(font, font_size); cur_styles = new Gtk.CssProvider(); cur_styles.load_from_data(css); Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), cur_styles, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION); canvas.left_margin = canvas.right_margin = (int) padding; if (save_theme) theme_save(); } catch (Error e) { warning(e.message); } } private void theme_save() { if (!loaded_theme) return; theme.set_boolean("Theme", "darkmode", dark_mode); theme.set_integer("Theme", "fontsize", font_size); theme.set_string("Post", "font", font); theme.set_string("Post", "fontstyle", fontstyle); try { theme.save_to_file(get_data_dir() + "/prefs.ini"); } catch (FileError err) {/* Oh well. */} } private string publish() { try { if (text_changed) {; draft_file().replace_contents(canvas.buffer.text.data, null, false, FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION, null); text_changed = false; } var cmd = "sh -c 'cat ~/" + data_dir + "/draft.txt | writeas --md --font %s --user-agent \"writeas-gtk v" + version + "\"'"; cmd = cmd.printf(fontstyle); string stdout, stderr; int status; Process.spawn_command_line_sync(cmd, out stdout, out stderr, out status); // Open it in the browser if (status == 0) { var browser = AppInfo.get_default_for_uri_scheme("https"); var urls = new List(); urls.append(stdout.strip()); browser.launch_uris(urls, null); } return stderr.strip(); } catch (Error err) { return err.message; } } /* --- */ private void build_keyboard_shortcuts() { /* These operations are not exposed to the UI as buttons, as most people are very familiar with them and they are not the focus of this app. */ var accels = new Gtk.AccelGroup(); // App operations accels.connect(Gdk.Key.W, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => quit()); accels.connect(Gdk.Key.Q, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => quit()); // File operations accels.connect(Gdk.Key.S, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => save_as()); accels.connect(Gdk.Key.S, Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => save_as()); // Adjust text size accels.connect(Gdk.Key.minus, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => { if (font_size < 3) { return false; } if (font_size <= 10) { font_size -= 1; } else { font_size -= 2; } adjust_text_style(true); return true; }); accels.connect(Gdk.Key.equal, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => { if (font_size < 10) { font_size += 1; } else { font_size += 2; } adjust_text_style(true); return true; }); // Toggle theme with Ctrl+T accels.connect(Gdk.Key.T, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => { darkmode_switch.active = !darkmode_switch.active; return true; }); // Publish with Ctrl+Enter accels.connect(Gdk.Key.Return, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => { canvas.buffer.text += "\n\n" + publish(); return true; }); add_accel_group(accels); } private bool save_as() { try { var file = prompt_file(Gtk.FileChooserAction.SAVE, _("Save as")); file.replace_contents(canvas.buffer.text.data, null, false, FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION, null); } catch (Error e) { // It's fine... } return true; } private File prompt_file(Gtk.FileChooserAction mode, string action) throws UserCancellable { var file_chooser = new Gtk.FileChooserDialog(action, this, mode, _("Cancel"), Gtk.ResponseType.CANCEL, action, Gtk.ResponseType.ACCEPT); file_chooser.select_multiple = false; var filter = new Gtk.FileFilter(); filter.add_mime_type("text/plain"); file_chooser.set_filter(filter); var resp = file_chooser.run(); file_chooser.close(); if (resp == Gtk.ResponseType.ACCEPT) { return file_chooser.get_file(); } else { throw new UserCancellable.USER_CANCELLED("FileChooserDialog"); } } public void open_file(File file) throws Error { uint8[] text; file.load_contents(null, out text, null); canvas.buffer.text = (string) text; } private bool quit() { this.close(); return true; } } errordomain WriteAs.UserCancellable {USER_CANCELLED}