Skip to content

Commit 61f6573

Browse files
authored
library: Port Column View demo to Vala (#789)
* library: Port Column View demo to Vala * library: Use array instead of `.splice` in Column View, Vala
1 parent b0efbc7 commit 61f6573

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env -S vala workbench.vala --pkg libadwaita-1
2+
3+
// Define our class for our custom model
4+
public class Book : Object {
5+
public string title { get; set; }
6+
public string author { get; set; }
7+
public int year { get; set; }
8+
9+
public Book (string title, string author, int year) {
10+
Object (
11+
title: title,
12+
author: author,
13+
year: year
14+
);
15+
}
16+
}
17+
18+
public void main () {
19+
var column_view = (Gtk.ColumnView) workbench.builder.get_object ("column_view");
20+
var col1 = (Gtk.ColumnViewColumn) workbench.builder.get_object ("col1");
21+
var col2 = (Gtk.ColumnViewColumn) workbench.builder.get_object ("col2");
22+
var col3 = (Gtk.ColumnViewColumn) workbench.builder.get_object ("col3");
23+
24+
Book[] books = {
25+
new Book ("Winds from Afar", "Kenji Miyazawa", 1972),
26+
new Book ("Like Water for Chocolate", "Laura Esquivel", 1989),
27+
new Book ("Works and Nights", "Alejandra Pizarnik", 1965),
28+
new Book ("Understanding Analysis", "Stephen Abbott", 2002),
29+
new Book ("The Timeless Way of Building", "Cristopher Alexander", 1979),
30+
new Book ("Bitter", "Akwaeke Emezi", 2022),
31+
new Book ("Saying Yes", "Griselda Gambaro", 1981),
32+
new Book ("Itinerary of a Dramatist", "Rodolfo Usigli", 1940),
33+
};
34+
35+
// Create the data model
36+
var data_model = new ListStore (typeof (Book));
37+
foreach (Book book in books) {
38+
data_model.append (book);
39+
}
40+
41+
col1.sorter = new Gtk.StringSorter (
42+
new Gtk.PropertyExpression (typeof (Book), null, "title")
43+
);
44+
col2.sorter = new Gtk.StringSorter (
45+
new Gtk.PropertyExpression (typeof (Book), null, "author")
46+
);
47+
col3.sorter = new Gtk.NumericSorter (
48+
new Gtk.PropertyExpression (typeof (Book), null, "year")
49+
);
50+
51+
// View
52+
// Column 1
53+
var factory_col1 = (Gtk.SignalListItemFactory) col1.factory;
54+
factory_col1.setup.connect ((item) => {
55+
var list_item = (Gtk.ListItem) item;
56+
list_item.child = new Gtk.Label ("") {
57+
margin_top = 12,
58+
margin_bottom = 12,
59+
};
60+
});
61+
factory_col1.bind.connect ((item) => {
62+
var list_item = (Gtk.ListItem) item;
63+
var label = (Gtk.Label) list_item.child;
64+
var book = (Book) list_item.item;
65+
66+
label.label = book.title;
67+
});
68+
69+
// Column 2
70+
var factory_col2 = (Gtk.SignalListItemFactory) col2.factory;
71+
factory_col2.setup.connect ((item) => {
72+
var list_item = (Gtk.ListItem) item;
73+
list_item.child = new Gtk.Label ("") {
74+
margin_top = 12,
75+
margin_bottom = 12,
76+
};
77+
});
78+
factory_col2.bind.connect ((item) => {
79+
var list_item = (Gtk.ListItem) item;
80+
var label = (Gtk.Label) list_item.child;
81+
var book = (Book) list_item.item;
82+
83+
label.label = book.author;
84+
});
85+
86+
// Column 3
87+
var factory_col3 = (Gtk.SignalListItemFactory) col3.factory;
88+
factory_col3.setup.connect ((item) => {
89+
var list_item = (Gtk.ListItem) item;
90+
list_item.child = new Gtk.Label ("") {
91+
margin_top = 12,
92+
margin_bottom = 12,
93+
};
94+
});
95+
factory_col3.bind.connect ((item) => {
96+
var list_item = (Gtk.ListItem) item;
97+
var label = (Gtk.Label) list_item.child;
98+
var book = (Book) list_item.item;
99+
100+
label.label = book.year.to_string ();
101+
});
102+
103+
var sort_model = new Gtk.SortListModel (data_model, column_view.sorter);
104+
column_view.model = new Gtk.SingleSelection (sort_model);
105+
}

0 commit comments

Comments
 (0)