1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | use glib::{gobject_ffi, translate::*, ParamSpec}; |
4 | |
5 | glib::wrapper! { |
6 | #[derive (Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
7 | #[doc (alias = "GstParamSpecFraction" )] |
8 | pub struct ParamSpecFraction(Shared<ffi::GstParamSpecFraction>); |
9 | |
10 | match fn { |
11 | ref => |ptr| gobject_ffi::g_param_spec_ref_sink(ptr as *mut gobject_ffi::GParamSpec), |
12 | unref => |ptr| gobject_ffi::g_param_spec_unref(ptr as *mut gobject_ffi::GParamSpec), |
13 | type_ => || ffi::gst_param_spec_fraction_get_type(), |
14 | } |
15 | } |
16 | |
17 | unsafe impl Send for ParamSpecFraction {} |
18 | unsafe impl Sync for ParamSpecFraction {} |
19 | |
20 | impl std::ops::Deref for ParamSpecFraction { |
21 | type Target = ParamSpec; |
22 | |
23 | #[inline ] |
24 | fn deref(&self) -> &Self::Target { |
25 | unsafe { &*(self as *const ParamSpecFraction as *const ParamSpec) } |
26 | } |
27 | } |
28 | |
29 | unsafe impl glib::ParamSpecType for ParamSpecFraction {} |
30 | |
31 | impl glib::HasParamSpec for crate::Fraction { |
32 | type ParamSpec = ParamSpecFraction; |
33 | |
34 | type SetValue = crate::Fraction; |
35 | type BuilderFn = for<'a> fn(&'a str) -> ParamSpecFractionBuilder; |
36 | |
37 | fn param_spec_builder() -> Self::BuilderFn { |
38 | ParamSpecFraction::builder |
39 | } |
40 | } |
41 | |
42 | #[doc (hidden)] |
43 | impl FromGlibPtrFull<*mut gobject_ffi::GParamSpec> for ParamSpecFraction { |
44 | #[inline ] |
45 | unsafe fn from_glib_full(ptr: *mut gobject_ffi::GParamSpec) -> Self { |
46 | from_glib_full(ptr as *mut ffi::GstParamSpecFraction) |
47 | } |
48 | } |
49 | |
50 | impl ParamSpecFraction { |
51 | pub fn builder(name: &str) -> ParamSpecFractionBuilder { |
52 | assert_initialized_main_thread!(); |
53 | ParamSpecFractionBuilder::new(name) |
54 | } |
55 | |
56 | #[deprecated = "Use builder() instead" ] |
57 | #[allow (clippy::new_ret_no_self)] |
58 | #[doc (alias = "gst_param_spec_fraction" )] |
59 | pub fn new<'a>( |
60 | name: &str, |
61 | nick: impl Into<Option<&'a str>>, |
62 | blurb: impl Into<Option<&'a str>>, |
63 | min: crate::Fraction, |
64 | max: crate::Fraction, |
65 | default: crate::Fraction, |
66 | flags: glib::ParamFlags, |
67 | ) -> glib::ParamSpec { |
68 | assert_initialized_main_thread!(); |
69 | unsafe { Self::new_unchecked(name, nick, blurb, min, max, default, flags) } |
70 | } |
71 | |
72 | unsafe fn new_unchecked<'a>( |
73 | name: &str, |
74 | nick: impl Into<Option<&'a str>>, |
75 | blurb: impl Into<Option<&'a str>>, |
76 | min: crate::Fraction, |
77 | max: crate::Fraction, |
78 | default: crate::Fraction, |
79 | flags: glib::ParamFlags, |
80 | ) -> glib::ParamSpec { |
81 | unsafe { |
82 | from_glib_none(ffi::gst_param_spec_fraction( |
83 | name.to_glib_none().0, |
84 | nick.into().to_glib_none().0, |
85 | blurb.into().to_glib_none().0, |
86 | min.numer(), |
87 | min.denom(), |
88 | max.numer(), |
89 | max.denom(), |
90 | default.numer(), |
91 | default.denom(), |
92 | flags.into_glib(), |
93 | )) |
94 | } |
95 | } |
96 | |
97 | #[inline ] |
98 | pub fn minimum(&self) -> crate::Fraction { |
99 | unsafe { |
100 | let ptr = self.as_ptr(); |
101 | |
102 | crate::Fraction::new((*ptr).min_num, (*ptr).min_den) |
103 | } |
104 | } |
105 | |
106 | #[inline ] |
107 | pub fn maximum(&self) -> crate::Fraction { |
108 | unsafe { |
109 | let ptr = self.as_ptr(); |
110 | |
111 | crate::Fraction::new((*ptr).max_num, (*ptr).max_den) |
112 | } |
113 | } |
114 | |
115 | #[inline ] |
116 | pub fn default_value(&self) -> crate::Fraction { |
117 | unsafe { |
118 | let ptr = self.as_ptr(); |
119 | |
120 | crate::Fraction::new((*ptr).def_num, (*ptr).def_den) |
121 | } |
122 | } |
123 | |
124 | #[inline ] |
125 | pub fn upcast(self) -> ParamSpec { |
126 | unsafe { |
127 | from_glib_full( |
128 | IntoGlibPtr::<*mut ffi::GstParamSpecFraction>::into_glib_ptr(self) |
129 | as *mut gobject_ffi::GParamSpec, |
130 | ) |
131 | } |
132 | } |
133 | |
134 | #[inline ] |
135 | pub fn upcast_ref(&self) -> &ParamSpec { |
136 | self |
137 | } |
138 | } |
139 | |
140 | #[derive (Default)] |
141 | #[must_use ] |
142 | pub struct ParamSpecFractionBuilder<'a> { |
143 | name: &'a str, |
144 | nick: Option<&'a str>, |
145 | blurb: Option<&'a str>, |
146 | flags: glib::ParamFlags, |
147 | minimum: Option<crate::Fraction>, |
148 | maximum: Option<crate::Fraction>, |
149 | default_value: Option<crate::Fraction>, |
150 | } |
151 | |
152 | impl<'a> ParamSpecFractionBuilder<'a> { |
153 | fn new(name: &'a str) -> Self { |
154 | assert_initialized_main_thread!(); |
155 | Self { |
156 | name, |
157 | ..Default::default() |
158 | } |
159 | } |
160 | |
161 | // rustdoc-stripper-ignore-next |
162 | /// Default: `-i32::MAX/1` |
163 | pub fn minimum(mut self, minimum: crate::Fraction) -> Self { |
164 | self.minimum = Some(minimum); |
165 | self |
166 | } |
167 | |
168 | // rustdoc-stripper-ignore-next |
169 | /// Default: `i32::MAX/1` |
170 | pub fn maximum(mut self, maximum: crate::Fraction) -> Self { |
171 | self.maximum = Some(maximum); |
172 | self |
173 | } |
174 | |
175 | // rustdoc-stripper-ignore-next |
176 | /// Default: `0/1` |
177 | pub fn default_value(mut self, default_value: crate::Fraction) -> Self { |
178 | self.default_value = Some(default_value); |
179 | self |
180 | } |
181 | |
182 | #[must_use ] |
183 | pub fn build(self) -> ParamSpec { |
184 | unsafe { |
185 | ParamSpecFraction::new_unchecked( |
186 | self.name, |
187 | self.nick.unwrap_or(self.name), |
188 | self.blurb.unwrap_or(self.name), |
189 | self.minimum |
190 | .unwrap_or_else(|| crate::Fraction::new(-i32::MAX, 1)), |
191 | self.maximum |
192 | .unwrap_or_else(|| crate::Fraction::new(i32::MAX, 1)), |
193 | self.default_value |
194 | .unwrap_or_else(|| crate::Fraction::new(0, 1)), |
195 | self.flags, |
196 | ) |
197 | } |
198 | } |
199 | } |
200 | |
201 | impl<'a> glib::prelude::ParamSpecBuilderExt<'a> for ParamSpecFractionBuilder<'a> { |
202 | fn set_nick(&mut self, nick: Option<&'a str>) { |
203 | self.nick = nick; |
204 | } |
205 | fn set_blurb(&mut self, blurb: Option<&'a str>) { |
206 | self.blurb = blurb; |
207 | } |
208 | fn set_flags(&mut self, flags: glib::ParamFlags) { |
209 | self.flags = flags; |
210 | } |
211 | fn current_flags(&self) -> glib::ParamFlags { |
212 | self.flags |
213 | } |
214 | } |
215 | |
216 | glib::wrapper! { |
217 | #[derive (Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
218 | #[doc (alias = "GstParamSpecArray" )] |
219 | pub struct ParamSpecArray(Shared<ffi::GstParamSpecArray>); |
220 | |
221 | match fn { |
222 | ref => |ptr| gobject_ffi::g_param_spec_ref_sink(ptr as *mut gobject_ffi::GParamSpec), |
223 | unref => |ptr| gobject_ffi::g_param_spec_unref(ptr as *mut gobject_ffi::GParamSpec), |
224 | type_ => || ffi::gst_param_spec_array_get_type(), |
225 | } |
226 | } |
227 | |
228 | unsafe impl Send for ParamSpecArray {} |
229 | unsafe impl Sync for ParamSpecArray {} |
230 | |
231 | impl std::ops::Deref for ParamSpecArray { |
232 | type Target = ParamSpec; |
233 | |
234 | #[inline ] |
235 | fn deref(&self) -> &Self::Target { |
236 | unsafe { &*(self as *const ParamSpecArray as *const ParamSpec) } |
237 | } |
238 | } |
239 | |
240 | unsafe impl glib::ParamSpecType for ParamSpecArray {} |
241 | |
242 | impl glib::HasParamSpec for crate::Array { |
243 | type ParamSpec = ParamSpecArray; |
244 | |
245 | type SetValue = crate::Array; |
246 | type BuilderFn = for<'a> fn(&'a str) -> ParamSpecArrayBuilder; |
247 | |
248 | fn param_spec_builder() -> Self::BuilderFn { |
249 | ParamSpecArray::builder |
250 | } |
251 | } |
252 | |
253 | #[doc (hidden)] |
254 | impl FromGlibPtrFull<*mut gobject_ffi::GParamSpec> for ParamSpecArray { |
255 | #[inline ] |
256 | unsafe fn from_glib_full(ptr: *mut gobject_ffi::GParamSpec) -> Self { |
257 | from_glib_full(ptr as *mut ffi::GstParamSpecArray) |
258 | } |
259 | } |
260 | |
261 | impl ParamSpecArray { |
262 | pub fn builder(name: &str) -> ParamSpecArrayBuilder { |
263 | assert_initialized_main_thread!(); |
264 | ParamSpecArrayBuilder::new(name) |
265 | } |
266 | |
267 | #[allow (clippy::new_ret_no_self)] |
268 | #[doc (alias = "gst_param_spec_array" )] |
269 | #[deprecated = "Use builder() instead" ] |
270 | pub fn new<'a>( |
271 | name: &str, |
272 | nick: impl Into<Option<&'a str>>, |
273 | blurb: impl Into<Option<&'a str>>, |
274 | element_spec: Option<&glib::ParamSpec>, |
275 | flags: glib::ParamFlags, |
276 | ) -> glib::ParamSpec { |
277 | assert_initialized_main_thread!(); |
278 | |
279 | unsafe { Self::new_unchecked(name, nick, blurb, element_spec, flags) } |
280 | } |
281 | |
282 | unsafe fn new_unchecked<'a>( |
283 | name: &str, |
284 | nick: impl Into<Option<&'a str>>, |
285 | blurb: impl Into<Option<&'a str>>, |
286 | element_spec: Option<&glib::ParamSpec>, |
287 | flags: glib::ParamFlags, |
288 | ) -> glib::ParamSpec { |
289 | unsafe { |
290 | from_glib_none(ffi::gst_param_spec_array( |
291 | name.to_glib_none().0, |
292 | nick.into().to_glib_none().0, |
293 | blurb.into().to_glib_none().0, |
294 | element_spec.to_glib_none().0, |
295 | flags.into_glib(), |
296 | )) |
297 | } |
298 | } |
299 | |
300 | #[inline ] |
301 | pub fn element_spec(&self) -> Option<&ParamSpec> { |
302 | unsafe { |
303 | let ptr = self.as_ptr(); |
304 | |
305 | if (*ptr).element_spec.is_null() { |
306 | None |
307 | } else { |
308 | Some( |
309 | &*(&(*ptr).element_spec as *const *mut glib::gobject_ffi::GParamSpec |
310 | as *const glib::ParamSpec), |
311 | ) |
312 | } |
313 | } |
314 | } |
315 | |
316 | #[inline ] |
317 | pub fn upcast(self) -> ParamSpec { |
318 | unsafe { |
319 | from_glib_full( |
320 | IntoGlibPtr::<*mut ffi::GstParamSpecArray>::into_glib_ptr(self) |
321 | as *mut gobject_ffi::GParamSpec, |
322 | ) |
323 | } |
324 | } |
325 | |
326 | #[inline ] |
327 | pub fn upcast_ref(&self) -> &ParamSpec { |
328 | self |
329 | } |
330 | } |
331 | |
332 | #[derive (Default)] |
333 | #[must_use ] |
334 | pub struct ParamSpecArrayBuilder<'a> { |
335 | name: &'a str, |
336 | nick: Option<&'a str>, |
337 | blurb: Option<&'a str>, |
338 | flags: glib::ParamFlags, |
339 | element_spec: Option<&'a glib::ParamSpec>, |
340 | } |
341 | |
342 | impl<'a> ParamSpecArrayBuilder<'a> { |
343 | fn new(name: &'a str) -> Self { |
344 | assert_initialized_main_thread!(); |
345 | Self { |
346 | name, |
347 | ..Default::default() |
348 | } |
349 | } |
350 | |
351 | // rustdoc-stripper-ignore-next |
352 | /// Default: `None` |
353 | pub fn element_spec(mut self, element_spec: impl Into<Option<&'a glib::ParamSpec>>) -> Self { |
354 | self.element_spec = element_spec.into(); |
355 | self |
356 | } |
357 | |
358 | #[must_use ] |
359 | pub fn build(self) -> ParamSpec { |
360 | unsafe { |
361 | ParamSpecArray::new_unchecked( |
362 | self.name, |
363 | self.nick.unwrap_or(self.name), |
364 | self.blurb.unwrap_or(self.name), |
365 | self.element_spec, |
366 | self.flags, |
367 | ) |
368 | } |
369 | } |
370 | } |
371 | |
372 | impl<'a> glib::prelude::ParamSpecBuilderExt<'a> for ParamSpecArrayBuilder<'a> { |
373 | fn set_nick(&mut self, nick: Option<&'a str>) { |
374 | self.nick = nick; |
375 | } |
376 | fn set_blurb(&mut self, blurb: Option<&'a str>) { |
377 | self.blurb = blurb; |
378 | } |
379 | fn set_flags(&mut self, flags: glib::ParamFlags) { |
380 | self.flags = flags; |
381 | } |
382 | fn current_flags(&self) -> glib::ParamFlags { |
383 | self.flags |
384 | } |
385 | } |
386 | |
387 | pub trait GstParamSpecBuilderExt<'a>: glib::prelude::ParamSpecBuilderExt<'a> { |
388 | // rustdoc-stripper-ignore-next |
389 | /// Mark the property as controllable |
390 | fn controllable(self) -> Self { |
391 | let flags = self.current_flags() | crate::PARAM_FLAG_CONTROLLABLE; |
392 | self.flags(flags) |
393 | } |
394 | |
395 | // rustdoc-stripper-ignore-next |
396 | /// Mark the property as mutable in ready state |
397 | fn mutable_ready(self) -> Self { |
398 | let flags = self.current_flags() | crate::PARAM_FLAG_MUTABLE_READY; |
399 | self.flags(flags) |
400 | } |
401 | |
402 | // rustdoc-stripper-ignore-next |
403 | /// Mark the property as mutable in paused state |
404 | fn mutable_paused(self) -> Self { |
405 | let flags = self.current_flags() | crate::PARAM_FLAG_MUTABLE_PAUSED; |
406 | self.flags(flags) |
407 | } |
408 | |
409 | // rustdoc-stripper-ignore-next |
410 | /// Mark the property as mutable in playing state |
411 | fn mutable_playing(self) -> Self { |
412 | let flags = self.current_flags() | crate::PARAM_FLAG_MUTABLE_PLAYING; |
413 | self.flags(flags) |
414 | } |
415 | |
416 | #[cfg (feature = "v1_18" )] |
417 | // rustdoc-stripper-ignore-next |
418 | /// Mark the property for showing the default value in the docs |
419 | fn doc_show_default(self) -> Self { |
420 | let flags = self.current_flags() | crate::PARAM_FLAG_DOC_SHOW_DEFAULT; |
421 | self.flags(flags) |
422 | } |
423 | |
424 | #[cfg (feature = "v1_18" )] |
425 | // rustdoc-stripper-ignore-next |
426 | /// Mark the property for being only conditionally available |
427 | fn conditionally_available(self) -> Self { |
428 | let flags = self.current_flags() | crate::PARAM_FLAG_CONDITIONALLY_AVAILABLE; |
429 | self.flags(flags) |
430 | } |
431 | } |
432 | |
433 | impl<'a, T: glib::prelude::ParamSpecBuilderExt<'a>> GstParamSpecBuilderExt<'a> for T {} |
434 | |
435 | #[cfg (test)] |
436 | mod tests { |
437 | use glib::prelude::*; |
438 | |
439 | use super::*; |
440 | |
441 | #[test ] |
442 | #[allow (deprecated)] |
443 | fn test_trait() { |
444 | crate::init().unwrap(); |
445 | |
446 | let _pspec = ParamSpecFraction::new( |
447 | "foo" , |
448 | "Foo" , |
449 | "Foo Bar" , |
450 | (0, 1).into(), |
451 | (100, 1).into(), |
452 | (1, 1).into(), |
453 | glib::ParamFlags::READWRITE, |
454 | ); |
455 | |
456 | let _pspec = ParamSpecFraction::builder("foo" ) |
457 | .nick("Foo" ) |
458 | .blurb("Foo Bar" ) |
459 | .minimum((0, 1).into()) |
460 | .maximum((100, 1).into()) |
461 | .default_value((1, 1).into()) |
462 | .readwrite() |
463 | .mutable_playing() |
464 | .build(); |
465 | } |
466 | } |
467 | |