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

Sync code snippet with selected icon #29

Closed
wants to merge 11 commits into from
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ You'll need the following dependencies to build:
* libgranite-7-dev
* libgtk-4-dev
* libgtksourceview-5-dev
* libadwaita-1-dev
* meson
* valac

Expand Down
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ executable(
dependency('gobject-2.0'),
dependency('granite-7'),
dependency('gtk4'),
dependency('gtksourceview-5')
dependency('gtksourceview-5'),
dependency('libadwaita-1'),
],
install : true
)
Expand Down
68 changes: 61 additions & 7 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 @@ -141,14 +146,22 @@ 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 (() => {
var name = selected_icon.full_icon_name;
var size = selected_icon.size_in_px;
source_buffer.text = """var icon = new Gtk.Image.from_icon_name ("%s") {
pixel_size = %d
};""".printf (name, size);
});
}

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 +208,51 @@ 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 () {
valign = Gtk.Align.CENTER,
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);
}
});
var icon_clamp = new Adw.Clamp () {
child = icon_btn,
maximum_size = 128
};
row.attach (icon_clamp, 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 Adw.Clamp;
var row = has_color_icons ? color_row : icon_row;
var clamp = (Adw.Clamp) (row.get_child_at (1, 0) ?? row.get_first_child ());
((Gtk.ToggleButton) clamp.child).active = true;
}
}

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
);
}
}