Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit df52c8f

Browse files
committed
Implement a builder for signals and use ObjectImpl::signals() for registering signals
1 parent 1135c7c commit df52c8f

File tree

5 files changed

+599
-470
lines changed

5 files changed

+599
-470
lines changed

glib/src/subclass/interface.rs

+13-113
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use super::InitializingType;
3+
use super::{InitializingType, Signal};
44
use crate::translate::*;
5-
use crate::{IsA, Object, ObjectExt, ParamSpec, SignalFlags, StaticType, Type, Value};
5+
use crate::{IsA, Object, ObjectExt, ParamSpec, StaticType, Type};
66
use std::marker;
77
use std::mem;
88

@@ -97,6 +97,11 @@ pub trait ObjectInterface: Sized + 'static {
9797
fn properties() -> &'static [ParamSpec] {
9898
&[]
9999
}
100+
101+
/// Signals installed for this interface.
102+
fn signals() -> &'static [Signal] {
103+
&[]
104+
}
100105
}
101106

102107
pub trait ObjectInterfaceExt: ObjectInterface {
@@ -114,117 +119,6 @@ pub trait ObjectInterfaceExt: ObjectInterface {
114119
&*(interface as *const Self)
115120
}
116121
}
117-
118-
/// Add a new signal to the interface.
119-
///
120-
/// This can be emitted later by `glib::Object::emit` and external code
121-
/// can connect to the signal to get notified about emissions.
122-
fn add_signal(&mut self, name: &str, flags: SignalFlags, arg_types: &[Type], ret_type: Type) {
123-
unsafe {
124-
super::types::add_signal(
125-
*(self as *mut _ as *mut ffi::GType),
126-
name,
127-
flags,
128-
arg_types,
129-
ret_type,
130-
);
131-
}
132-
}
133-
134-
/// Add a new signal with class handler to the interface.
135-
///
136-
/// This can be emitted later by `glib::Object::emit` and external code
137-
/// can connect to the signal to get notified about emissions.
138-
///
139-
/// The class handler will be called during the signal emission at the corresponding stage.
140-
fn add_signal_with_class_handler<F>(
141-
&mut self,
142-
name: &str,
143-
flags: SignalFlags,
144-
arg_types: &[Type],
145-
ret_type: Type,
146-
class_handler: F,
147-
) where
148-
F: Fn(&super::SignalClassHandlerToken, &[Value]) -> Option<Value> + Send + Sync + 'static,
149-
{
150-
unsafe {
151-
super::types::add_signal_with_class_handler(
152-
*(self as *mut _ as *mut ffi::GType),
153-
name,
154-
flags,
155-
arg_types,
156-
ret_type,
157-
class_handler,
158-
);
159-
}
160-
}
161-
162-
/// Add a new signal with accumulator to the interface.
163-
///
164-
/// This can be emitted later by `glib::Object::emit` and external code
165-
/// can connect to the signal to get notified about emissions.
166-
///
167-
/// The accumulator function is used for accumulating the return values of
168-
/// multiple signal handlers. The new value is passed as second argument and
169-
/// should be combined with the old value in the first argument. If no further
170-
/// signal handlers should be called, `false` should be returned.
171-
fn add_signal_with_accumulator<F>(
172-
&mut self,
173-
name: &str,
174-
flags: SignalFlags,
175-
arg_types: &[Type],
176-
ret_type: Type,
177-
accumulator: F,
178-
) where
179-
F: Fn(&super::SignalInvocationHint, &mut Value, &Value) -> bool + Send + Sync + 'static,
180-
{
181-
unsafe {
182-
super::types::add_signal_with_accumulator(
183-
*(self as *mut _ as *mut ffi::GType),
184-
name,
185-
flags,
186-
arg_types,
187-
ret_type,
188-
accumulator,
189-
);
190-
}
191-
}
192-
193-
/// Add a new signal with accumulator and class handler to the interface.
194-
///
195-
/// This can be emitted later by `glib::Object::emit` and external code
196-
/// can connect to the signal to get notified about emissions.
197-
///
198-
/// The accumulator function is used for accumulating the return values of
199-
/// multiple signal handlers. The new value is passed as second argument and
200-
/// should be combined with the old value in the first argument. If no further
201-
/// signal handlers should be called, `false` should be returned.
202-
///
203-
/// The class handler will be called during the signal emission at the corresponding stage.
204-
fn add_signal_with_class_handler_and_accumulator<F, G>(
205-
&mut self,
206-
name: &str,
207-
flags: SignalFlags,
208-
arg_types: &[Type],
209-
ret_type: Type,
210-
class_handler: F,
211-
accumulator: G,
212-
) where
213-
F: Fn(&super::SignalClassHandlerToken, &[Value]) -> Option<Value> + Send + Sync + 'static,
214-
G: Fn(&super::SignalInvocationHint, &mut Value, &Value) -> bool + Send + Sync + 'static,
215-
{
216-
unsafe {
217-
super::types::add_signal_with_class_handler_and_accumulator(
218-
*(self as *mut _ as *mut ffi::GType),
219-
name,
220-
flags,
221-
arg_types,
222-
ret_type,
223-
class_handler,
224-
accumulator,
225-
);
226-
}
227-
}
228122
}
229123

230124
impl<T: ObjectInterface> ObjectInterfaceExt for T {}
@@ -243,6 +137,12 @@ unsafe extern "C" fn interface_init<T: ObjectInterface>(
243137
);
244138
}
245139

140+
let type_ = T::get_type();
141+
let signals = <T as ObjectInterface>::signals();
142+
for signal in signals {
143+
signal.register(type_);
144+
}
145+
246146
iface.interface_init();
247147
}
248148

glib/src/subclass/mod.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@
8989
//! // This macro defines some boilerplate.
9090
//! glib::object_subclass!();
9191
//!
92-
//! // Called right before the first time an instance of the new
93-
//! // type is created. Here class specific settings can be performed,
94-
//! // including registration of signals for the new type.
95-
//! fn class_init(klass: &mut Self::Class) {}
96-
//!
9792
//! // Called every time a new instance is created. This should return
9893
//! // a new instance of our type with its basic values.
9994
//! fn new() -> Self {
@@ -259,6 +254,8 @@ pub mod object;
259254
#[macro_use]
260255
pub mod boxed;
261256

257+
pub mod signal;
258+
262259
pub mod prelude {
263260
//! Prelude that re-exports all important traits from this crate.
264261
pub use super::boxed::BoxedType;
@@ -271,7 +268,5 @@ pub mod prelude {
271268

272269
pub use self::boxed::register_boxed_type;
273270
pub use self::interface::register_interface;
274-
pub use self::types::{
275-
register_type, InitializingObject, InitializingType, SignalClassHandlerToken,
276-
SignalInvocationHint, TypeData,
277-
};
271+
pub use self::signal::{Signal, SignalClassHandlerToken, SignalId, SignalInvocationHint};
272+
pub use self::types::{register_type, InitializingObject, InitializingType, TypeData};

0 commit comments

Comments
 (0)