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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::cell::RefCell;
use std::rc::{Rc, Weak};
use typemap::TypeMap;
use crate::{control::ControlObject, ObservableCollection};
pub struct ViewContext {
pub attached_values: TypeMap,
pub children: Box<dyn ObservableCollection<Rc<RefCell<dyn ControlObject>>>>,
}
impl ViewContext {
pub fn empty() -> ViewContext {
ViewContext {
attached_values: TypeMap::new(),
children: Box::new(Vec::new()),
}
}
}
pub trait ViewModel {
fn create_view(view_model: &Rc<RefCell<Self>>) -> Rc<RefCell<dyn ControlObject>>;
}
pub trait ViewModelObject {
fn create_view(&self) -> Rc<RefCell<dyn ControlObject>>;
fn box_clone(&self) -> Box<dyn ViewModelObject>;
fn downgrade(&self) -> Box<dyn WeakViewModelObject>;
}
impl<T: ViewModel + 'static> ViewModelObject for Rc<RefCell<T>> {
fn create_view(&self) -> Rc<RefCell<dyn ControlObject>> {
ViewModel::create_view(self)
}
fn box_clone(&self) -> Box<dyn ViewModelObject> {
Box::new(std::clone::Clone::clone(self))
}
fn downgrade(&self) -> Box<dyn WeakViewModelObject> {
Box::new(Rc::downgrade(self))
}
}
impl Clone for Box<dyn ViewModelObject> {
fn clone(&self) -> Self {
(*self).box_clone()
}
}
pub trait WeakViewModelObject {
fn box_clone(&self) -> Box<dyn WeakViewModelObject>;
fn upgrade(&self) -> Option<Box<dyn ViewModelObject>>;
}
impl<T: ViewModel + 'static> WeakViewModelObject for Weak<RefCell<T>> {
fn box_clone(&self) -> Box<dyn WeakViewModelObject> {
Box::new(std::clone::Clone::clone(self))
}
fn upgrade(&self) -> Option<Box<dyn ViewModelObject>> {
self.upgrade()
.map(|rc| Box::new(rc) as Box<(dyn ViewModelObject + 'static)>)
}
}
impl Clone for Box<dyn WeakViewModelObject> {
fn clone(&self) -> Self {
(*self).box_clone()
}
}