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

Added list view section demo #206

Merged
merged 2 commits into from
Oct 8, 2024
Merged
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
42 changes: 42 additions & 0 deletions src/List View with Sections/main.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Gtk 4.0;
using Adw 1;

Adw.StatusPage {
title: _("List View with Sections");
description: _("Divide items in a list view with sections");
valign: start;

Adw.Clamp {
maximum-size: 360;

Box {
orientation: vertical;
spacing: 18;

Box {
halign: center;

LinkButton {
label: _("API Reference");
uri: "https://docs.gtk.org/gtk4/iface.SectionModel.html";
}

LinkButton {
label: _("Documentation");
uri: "https://docs.gtk.org/gtk4/section-list-widget.html#sections";
}
}

ScrolledWindow {
height-request: 330;
has-frame: true;

child: ListView list_view {
factory: SignalListItemFactory item_factory {};

header-factory: SignalListItemFactory header_factory {};
};
}
}
}
}
6 changes: 6 additions & 0 deletions src/List View with Sections/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "layout",
"description": "Divide items in a list view with sections",
"panels": ["ui", "preview"],
"autorun": true
}
50 changes: 50 additions & 0 deletions src/List View with Sections/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import gi

gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Gtk
import workbench

list_view = workbench.builder.get_object("list_view")
item_factory = workbench.builder.get_object("item_factory")
header_factory = workbench.builder.get_object("header_factory")


class CustomModel(Gtk.StringList, Gtk.SectionModel):
def __init__(self):
super().__init__()

def do_get_section(self, position):
start = (position // 5) * 5
end = start + 5
return (start, end)


def on_setup_item(_, list_item):
list_item.set_child(Gtk.Label(margin_start=12, xalign=0))


def on_bind_item(_, list_item):
item = list_item.get_item()
label = list_item.get_child()

label.set_label(item.get_string())


def on_setup_header(_, list_item):
list_item.set_child(Gtk.Label(label="Header", xalign=0))


custom_model = CustomModel()

for i in range(0, 200):
custom_model.append(f"Item {i}")

item_factory.connect("setup", on_setup_item)
item_factory.connect("bind", on_bind_item)

header_factory.connect("setup", on_setup_header)

selection_model = Gtk.NoSelection(model=custom_model)

list_view.set_model(selection_model)
Loading