diff --git a/data/Application.css b/data/Application.css index 320ad23..01d1021 100644 --- a/data/Application.css +++ b/data/Application.css @@ -40,3 +40,9 @@ .content-area { padding: 1.33rem; } + +.copy-button { + background: none; + border: 1px solid @borders; + box-shadow: none; +} diff --git a/meson.build b/meson.build index 2b3eb14..74ed227 100644 --- a/meson.build +++ b/meson.build @@ -36,7 +36,7 @@ executable( 'src/Widgets/IconListRow.vala', 'src/Widgets/SidebarRow.vala', dependencies: [ - dependency('glib-2.0'), + dependency('glib-2.0', version: '>= 2.78'), dependency('gobject-2.0'), dependency('granite-7'), dependency('gtk4'), diff --git a/src/IconView.vala b/src/IconView.vala index 6578964..e6edd14 100644 --- a/src/IconView.vala +++ b/src/IconView.vala @@ -8,6 +8,7 @@ public class IconView : Gtk.Box { public CategoryView.Category category { get; construct set; } private GtkSource.Buffer source_buffer; + private Gtk.Button copy_button; public IconView () { Object ( @@ -103,6 +104,24 @@ public class IconView : Gtk.Box { source_view.add_css_class (Granite.STYLE_CLASS_CARD); source_view.add_css_class (Granite.STYLE_CLASS_ROUNDED); + copy_button = new Gtk.Button.with_label (_("Copy")) { + valign = START, + halign = END, + visible = false, + margin_top = 8, + margin_end = 8 + }; + copy_button.add_css_class (Granite.STYLE_CLASS_SMALL_LABEL); + copy_button.add_css_class ("copy-button"); + + var copy_visible_controller = new Gtk.EventControllerMotion (); + + var source_overlay = new Gtk.Overlay () { + child = source_view + }; + source_overlay.add_overlay (copy_button); + source_overlay.add_controller (copy_visible_controller); + var content_area = new Gtk.Box (Gtk.Orientation.VERTICAL, 12) { vexpand = true }; @@ -112,7 +131,7 @@ public class IconView : Gtk.Box { content_area.append (symbolic_title); content_area.append (symbolic_row); content_area.append (snippet_title); - content_area.append (source_view); + content_area.append (source_overlay); var scrolled = new Gtk.ScrolledWindow () { child = content_area, @@ -163,6 +182,16 @@ public class IconView : Gtk.Box { color_row.unselect_all (); child_activated (child); }); + + copy_button.clicked.connect (copy_button_clicked); + + copy_visible_controller.enter.connect (() => { + copy_button.visible = true; + }); + + copy_visible_controller.leave.connect (() => { + copy_button.visible = false; + }); } private void child_activated (Gtk.FlowBoxChild child) { @@ -179,6 +208,16 @@ public class IconView : Gtk.Box { } } + private void copy_button_clicked () { + unowned var clipboard = get_clipboard (); + clipboard.set_text (source_buffer.text); + + copy_button.label = _("Copied!"); + Timeout.add_once (1000, () => { + copy_button.label = _("Copy"); + }); + } + private void fill_icon_row (string _icon_name, Gtk.FlowBox row) { while (row.get_first_child () != null) { row.remove (row.get_first_child ());