|
| 1 | +import importlib.resources |
| 2 | + |
| 3 | +from gi.repository import Gio, GLib, GObject, Gtk |
| 4 | + |
| 5 | + |
| 6 | +def new_list_item_ui(): |
| 7 | + with importlib.resources.path("gtk4_list_view", "simple_tree.ui") as ui_path: |
| 8 | + return GLib.Bytes.new(ui_path.read_bytes()) |
| 9 | + |
| 10 | + |
| 11 | +class Demo(GObject.GObject): |
| 12 | + def __init__(self, name): |
| 13 | + super().__init__() |
| 14 | + self._name = name |
| 15 | + |
| 16 | + @GObject.Property(type=str) |
| 17 | + def name(self): |
| 18 | + return self._name |
| 19 | + |
| 20 | + |
| 21 | +if __name__ == "__main__": |
| 22 | + |
| 23 | + model = Gio.ListStore.new(Demo) |
| 24 | + model.append(Demo("one")) |
| 25 | + model.append(Demo("two")) |
| 26 | + model.append(Demo("three")) |
| 27 | + |
| 28 | + app = Gtk.Application.new("gtk4.listview.SimpleTree", 0) |
| 29 | + |
| 30 | + def activate(app): |
| 31 | + w = Gtk.Window() |
| 32 | + w.set_title("Simple tree") |
| 33 | + w.set_default_size(400, 400) |
| 34 | + app.add_window(w) |
| 35 | + |
| 36 | + def child_model(item, user_data): |
| 37 | + return model |
| 38 | + |
| 39 | + # Our data objects are wrapped by a Gtk.TreeListRow |
| 40 | + tree = Gtk.TreeListModel.new( |
| 41 | + model, |
| 42 | + passthrough=False, |
| 43 | + autoexpand=False, |
| 44 | + create_func=child_model, |
| 45 | + user_data=None, |
| 46 | + ) |
| 47 | + |
| 48 | + selection = Gtk.SingleSelection.new(tree) |
| 49 | + factory = Gtk.BuilderListItemFactory.new_from_bytes(None, new_list_item_ui()) |
| 50 | + |
| 51 | + lv = Gtk.ListView.new(selection, factory) |
| 52 | + w.set_child(lv) |
| 53 | + w.show() |
| 54 | + |
| 55 | + app.connect("activate", activate) |
| 56 | + app.run() |
0 commit comments