I wanted to have a simple list view with a possibility of expanding some of them, but I got stuck on implementing it. I know that there is an example in gtk4 with setting how to use it properly, but for some reason I can’t wrap my head around it.
password_entry.blp
template GtkListItem {
child: TreeExpander expander {
child: Label {
label: bind (GtkListItem.item) as ($PasswordEntry).name;
};
};
}
...
ScrolledWindow {
vexpand: true;
ListView passwords_list {
styles ["navigation-sidebar"]
factory: BuilderListItemFactory {
resource: "...password_entry.ui";
};
}
}
...
class PasswordEntry : GLib.Object {
public string name {set;get;default = "NAME PLACEHOLDER";}
public GLib.ListStore children_model {set;get;default = null;}
}
namespace ... {
public GLib.ListStore get_example_passwords () {
var list_model = new GLib.ListStore(typeof (PasswordEntry));
var x = new PasswordEntry () {};
x.children_model = new GLib.ListStore(typeof (PasswordEntry));
x.children_model.append ( new PasswordEntry () {} );
x.name = "X Password";
list_model.append ( x );
list_model.append ( new PasswordEntry () {} );
list_model.append ( new PasswordEntry () {} );
return list_model;
}
[GtkTemplate (ui = ".../window.ui")]
public class Window : Adw.ApplicationWindow {
[GtkChild] private unowned Gtk.ListView passwords_list;
public Window (Gtk.Application app) {
Object (application: app);
var list_model = get_example_passwords ();
var tree_model = new Gtk.TreeListModel(
list_model, true, false, (entry) => {
return ((PasswordEntry) entry).children_model;
}
);
passwords_list.set_model (new Gtk.NoSelection (tree_model));
}
}
}
If I run it, I will only get three elements, without any possibility of expanding the firs one.