1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#![doc(html_root_url = "https://docs.rs/glib-signal/0.4.0/")]
#![cfg_attr(docsrs, feature(doc_notable_trait, doc_cfg))]

#[cfg(feature = "futures")]
pub use self::signal_stream::{ConnectEof, OnceFuture, SignalStream};
#[doc(hidden)]
pub use glib; // for macro use
pub use {
	self::{
		borrowed_object::BorrowedObject,
		from_values::FromValues,
		pointer::Pointer,
		value_option::{PrimitiveValue, ToValueOption},
	},
	glib::SignalFlags,
};
use {
	glib::{
		object::{ObjectExt, ObjectType},
		subclass::{signal::SignalBuilder, SignalId},
		translate::{from_glib, IntoGlib, ToGlibPtr},
		types::StaticType,
		value::FromValue,
		BoolError, Closure, Quark, SignalHandlerId,
	},
	std::{fmt::Debug, marker::PhantomData},
};

#[cfg(feature = "futures")]
mod signal_stream;

mod borrowed_object;

mod pointer;

mod value_option;

mod from_values;

mod macros;

pub trait Signal: Copy + Debug {
	type Object: ObjectType;
	type Arguments: for<'a> FromValues<'a> + 'static;
	type Return: ToValueOption;

	const NAME: &'static str;
	const FLAGS: SignalFlags = SignalFlags::empty();

	fn signal() -> SignalId {
		SignalId::lookup(Self::NAME, <Self::Object as StaticType>::static_type()).expect(Self::NAME)
	}
}

pub trait DetailedSignal: Copy + Debug + Into<ConnectDetails<Self>> {
	type Signal: Signal;
	type Object: ObjectType;
	type Arguments: for<'a> FromValues<'a> + 'static;
	type Return: ToValueOption;

	const DETAIL: Option<&'static str>;

	fn detail() -> Option<Quark> {
		match Self::DETAIL {
			Some(detail) => Some(Quark::try_from_str(detail).expect(detail)),
			None => None,
		}
	}

	fn create_detail() -> Quark {
		Quark::from_str(Self::DETAIL.expect("detail string required"))
	}
}

impl<T: Signal> DetailedSignal for T {
	type Arguments = <Self::Signal as Signal>::Arguments;
	type Object = <Self::Signal as Signal>::Object;
	type Return = <Self::Signal as Signal>::Return;
	type Signal = Self;

	const DETAIL: Option<&'static str> = None;
}

pub trait BuildableSignal: Signal {
	fn builder<F: FnOnce(SignalBuilder) -> glib::subclass::Signal>(f: F) -> glib::subclass::Signal;
}

impl<T: Signal> BuildableSignal for T
where
	<Self::Return as ToValueOption>::Type: StaticType,
{
	fn builder<F: FnOnce(SignalBuilder) -> glib::subclass::Signal>(f: F) -> glib::subclass::Signal {
		let builder = glib::subclass::Signal::builder(Self::NAME)
			.param_types(<Self::Arguments as FromValues>::static_types())
			.return_type::<<Self::Return as ToValueOption>::Type>()
			.flags(Self::FLAGS);
		f(builder)
	}
}

pub trait BuildSignal: BuildableSignal {
	fn build() -> glib::subclass::Signal {
		Self::builder(|b| b.build())
	}
}

#[cfg_attr(docsrs, doc(notable_trait))]
pub trait Notifies<T: Signal>: ObjectType {}

#[derive(Copy, Clone, Debug)]
pub struct ConnectDetails<S = ()> {
	signal: SignalId,
	detail: Option<Quark>,
	pub run_after: bool,
	_signal: PhantomData<S>,
}

impl<S> ConnectDetails<S> {
	pub unsafe fn with_parts(signal: SignalId, detail: Option<Quark>, run_after: bool) -> Self {
		Self {
			signal,
			detail,
			run_after,
			_signal: PhantomData,
		}
	}

	pub fn normalize(&self) -> ConnectDetails<()> {
		unsafe { ConnectDetails::with_parts(self.signal(), self.detail(), self.run_after) }
	}

	pub fn signal(&self) -> SignalId {
		self.signal
	}

	pub fn detail(&self) -> Option<Quark> {
		self.detail
	}
}

impl<S: DetailedSignal> ConnectDetails<S> {
	pub fn new() -> Self {
		Self::with_after(false)
	}

