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

Commit 7284c46

Browse files
authored
Merge pull request #270 from sdroege/class-refactoring
Subclassing refactoring
2 parents 757f070 + 8b74a4c commit 7284c46

21 files changed

+968
-854
lines changed

examples/src/bin/basic_subclass.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mod imp_win {
4040
const NAME: &'static str = "SimpleWindow";
4141
type Type = super::SimpleWindow;
4242
type ParentType = gtk::ApplicationWindow;
43+
type Interfaces = ();
4344
type Instance = subclass::simple::InstanceStruct<Self>;
4445
type Class = subclass::simple::ClassStruct<Self>;
4546

@@ -128,6 +129,7 @@ mod imp_app {
128129
const NAME: &'static str = "SimpleApplication";
129130
type Type = super::SimpleApplication;
130131
type ParentType = gtk::Application;
132+
type Interfaces = ();
131133
type Instance = subclass::simple::InstanceStruct<Self>;
132134
type Class = subclass::simple::ClassStruct<Self>;
133135

examples/src/bin/composite_template.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod imp {
2929
const NAME: &'static str = "ExApplicationWindow";
3030
type Type = super::ExApplicationWindow;
3131
type ParentType = gtk::ApplicationWindow;
32+
type Interfaces = ();
3233
type Instance = subclass::simple::InstanceStruct<Self>;
3334
type Class = subclass::simple::ClassStruct<Self>;
3435

examples/src/bin/gio_task.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod imp {
2727
const NAME: &'static str = "FileSize";
2828
type ParentType = glib::Object;
2929
type Instance = subclass::simple::InstanceStruct<Self>;
30+
type Interfaces = ();
3031
type Class = subclass::simple::ClassStruct<Self>;
3132
type Type = super::FileSize;
3233
glib::object_subclass!();

examples/src/bin/listbox_model.rs

+46-49
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,12 @@ mod model {
3434
const NAME: &'static str = "Model";
3535
type Type = super::Model;
3636
type ParentType = glib::Object;
37+
type Interfaces = (gio::ListModel,);
3738
type Instance = subclass::simple::InstanceStruct<Self>;
3839
type Class = subclass::simple::ClassStruct<Self>;
3940

4041
glib::object_subclass!();
4142

42-
// Called right before class_init and allows a GObject to specify
43-
// which interfaces it implement, in this case gio::ListModel
44-
fn type_init(type_: &mut subclass::InitializingType<Self>) {
45-
type_.add_interface::<gio::ListModel>();
46-
}
47-
4843
// Called once at the very beginning of instantiation
4944
fn new() -> Self {
5045
Self(RefCell::new(Vec::new()))
@@ -306,48 +301,17 @@ mod row_data {
306301
count: RefCell<u32>,
307302
}
308303

309-
// GObject property definitions for our two values
310-
static PROPERTIES: [subclass::Property; 2] = [
311-
subclass::Property("name", |name| {
312-
glib::ParamSpec::string(
313-
name,
314-
"Name",
315-
"Name",
316-
None, // Default value
317-
glib::ParamFlags::READWRITE,
318-
)
319-
}),
320-
subclass::Property("count", |name| {
321-
glib::ParamSpec::uint(
322-
name,
323-
"Count",
324-
"Count",
325-
0,
326-
100,
327-
0, // Allowed range and default value
328-
glib::ParamFlags::READWRITE,
329-
)
330-
}),
331-
];
332-
333304
// Basic declaration of our type for the GObject type system
334305
impl ObjectSubclass for RowData {
335306
const NAME: &'static str = "RowData";
336307
type Type = super::RowData;
337308
type ParentType = glib::Object;
309+
type Interfaces = ();
338310
type Instance = subclass::simple::InstanceStruct<Self>;
339311
type Class = subclass::simple::ClassStruct<Self>;
340312

341313
glib::object_subclass!();
342314

343-
// Called exactly once before the first instantiation of an instance. This
344-
// sets up any type-specific things, in this specific case it installs the
345-
// properties so that GObject knows about their existence and they can be
346-
// used on instances of our type
347-
fn class_init(klass: &mut Self::Class) {
348-
klass.install_properties(&PROPERTIES);
349-
}
350-
351315
// Called once at the very beginning of instantiation of each instance and
352316
// creates the data structure that contains all our state
353317
fn new() -> Self {
@@ -365,17 +329,47 @@ mod row_data {
365329
// This maps between the GObject properties and our internal storage of the
366330
// corresponding values of the properties.
367331
impl ObjectImpl for RowData {
368-
fn set_property(&self, _obj: &Self::Type, id: usize, value: &glib::Value) {
369-
let prop = &PROPERTIES[id];
332+
fn properties() -> &'static [glib::ParamSpec] {
333+
use once_cell::sync::Lazy;
334+
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
335+
vec![
336+
glib::ParamSpec::string(
337+
"name",
338+
"Name",
339+
"Name",
340+
None, // Default value
341+
glib::ParamFlags::READWRITE,
342+
),
343+
glib::ParamSpec::uint(
344+
"count",
345+
"Count",
346+
"Count",
347+
0,
348+
100,
349+
0, // Allowed range and default value
350+
glib::ParamFlags::READWRITE,
351+
),
352+
]
353+
});
354+
355+
PROPERTIES.as_ref()
356+
}
370357

371-
match *prop {
372-
subclass::Property("name", ..) => {
358+
fn set_property(
359+
&self,
360+
_obj: &Self::Type,
361+
_id: usize,
362+
value: &glib::Value,
363+
pspec: &glib::ParamSpec,
364+
) {
365+
match pspec.get_name() {
366+
"name" => {
373367
let name = value
374368
.get()
375369
.expect("type conformity checked by `Object::set_property`");
376370
self.name.replace(name);
377371
}
378-
subclass::Property("count", ..) => {
372+
"count" => {
379373
let count = value
380374
.get_some()
381375
.expect("type conformity checked by `Object::set_property`");
@@ -385,12 +379,15 @@ mod row_data {
385379
}
386380
}
387381

388-
fn get_property(&self, _obj: &Self::Type, id: usize) -> glib::Value {
389-
let prop = &PROPERTIES[id];
390-
391-
match *prop {
392-
subclass::Property("name", ..) => self.name.borrow().to_value(),
393-
subclass::Property("count", ..) => self.count.borrow().to_value(),
382+
fn get_property(
383+
&self,
384+
_obj: &Self::Type,
385+
_id: usize,
386+
pspec: &glib::ParamSpec,
387+
) -> glib::Value {
388+
match pspec.get_name() {
389+
"name" => self.name.borrow().to_value(),
390+
"count" => self.count.borrow().to_value(),
394391
_ => unimplemented!(),
395392
}
396393
}

gio/src/read_input_stream.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod imp {
2525
const NAME: &'static str = "ReadInputStream";
2626
type Type = super::ReadInputStream;
2727
type ParentType = InputStream;
28+
type Interfaces = (crate::Seekable,);
2829
type Instance = subclass::simple::InstanceStruct<Self>;
2930
type Class = subclass::simple::ClassStruct<Self>;
3031

@@ -35,10 +36,6 @@ mod imp {
3536
read: RefCell::new(None),
3637
}
3738
}
38-
39-
fn type_init(type_: &mut subclass::InitializingType<Self>) {
40-
type_.add_interface::<crate::Seekable>();
41-
}
4239
}
4340

4441
impl ObjectImpl for ReadInputStream {}

gio/src/subclass/application.rs

+1
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ mod tests {
493493
const NAME: &'static str = "SimpleApplication";
494494
type Type = super::SimpleApplication;
495495
type ParentType = Application;
496+
type Interfaces = ();
496497
type Instance = subclass::simple::InstanceStruct<Self>;
497498
type Class = subclass::simple::ClassStruct<Self>;
498499

gio/src/subclass/input_stream.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ mod tests {
269269
const NAME: &'static str = "SimpleInputStream";
270270
type Type = super::SimpleInputStream;
271271
type ParentType = InputStream;
272+
type Interfaces = (crate::Seekable,);
272273
type Instance = subclass::simple::InstanceStruct<Self>;
273274
type Class = subclass::simple::ClassStruct<Self>;
274275

@@ -279,10 +280,6 @@ mod tests {
279280
pos: RefCell::new(0),
280281
}
281282
}
282-
283-
fn type_init(type_: &mut subclass::InitializingType<Self>) {
284-
type_.add_interface::<crate::Seekable>();
285-
}
286283
}
287284

288285
impl ObjectImpl for SimpleInputStream {}

gio/src/subclass/output_stream.rs

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ mod tests {
331331
const NAME: &'static str = "SimpleOutputStream";
332332
type Type = super::SimpleOutputStream;
333333
type ParentType = OutputStream;
334+
type Interfaces = ();
334335
type Instance = subclass::simple::InstanceStruct<Self>;
335336
type Class = subclass::simple::ClassStruct<Self>;
336337

gio/src/task.rs

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ mod test {
129129
const NAME: &'static str = "MySimpleObjectPrivate";
130130
type ParentType = glib::Object;
131131
type Instance = subclass::simple::InstanceStruct<Self>;
132+
type Interfaces = ();
132133
type Class = subclass::simple::ClassStruct<Self>;
133134
type Type = MySimpleObject;
134135
glib::object_subclass!();

gio/src/write_output_stream.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod imp {
2727
const NAME: &'static str = "WriteOutputStream";
2828
type Type = super::WriteOutputStream;
2929
type ParentType = OutputStream;
30+
type Interfaces = (crate::Seekable,);
3031
type Instance = subclass::simple::InstanceStruct<Self>;
3132
type Class = subclass::simple::ClassStruct<Self>;
3233

@@ -37,10 +38,6 @@ mod imp {
3738
write: RefCell::new(None),
3839
}
3940
}
40-
41-
fn type_init(type_: &mut subclass::InitializingType<Self>) {
42-
type_.add_interface::<crate::Seekable>();
43-
}
4441
}
4542

4643
impl ObjectImpl for WriteOutputStream {}

glib/Gir_GObject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ trust_return_value_nullability = true
1212

1313
generate = [
1414
"GObject.BindingFlags",
15-
"GObject.ParamFlags",
1615
"GObject.SignalFlags",
1716
]
1817

@@ -22,6 +21,7 @@ ignore = [
2221
manual = [
2322
"GObject.Object",
2423
"GObject.Value",
24+
"GObject.ParamFlags",
2525
]
2626

2727
[[object]]

glib/src/gobject/auto/flags.rs

-39
Original file line numberDiff line numberDiff line change
@@ -68,45 +68,6 @@ impl SetValue for BindingFlags {
6868
}
6969
}
7070

71-
bitflags! {
72-
pub struct ParamFlags: u32 {
73-
const READABLE = 1;
74-
const WRITABLE = 2;
75-
const READWRITE = 3;
76-
const CONSTRUCT = 4;
77-
const CONSTRUCT_ONLY = 8;
78-
const LAX_VALIDATION = 16;
79-
const STATIC_NAME = 32;
80-
const PRIVATE = 32;
81-
const STATIC_NICK = 64;
82-
const STATIC_BLURB = 128;
83-
const EXPLICIT_NOTIFY = 1073741824;
84-
const DEPRECATED = 2147483648;
85-
}
86-
}
87-
88-
impl fmt::Display for ParamFlags {
89-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90-
<Self as fmt::Debug>::fmt(self, f)
91-
}
92-
}
93-
94-
#[doc(hidden)]
95-
impl ToGlib for ParamFlags {
96-
type GlibType = gobject_ffi::GParamFlags;
97-
98-
fn to_glib(&self) -> gobject_ffi::GParamFlags {
99-
self.bits()
100-
}
101-
}
102-
103-
#[doc(hidden)]
104-
impl FromGlib<gobject_ffi::GParamFlags> for ParamFlags {
105-
unsafe fn from_glib(value: gobject_ffi::GParamFlags) -> ParamFlags {
106-
ParamFlags::from_bits_truncate(value)
107-
}
108-
}
109-
11071
bitflags! {
11172
pub struct SignalFlags: u32 {
11273
const RUN_FIRST = 1;

glib/src/gobject/auto/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub use self::binding::Binding;
77

88
mod flags;
99
pub use self::flags::BindingFlags;
10-
pub use self::flags::ParamFlags;
1110
pub use self::flags::SignalFlags;
1211

1312
#[doc(hidden)]

glib/src/gobject/flags.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use crate::translate::*;
4+
5+
bitflags::bitflags! {
6+
pub struct ParamFlags: u32 {
7+
const READABLE = 1;
8+
const WRITABLE = 2;
9+
const READWRITE = 3;
10+
const CONSTRUCT = 4;
11+
const CONSTRUCT_ONLY = 8;
12+
const LAX_VALIDATION = 16;
13+
const USER_0 = 128;
14+
const USER_1 = 256;
15+
const USER_2 = 1024;
16+
const USER_3 = 2048;
17+
const USER_4 = 4096;
18+
const USER_5 = 8192;
19+
const USER_6 = 16384;
20+
const USER_7 = 32768;
21+
const USER_8 = 65536;
22+
const EXPLICIT_NOTIFY = 1073741824;
23+
const DEPRECATED = 2147483648;
24+
}
25+
}
26+
27+
#[doc(hidden)]
28+
impl ToGlib for ParamFlags {
29+
type GlibType = gobject_ffi::GParamFlags;
30+
31+
fn to_glib(&self) -> gobject_ffi::GParamFlags {
32+
self.bits()
33+
}
34+
}
35+
36+
#[doc(hidden)]
37+
impl FromGlib<gobject_ffi::GParamFlags> for ParamFlags {
38+
unsafe fn from_glib(value: gobject_ffi::GParamFlags) -> ParamFlags {
39+
ParamFlags::from_bits_truncate(value)
40+
}
41+
}

glib/src/gobject/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
//! GObject bindings
44
5-
pub mod auto;
5+
mod auto;
66
mod binding;
7+
mod flags;
78

89
pub use self::auto::*;
10+
pub use self::flags::*;
911
//pub use self::auto::functions::*;

0 commit comments

Comments
 (0)