gobject_subclass/
properties.rs

1// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use gobject_ffi;
10
11use glib;
12use glib::translate::*;
13
14#[derive(Clone, Copy, PartialEq, Eq, Debug)]
15pub enum PropertyMutability {
16    Readable,
17    Writable,
18    ReadWrite,
19}
20
21impl Into<gobject_ffi::GParamFlags> for PropertyMutability {
22    fn into(self) -> gobject_ffi::GParamFlags {
23        use self::PropertyMutability::*;
24
25        match self {
26            Readable => gobject_ffi::G_PARAM_READABLE,
27            Writable => gobject_ffi::G_PARAM_WRITABLE,
28            ReadWrite => gobject_ffi::G_PARAM_READWRITE,
29        }
30    }
31}
32
33#[derive(Debug, Clone)]
34pub enum Property<'a> {
35    Boolean(&'a str, &'a str, &'a str, bool, PropertyMutability),
36    Int(
37        &'a str,
38        &'a str,
39        &'a str,
40        (i32, i32),
41        i32,
42        PropertyMutability,
43    ),
44    Int64(
45        &'a str,
46        &'a str,
47        &'a str,
48        (i64, i64),
49        i64,
50        PropertyMutability,
51    ),
52    UInt(
53        &'a str,
54        &'a str,
55        &'a str,
56        (u32, u32),
57        u32,
58        PropertyMutability,
59    ),
60    UInt64(
61        &'a str,
62        &'a str,
63        &'a str,
64        (u64, u64),
65        u64,
66        PropertyMutability,
67    ),
68    Float(
69        &'a str,
70        &'a str,
71        &'a str,
72        (f32, f32),
73        f32,
74        PropertyMutability,
75    ),
76    Double(
77        &'a str,
78        &'a str,
79        &'a str,
80        (f64, f64),
81        f64,
82        PropertyMutability,
83    ),
84    String(
85        &'a str,
86        &'a str,
87        &'a str,
88        Option<&'a str>,
89        PropertyMutability,
90    ),
91    Boxed(
92        &'a str,
93        &'a str,
94        &'a str,
95        fn() -> glib::Type,
96        PropertyMutability,
97    ),
98    Object(
99        &'a str,
100        &'a str,
101        &'a str,
102        fn() -> glib::Type,
103        PropertyMutability,
104    ),
105    Variant(
106        &'a str,
107        &'a str,
108        &'a str,
109        fn() -> glib::VariantType,
110        Option<&'a glib::Variant>,
111        PropertyMutability,
112    ),
113}
114
115impl<'a> Into<*mut gobject_ffi::GParamSpec> for &'a Property<'a> {
116    fn into(self) -> *mut gobject_ffi::GParamSpec {
117        unsafe {
118            match *self {
119                Property::Boolean(name, nick, description, default, mutability) => {
120                    gobject_ffi::g_param_spec_boolean(
121                        name.to_glib_none().0,
122                        nick.to_glib_none().0,
123                        description.to_glib_none().0,
124                        default.to_glib(),
125                        mutability.into(),
126                    )
127                }
128                Property::Int(name, nick, description, (min, max), default, mutability) => {
129                    gobject_ffi::g_param_spec_int(
130                        name.to_glib_none().0,
131                        nick.to_glib_none().0,
132                        description.to_glib_none().0,
133                        min,
134                        max,
135                        default,
136                        mutability.into(),
137                    )
138                }
139                Property::Int64(name, nick, description, (min, max), default, mutability) => {
140                    gobject_ffi::g_param_spec_int64(
141                        name.to_glib_none().0,
142                        nick.to_glib_none().0,
143                        description.to_glib_none().0,
144                        min,
145                        max,
146                        default,
147                        mutability.into(),
148                    )
149                }
150                Property::UInt(name, nick, description, (min, max), default, mutability) => {
151                    gobject_ffi::g_param_spec_uint(
152                        name.to_glib_none().0,
153                        nick.to_glib_none().0,
154                        description.to_glib_none().0,
155                        min,
156                        max,
157                        default,
158                        mutability.into(),
159                    )
160                }
161                Property::UInt64(name, nick, description, (min, max), default, mutability) => {
162                    gobject_ffi::g_param_spec_uint64(
163                        name.to_glib_none().0,
164                        nick.to_glib_none().0,
165                        description.to_glib_none().0,
166                        min,
167                        max,
168                        default,
169                        mutability.into(),
170                    )
171                }
172                Property::Float(name, nick, description, (min, max), default, mutability) => {
173                    gobject_ffi::g_param_spec_float(
174                        name.to_glib_none().0,
175                        nick.to_glib_none().0,
176                        description.to_glib_none().0,
177                        min,
178                        max,
179                        default,
180                        mutability.into(),
181                    )
182                }
183                Property::Double(name, nick, description, (min, max), default, mutability) => {
184                    gobject_ffi::g_param_spec_double(
185                        name.to_glib_none().0,
186                        nick.to_glib_none().0,
187                        description.to_glib_none().0,
188                        min,
189                        max,
190                        default,
191                        mutability.into(),
192                    )
193                }
194                Property::String(name, nick, description, default, mutability) => {
195                    gobject_ffi::g_param_spec_string(
196                        name.to_glib_none().0,
197                        nick.to_glib_none().0,
198                        description.to_glib_none().0,
199                        default.to_glib_none().0,
200                        mutability.into(),
201                    )
202                }
203                Property::Boxed(name, nick, description, get_type, mutability) => {
204                    gobject_ffi::g_param_spec_boxed(
205                        name.to_glib_none().0,
206                        nick.to_glib_none().0,
207                        description.to_glib_none().0,
208                        get_type().to_glib(),
209                        mutability.into(),
210                    )
211                }
212                Property::Object(name, nick, description, get_type, mutability) => {
213                    gobject_ffi::g_param_spec_object(
214                        name.to_glib_none().0,
215                        nick.to_glib_none().0,
216                        description.to_glib_none().0,
217                        get_type().to_glib(),
218                        mutability.into(),
219                    )
220                }
221                Property::Variant(name, nick, description, get_type, default, mutability) => {
222                    gobject_ffi::g_param_spec_variant(
223                        name.to_glib_none().0,
224                        nick.to_glib_none().0,
225                        description.to_glib_none().0,
226                        get_type().to_glib_none().0,
227                        default.to_glib_none().0,
228                        mutability.into(),
229                    )
230                }
231            }
232        }
233    }
234}