|
| 1 | +import gi |
| 2 | + |
| 3 | +gi.require_version("Gtk", "4.0") |
| 4 | +gi.require_version("Adw", "1") |
| 5 | +from gi.repository import Gtk |
| 6 | +import workbench |
| 7 | + |
| 8 | +list_view = workbench.builder.get_object("list_view") |
| 9 | +item_factory = workbench.builder.get_object("item_factory") |
| 10 | +header_factory = workbench.builder.get_object("header_factory") |
| 11 | + |
| 12 | + |
| 13 | +class CustomModel(Gtk.StringList, Gtk.SectionModel): |
| 14 | + def __init__(self): |
| 15 | + super().__init__() |
| 16 | + |
| 17 | + def do_get_section(self, position): |
| 18 | + start = (position // 5) * 5 |
| 19 | + end = start + 5 |
| 20 | + return (start, end) |
| 21 | + |
| 22 | + |
| 23 | +def on_setup_item(_, list_item): |
| 24 | + list_item.set_child(Gtk.Label(margin_start=12, xalign=0)) |
| 25 | + |
| 26 | + |
| 27 | +def on_bind_item(_, list_item): |
| 28 | + item = list_item.get_item() |
| 29 | + label = list_item.get_child() |
| 30 | + |
| 31 | + label.set_label(item.get_string()) |
| 32 | + |
| 33 | + |
| 34 | +def on_setup_header(_, list_item): |
| 35 | + list_item.set_child(Gtk.Label(label="Header", xalign=0)) |
| 36 | + |
| 37 | + |
| 38 | +custom_model = CustomModel() |
| 39 | + |
| 40 | +for i in range(0, 200): |
| 41 | + custom_model.append(f"Item {i}") |
| 42 | + |
| 43 | +item_factory.connect("setup", on_setup_item) |
| 44 | +item_factory.connect("bind", on_bind_item) |
| 45 | + |
| 46 | +header_factory.connect("setup", on_setup_header) |
| 47 | + |
| 48 | +selection_model = Gtk.NoSelection(model=custom_model) |
| 49 | + |
| 50 | +list_view.set_model(selection_model) |
0 commit comments