Skip to content

Commit fae40ed

Browse files
authored
Port missing List View demos to JavaScript (#212)
1 parent 9ad259c commit fae40ed

File tree

5 files changed

+147
-21
lines changed

5 files changed

+147
-21
lines changed

src/List View with Sections/main.blp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ Adw.StatusPage {
1313
orientation: vertical;
1414
spacing: 18;
1515

16+
ScrolledWindow {
17+
height-request: 330;
18+
has-frame: true;
19+
20+
child: ListView list_view {
21+
factory: SignalListItemFactory item_factory {};
22+
23+
header-factory: SignalListItemFactory header_factory {};
24+
};
25+
}
26+
1627
Box {
1728
halign: center;
1829

@@ -26,17 +37,6 @@ Adw.StatusPage {
2637
uri: "https://docs.gtk.org/gtk4/section-list-widget.html#sections";
2738
}
2839
}
29-
30-
ScrolledWindow {
31-
height-request: 330;
32-
has-frame: true;
33-
34-
child: ListView list_view {
35-
factory: SignalListItemFactory item_factory {};
36-
37-
header-factory: SignalListItemFactory header_factory {};
38-
};
39-
}
4040
}
4141
}
4242
}

src/List View with Sections/main.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Gtk from "gi://Gtk";
2+
import GObject from "gi://GObject";
3+
4+
const list_view = workbench.builder.get_object("list_view");
5+
const item_factory = workbench.builder.get_object("item_factory");
6+
const header_factory = workbench.builder.get_object("header_factory");
7+
8+
const CustomModel = GObject.registerClass(
9+
{
10+
Implements: [Gtk.SectionModel],
11+
},
12+
class CustomModel extends Gtk.StringList {
13+
vfunc_get_section(position) {
14+
const start = position;
15+
const end = start + 5;
16+
return [start, end];
17+
}
18+
},
19+
);
20+
21+
item_factory.connect("setup", (_self, list_item) => {
22+
list_item.set_child(
23+
new Gtk.Label({ margin_start: 12, halign: Gtk.Align.START }),
24+
);
25+
});
26+
27+
item_factory.connect("bind", (_self, list_item) => {
28+
const item = list_item.get_item();
29+
const label = list_item.get_child();
30+
31+
label.set_label(item.get_string());
32+
});
33+
34+
header_factory.connect("setup", (_self, list_item) => {
35+
list_item.set_child(
36+
new Gtk.Label({ label: "Header", halign: Gtk.Align.START }),
37+
);
38+
});
39+
40+
const custom_model = new CustomModel();
41+
42+
for (let i = 1; i <= 200; i++) {
43+
custom_model.append(`Item ${i}`);
44+
}
45+
46+
const selection_model = new Gtk.NoSelection({ model: custom_model });
47+
list_view.set_model(selection_model);

src/List View with Sections/main.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import gi
22

33
gi.require_version("Gtk", "4.0")
4-
gi.require_version("Adw", "1")
54
from gi.repository import Gtk
65
import workbench
76

@@ -11,17 +10,14 @@
1110

1211

1312
class CustomModel(Gtk.StringList, Gtk.SectionModel):
14-
def __init__(self):
15-
super().__init__()
16-
1713
def do_get_section(self, position):
18-
start = (position // 5) * 5
14+
start = position
1915
end = start + 5
2016
return (start, end)
2117

2218

2319
def on_setup_item(_, list_item):
24-
list_item.set_child(Gtk.Label(margin_start=12, xalign=0))
20+
list_item.set_child(Gtk.Label(margin_start=12, halign=Gtk.Align.START))
2521

2622

2723
def on_bind_item(_, list_item):
@@ -32,12 +28,12 @@ def on_bind_item(_, list_item):
3228

3329

3430
def on_setup_header(_, list_item):
35-
list_item.set_child(Gtk.Label(label="Header", xalign=0))
31+
list_item.set_child(Gtk.Label(label="Header", halign=Gtk.Align.START))
3632

3733

3834
custom_model = CustomModel()
3935

40-
for i in range(0, 200):
36+
for i in range(1, 201):
4137
custom_model.append(f"Item {i}")
4238

4339
item_factory.connect("setup", on_setup_item)

src/List View with a Tree/main.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import Gtk from "gi://Gtk";
2+
import GObject from "gi://GObject";
3+
import Gio from "gi://Gio";
4+
5+
const list_view = workbench.builder.get_object("list_view");
6+
const factory = workbench.builder.get_object("factory");
7+
8+
const TreeNode = GObject.registerClass(
9+
class TreeNode extends GObject.Object {
10+
constructor(title, children) {
11+
super();
12+
this.title = title;
13+
this.children = children;
14+
}
15+
},
16+
);
17+
18+
const TreeWidget = GObject.registerClass(
19+
class TreeWidget extends Gtk.Box {
20+
constructor(...args) {
21+
super(...args);
22+
this.spacing = 6;
23+
this.margin_start = 6;
24+
this.margin_end = 12;
25+
this.margin_top = 6;
26+
this.margin_bottom = 6;
27+
28+
this.expander = new Gtk.TreeExpander();
29+
this.label = new Gtk.Label({ halign: Gtk.Align.START });
30+
31+
this.append(this.expander);
32+
this.append(this.label);
33+
}
34+
},
35+
);
36+
37+
function create_model_func(item) {
38+
if (item.children.length < 1) return null;
39+
const child_model = new Gio.ListStore(TreeNode);
40+
for (const child of item.children) {
41+
child_model.append(child);
42+
}
43+
return child_model;
44+
}
45+
46+
factory.connect("setup", (_self, list_item) => {
47+
list_item.set_child(new TreeWidget());
48+
});
49+
50+
factory.connect("bind", (_self, list_item) => {
51+
const list_row = list_item.get_item();
52+
const widget = list_item.get_child();
53+
const item = list_row.get_item();
54+
55+
widget.expander.set_list_row(list_row);
56+
widget.label.set_label(item.title);
57+
});
58+
59+
const root_model = new TreeNode("Root", [
60+
new TreeNode("Child 1", [
61+
new TreeNode("Child 1.1", []),
62+
new TreeNode("Child 1.2", []),
63+
]),
64+
new TreeNode("Child 2", [
65+
new TreeNode("Child 2.1", []),
66+
new TreeNode("Child 2.2", []),
67+
new TreeNode("Child 2.3", [new TreeNode("Child 3.1", [])]),
68+
]),
69+
]);
70+
71+
const tree_model = new Gio.ListStore(TreeNode);
72+
tree_model.append(root_model);
73+
74+
const tree_list_model = Gtk.TreeListModel.new(
75+
tree_model,
76+
false,
77+
true,
78+
create_model_func,
79+
);
80+
tree_list_model.set_autoexpand(false);
81+
82+
const selection_model = new Gtk.NoSelection({ model: tree_list_model });
83+
84+
list_view.set_model(selection_model);

src/List View with a Tree/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import gi
22

33
gi.require_version("Gtk", "4.0")
4-
gi.require_version("Adw", "1")
54
from gi.repository import Gtk, GObject, Gio
65
import workbench
76

@@ -24,7 +23,7 @@ def __init__(self):
2423

2524
self.expander = Gtk.TreeExpander.new()
2625

27-
self.label = Gtk.Label(xalign=0, ellipsize=3)
26+
self.label = Gtk.Label(halign=Gtk.Align.START)
2827

2928
self.append(self.expander)
3029
self.append(self.label)

0 commit comments

Comments
 (0)