From 8737c39a703a54ebd45d2f4cd52d5d6cc2706b16 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Fri, 14 Jan 2022 11:10:11 -0500 Subject: [PATCH] fix method of getting data from list model object --- examples/launcher/search_result_object/mod.rs | 14 +++- examples/launcher/window/mod.rs | 65 +++++++++---------- 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/examples/launcher/search_result_object/mod.rs b/examples/launcher/search_result_object/mod.rs index f1457dbe..0d2266b9 100644 --- a/examples/launcher/search_result_object/mod.rs +++ b/examples/launcher/search_result_object/mod.rs @@ -1,6 +1,7 @@ -use gtk4::glib; - use crate::utils::BoxedSearchResult; +use gtk4::glib; +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; mod imp; @@ -12,4 +13,13 @@ impl SearchResultObject { pub fn new(search_result: &BoxedSearchResult) -> Self { glib::Object::new(&[("data", search_result)]).expect("Failed to create Application Object") } + + pub fn data(&self) -> Option { + if let Ok(data) = self.property("data") { + if let Ok(search_result) = data.get::() { + return search_result.0; + } + } + None + } } diff --git a/examples/launcher/window/mod.rs b/examples/launcher/window/mod.rs index 2bcc209d..08e3f1a5 100644 --- a/examples/launcher/window/mod.rs +++ b/examples/launcher/window/mod.rs @@ -18,6 +18,7 @@ use x11rb::protocol::xproto::ConnectionExt; use libcosmic::x; use crate::search_result_row::SearchResultRow; +use crate::utils::BoxedSearchResult; use crate::SearchResultObject; use crate::TX; use crate::X11_CONN; @@ -62,6 +63,7 @@ impl Window { let list_view = cascade! { ListView::default(); ..set_orientation(Orientation::Vertical); + ..set_single_click_activate(true); }; container.append(&list_view); @@ -87,12 +89,7 @@ impl Window { let model = gio::ListStore::new(SearchResultObject::static_type()); let slice_model = gtk4::SliceListModel::new(Some(&model), 0, NUM_LAUNCHER_ITEMS.into()); - let selection_model = gtk4::SingleSelection::builder() - .model(&slice_model) - .autoselect(false) - .can_unselect(true) - .selected(gtk4::INVALID_LIST_POSITION) - .build(); + let selection_model = gtk4::NoSelection::new(Some(&slice_model)); imp.model.set(model).expect("Could not set model"); // Wrap model with selection and pass it to the list view @@ -113,48 +110,50 @@ impl Window { let action_launchi = gio::SimpleAction::new(&format!("launch{}", i), None); self.add_action(&action_launchi); action_launchi.connect_activate(glib::clone!(@weak lv => move |_action, _parameter| { - println!("acitvating... {}", i); + let i = i - 1; + println!("activating... {}", i); let model = lv.model().unwrap(); - let app_info = model.item(i - 1); - if app_info.is_none() { - println!("oops no app for this row..."); - return; - } - if let Ok(id)= app_info.unwrap().property("id") { - let id = id.get::().expect("App ID must be u32"); - + let obj = match model.item(i) { + Some(obj) => obj.downcast::().unwrap(), + None => { + dbg!(model.item(i)); + return; + }, + }; + if let Some(search_result) = obj.data() { + println!("activating... {}", i + 1); glib::MainContext::default().spawn_local(async move { if let Some(tx) = TX.get() { - let _ = tx.send(crate::Event::Activate(id)).await; + let _ = tx.send(crate::Event::Activate(search_result.id)).await; } }); } })); } - let app_selection_model = lv - .model() - .expect("List view missing selection model") - .downcast::() - .expect("could not downcast listview model to single selection model"); + lv.connect_activate(glib::clone!(@weak window => move |list_view, i| { + dbg!(i); + let model = list_view.model() + .expect("List view missing selection model") + .downcast::() + .expect("could not downcast listview model to no selection model"); - app_selection_model.connect_selected_notify(glib::clone!(@weak window => move |model| { - let i = model.selected(); if i >= model.n_items() { + dbg!("index out of range"); return; } - println!("acitvating... {}", i + 1); - let app_info = model.item(i); - if app_info.is_none() { - println!("oops no app for this row..."); - return; - } - if let Ok(id) = app_info.unwrap().property("id") { - let id = id.get::().expect("App ID must be u32"); - + let obj = match model.item(i) { + Some(obj) => obj.downcast::().unwrap(), + None => { + dbg!(model.item(i)); + return; + }, + }; + if let Some(search_result) = obj.data() { + println!("activating... {}", i + 1); glib::MainContext::default().spawn_local(async move { if let Some(tx) = TX.get() { - let _ = tx.send(crate::Event::Activate(id)).await; + let _ = tx.send(crate::Event::Activate(search_result.id)).await; } }); }