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

Subclassing refactoring #270

Merged
merged 6 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/src/bin/basic_subclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod imp_win {
const NAME: &'static str = "SimpleWindow";
type Type = super::SimpleWindow;
type ParentType = gtk::ApplicationWindow;
type Interfaces = ();
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;

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

Expand Down
1 change: 1 addition & 0 deletions examples/src/bin/composite_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod imp {
const NAME: &'static str = "ExApplicationWindow";
type Type = super::ExApplicationWindow;
type ParentType = gtk::ApplicationWindow;
type Interfaces = ();
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;

Expand Down
1 change: 1 addition & 0 deletions examples/src/bin/gio_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod imp {
const NAME: &'static str = "FileSize";
type ParentType = glib::Object;
type Instance = subclass::simple::InstanceStruct<Self>;
type Interfaces = ();
type Class = subclass::simple::ClassStruct<Self>;
type Type = super::FileSize;
glib::object_subclass!();
Expand Down
95 changes: 46 additions & 49 deletions examples/src/bin/listbox_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,12 @@ mod model {
const NAME: &'static str = "Model";
type Type = super::Model;
type ParentType = glib::Object;
type Interfaces = (gio::ListModel,);
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;

glib::object_subclass!();

// Called right before class_init and allows a GObject to specify
// which interfaces it implement, in this case gio::ListModel
fn type_init(type_: &mut subclass::InitializingType<Self>) {
type_.add_interface::<gio::ListModel>();
}

// Called once at the very beginning of instantiation
fn new() -> Self {
Self(RefCell::new(Vec::new()))
Expand Down Expand Up @@ -306,48 +301,17 @@ mod row_data {
count: RefCell<u32>,
}

// GObject property definitions for our two values
static PROPERTIES: [subclass::Property; 2] = [
subclass::Property("name", |name| {
glib::ParamSpec::string(
name,
"Name",
"Name",
None, // Default value
glib::ParamFlags::READWRITE,
)
}),
subclass::Property("count", |name| {
glib::ParamSpec::uint(
name,
"Count",
"Count",
0,
100,
0, // Allowed range and default value
glib::ParamFlags::READWRITE,
)
}),
];

// Basic declaration of our type for the GObject type system
impl ObjectSubclass for RowData {
const NAME: &'static str = "RowData";
type Type = super::RowData;
type ParentType = glib::Object;
type Interfaces = ();
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;

glib::object_subclass!();

// Called exactly once before the first instantiation of an instance. This
// sets up any type-specific things, in this specific case it installs the
// properties so that GObject knows about their existence and they can be
// used on instances of our type
fn class_init(klass: &mut Self::Class) {
klass.install_properties(&PROPERTIES);
}

// Called once at the very beginning of instantiation of each instance and
// creates the data structure that contains all our state
fn new() -> Self {
Expand All @@ -365,17 +329,47 @@ mod row_data {
// This maps between the GObject properties and our internal storage of the
// corresponding values of the properties.
impl ObjectImpl for RowData {
fn set_property(&self, _obj: &Self::Type, id: usize, value: &glib::Value) {
let prop = &PROPERTIES[id];
fn properties() -> &'static [glib::ParamSpec] {
use once_cell::sync::Lazy;
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
vec![
glib::ParamSpec::string(
"name",
"Name",
"Name",
None, // Default value
glib::ParamFlags::READWRITE,
),
glib::ParamSpec::uint(
"count",
"Count",
"Count",
0,
100,
0, // Allowed range and default value
glib::ParamFlags::READWRITE,
),
]
});

PROPERTIES.as_ref()
}

match *prop {
subclass::Property("name", ..) => {
fn set_property(
&self,
_obj: &Self::Type,
_id: usize,
value: &glib::Value,
pspec: &glib::ParamSpec,
) {
match pspec.get_name() {
"name" => {
let name = value
.get()
.expect("type conformity checked by `Object::set_property`");
self.name.replace(name);
}
subclass::Property("count", ..) => {
"count" => {
let count = value
.get_some()
.expect("type conformity checked by `Object::set_property`");
Expand All @@ -385,12 +379,15 @@ mod row_data {
}
}

fn get_property(&self, _obj: &Self::Type, id: usize) -> glib::Value {
let prop = &PROPERTIES[id];

match *prop {
subclass::Property("name", ..) => self.name.borrow().to_value(),
subclass::Property("count", ..) => self.count.borrow().to_value(),
fn get_property(
&self,
_obj: &Self::Type,
_id: usize,
pspec: &glib::ParamSpec,
) -> glib::Value {
match pspec.get_name() {
"name" => self.name.borrow().to_value(),
"count" => self.count.borrow().to_value(),
_ => unimplemented!(),
}
}
Expand Down
5 changes: 1 addition & 4 deletions gio/src/read_input_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod imp {
const NAME: &'static str = "ReadInputStream";
type Type = super::ReadInputStream;
type ParentType = InputStream;
type Interfaces = (crate::Seekable,);
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;

Expand All @@ -35,10 +36,6 @@ mod imp {
read: RefCell::new(None),
}
}

fn type_init(type_: &mut subclass::InitializingType<Self>) {
type_.add_interface::<crate::Seekable>();
}
}

impl ObjectImpl for ReadInputStream {}
Expand Down
1 change: 1 addition & 0 deletions gio/src/subclass/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ mod tests {
const NAME: &'static str = "SimpleApplication";
type Type = super::SimpleApplication;
type ParentType = Application;
type Interfaces = ();
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;

Expand Down
5 changes: 1 addition & 4 deletions gio/src/subclass/input_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ mod tests {
const NAME: &'static str = "SimpleInputStream";
type Type = super::SimpleInputStream;
type ParentType = InputStream;
type Interfaces = (crate::Seekable,);
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;

Expand All @@ -279,10 +280,6 @@ mod tests {
pos: RefCell::new(0),
}
}

fn type_init(type_: &mut subclass::InitializingType<Self>) {
type_.add_interface::<crate::Seekable>();
}
}

impl ObjectImpl for SimpleInputStream {}
Expand Down
1 change: 1 addition & 0 deletions gio/src/subclass/output_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ mod tests {
const NAME: &'static str = "SimpleOutputStream";
type Type = super::SimpleOutputStream;
type ParentType = OutputStream;
type Interfaces = ();
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;

Expand Down
1 change: 1 addition & 0 deletions gio/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ mod test {
const NAME: &'static str = "MySimpleObjectPrivate";
type ParentType = glib::Object;
type Instance = subclass::simple::InstanceStruct<Self>;
type Interfaces = ();
type Class = subclass::simple::ClassStruct<Self>;
type Type = MySimpleObject;
glib::object_subclass!();
Expand Down
5 changes: 1 addition & 4 deletions gio/src/write_output_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod imp {
const NAME: &'static str = "WriteOutputStream";
type Type = super::WriteOutputStream;
type ParentType = OutputStream;
type Interfaces = (crate::Seekable,);
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;

Expand All @@ -37,10 +38,6 @@ mod imp {
write: RefCell::new(None),
}
}

fn type_init(type_: &mut subclass::InitializingType<Self>) {
type_.add_interface::<crate::Seekable>();
}
}

impl ObjectImpl for WriteOutputStream {}
Expand Down
2 changes: 1 addition & 1 deletion glib/Gir_GObject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ trust_return_value_nullability = true

generate = [
"GObject.BindingFlags",
"GObject.ParamFlags",
"GObject.SignalFlags",
]

Expand All @@ -22,6 +21,7 @@ ignore = [
manual = [
"GObject.Object",
"GObject.Value",
"GObject.ParamFlags",
]

[[object]]
Expand Down
39 changes: 0 additions & 39 deletions glib/src/gobject/auto/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,45 +68,6 @@ impl SetValue for BindingFlags {
}
}

bitflags! {
pub struct ParamFlags: u32 {
const READABLE = 1;
const WRITABLE = 2;
const READWRITE = 3;
const CONSTRUCT = 4;
const CONSTRUCT_ONLY = 8;
const LAX_VALIDATION = 16;
const STATIC_NAME = 32;
const PRIVATE = 32;
const STATIC_NICK = 64;
const STATIC_BLURB = 128;
const EXPLICIT_NOTIFY = 1073741824;
const DEPRECATED = 2147483648;
}
}

impl fmt::Display for ParamFlags {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<Self as fmt::Debug>::fmt(self, f)
}
}

#[doc(hidden)]
impl ToGlib for ParamFlags {
type GlibType = gobject_ffi::GParamFlags;

fn to_glib(&self) -> gobject_ffi::GParamFlags {
self.bits()
}
}

#[doc(hidden)]
impl FromGlib<gobject_ffi::GParamFlags> for ParamFlags {
unsafe fn from_glib(value: gobject_ffi::GParamFlags) -> ParamFlags {
ParamFlags::from_bits_truncate(value)
}
}

bitflags! {
pub struct SignalFlags: u32 {
const RUN_FIRST = 1;
Expand Down
1 change: 0 additions & 1 deletion glib/src/gobject/auto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub use self::binding::Binding;

mod flags;
pub use self::flags::BindingFlags;
pub use self::flags::ParamFlags;
pub use self::flags::SignalFlags;

#[doc(hidden)]
Expand Down
41 changes: 41 additions & 0 deletions glib/src/gobject/flags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::translate::*;

bitflags::bitflags! {
pub struct ParamFlags: u32 {
const READABLE = 1;
const WRITABLE = 2;
const READWRITE = 3;
const CONSTRUCT = 4;
const CONSTRUCT_ONLY = 8;
const LAX_VALIDATION = 16;
const USER_0 = 128;
const USER_1 = 256;
const USER_2 = 1024;
const USER_3 = 2048;
const USER_4 = 4096;
const USER_5 = 8192;
const USER_6 = 16384;
const USER_7 = 32768;
const USER_8 = 65536;
const EXPLICIT_NOTIFY = 1073741824;
const DEPRECATED = 2147483648;
}
}

#[doc(hidden)]
impl ToGlib for ParamFlags {
type GlibType = gobject_ffi::GParamFlags;

fn to_glib(&self) -> gobject_ffi::GParamFlags {
self.bits()
}
}

#[doc(hidden)]
impl FromGlib<gobject_ffi::GParamFlags> for ParamFlags {
unsafe fn from_glib(value: gobject_ffi::GParamFlags) -> ParamFlags {
ParamFlags::from_bits_truncate(value)
}
}
4 changes: 3 additions & 1 deletion glib/src/gobject/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

//! GObject bindings

pub mod auto;
mod auto;
mod binding;
mod flags;

pub use self::auto::*;
pub use self::flags::*;
//pub use self::auto::functions::*;
Loading