-
-
Save zeroeightysix/58ee1102aaa5831617bd5433c864ac41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cell::Ref; | |
use gtk::{Application, gio}; | |
use gtk::glib::BoxedAnyObject; | |
use gtk::prelude::*; | |
// Cargo.toml's dependencies: | |
// gtk = { version = "0.4.8", package = "gtk4" } | |
fn main() { | |
let application = Application::builder() | |
.application_id("org.gnome.test") | |
.build(); | |
application.connect_activate(build_ui); | |
application.run(); | |
} | |
struct Entry(&'static str, usize); | |
fn build_ui(app: &Application) { | |
let model = gio::ListStore::new(BoxedAnyObject::static_type()); | |
let v = vec![ | |
BoxedAnyObject::new(Entry("Row 1", 10)), | |
BoxedAnyObject::new(Entry("Row 2", 20)), | |
BoxedAnyObject::new(Entry("Row 3", 35)), | |
BoxedAnyObject::new(Entry("Row 4", 49)), | |
]; | |
model.extend_from_slice(&v); | |
let model = gtk::SingleSelection::new(Some(&model)); | |
let columnview = gtk::ColumnView::builder() | |
.model(&model) | |
.vexpand(true) | |
.hexpand(true) | |
.margin_bottom(12) | |
.margin_end(12) | |
.margin_start(12) | |
.margin_top(12) | |
.show_column_separators(true) | |
.build(); | |
{ | |
let factory = gtk::SignalListItemFactory::new(); | |
factory.connect_setup(move |_, li| { | |
let label = gtk::Label::new(None); | |
li.set_child(Some(&label)); | |
}); | |
factory.connect_bind(move |_, li| { | |
let obj = li.item().unwrap().downcast::<BoxedAnyObject>().unwrap(); | |
let entry: Ref<Entry> = obj.borrow(); | |
let label = li.child().unwrap().downcast::<gtk::Label>().unwrap(); | |
label.set_label(entry.0); | |
}); | |
let col = gtk::ColumnViewColumn::new(Some("Title"), Some(&factory)); | |
col.set_expand(true); | |
columnview.append_column(&col); | |
} | |
{ | |
let factory = gtk::SignalListItemFactory::new(); | |
factory.connect_setup(move |_, li| { | |
let label = gtk::Label::new(None); | |
li.set_child(Some(&label)); | |
}); | |
factory.connect_bind(move |_, li| { | |
let obj = li.item().unwrap().downcast::<BoxedAnyObject>().unwrap(); | |
let entry: Ref<Entry> = obj.borrow(); | |
let label = li.child().unwrap().downcast::<gtk::Label>().unwrap(); | |
label.set_label(format!("{}", entry.1).as_str()); | |
}); | |
let col = gtk::ColumnViewColumn::new(Some("Number"), Some(&factory)); | |
columnview.append_column(&col); | |
} | |
let window = gtk::ApplicationWindow::builder() | |
.application(app) | |
.child(&columnview) | |
.build(); | |
window.show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment