Skip to content

Commit

Permalink
Merge pull request #1145 from ranfdev/master
Browse files Browse the repository at this point in the history
Add object_subclass example
  • Loading branch information
ranfdev authored Oct 28, 2023
2 parents 03bce03 + bddbc65 commit e93e899
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ path = "gio_task/main.rs"
[[bin]]
name = "gio_cancellable_future"
path = "gio_cancellable_future/main.rs"

[[bin]]
name = "object_subclass"
path = "object_subclass/main.rs"
49 changes: 49 additions & 0 deletions examples/object_subclass/author.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// You can copy/paste this file every time you need a simple GObject
// to hold some data

use glib::once_cell::sync::Lazy;
use glib::prelude::*;
use glib::subclass::prelude::*;
use glib::subclass::Signal;
use glib::Properties;
use std::cell::RefCell;

mod imp {
use super::*;

#[derive(Properties, Default)]
#[properties(wrapper_type = super::Author)]
pub struct Author {
#[property(get, set)]
name: RefCell<String>,
#[property(get, set)]
surname: RefCell<String>,
}

#[glib::derived_properties]
impl ObjectImpl for Author {
fn signals() -> &'static [Signal] {
static SIGNALS: Lazy<Vec<Signal>> =
Lazy::new(|| vec![Signal::builder("awarded").build()]);
SIGNALS.as_ref()
}
}

#[glib::object_subclass]
impl ObjectSubclass for Author {
const NAME: &'static str = "Author";
type Type = super::Author;
}
}

glib::wrapper! {
pub struct Author(ObjectSubclass<imp::Author>);
}
impl Author {
pub fn new(name: &str, surname: &str) -> Self {
glib::Object::builder()
.property("name", name)
.property("surname", surname)
.build()
}
}
14 changes: 14 additions & 0 deletions examples/object_subclass/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mod author;

use glib::prelude::*;

fn main() {
let author = author::Author::new("John", "Doe");
author.set_name("Jane");
author.connect("awarded", true, |_author| {
println!("Author received a new award!");
None
});

println!("Author: {} {}", author.name(), author.surname());
}

0 comments on commit e93e899

Please sign in to comment.