Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy code for code snippet #31

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*~
build/*
.flatpak-builder/*
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ executable(
gresource,
'src/Application.vala',
'src/CategoryView.vala',
'src/IconDetails.vala',
'src/IconView.vala',
'src/MainWindow.vala',
'src/Widgets/IconListRow.vala',
Expand Down
9 changes: 9 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public class IconBrowser.App : Gtk.Application {

quit_action.activate.connect (quit);

var copy_action = new SimpleAction ("copy", null);

add_action (copy_action);
set_accels_for_action ("app.copy", {"<Control>c"});

copy_action.activate.connect (() => {
((MainWindow) active_window).copy_code_snippet ();
});

var provider = new Gtk.CssProvider ();
provider.load_from_resource ("/io/elementary/iconbrowser/Application.css");
Gtk.StyleContext.add_provider_for_display (Gdk.Display.get_default (), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
Expand Down
5 changes: 4 additions & 1 deletion src/CategoryView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class CategoryView : Gtk.Box {

public Gtk.ListBox listbox { get; private set; }
public Gtk.SearchEntry search_entry { get; private set; }
public IconDetails selected_icon { get {return icon_view.selected_icon; } }

private IconView icon_view;

public enum Category {
ACTIONS,
Expand Down Expand Up @@ -2375,7 +2378,7 @@ public class CategoryView : Gtk.Box {
};

construct {
var icon_view = new IconView ();
icon_view = new IconView ();

search_entry = new Gtk.SearchEntry () {
hexpand = true,
Expand Down
21 changes: 21 additions & 0 deletions src/IconDetails.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2017-2022 elementary, Inc. (https://elementary.io)
*/
public class IconDetails : Object {
public string full_icon_name { get; construct; }
public int size_in_px { get; construct; }

public IconDetails (string name, int size) {
Object (
full_icon_name: name,
size_in_px: size
);
}

public string code_snippet () {
return """var icon = new Gtk.Image.from_icon_name ("%s") {
pixel_size = %d
};""".printf (full_icon_name, size_in_px);
}
}
74 changes: 66 additions & 8 deletions src/IconView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ public class IconView : Gtk.Box {
public string icon_name { get; construct set; }
public string description { get; construct set; }
public CategoryView.Category category { get; construct set; }
public IconDetails selected_icon { get; construct set; }

public IconView () {
Object (
icon_name: "address-book-new",
description: _("Create a new address book"),
category: CategoryView.Category.ACTIONS
category: CategoryView.Category.ACTIONS,
selected_icon: new IconDetails (
"address-book-new",
24
)
);
}

Expand Down Expand Up @@ -101,6 +106,30 @@ public class IconView : Gtk.Box {
source_view.add_css_class (Granite.STYLE_CLASS_CARD);
source_view.add_css_class (Granite.STYLE_CLASS_ROUNDED);

var source_overlay = new Gtk.Overlay () {
child = source_view
};

var copy_button = new Gtk.Button.from_icon_name ("edit-copy") {
tooltip_markup = Granite.markup_accel_tooltip ({"<Ctrl>C"}, _("Copy")),
action_name = "app.copy",
valign = Gtk.Align.START,
halign = Gtk.Align.END,
visible = false,
margin_top = 8,
margin_end = 8
};
source_overlay.add_overlay (copy_button);

var enter_leave_ctrl = new Gtk.EventControllerMotion ();
enter_leave_ctrl.enter.connect (() => {
copy_button.show ();
});
enter_leave_ctrl.leave.connect (() => {
copy_button.hide ();
});
source_overlay.add_controller (enter_leave_ctrl);

var content_area = new Gtk.Box (Gtk.Orientation.VERTICAL, 12) {
vexpand = true
};
Expand All @@ -110,7 +139,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 @@ -141,14 +170,18 @@ public class IconView : Gtk.Box {
bind_property ("description", description_label, "label");

notify["icon-name"].connect (() => {
source_buffer.text = "var icon = new Gtk.Image.from_icon_name (\"%s\") {\n pixel_size = 24\n};".printf (icon_name);
Gtk.ToggleButton btn_group = null;
fill_icon_row (icon_name, color_row, ref btn_group);
fill_icon_row (icon_name + "-symbolic", symbolic_row, ref btn_group);
select_one_icon (color_row, symbolic_row);
});

fill_icon_row (icon_name, color_row);
fill_icon_row (icon_name + "-symbolic", symbolic_row);
notify["selected-icon"].connect (() => {
source_buffer.text = selected_icon.code_snippet ();
});
}

private void fill_icon_row (string _icon_name, Gtk.Grid row) {
private void fill_icon_row (string _icon_name, Gtk.Grid row, ref Gtk.ToggleButton? icon_btn) {
while (row.get_first_child () != null) {
row.remove (row.get_first_child ());
}
Expand Down Expand Up @@ -195,10 +228,35 @@ public class IconView : Gtk.Box {
hexpand = true
};

row.attach (icon, i, 0);
row.attach (label, i, 1);
var icon_and_label = new Gtk.Grid () {
row_spacing = 12,
valign = Gtk.Align.CENTER,
margin_top = 4,
margin_bottom = 4
};
icon_and_label.attach (icon, 0, 0);
icon_and_label.attach (label, 0, 1);
icon_btn = new Gtk.ToggleButton () {
child = icon_and_label,
has_frame = false,
group = icon_btn
};
icon_btn.toggled.connect ((target) => {
if (target.active) {
selected_icon = new IconDetails (icon.icon_name, icon.pixel_size);
}
});

row.attach (icon_btn, i, 0);

i++;
}
}

private void select_one_icon (Gtk.Grid color_row, Gtk.Grid icon_row) {
var has_color_icons = color_row.get_first_child () is Gtk.ToggleButton;
var row = has_color_icons ? color_row : icon_row;
var btn = row.get_child_at (1, 0) ?? row.get_first_child ();
((Gtk.ToggleButton) btn).active = true;
}
}
7 changes: 7 additions & 0 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ public class IconBrowser.MainWindow : Gtk.ApplicationWindow {
});
}

public void copy_code_snippet () {
var snippet = category_view.selected_icon.code_snippet ();
var display = Gdk.Display.get_default ();
var clipboard = display.get_clipboard ();
clipboard.set_text (snippet);
}

[CCode (instance_pos = -1)]
private bool filter_function (Gtk.ListBoxRow row) {
if (category_view.search_entry.text == "") {
Expand Down