How to use an enum as property

In rust, I’m trying to set in my object a property of type gtk::Orientation, which is an enum, but I got the error:

expected 2 arguments, found 1

This error occurs in the macro glib::Properties, I’m not very experienced with rust’s macros syntax, so I don’t know what could be the error.

Minimal reproducible object:

glib::wrapper! {
    pub struct MyObject(ObjectSubclass<imp::MyObject>);
}

mod imp {

    #[derive(glib::Properties)]
    #[properties(wrapper_type = super::MyObject)]
    pub struct MyObject {
        #[property(get, set)]
        orientation: RefCell<gtk::Orientation>,
    }

    impl Default for MyObject {
        fn default() -> Self {
            Self {
                orientation: RefCell::new(gtk::Orientation::Horizontal),
            }
        }
    }

    #[glib::object_subclass]
    impl ObjectSubclass for MyObject {
        const NAME: &'static = "MyObject";

        type Type = super::MyObject;

        type ParentType = glib::Object;
    }

    #[glib::derived_properties]
    impl ObjectImpl for MyObject {}
}

What I did was to implement gtk::Orientable in my object and use the override_interface directive like so:

glib::wrapper! {
    pub struct MyObject(ObjectSubclass<imp::MyObject>)
        @implements gtk::Orientable;
}

mod imp {

    #[derive(glib::Properties)]
    #[properties(wrapper_type = super::MyObject)]
    pub struct MyObject {
        #[property(get, set, override_interface = gtk::Orientable)]
        orientation: RefCell<gtk::Orientation>,
    }

    impl Default for MyObject {
        fn default() -> Self {
            Self {
                orientation: RefCell::new(gtk::Orientation::Horizontal),
            }
        }
    }

    #[glib::object_subclass]
    impl ObjectSubclass for MyObject {
        const NAME: &'static = "MyObject";

        type Type = super::MyObject;

        type ParentType = glib::Object;
    }

    #[glib::derived_properties]
    impl ObjectImpl for MyObject {}
}

Still don’t know how to use properties as enums that are not part of interfaces or parent classes, so I don’t know if mark this as final answer.

The code works, but when I open the GTK Inspector, and check the properties of MyObject, the orientation property does not appear.

Without overriding any class nor inteface, in rust, you must specify a default value for the param spec enum builder, I don’t know too much details about it, but I guess it makes sense, since an enum value must be of any of its variants (null/unspecified would be invalid).

You can specify this with the #[builder()] attribute.

Example:

#[derive(glib::Properties)]
#[properties(wrapper_type = super::MyObject)]
pub struct MyObject {
    // Inside builder() you must specify the default value
    #[property(get, set, builder(gtk::Orientation::Horizontal))]
    orientation: RefCell<gtk::Orientation>,
}

impl Default for MyObject {
    fn default() -> Self {
        Self {
            orientation: RefCell::new(gtk::Orientation::Horizontal),
        }
    }
}

You still have to implement Default trait for the object.

1 Like