	pub fn with_after(run_after: bool) -> Self {
		Self {
			signal: <S::Signal as Signal>::signal(),
			detail: S::detail(),
			run_after,
			_signal: PhantomData,
		}
	}

	pub fn set_detail(&mut self, detail: Quark) {
		assert!(self.detail.is_none());
		self.detail = Some(detail);
	}
}

impl<S: Signal> ConnectDetails<S> {
	pub fn with_detail<D: Into<Option<Quark>>>(detail: D) -> Self {
		Self {
			signal: S::signal(),
			detail: detail.into(),
			run_after: false,
			_signal: PhantomData,
		}
	}
}

impl ConnectDetails<()> {
	pub fn with_name<O: StaticType>(signal: &str, after: bool) -> Option<Self> {
		let (signal, detail) = SignalId::parse_name(signal, O::static_type(), false)?;
		Some(unsafe { Self::with_parts(signal, detail, after) })
	}
}

impl<S: DetailedSignal> From<ConnectDetails<S>> for ConnectDetails<()> {
	fn from(v: ConnectDetails<S>) -> Self {
		v.normalize()
	}
}

impl<S: DetailedSignal> From<S> for ConnectDetails<S> {
	fn from(_: S) -> Self {
		Self::new()
	}
}

pub trait ObjectSignalExt: ObjectType {
	unsafe fn handle_closure(&self, signal: &ConnectDetails, callback: &Closure) -> Result<SignalHandlerId, BoolError>;
	fn remove_handle(&self, handle: SignalHandlerId);

	fn handle<S, S_, C>(&self, signal: S_, callback: C) -> SignalHandlerId
	where
		C: Fn(&Self, S::Arguments) -> <S::Return as ToValueOption>::Type,
		S: DetailedSignal,
		S_: Into<ConnectDetails<S>>,
		Self: Notifies<S::Signal>;

	#[cfg(feature = "futures")]
	fn signal_stream<S, S_>(&self, signal: S_) -> SignalStream<Self, S::Arguments>
	where
		S: DetailedSignal,
		S_: Into<ConnectDetails<S>>,
		Self: Notifies<S::Signal>,
		<S::Return as ToValueOption>::Type: Default;
}

impl<O: ObjectType> ObjectSignalExt for O
where
	for<'a> BorrowedObject<'a, O>: FromValue<'a>,
{
	unsafe fn handle_closure(&self, signal: &ConnectDetails, callback: &Closure) -> Result<SignalHandlerId, BoolError> {
		let handle = glib::gobject_ffi::g_signal_connect_closure_by_id(
			self.as_object_ref().to_glib_none().0,
			signal.signal().into_glib(),
			signal.detail().map(|q| q.into_glib()).unwrap_or(0),
			callback.to_glib_none().0,
			signal.run_after.into_glib(),
		);
		match handle {
			0 => Err(glib::bool_error!(
				"failed to connect signal {:?} of type {:?}",
				signal,
				Self::static_type()
			)),
			handle => Ok(from_glib(handle)),
		}
	}

	fn handle<S, S_, C>(&self, signal: S_, callback: C) -> SignalHandlerId
	where
		C: Fn(&Self, S::Arguments) -> <S::Return as ToValueOption>::Type,
		S: DetailedSignal,
		S_: Into<ConnectDetails<S>>,
		Self: Notifies<S::Signal>,
	{
		let signal = signal.into();
		unsafe {
			let callback = Closure::new_unsafe(move |values| {
				let (this, args) = values.split_first().unwrap();
				let this: BorrowedObject<Self> = this.get().unwrap();
				let args = S::Arguments::from_values(args).unwrap();
				callback(&this, args).into().to_value_option()
			});
			self.handle_closure(&signal.normalize(), &callback).unwrap()
		}
	}

	fn remove_handle(&self, handle: SignalHandlerId) {
		self.disconnect(handle)
	}

	#[cfg(feature = "futures")]
	fn signal_stream<S, S_>(&self, signal: S_) -> SignalStream<Self, S::Arguments>
	where
		S: DetailedSignal,
		S_: Into<ConnectDetails<S>>,
		Self: Notifies<S::Signal>,
		<S::Return as ToValueOption>::Type: Default,
	{
		let signal = signal.into();
		SignalStream::connect(self, signal, |_, _| Default::default())
	}
}