Skip to content

Commit

Permalink
Add button to copy code snippet
Browse files Browse the repository at this point in the history
Co-authored-by: Antolius <[email protected]>
  • Loading branch information
ryonakano and Antolius committed Aug 14, 2024
1 parent 2bd4328 commit d251b65
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions data/Application.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@
.content-area {
padding: 1.33rem;
}

.copy-button {
background: none;
border: 1px solid @borders;
box-shadow: none;
}
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
41 changes: 40 additions & 1 deletion src/IconView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
};
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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 ());
Expand Down

0 comments on commit d251b65

Please sign in to comment.