1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use crate::Text;
use fui_core::{ControlObject, ViewContext, ViewModel};
use fui_macros::ui;
use std::cell::RefCell;
use std::rc::Rc;
use typemap::TypeMap;

///
/// This view model is useful to use with many controls
/// to represent simple static text.
/// For example as an text item for DropDown's items collection.
///
#[derive(PartialEq)]
pub struct StringViewModel {
    pub text: String,
}

impl StringViewModel {
    pub fn new<T: Into<String>>(text: T) -> Rc<RefCell<Self>> {
        Rc::new(RefCell::new(StringViewModel { text: text.into() }))
    }
}

impl ViewModel for StringViewModel {
    fn create_view(view_model: &Rc<RefCell<Self>>) -> Rc<RefCell<dyn ControlObject>> {
        ui! {
            Text { text: &*view_model.borrow().text }
        }
    }
}