Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add object_subclass example #1145

Merged
merged 1 commit into from
Oct 28, 2023
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
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());
}
Loading