Skip to content

Commit

Permalink
Add object_subclass example
Browse files Browse the repository at this point in the history
  • Loading branch information
ranfdev committed Oct 27, 2023
1 parent 03bce03 commit 444bf6b
Show file tree
Hide file tree
Showing 3 changed files with 64 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"
46 changes: 46 additions & 0 deletions examples/object_subclass/author.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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() -> Self {
glib::Object::builder().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();
author.set_name("John");
author.set_surname("Doe");
author.connect("awarded", true, |_author| {
println!("Author received a new award!");
None
});
println!("Author: {} {}", author.name(), author.surname());
}

0 comments on commit 444bf6b

Please sign in to comment.