cleanup and note about gtk critical error (need to test upstream fix)

This commit is contained in:
Ashley Wulber 2022-01-11 11:16:09 -05:00
parent 537539f43d
commit 9eba89432e
5 changed files with 71 additions and 64 deletions

View file

@ -1,5 +1,4 @@
use cascade::cascade;
use gio::DesktopAppInfo;
use gtk4::glib;
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
@ -12,7 +11,6 @@ use gtk4::Popover;
use crate::dock_object::DockObject;
use crate::dock_popover::DockPopover;
use crate::plugin;
use crate::utils::BoxedWindowList;
mod imp;
@ -107,6 +105,9 @@ impl DockItem {
}
self_.item_box.borrow().prepend(&image);
let old_image = self_.image.replace(Some(image));
if let Some(old_image) = old_image {
self_.item_box.borrow().remove(&old_image);
}
if let Ok(active_value) = dock_object.property("active") {
if let Ok(active) = active_value.get::<BoxedWindowList>() {
let dots = self_.dots.borrow();

View file

@ -150,73 +150,80 @@ impl DockList {
path.push("dock_plugin_uwu.so");
let mut path_css = path_dir.clone();
path_css.push("dock_plugin_uwu.css");
let path = path
.as_os_str()
.to_str()
.expect("plugin path needs to be a valid string");
let provider = gtk4::CssProvider::new();
if let Ok(f) = File::open(path_css) {
let mut reader = BufReader::new(f);
let mut buffer = Vec::new();
if path.exists() {
let path = path
.as_os_str()
.to_str()
.expect("plugin path needs to be a valid string");
if reader.read_to_end(&mut buffer).is_ok() {
provider.load_from_data(&buffer);
// Add the provider to the default screen
gtk4::StyleContext::add_provider_for_display(
&gdk4::Display::default().expect("Error initializing GTK CSS provider."),
&provider,
gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
if let Ok(f) = File::open(path_css) {
let mut reader = BufReader::new(f);
let mut buffer = Vec::new();
if reader.read_to_end(&mut buffer).is_ok() {
provider.load_from_data(&buffer);
// Add the provider to the default screen
gtk4::StyleContext::add_provider_for_display(
&gdk4::Display::default().expect("Error initializing GTK CSS provider."),
&provider,
gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
} else {
eprintln!("loading plugin css failed");
}
} else {
eprintln!("loading plugin css failed");
}
} else {
eprintln!("loading plugin css failed");
}
let (popover_menu, image, name, lib) = unsafe {
let lib = libloading::Library::new(path).unwrap();
// store library until unloading the plugin
let image_func: libloading::Symbol<unsafe extern "C" fn() -> *mut gtk4_sys::GtkWidget> =
lib.get(b"dock_plugin_image").unwrap();
let popover_func: libloading::Symbol<
unsafe extern "C" fn() -> *mut gtk4_sys::GtkWidget,
> = lib.get(b"dock_plugin_popover_menu").unwrap();
let name_func: libloading::Symbol<
unsafe extern "C" fn() -> *const std::os::raw::c_char,
> = lib.get(b"dock_plugin_name").unwrap();
// click handler is optional
let (popover_menu, image, name, lib) = unsafe {
let lib = libloading::Library::new(path).unwrap();
// store library until unloading the plugin
let image_func: libloading::Symbol<
unsafe extern "C" fn() -> *mut gtk4_sys::GtkWidget,
> = lib.get(b"dock_plugin_image").unwrap();
let popover_func: libloading::Symbol<
unsafe extern "C" fn() -> *mut gtk4_sys::GtkWidget,
> = lib.get(b"dock_plugin_popover_menu").unwrap();
let name_func: libloading::Symbol<
unsafe extern "C" fn() -> *const std::os::raw::c_char,
> = lib.get(b"dock_plugin_name").unwrap();
// click handler is optional
(popover_func(), image_func(), name_func(), lib)
};
if let Ok(ref mut mutex) = PLUGINS.try_lock() {
mutex.insert(String::from(path), lib);
}
let name = if !name.is_null() {
unsafe { String::from(CStr::from_ptr(name).to_str().unwrap_or_default()) }
} else {
String::new()
};
let image = if !image.is_null() {
unsafe { gtk4::glib::translate::from_glib_none::<_, gtk4::Widget>(image).unsafe_cast() }
} else {
gtk4::Image::new()
};
let popover_menu = if !popover_menu.is_null() {
unsafe {
gtk4::glib::translate::from_glib_none::<_, gtk4::Widget>(popover_menu).unsafe_cast()
(popover_func(), image_func(), name_func(), lib)
};
if let Ok(ref mut mutex) = PLUGINS.try_lock() {
mutex.insert(String::from(path), lib);
}
} else {
gtk4::Box::new(Orientation::Vertical, 4)
};
let boxed_plugin = plugin::BoxedDockPlugin {
path: String::from(path),
name,
image,
popover_menu,
};
let model = self.model();
model.append(&DockObject::from_plugin(boxed_plugin).upcast::<Object>());
let name = if !name.is_null() {
unsafe { String::from(CStr::from_ptr(name).to_str().unwrap_or_default()) }
} else {
String::new()
};
let image = if !image.is_null() {
unsafe {
gtk4::glib::translate::from_glib_none::<_, gtk4::Widget>(image).unsafe_cast()
}
} else {
gtk4::Image::new()
};
let popover_menu = if !popover_menu.is_null() {
unsafe {
gtk4::glib::translate::from_glib_none::<_, gtk4::Widget>(popover_menu)
.unsafe_cast()
}
} else {
gtk4::Box::new(Orientation::Vertical, 4)
};
let boxed_plugin = plugin::BoxedDockPlugin {
path: String::from(path),
name,
image,
popover_menu,
};
let model = self.model();
model.append(&DockObject::from_plugin(boxed_plugin).upcast::<Object>());
}
}
fn store_data(model: &gio::ListStore) {

View file

@ -1 +0,0 @@
ashleywulber@pop-os.190471:1641501268

View file

@ -1,13 +1,12 @@
use std::path::Path;
use crate::plugin::{self, BoxedDockPlugin};
use crate::plugin;
use crate::utils::BoxedWindowList;
use gdk4::glib::Object;
use gdk4::subclass::prelude::ObjectSubclassExt;
use gio::{DesktopAppInfo, Icon};
use gtk4::prelude::*;
use gtk4::{glib, Image};
use std::cell::Ref;
mod imp;

View file

@ -52,6 +52,7 @@ impl DockPopover {
if let Some(menu) = dock_object.get_popover_menu() {
// TODO investigate (dock:255244): Gtk-CRITICAL **: 19:12:38.668: gtk_at_context_set_accessible_role: assertion '!self->realized' failed
// appears after setting the menu handle a second time
// possibly solved by https://gitlab.gnome.org/GNOME/gtk/-/issues/4421
menu_handle.append(&menu);
} else {
cascade! {