feat: basic list store implementation
This commit is contained in:
parent
0a91ca0ccd
commit
32b47eea02
4 changed files with 329 additions and 50 deletions
|
|
@ -11,5 +11,6 @@ gio = "0.14.8"
|
||||||
gtk4 = "0.3.1"
|
gtk4 = "0.3.1"
|
||||||
x11 = { version = "2", features = ["xlib"] }
|
x11 = { version = "2", features = ["xlib"] }
|
||||||
# examples/launcher
|
# examples/launcher
|
||||||
pop-launcher = "1.0.3"
|
pop-launcher = "=1.0.3"
|
||||||
serde_json = "1.0.70"
|
serde_json = "1.0.70"
|
||||||
|
once_cell = "1.8.0"
|
||||||
|
|
|
||||||
199
examples/launcher/application_object/imp.rs
Normal file
199
examples/launcher/application_object/imp.rs
Normal file
|
|
@ -0,0 +1,199 @@
|
||||||
|
use glib::{
|
||||||
|
types::Type, FromVariant, ParamFlags, ParamSpec, ParamSpecObject, ToVariant, Value, Variant,
|
||||||
|
VariantTy,
|
||||||
|
};
|
||||||
|
use gtk4::glib;
|
||||||
|
use gtk4::prelude::*;
|
||||||
|
use gtk4::subclass::prelude::*;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use super::ApplicationData;
|
||||||
|
|
||||||
|
// Object holding the state
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct ApplicationObject {
|
||||||
|
data: Rc<RefCell<ApplicationData>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// The central trait for subclassing a GObject
|
||||||
|
#[glib::object_subclass]
|
||||||
|
impl ObjectSubclass for ApplicationObject {
|
||||||
|
const NAME: &'static str = "LibCosmicLauncherApplicationObject";
|
||||||
|
type Type = super::ApplicationObject;
|
||||||
|
type ParentType = glib::Object;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trait shared by all GObjects
|
||||||
|
impl ObjectImpl for ApplicationObject {
|
||||||
|
fn properties() -> &'static [ParamSpec] {
|
||||||
|
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
|
||||||
|
vec![
|
||||||
|
ParamSpec::new_string(
|
||||||
|
// Name
|
||||||
|
"name",
|
||||||
|
// Nickname
|
||||||
|
"name",
|
||||||
|
// Short description
|
||||||
|
"Name of application in launcher search result",
|
||||||
|
// Default value
|
||||||
|
Some(""),
|
||||||
|
// The property can be read and written to
|
||||||
|
ParamFlags::READWRITE,
|
||||||
|
),
|
||||||
|
ParamSpec::new_string(
|
||||||
|
// Name
|
||||||
|
"description",
|
||||||
|
// Nickname
|
||||||
|
"description",
|
||||||
|
// Short description
|
||||||
|
"Description of application in launcher search result",
|
||||||
|
// Default value
|
||||||
|
Some(""),
|
||||||
|
// The property can be read and written to
|
||||||
|
ParamFlags::READWRITE,
|
||||||
|
),
|
||||||
|
ParamSpec::new_variant(
|
||||||
|
// Name
|
||||||
|
"icon",
|
||||||
|
// Nickname
|
||||||
|
"icon",
|
||||||
|
// Short description
|
||||||
|
"Icon of application in launcher search result",
|
||||||
|
VariantTy::new("(is)").expect("Oops invalid string for VariantTy tuple."),
|
||||||
|
// Default value
|
||||||
|
None,
|
||||||
|
// The property can be read and written to
|
||||||
|
ParamFlags::READWRITE,
|
||||||
|
),
|
||||||
|
ParamSpec::new_variant(
|
||||||
|
// Name
|
||||||
|
"categoryicon",
|
||||||
|
// Nickname
|
||||||
|
"categoryicon",
|
||||||
|
// Short description
|
||||||
|
"Category icon of application in launcher search result",
|
||||||
|
VariantTy::new("(is)").expect("Oops invalid string for VariantTy tuple."),
|
||||||
|
// Default value
|
||||||
|
None,
|
||||||
|
// The property can be read and written to
|
||||||
|
ParamFlags::READWRITE,
|
||||||
|
),
|
||||||
|
ParamSpec::new_variant(
|
||||||
|
// Name
|
||||||
|
"window",
|
||||||
|
// Nickname
|
||||||
|
"window",
|
||||||
|
// Short description
|
||||||
|
"Window of application in launcher search result",
|
||||||
|
// type (tuple of two uint32)
|
||||||
|
VariantTy::new("(uu)").expect("Oops invalid string for VariantTy tuple."),
|
||||||
|
// Default value
|
||||||
|
None,
|
||||||
|
// The property can be read and written to
|
||||||
|
ParamFlags::READWRITE,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
});
|
||||||
|
PROPERTIES.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_property(&self, _obj: &Self::Type, _id: usize, value: &Value, pspec: &ParamSpec) {
|
||||||
|
match pspec.name() {
|
||||||
|
"name" => {
|
||||||
|
let name = value
|
||||||
|
.get()
|
||||||
|
.expect("The value needs to be of type `String`.");
|
||||||
|
self.data.borrow_mut().0.name = name;
|
||||||
|
}
|
||||||
|
"description" => {
|
||||||
|
let description = value
|
||||||
|
.get()
|
||||||
|
.expect("The description needs to be of type `String`");
|
||||||
|
self.data.borrow_mut().0.description = description;
|
||||||
|
}
|
||||||
|
"icon" => {
|
||||||
|
let icon = <Option<(i32, String)>>::from_variant(
|
||||||
|
&value
|
||||||
|
.get::<Variant>()
|
||||||
|
.expect("The icon needs to be a Variant"),
|
||||||
|
)
|
||||||
|
.expect("The icon variant needs to be an Option<(i32, String)>");
|
||||||
|
if let Some(icon) = icon {
|
||||||
|
self.data.borrow_mut().0.icon = match icon {
|
||||||
|
(i_type, name) if i_type == pop_launcher::IconSource::Name as i32 => {
|
||||||
|
Some(pop_launcher::IconSource::Name(name.into()))
|
||||||
|
}
|
||||||
|
(i_type, name) if i_type == pop_launcher::IconSource::Mime as i32 => {
|
||||||
|
Some(pop_launcher::IconSource::Mime(name.into()))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
self.data.borrow_mut().0.icon = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"categoryicon" => {
|
||||||
|
let icon = <Option<(i32, String)>>::from_variant(
|
||||||
|
&value
|
||||||
|
.get::<Variant>()
|
||||||
|
.expect("The icon needs to be a Variant"),
|
||||||
|
)
|
||||||
|
.expect("The icon variant needs to be an Option<(i32, String)>");
|
||||||
|
if let Some(icon) = icon {
|
||||||
|
self.data.borrow_mut().0.icon = match icon {
|
||||||
|
(i_type, name) if i_type == pop_launcher::IconSource::Name as i32 => {
|
||||||
|
Some(pop_launcher::IconSource::Name(name.into()))
|
||||||
|
}
|
||||||
|
(i_type, name) if i_type == pop_launcher::IconSource::Mime as i32 => {
|
||||||
|
Some(pop_launcher::IconSource::Mime(name.into()))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
self.data.borrow_mut().0.icon = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"window" => {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
|
||||||
|
match pspec.name() {
|
||||||
|
"name" => self.data.borrow().0.name.to_value(),
|
||||||
|
"description" => self.data.borrow().0.name.to_value(),
|
||||||
|
"icon" => match &self.data.borrow().0.icon {
|
||||||
|
Some(pop_launcher::IconSource::Name(icon_name)) => {
|
||||||
|
(pop_launcher::IconSource::Name as i32, icon_name.to_string())
|
||||||
|
.to_variant()
|
||||||
|
.to_value()
|
||||||
|
}
|
||||||
|
Some(pop_launcher::IconSource::Mime(icon_name)) => {
|
||||||
|
(pop_launcher::IconSource::Mime as i32, icon_name.to_string())
|
||||||
|
.to_variant()
|
||||||
|
.to_value()
|
||||||
|
}
|
||||||
|
_ => None::<Variant>.to_value(),
|
||||||
|
},
|
||||||
|
"categoryicon" => match &self.data.borrow().0.category_icon {
|
||||||
|
Some(pop_launcher::IconSource::Name(icon_name)) => {
|
||||||
|
(pop_launcher::IconSource::Name as i32, icon_name.to_string())
|
||||||
|
.to_variant()
|
||||||
|
.to_value()
|
||||||
|
}
|
||||||
|
Some(pop_launcher::IconSource::Mime(icon_name)) => {
|
||||||
|
(pop_launcher::IconSource::Mime as i32, icon_name.to_string())
|
||||||
|
.to_variant()
|
||||||
|
.to_value()
|
||||||
|
}
|
||||||
|
_ => None::<Variant>.to_value(),
|
||||||
|
},
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
examples/launcher/application_object/mod.rs
Normal file
34
examples/launcher/application_object/mod.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
mod imp;
|
||||||
|
|
||||||
|
use gdk4::glib::Object;
|
||||||
|
use gtk4::glib;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
glib::wrapper! {
|
||||||
|
pub struct ApplicationObject(ObjectSubclass<imp::ApplicationObject>);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ApplicationObject {
|
||||||
|
pub fn new(application_search_result: &pop_launcher::SearchResult) -> Self {
|
||||||
|
Object::new(&[("name", &application_search_result.name)])
|
||||||
|
.expect("Failed to create `ApplicationObject`.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Object holding the state
|
||||||
|
pub struct ApplicationData(pop_launcher::SearchResult);
|
||||||
|
|
||||||
|
impl Default for ApplicationData {
|
||||||
|
fn default() -> Self {
|
||||||
|
let default_application = pop_launcher::SearchResult {
|
||||||
|
id: 0,
|
||||||
|
name: String::default(),
|
||||||
|
description: String::default(),
|
||||||
|
icon: None,
|
||||||
|
category_icon: None,
|
||||||
|
window: None,
|
||||||
|
};
|
||||||
|
Self(default_application)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,12 +1,16 @@
|
||||||
|
mod application_object;
|
||||||
use gtk4 as gtk;
|
use gtk4 as gtk;
|
||||||
|
|
||||||
|
use gtk::gio;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use libcosmic::x;
|
use gtk::{
|
||||||
use std::{
|
glib, Application, ApplicationWindow, Label, ListView, PolicyType, ScrolledWindow,
|
||||||
cell::RefCell,
|
SignalListItemFactory, SingleSelection,
|
||||||
rc::Rc,
|
|
||||||
};
|
};
|
||||||
|
use libcosmic::x;
|
||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
|
use self::application_object::ApplicationObject;
|
||||||
use self::ipc::LauncherIpc;
|
use self::ipc::LauncherIpc;
|
||||||
mod ipc;
|
mod ipc;
|
||||||
|
|
||||||
|
|
@ -14,12 +18,10 @@ fn icon_source(icon: >k::Image, source: &Option<pop_launcher::IconSource>) {
|
||||||
match source {
|
match source {
|
||||||
Some(pop_launcher::IconSource::Name(name)) => {
|
Some(pop_launcher::IconSource::Name(name)) => {
|
||||||
icon.set_from_icon_name(Some(name));
|
icon.set_from_icon_name(Some(name));
|
||||||
},
|
}
|
||||||
Some(pop_launcher::IconSource::Mime(content_type)) => {
|
Some(pop_launcher::IconSource::Mime(content_type)) => {
|
||||||
icon.set_from_gicon(
|
icon.set_from_gicon(&gio::content_type_get_icon(content_type));
|
||||||
&gio::content_type_get_icon(content_type)
|
}
|
||||||
);
|
|
||||||
},
|
|
||||||
_ => {
|
_ => {
|
||||||
icon.set_from_icon_name(None);
|
icon.set_from_icon_name(None);
|
||||||
}
|
}
|
||||||
|
|
@ -28,7 +30,7 @@ fn icon_source(icon: >k::Image, source: &Option<pop_launcher::IconSource>) {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let launcher = Rc::new(RefCell::new(
|
let launcher = Rc::new(RefCell::new(
|
||||||
LauncherIpc::new().expect("failed to connect to launcher service")
|
LauncherIpc::new().expect("failed to connect to launcher service"),
|
||||||
));
|
));
|
||||||
|
|
||||||
let app = gtk::Application::builder()
|
let app = gtk::Application::builder()
|
||||||
|
|
@ -56,63 +58,106 @@ fn main() {
|
||||||
vbox.append(&search);
|
vbox.append(&search);
|
||||||
|
|
||||||
let listbox = gtk::ListBox::new();
|
let listbox = gtk::ListBox::new();
|
||||||
vbox.append(&listbox);
|
//vbox.append(&listbox);
|
||||||
|
|
||||||
|
let model = gio::ListStore::new(ApplicationObject::static_type());
|
||||||
|
let factory = SignalListItemFactory::new();
|
||||||
|
factory.connect_setup(move |_, list_item| {
|
||||||
|
let label = Label::new(None);
|
||||||
|
list_item.set_child(Some(&label))
|
||||||
|
});
|
||||||
|
factory.connect_bind(move |_, list_item| {
|
||||||
|
let application_object = list_item
|
||||||
|
.item()
|
||||||
|
.expect("The item has to exist.")
|
||||||
|
.downcast::<ApplicationObject>()
|
||||||
|
.expect("The item has to be an `ApplicationObject`");
|
||||||
|
let name = application_object
|
||||||
|
.property("name")
|
||||||
|
.expect("Property name of the wrong type or does not exist!")
|
||||||
|
.get::<String>()
|
||||||
|
.expect("Property name needs to be a String.");
|
||||||
|
let label = list_item
|
||||||
|
.child()
|
||||||
|
.expect("The child has to exist.")
|
||||||
|
.downcast::<Label>()
|
||||||
|
.expect("The child has to be a `label`");
|
||||||
|
label.set_label(&name);
|
||||||
|
});
|
||||||
|
let selection_model = SingleSelection::new(Some(&model));
|
||||||
|
let list_view = ListView::new(Some(&selection_model), Some(&factory));
|
||||||
|
vbox.append(&list_view);
|
||||||
|
|
||||||
{
|
{
|
||||||
let launcher = launcher.clone();
|
let launcher = launcher.clone();
|
||||||
let search_changed = move |search: >k::Entry| {
|
let search_changed = move |search: >k::Entry| {
|
||||||
|
let model_len = model.n_items();
|
||||||
//TODO: is this the best way to clear a listbox?
|
//TODO: is this the best way to clear a listbox?
|
||||||
while let Some(child) = listbox.last_child() {
|
//while let Some(child) = listbox.last_child() {
|
||||||
listbox.remove(&child);
|
// listbox.remove(&child);
|
||||||
}
|
//}
|
||||||
|
|
||||||
let response_res = launcher.borrow_mut().request(
|
let response_res = launcher
|
||||||
pop_launcher::Request::Search(search.text().to_string())
|
.borrow_mut()
|
||||||
);
|
.request(pop_launcher::Request::Search(search.text().to_string()));
|
||||||
|
|
||||||
println!("{:#?}", response_res);
|
println!("{:#?}", response_res);
|
||||||
|
|
||||||
|
let num_items = 9;
|
||||||
|
|
||||||
if let Ok(pop_launcher::Response::Update(results)) = response_res {
|
if let Ok(pop_launcher::Response::Update(results)) = response_res {
|
||||||
for (i, result) in results.iter().enumerate() {
|
let new_results: Vec<glib::Object> = results
|
||||||
// Limit to 9 results
|
[0..std::cmp::min(results.len(), num_items)]
|
||||||
if i >= 9 { continue; }
|
.iter()
|
||||||
|
.map(|result| ApplicationObject::new(result).upcast())
|
||||||
|
.collect();
|
||||||
|
model.splice(0, model_len, &new_results[..]);
|
||||||
|
// for (i, result) in results[0..std::cmp::min(results.len(), num_items)]
|
||||||
|
// .iter()
|
||||||
|
// .enumerate()
|
||||||
|
// {
|
||||||
|
// dbg!(result);
|
||||||
|
// // Limit to 9 results
|
||||||
|
// // if i >= 9 {
|
||||||
|
// // break 'result_loop;
|
||||||
|
// // }
|
||||||
|
|
||||||
let row = gtk::ListBoxRow::new();
|
// let row = gtk::ListBoxRow::new();
|
||||||
listbox.append(&row);
|
// listbox.append(&row);
|
||||||
|
|
||||||
// Select first row
|
// // Select first row
|
||||||
if i == 0 {
|
// if i == 0 {
|
||||||
listbox.select_row(Some(&row));
|
// listbox.select_row(Some(&row));
|
||||||
}
|
// }
|
||||||
|
|
||||||
let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 8);
|
// let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 8);
|
||||||
hbox.set_margin_start(8);
|
// hbox.set_margin_start(8);
|
||||||
hbox.set_margin_end(8);
|
// hbox.set_margin_end(8);
|
||||||
hbox.set_margin_top(8);
|
// hbox.set_margin_top(8);
|
||||||
hbox.set_margin_bottom(8);
|
// hbox.set_margin_bottom(8);
|
||||||
row.set_child(Some(&hbox));
|
// row.set_child(Some(&hbox));
|
||||||
|
|
||||||
let category_icon = gtk::Image::new();
|
// let category_icon = gtk::Image::new();
|
||||||
category_icon.set_pixel_size(16);
|
// category_icon.set_pixel_size(16);
|
||||||
icon_source(&category_icon, &result.category_icon);
|
// icon_source(&category_icon, &result.category_icon);
|
||||||
hbox.append(&category_icon);
|
// hbox.append(&category_icon);
|
||||||
|
|
||||||
let icon = gtk::Image::new();
|
// let icon = gtk::Image::new();
|
||||||
icon.set_pixel_size(32);
|
// icon.set_pixel_size(32);
|
||||||
icon_source(&icon, &result.icon);
|
// icon_source(&icon, &result.icon);
|
||||||
hbox.append(&icon);
|
// hbox.append(&icon);
|
||||||
|
|
||||||
let labels = gtk::Box::new(gtk::Orientation::Vertical, 4);
|
// let labels = gtk::Box::new(gtk::Orientation::Vertical, 4);
|
||||||
hbox.append(&labels);
|
// hbox.append(&labels);
|
||||||
|
|
||||||
let name = gtk::Label::new(Some(&result.name));
|
// let name = gtk::Label::new(Some(&result.name));
|
||||||
name.set_halign(gtk::Align::Start);
|
// name.set_halign(gtk::Align::Start);
|
||||||
labels.append(&name);
|
// labels.append(&name);
|
||||||
|
|
||||||
let description = gtk::Label::new(Some(&result.description));
|
// let description = gtk::Label::new(Some(&result.description));
|
||||||
description.set_halign(gtk::Align::Start);
|
// description.set_halign(gtk::Align::Start);
|
||||||
labels.append(&description);
|
// labels.append(&description);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue