fix method of getting data from list model object
This commit is contained in:
parent
c876b46f44
commit
8737c39a70
2 changed files with 44 additions and 35 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
use gtk4::glib;
|
|
||||||
|
|
||||||
use crate::utils::BoxedSearchResult;
|
use crate::utils::BoxedSearchResult;
|
||||||
|
use gtk4::glib;
|
||||||
|
use gtk4::prelude::*;
|
||||||
|
use gtk4::subclass::prelude::*;
|
||||||
|
|
||||||
mod imp;
|
mod imp;
|
||||||
|
|
||||||
|
|
@ -12,4 +13,13 @@ impl SearchResultObject {
|
||||||
pub fn new(search_result: &BoxedSearchResult) -> Self {
|
pub fn new(search_result: &BoxedSearchResult) -> Self {
|
||||||
glib::Object::new(&[("data", search_result)]).expect("Failed to create Application Object")
|
glib::Object::new(&[("data", search_result)]).expect("Failed to create Application Object")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn data(&self) -> Option<pop_launcher::SearchResult> {
|
||||||
|
if let Ok(data) = self.property("data") {
|
||||||
|
if let Ok(search_result) = data.get::<BoxedSearchResult>() {
|
||||||
|
return search_result.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ use x11rb::protocol::xproto::ConnectionExt;
|
||||||
use libcosmic::x;
|
use libcosmic::x;
|
||||||
|
|
||||||
use crate::search_result_row::SearchResultRow;
|
use crate::search_result_row::SearchResultRow;
|
||||||
|
use crate::utils::BoxedSearchResult;
|
||||||
use crate::SearchResultObject;
|
use crate::SearchResultObject;
|
||||||
use crate::TX;
|
use crate::TX;
|
||||||
use crate::X11_CONN;
|
use crate::X11_CONN;
|
||||||
|
|
@ -62,6 +63,7 @@ impl Window {
|
||||||
let list_view = cascade! {
|
let list_view = cascade! {
|
||||||
ListView::default();
|
ListView::default();
|
||||||
..set_orientation(Orientation::Vertical);
|
..set_orientation(Orientation::Vertical);
|
||||||
|
..set_single_click_activate(true);
|
||||||
};
|
};
|
||||||
container.append(&list_view);
|
container.append(&list_view);
|
||||||
|
|
||||||
|
|
@ -87,12 +89,7 @@ impl Window {
|
||||||
let model = gio::ListStore::new(SearchResultObject::static_type());
|
let model = gio::ListStore::new(SearchResultObject::static_type());
|
||||||
|
|
||||||
let slice_model = gtk4::SliceListModel::new(Some(&model), 0, NUM_LAUNCHER_ITEMS.into());
|
let slice_model = gtk4::SliceListModel::new(Some(&model), 0, NUM_LAUNCHER_ITEMS.into());
|
||||||
let selection_model = gtk4::SingleSelection::builder()
|
let selection_model = gtk4::NoSelection::new(Some(&slice_model));
|
||||||
.model(&slice_model)
|
|
||||||
.autoselect(false)
|
|
||||||
.can_unselect(true)
|
|
||||||
.selected(gtk4::INVALID_LIST_POSITION)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
imp.model.set(model).expect("Could not set model");
|
imp.model.set(model).expect("Could not set model");
|
||||||
// Wrap model with selection and pass it to the list view
|
// 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);
|
let action_launchi = gio::SimpleAction::new(&format!("launch{}", i), None);
|
||||||
self.add_action(&action_launchi);
|
self.add_action(&action_launchi);
|
||||||
action_launchi.connect_activate(glib::clone!(@weak lv => move |_action, _parameter| {
|
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 model = lv.model().unwrap();
|
||||||
let app_info = model.item(i - 1);
|
let obj = match model.item(i) {
|
||||||
if app_info.is_none() {
|
Some(obj) => obj.downcast::<SearchResultObject>().unwrap(),
|
||||||
println!("oops no app for this row...");
|
None => {
|
||||||
return;
|
dbg!(model.item(i));
|
||||||
}
|
return;
|
||||||
if let Ok(id)= app_info.unwrap().property("id") {
|
},
|
||||||
let id = id.get::<u32>().expect("App ID must be u32");
|
};
|
||||||
|
if let Some(search_result) = obj.data() {
|
||||||
|
println!("activating... {}", i + 1);
|
||||||
glib::MainContext::default().spawn_local(async move {
|
glib::MainContext::default().spawn_local(async move {
|
||||||
if let Some(tx) = TX.get() {
|
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
|
lv.connect_activate(glib::clone!(@weak window => move |list_view, i| {
|
||||||
.model()
|
dbg!(i);
|
||||||
.expect("List view missing selection model")
|
let model = list_view.model()
|
||||||
.downcast::<gtk4::SingleSelection>()
|
.expect("List view missing selection model")
|
||||||
.expect("could not downcast listview model to single selection model");
|
.downcast::<gtk4::NoSelection>()
|
||||||
|
.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() {
|
if i >= model.n_items() {
|
||||||
|
dbg!("index out of range");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
println!("acitvating... {}", i + 1);
|
let obj = match model.item(i) {
|
||||||
let app_info = model.item(i);
|
Some(obj) => obj.downcast::<SearchResultObject>().unwrap(),
|
||||||
if app_info.is_none() {
|
None => {
|
||||||
println!("oops no app for this row...");
|
dbg!(model.item(i));
|
||||||
return;
|
return;
|
||||||
}
|
},
|
||||||
if let Ok(id) = app_info.unwrap().property("id") {
|
};
|
||||||
let id = id.get::<u32>().expect("App ID must be u32");
|
if let Some(search_result) = obj.data() {
|
||||||
|
println!("activating... {}", i + 1);
|
||||||
glib::MainContext::default().spawn_local(async move {
|
glib::MainContext::default().spawn_local(async move {
|
||||||
if let Some(tx) = TX.get() {
|
if let Some(tx) = TX.get() {
|
||||||
let _ = tx.send(crate::Event::Activate(id)).await;
|
let _ = tx.send(crate::Event::Activate(search_result.id)).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue