Hi everyone,
This is my first post here. I’m trying to get into Gtk-rs and after reading the docs everything looked straightforward, but I’m hitting a wall.
I’m working on an object called Lap, which has two properties: duration (u64) and variant (enum LapVariant). I also have a builder to create these objects.
mod.rs
use gtk::glib;
mod builder;
mod imp;
glib::wrapper! {
pub struct Lap(ObjectSubclass<imp::Lap>);
}
impl Default for Lap {
fn default() -> Self {
builder::LapBuilder::default().build()
}
}
impl Lap {
pub fn builder() -> builder::LapBuilder {
builder::LapBuilder::default()
}
}
imp.rs
use std::cell::RefCell;
use glib::Properties;
use gtk::glib;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
#[derive(Default, Properties)]
#[properties(wrapper_type = super::Lap)]
pub struct Lap {
#[property(get, set)]
duration: RefCell<u64>,
#[property(get, set, builder(LapVariant::Work))]
variant: RefCell<LapVariant>,
}
#[glib::object_subclass]
impl ObjectSubclass for Lap {
const NAME: &'static str = "TomateLap";
type Type = super::Lap;
}
impl ObjectImpl for Lap {}
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, glib::Enum)]
#[enum_type(name = "TomateLapVariant")]
pub enum LapVariant {
#[default]
#[enum_value(name = "WORK")]
Work,
#[enum_value(name = "SHORT_BREAK")]
ShortBreak,
#[enum_value(name = "LONG_BREAK")]
LongBreak,
}
builder.rs
use glib::object::ObjectBuilder;
use gtk::glib;
use gtk::glib::Object;
use super::{imp::LapVariant, Lap};
pub struct LapBuilder {
builder: ObjectBuilder<'static, Lap>,
}
impl Default for LapBuilder {
fn default() -> Self {
Self {
builder: Object::builder(),
}
}
}
impl LapBuilder {
pub fn work(mut self) -> Self {
self.builder = self.builder.property("variant", LapVariant::Work);
self
}
pub fn duration(mut self, duration: u64) -> Self {
self.builder = self.builder.property("duration", duration);
self
}
pub fn build(self) -> Lap {
self.builder.build()
}
}
The problem comes when I try to do something like:
Lap::builder().work().duration(0).build()
Running cargo r gives me:
Can’t find property ‘duration’ for type ‘TomateLap’
Can’t find property ‘variant’ for type ‘TomateLap’
I’ve been stuck on this — am I missing something obvious about how properties are registered, or is there something else I need to implement?