FerroUI/ docs

Search guides, controls, Rust APIs, and XAML.

    GitHub ↗

    reactive_properties

    macro · ferroui-reactiveui · v0.5.0

    macro_rules! reactive_properties
    

    Declares a view model's struct with its notifying properties (ReactiveUI.SourceGenerators' [Reactive]).

    The macro takes a struct declaration. Each field marked #[reactive] becomes a RefCell<Type> field with two methods:

    • a getter name(&self) -> Type, which clones the value (the type is Clone);
    • a setter set_name(&self, value: Type), which calls raise_and_set_if_changed with the name of the property in PascalCase (search_term is SearchTerm, the name that prop! derives from the getter). The type is PartialEq; a shared object compares by identity when its PartialEq does.

    The other fields are kept as they are. #[reactive] comes before the other attributes of its field; the struct is not generic. The accessors are pub, whatever the visibility of the field. The struct holds a ReactiveObject (or a ReactiveRecord) and reactive_object! is invoked for it, which gives the setters the raising methods.

    use ferroui_reactiveui::{reactive_object, reactive_properties, IReactiveNotifyPropertyChanged, ReactiveObject};
    use ferroui_reactive::ObservableExt;
    use std::cell::RefCell;
    use std::rc::Rc;
    
    reactive_properties! {
        /// The state of a search screen.
        #[derive(Default)]
        pub struct Search {
            reactive: ReactiveObject,
            #[reactive]
            search_term: String,
            #[reactive]
            page: u32,
            history: RefCell<Vec<String>>,
        }
    }
    reactive_object!(Search);
    
    let search = Rc::new(Search::default());
    let changes = Rc::new(RefCell::new(Vec::new()));
    let log = changes.clone();
    let _subscription = search.changed().subscribe_next(move |change| log.borrow_mut().push(change.property_name));
    
    search.set_search_term("reactive".to_owned());
    search.set_page(2);
    search.set_page(2);
    search.history.borrow_mut().push(search.search_term());
    
    assert_eq!(search.search_term(), "reactive");
    assert_eq!(search.page(), 2);
    assert_eq!(*changes.borrow(), ["SearchTerm", "Page"]);
    

    View source

    Availability

    Extracted for aarch64-apple-darwin. Cargo features are listed in the crate index.