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 isClone); - a setter
set_name(&self, value: Type), which callsraise_and_set_if_changedwith the name of the property in PascalCase (search_termisSearchTerm, the name thatprop!derives from the getter). The type isPartialEq; a shared object compares by identity when itsPartialEqdoes.
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"]);
Availability
Extracted for aarch64-apple-darwin. Cargo features are listed in the crate index.