Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion glib/src/subclass/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ mod test {
.param_types([String::static_type()])
.return_type::<String>()
.action()
.class_handler(|_, args| {
.class_handler(|args| {
let obj = args[0]
.get::<super::SimpleObject>()
.expect("Failed to get Object from args[0]");
Expand All @@ -426,6 +426,16 @@ mod test {
super::Signal::builder("create-child-object")
.return_type::<super::ChildObject>()
.build(),
super::Signal::builder("return-string")
.return_type::<String>()
.action()
.class_handler(|args| {
let _obj = args[0]
.get::<super::SimpleObject>()
.expect("Failed to get Object from args[0]");
Some("base".to_value())
})
.build(),
]
})
}
Expand Down Expand Up @@ -487,6 +497,33 @@ mod test {
}
}

#[derive(Default)]
pub struct SimpleSubObject;

#[glib::object_subclass]
impl ObjectSubclass for SimpleSubObject {
const NAME: &'static str = "SimpleSubObject";
type Type = super::SimpleSubObject;
type ParentType = super::SimpleObject;

fn class_init(class: &mut Self::Class) {
class.override_signal_class_handler("return-string", |token, args| {
let obj = args[0]
.get::<super::SimpleSubObject>()
.expect("Failed to get Object from args[0]");

let res = obj.imp().signal_chain_from_overridden(token, args);
assert_eq!(res.unwrap().get::<&str>().unwrap(), "base");

Some("sub".to_value())
});
}
}

impl ObjectImpl for SimpleSubObject {}

impl SimpleObjectImpl for SimpleSubObject {}

#[derive(Clone, Copy)]
#[repr(C)]
pub struct DummyInterface {
Expand Down Expand Up @@ -514,6 +551,14 @@ mod test {
pub struct SimpleObject(ObjectSubclass<imp::SimpleObject>);
}

pub trait SimpleObjectImpl: ObjectImpl {}

unsafe impl<Obj: SimpleObjectImpl> IsSubclassable<Obj> for SimpleObject {}

wrapper! {
pub struct SimpleSubObject(ObjectSubclass<imp::SimpleSubObject>) @extends SimpleObject;
}

wrapper! {
pub struct Dummy(ObjectInterface<imp::Dummy>);
}
Expand Down Expand Up @@ -543,6 +588,14 @@ mod test {
assert!(weak.upgrade().is_none());
}

#[test]
fn test_sub_create() {
let obj = Object::builder::<SimpleSubObject>().build();
assert!(obj.type_().is_a(SimpleObject::static_type()));
assert_eq!(obj.type_(), SimpleSubObject::static_type());
assert!(obj.property::<bool>("constructed"));
}

#[test]
fn test_properties() {
let type_ = SimpleObject::static_type();
Expand Down Expand Up @@ -829,6 +882,8 @@ mod test {
"old-name"
);
assert!(name_changed_triggered.load(Ordering::Relaxed));

assert_eq!(obj.emit_by_name::<String>("return-string", &[]), "base");
}

#[test]
Expand All @@ -845,6 +900,13 @@ mod test {
assert_eq!(value, "return value");
}

#[test]
fn test_signal_override() {
let obj = Object::builder::<SimpleSubObject>().build();

assert_eq!(obj.emit_by_name::<String>("return-string", &[]), "sub");
}

#[test]
fn test_callback_validity() {
use std::sync::{
Expand Down
19 changes: 5 additions & 14 deletions glib/src/subclass/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ pub struct SignalBuilder {
flags: SignalFlags,
param_types: Vec<SignalType>,
return_type: SignalType,
class_handler: Option<
Box<dyn Fn(&SignalClassHandlerToken, &[Value]) -> Option<Value> + Send + Sync + 'static>,
>,
class_handler: Option<Box<dyn Fn(&[Value]) -> Option<Value> + Send + Sync + 'static>>,
accumulator: Option<
Box<dyn Fn(&SignalInvocationHint, &mut Value, &Value) -> bool + Send + Sync + 'static>,
>,
Expand Down Expand Up @@ -352,11 +350,7 @@ impl IntoGlib for SignalType {
#[allow(clippy::type_complexity)]
enum SignalRegistration {
Unregistered {
class_handler: Option<
Box<
dyn Fn(&SignalClassHandlerToken, &[Value]) -> Option<Value> + Send + Sync + 'static,
>,
>,
class_handler: Option<Box<dyn Fn(&[Value]) -> Option<Value> + Send + Sync + 'static>>,
accumulator: Option<
Box<dyn Fn(&SignalInvocationHint, &mut Value, &Value) -> bool + Send + Sync + 'static>,
>,
Expand Down Expand Up @@ -472,9 +466,7 @@ impl SignalBuilder {

// rustdoc-stripper-ignore-next
/// Class handler for this signal.
pub fn class_handler<
F: Fn(&SignalClassHandlerToken, &[Value]) -> Option<Value> + Send + Sync + 'static,
>(
pub fn class_handler<F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static>(
mut self,
func: F,
) -> Self {
Expand Down Expand Up @@ -610,9 +602,8 @@ impl Signal {
let return_type = self.return_type;

let class_handler = class_handler.map(|class_handler| {
Closure::new(move |values| unsafe {
let instance = gobject_ffi::g_value_get_object(values[0].to_glib_none().0);
let res = class_handler(&SignalClassHandlerToken(instance as *mut _, return_type.into(), values.as_ptr()), values);
Closure::new(move |values| {
let res = class_handler(values);

if return_type == Type::UNIT {
if let Some(ref v) = res {
Expand Down
Loading