upgrade gtk-rs version

This commit is contained in:
Ashley Wulber 2022-01-19 10:19:56 -05:00
parent 0b5f6b8386
commit cc577b1367
23 changed files with 401 additions and 317 deletions

View file

@ -1,6 +1,10 @@
use std::cell::RefCell;
use std::rc::Rc;
use gdk4::glib::ParamSpecBoolean;
use gdk4::glib::ParamSpecString;
use gdk4::glib::ParamSpecUInt;
use gdk4::glib::ParamSpecVariant;
use glib::{FromVariant, ParamFlags, ParamSpec, ToVariant, Value, Variant, VariantTy};
use gtk4::glib;
use gtk4::prelude::*;
@ -28,7 +32,7 @@ impl ObjectImpl for AppGroup {
fn properties() -> &'static [ParamSpec] {
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
vec![
ParamSpec::new_uint(
ParamSpecUInt::new(
// Name
"id",
// Nickname
@ -42,7 +46,7 @@ impl ObjectImpl for AppGroup {
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpec::new_string(
ParamSpecString::new(
// Name
"name",
// Nickname
@ -54,7 +58,7 @@ impl ObjectImpl for AppGroup {
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpec::new_boolean(
ParamSpecBoolean::new(
// Name
"mutable",
// Nickname
@ -66,7 +70,7 @@ impl ObjectImpl for AppGroup {
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpec::new_string(
ParamSpecString::new(
// Name
"icon",
// Nickname
@ -78,7 +82,7 @@ impl ObjectImpl for AppGroup {
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpec::new_string(
ParamSpecString::new(
// Name
"category",
// Nickname
@ -90,7 +94,7 @@ impl ObjectImpl for AppGroup {
// The property can be read and written to
ParamFlags::READWRITE,
),
ParamSpec::new_variant(
ParamSpecVariant::new(
// Name
"appnames",
// Nickname

View file

@ -21,10 +21,7 @@ impl AppGroup {
("category", &data.category),
])
.expect("Failed to create `ApplicationObject`.");
if let Err(e) = self_.set_property("appnames", data.app_names.to_variant()) {
println!("failed to set category icon property");
dbg!(e);
};
self_.set_property("appnames", data.app_names.to_variant());
self_
}

View file

@ -94,37 +94,29 @@ impl GridItem {
// set drag source icon if possible...
// gio Icon is not easily converted to a Paintable, but this seems to be the correct method
if let Some(default_display) = &Display::default() {
if let Some(icon_theme) = IconTheme::for_display(default_display) {
if let Some(paintable_icon) = icon_theme.lookup_by_gicon(
&icon,
64,
1,
gtk4::TextDirection::None,
gtk4::IconLookupFlags::empty(),
) {
_self.set_icon(Some(&paintable_icon), 32, 32);
}
}
let icon_theme = IconTheme::for_display(default_display);
let paintable_icon = icon_theme.lookup_by_gicon(
&icon,
64,
1,
gtk4::TextDirection::None,
gtk4::IconLookupFlags::empty(),
);
_self.set_icon(Some(&paintable_icon), 32, 32);
}
}));
}
pub fn set_group_info(&self, app_group: AppGroup) {
let self_ = imp::GridItem::from_instance(self);
if let Ok(name) = app_group.property("name") {
self_.name.borrow().set_text(
&name
.get::<String>()
.expect("property name needs to be a string."),
);
}
if let Ok(icon) = app_group.property("icon") {
self_.image.borrow().set_from_icon_name(Some(
&icon
.get::<String>()
.expect("Property name needs to be a String."),
));
}
self_
.name
.borrow()
.set_text(&app_group.property::<String>("name"));
self_
.image
.borrow()
.set_from_icon_name(Some(&app_group.property::<String>("icon")));
}
pub fn set_index(&self, index: u32) {

View file

@ -1,12 +1,12 @@
use cascade::cascade;
use glib::Object;
use glib::{FromVariant, Variant};
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
use gtk4::{
gio, glib, Dialog, Entry, GridView, Label, PolicyType, ScrolledWindow, SignalListItemFactory,
Window,
};
use gtk4::{prelude::*, CustomFilter};
use std::fs::File;
use crate::app_group::AppGroup;
@ -239,18 +239,10 @@ impl GroupGrid {
.downcast::<AppGroup>()
.unwrap();
let category =
if let Ok(category_prop) = app_info.property("category") {
category_prop.get::<String>().unwrap_or("".to_string()).to_lowercase()
} else {
"".to_string()
};
app_info.property::<String>("category").to_lowercase();
let app_names =
if let Ok(app_names_prop) = app_info.property("appnames") {
<Vec<String>>::from_variant(&app_names_prop.get::<Variant>().expect("appnames nneds to be a variant.")).unwrap_or_default()
} else {
vec![]
};
<Vec<String>>::from_variant(&app_info.property::<Variant>("appnames")).unwrap_or_default();
dbg!(&app_names);
let new_filter: gtk4::CustomFilter = gtk4::CustomFilter::new(move |obj| {
let app = obj
@ -265,8 +257,7 @@ impl GroupGrid {
}
});
self_clone
.emit_by_name::<&str>("group-changed", &[&new_filter])
.unwrap();
.emit_by_name::<CustomFilter>("group-changed", &[&new_filter]);
});
}

View file

@ -3,7 +3,6 @@ use std::rc::Rc;
use crate::app_grid::AppGrid;
use crate::group_grid::GroupGrid;
use cascade::cascade;
use gdk4::Rectangle;
use gdk4_x11::X11Display;
use gdk4_x11::X11Surface;
use glib::Object;
@ -98,17 +97,15 @@ impl AppLibraryWindow {
let entry = &imp.entry.get().unwrap();
group_grid
.connect_local(
"group-changed",
false,
glib::clone!(@weak app_grid => @default-return None, move |args| {
let new_filter = args[1].get::<CustomFilter>().unwrap();
app_grid.set_group_filter(&new_filter);
None
}),
)
.unwrap();
group_grid.connect_local(
"group-changed",
false,
glib::clone!(@weak app_grid => @default-return None, move |args| {
let new_filter = args[1].get::<CustomFilter>().unwrap();
app_grid.set_group_filter(&new_filter);
None
}),
);
entry.connect_changed(
glib::clone!(@weak app_grid => move |search: &gtk4::SearchEntry| {
@ -170,20 +167,17 @@ impl AppLibraryWindow {
);
}
let resize = glib::clone!(@weak window, @strong x11rb_conn => move || {
let s = window.surface().expect("Failed to get Surface for AppLibraryWindow");
let s = window.surface();
let height = window.height();
let width = window.width();
if let Some((display, _surface)) = x::get_window_x11(&window) {
let monitor = display
.primary_monitor()
.expect("Failed to get Monitor");
let Rectangle {
x: monitor_x,
y: monitor_y,
width: monitor_width,
height: monitor_height,
} = monitor.geometry();
let geom = display
.primary_monitor().geometry();
let monitor_x = geom.x();
let monitor_y = geom.y();
let monitor_width = geom.width();
let monitor_height = geom.height();
// dbg!(monitor_width);
// dbg!(monitor_height);
// dbg!(width);
@ -205,9 +199,7 @@ impl AppLibraryWindow {
conn.flush().expect("failed to flush");
}
});
let s = window
.surface()
.expect("Failed to get Surface for AppLibraryWindow");
let s = window.surface();
let resize_height = resize.clone();
s.connect_height_notify(move |_s| {
glib::source::idle_add_local_once(resize_height.clone());

View file

@ -0,0 +1,28 @@
use gtk4::glib;
use gtk4::subclass::prelude::*;
use gtk4::SearchEntry;
use once_cell::sync::OnceCell;
use crate::app_grid::AppGrid;
use crate::group_grid::GroupGrid;
#[derive(Default)]
pub struct AppLibraryWindowInner {
pub entry: OnceCell<SearchEntry>,
pub app_grid: OnceCell<AppGrid>,
pub group_grid: OnceCell<GroupGrid>,
}
#[glib::object_subclass]
impl ObjectSubclass for AppLibraryWindowInner {
// `NAME` needs to match `class` attribute of template
const NAME: &'static str = "AppLibraryWindowInner";
type Type = super::AppLibraryWindowInner;
type ParentType = gtk4::Box;
}
impl ObjectImpl for AppLibraryWindowInner {}
impl WidgetImpl for AppLibraryWindowInner {}
impl BoxImpl for AppLibraryWindowInner {}

View file

@ -0,0 +1,130 @@
use cascade::cascade;
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
use gtk4::{gio, glib, Align, CustomFilter, Orientation, SearchEntry, Separator};
use crate::app_grid::AppGrid;
use crate::group_grid::GroupGrid;
mod imp;
glib::wrapper! {
pub struct AppLibraryWindowInner(ObjectSubclass<imp::AppLibraryWindowInner>)
@extends gtk4::Widget, gtk4::Box,
@implements gtk4::Accessible, gtk4::Buildable, gtk4::ConstraintTarget, gtk4::Orientable;
}
impl Default for AppLibraryWindowInner {
fn default() -> Self {
Self::new()
}
}
impl AppLibraryWindowInner {
pub fn new() -> Self {
let self_: Self = glib::Object::new(&[]).expect("Failed to create AppLibraryWindowInner");
let imp = imp::AppLibraryWindowInner::from_instance(&self_);
cascade! {
&self_;
..set_orientation(Orientation::Vertical);
..add_css_class("app_library_container");
};
let entry = cascade! {
SearchEntry::new();
..set_width_request(300);
..set_halign(Align::Center);
..set_margin_top(12);
..set_margin_bottom(12);
..set_placeholder_text(Some(" Type to search"));
};
self_.append(&entry);
let app_grid = AppGrid::new();
self_.append(&app_grid);
let separator = cascade! {
Separator::new(Orientation::Horizontal);
..set_hexpand(true);
..set_margin_bottom(12);
..set_margin_top(12);
};
self_.append(&separator);
let group_grid = GroupGrid::new();
self_.append(&group_grid);
imp.entry.set(entry).unwrap();
imp.app_grid.set(app_grid).unwrap();
imp.group_grid.set(group_grid).unwrap();
Self::setup_callbacks(&self_);
self_
}
pub fn group_grid(&self) -> Option<&GroupGrid> {
let imp = imp::AppLibraryWindowInner::from_instance(self);
imp.group_grid.get()
}
fn setup_callbacks(&self) {
// Get state
let imp = imp::AppLibraryWindowInner::from_instance(self);
let app_grid = &imp.app_grid.get().unwrap();
let group_grid = &imp.group_grid.get().unwrap();
let entry = &imp.entry.get().unwrap();
group_grid
.connect_local(
"group-changed",
false,
glib::clone!(@weak app_grid => @default-return None, move |args| {
let new_filter = args[1].get::<CustomFilter>().unwrap();
app_grid.set_group_filter(&new_filter);
None
}),
)
.unwrap();
entry.connect_changed(
glib::clone!(@weak app_grid => move |search: &gtk4::SearchEntry| {
let search_text = search.text().to_string().to_lowercase();
let new_filter: gtk4::CustomFilter = gtk4::CustomFilter::new(move |obj| {
let search_res = obj.downcast_ref::<gio::DesktopAppInfo>()
.expect("The Object needs to be of type AppInfo");
search_res.name().to_string().to_lowercase().contains(&search_text)
});
let search_text = search.text().to_string().to_lowercase();
let new_sorter: gtk4::CustomSorter = gtk4::CustomSorter::new(move |obj1, obj2| {
let app_info1 = obj1.downcast_ref::<gio::DesktopAppInfo>().unwrap();
let app_info2 = obj2.downcast_ref::<gio::DesktopAppInfo>().unwrap();
if search_text == "" {
return app_info1
.name()
.to_lowercase()
.cmp(&app_info2.name().to_lowercase())
.into();
}
let i_1 = app_info1.name().to_lowercase().find(&search_text);
let i_2 = app_info2.name().to_lowercase().find(&search_text);
match (i_1, i_2) {
(Some(i_1), Some(i_2)) => i_1.cmp(&i_2).into(),
(Some(_), None) => std::cmp::Ordering::Less.into(),
(None, Some(_)) => std::cmp::Ordering::Greater.into(),
_ => app_info1
.name()
.to_lowercase()
.cmp(&app_info2.name().to_lowercase())
.into()
}
});
app_grid.set_search_filter(&new_filter);
app_grid.set_app_sorter(&new_sorter);
}),
);
}
}