diff --git a/src/HistoryWidget.vala b/src/HistoryWidget.vala index 448d331..b6a2bdc 100644 --- a/src/HistoryWidget.vala +++ b/src/HistoryWidget.vala @@ -10,6 +10,7 @@ public class Clipboard.HistoryWidget : Gtk.Box { private uint wait_timeout = 0; public signal void close_request (); + public signal void changed (); construct { clipboard_text_set = new Gee.HashSet (); @@ -52,6 +53,7 @@ public class Clipboard.HistoryWidget : Gtk.Box { clipboard_item_list.prepend (new_item); clipboard_item_list.select_row (new_item); clipboard_item_list.show_all (); + changed (); } }); } @@ -97,8 +99,8 @@ public class Clipboard.HistoryWidget : Gtk.Box { add (label); } - - private string prettify (string text) { + + private string prettify (string text) { var prettier = new StringBuilder (text); var ellipsis = "…"; var double_ellipsis = ellipsis + ellipsis; @@ -111,4 +113,18 @@ public class Clipboard.HistoryWidget : Gtk.Box { return prettier.str; } } + + public uint get_n_items () { + return clipboard_text_set.size; + } + + public void clear_history () { + clipboard_text_set.clear (); + clipboard_item_list.@foreach ((child) => { + child.destroy (); + }); + + changed (); + + } } diff --git a/src/Indicator.vala b/src/Indicator.vala index 3824488..5110ee2 100644 --- a/src/Indicator.vala +++ b/src/Indicator.vala @@ -5,7 +5,7 @@ public class Clipboard.Indicator : Wingpanel.Indicator { private static GLib.Settings settings; - private Gtk.Image panel_icon; + private Gtk.Widget display_widget; private HistoryWidget history_widget; public Wingpanel.IndicatorManager.ServerType server_type { get; construct set; } @@ -26,20 +26,43 @@ public class Clipboard.Indicator : Wingpanel.Indicator { } public override Gtk.Widget get_display_widget () { - if (panel_icon == null) { - panel_icon = new Gtk.Image.from_icon_name ("edit-copy-symbolic", Gtk.IconSize.SMALL_TOOLBAR); + if (display_widget == null) { + var panel_icon = new Gtk.Image.from_icon_name ( + "edit-copy-symbolic", + Gtk.IconSize.SMALL_TOOLBAR + ); + + display_widget = new Gtk.EventBox () { + child = panel_icon + }; + + display_widget.events = BUTTON_PRESS_MASK; + if (server_type == Wingpanel.IndicatorManager.ServerType.GREETER) { this.visible = false; } else { - // var visible_settings = new Settings ("io.elementary.desktop.wingpanel.clipboard"); - // visible_settings.bind ("show-indicator", this, "visible", SettingsBindFlags.DEFAULT); this.visible = true; } + + get_widget (); // Initialize history widget + history_widget.changed.connect (update_tooltip); + + // EventController does not work? + display_widget.button_press_event.connect ((event) => { + if (event.button == 3) { + history_widget.clear_history (); + return true; + } + + return false; + }); + + display_widget.show_all (); + update_tooltip (); } - get_widget (); - return panel_icon; + return display_widget; } public override Gtk.Widget? get_widget () { @@ -60,6 +83,28 @@ public class Clipboard.Indicator : Wingpanel.Indicator { public override void closed () { } + + private void update_tooltip () { + uint n_items = history_widget.get_n_items (); + string description; + if (n_items > 0) { + description = ngettext ( + _("Clipboard Manager: %u item"), + _("Clipboard Manager: %u items"), + n_items + ).printf (n_items); + } else { + description = _("Clipboard Manager: Empty"); + } + + if (n_items > 0) { + string accel_label = n_items > 0 ? _("Middle-click to clear") : ""; + accel_label = Granite.TOOLTIP_SECONDARY_TEXT_MARKUP.printf (accel_label); + display_widget.tooltip_markup = "%s\n%s".printf (description, accel_label); + } else { + display_widget.tooltip_markup = "%s".printf (description); + } + } } public Wingpanel.Indicator? get_indicator (Module module, Wingpanel.IndicatorManager.ServerType server_type) {