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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use super::{driver, Driver};
use kas::prelude::*;
use kas::updatable::{SingleData, UpdatableAll};
use std::fmt::{self};
#[derive(Clone, Widget)]
#[widget(config=noauto)]
#[layout(single)]
#[handler(handle=noauto, send=noauto)]
pub struct SingleView<
T: SingleData + UpdatableAll<(), V::Msg> + 'static,
V: Driver<T::Item> = driver::Default,
> {
#[widget_core]
core: CoreData,
view: V,
data: T,
#[widget]
child: V::Widget,
}
impl<
T: SingleData + UpdatableAll<(), V::Msg> + 'static + Default,
V: Driver<T::Item> + Default,
> Default for SingleView<T, V>
{
fn default() -> Self {
Self::new(T::default())
}
}
impl<T: SingleData + UpdatableAll<(), V::Msg> + 'static, V: Driver<T::Item> + Default>
SingleView<T, V>
{
pub fn new(data: T) -> Self {
Self::new_with_driver(<V as Default>::default(), data)
}
}
impl<T: SingleData + UpdatableAll<(), V::Msg> + 'static, V: Driver<T::Item>> SingleView<T, V> {
pub fn new_with_driver(view: V, data: T) -> Self {
let mut child = view.new();
let _ = view.set(&mut child, data.get_cloned());
SingleView {
core: Default::default(),
view,
data,
child,
}
}
pub fn data(&self) -> &T {
&self.data
}
pub fn data_mut(&mut self) -> &mut T {
&mut self.data
}
pub fn get_value(&self) -> T::Item {
self.data.get_cloned()
}
pub fn set_value(&self, mgr: &mut Manager, data: T::Item) {
if let Some(handle) = self.data.update(data) {
mgr.trigger_update(handle, 0);
}
}
pub fn update_value<F: Fn(T::Item) -> T::Item>(&self, mgr: &mut Manager, f: F) {
self.set_value(mgr, f(self.get_value()));
}
}
impl<T: SingleData + UpdatableAll<(), V::Msg> + 'static, V: Driver<T::Item>> WidgetConfig
for SingleView<T, V>
{
fn configure(&mut self, mgr: &mut Manager) {
self.data.enable_recursive_updates(mgr);
if let Some(handle) = self.data.update_handle() {
mgr.update_on_handle(handle, self.id());
}
}
}
impl<T: SingleData + UpdatableAll<(), V::Msg> + 'static, V: Driver<T::Item>> Handler
for SingleView<T, V>
{
type Msg = <V::Widget as Handler>::Msg;
fn handle(&mut self, mgr: &mut Manager, event: Event) -> Response<Self::Msg> {
match event {
Event::HandleUpdate { .. } => {
let value = self.data.get_cloned();
*mgr |= self.view.set(&mut self.child, value);
Response::Update
}
_ => Response::Unhandled,
}
}
}
impl<T: SingleData + UpdatableAll<(), V::Msg> + 'static, V: Driver<T::Item>> SendEvent
for SingleView<T, V>
{
fn send(&mut self, mgr: &mut Manager, id: WidgetId, event: Event) -> Response<Self::Msg> {
if self.is_disabled() {
return Response::Unhandled;
}
if id < self.id() {
let r = self.child.send(mgr, id, event);
if let Response::Msg(ref msg) = r {
log::trace!(
"Received by {} from {}: {:?}",
self.id(),
id,
kas::util::TryFormat(&msg)
);
if let Some(handle) = self.data.handle(&(), msg) {
mgr.trigger_update(handle, 0);
}
}
r
} else {
debug_assert!(id == self.id(), "SendEvent::send: bad WidgetId");
self.handle(mgr, event)
}
}
}
impl<T: SingleData + UpdatableAll<(), V::Msg> + 'static, V: Driver<T::Item>> fmt::Debug
for SingleView<T, V>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("SingleView")
.field("core", &self.core)
.field("data", &self.data)
.field("child", &self.child)
.finish_non_exhaustive()
}
}