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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::event::{Action, Address, Event, Manager, Response};
use crate::geom::Rect;
use crate::layout::{AxisInfo, SizeRules};
use crate::theme::{DrawHandle, SizeHandle};
use crate::{CoreData, TkWindow, Widget, WidgetCore};
pub trait Handler: Widget {
type Msg;
#[inline]
fn activation_via_press(&self) -> bool {
false
}
#[inline]
fn handle_action(&mut self, _: &mut dyn TkWindow, action: Action) -> Response<Self::Msg> {
Response::Unhandled(Event::Action(action))
}
#[inline]
fn handle(&mut self, tk: &mut dyn TkWindow, _: Address, event: Event) -> Response<Self::Msg> {
Manager::handle_generic(self, tk, event)
}
}
impl<M> Handler for Box<dyn Handler<Msg = M>> {
type Msg = M;
#[inline]
fn activation_via_press(&self) -> bool {
self.as_ref().activation_via_press()
}
#[inline]
fn handle_action(&mut self, tk: &mut dyn TkWindow, action: Action) -> Response<Self::Msg> {
self.as_mut().handle_action(tk, action)
}
#[inline]
fn handle(
&mut self,
tk: &mut dyn TkWindow,
addr: Address,
event: Event,
) -> Response<Self::Msg> {
self.as_mut().handle(tk, addr, event)
}
}
impl<M> Widget for Box<dyn Handler<Msg = M>> {
fn allow_focus(&self) -> bool {
self.as_ref().allow_focus()
}
fn size_rules(&mut self, size_handle: &mut dyn SizeHandle, axis: AxisInfo) -> SizeRules {
self.as_mut().size_rules(size_handle, axis)
}
fn set_rect(&mut self, size_handle: &mut dyn SizeHandle, rect: Rect) {
self.as_mut().set_rect(size_handle, rect);
}
fn draw(&self, draw_handle: &mut dyn DrawHandle, ev_mgr: &Manager) {
self.as_ref().draw(draw_handle, ev_mgr);
}
}
impl<M> WidgetCore for Box<dyn Handler<Msg = M>> {
fn core_data(&self) -> &CoreData {
self.as_ref().core_data()
}
fn core_data_mut(&mut self) -> &mut CoreData {
self.as_mut().core_data_mut()
}
fn as_widget(&self) -> &dyn Widget {
self.as_ref().as_widget()
}
fn as_widget_mut(&mut self) -> &mut dyn Widget {
self.as_mut().as_widget_mut()
}
fn len(&self) -> usize {
self.as_ref().len()
}
fn get(&self, index: usize) -> Option<&dyn Widget> {
self.as_ref().get(index)
}
fn get_mut(&mut self, index: usize) -> Option<&mut dyn Widget> {
self.as_mut().get_mut(index)
}
fn walk(&self, f: &mut dyn FnMut(&dyn Widget)) {
self.as_ref().walk(f);
}
fn walk_mut(&mut self, f: &mut dyn FnMut(&mut dyn Widget)) {
self.as_mut().walk_mut(f);
}
}
impl<M> Clone for Box<dyn Handler<Msg = M>> {
fn clone(&self) -> Self {
#[cfg(feature = "nightly")]
unsafe {
let mut x = Box::new_uninit();
self.clone_to(x.as_mut_ptr());
x.assume_init()
}
#[cfg(not(feature = "nightly"))]
panic!("Clone for Box<dyn Widget> only supported on nightly");
}
}
impl Manager {
pub fn handle_generic<W>(
widget: &mut W,
tk: &mut dyn TkWindow,
event: Event,
) -> Response<<W as Handler>::Msg>
where
W: Handler + ?Sized,
{
let activable = widget.activation_via_press();
match event {
Event::Action(action) => widget.handle_action(tk, action),
Event::Identify => Response::Identify(widget.id()),
Event::PressStart { source, coord } if activable && source.is_primary() => {
tk.update_data(&mut |data| {
data.request_press_grab(source, widget.as_widget(), coord)
});
Response::None
}
Event::PressMove { .. } if activable => {
Response::None
}
Event::PressEnd {
start_id, end_id, ..
} if activable && start_id == Some(widget.id()) && start_id == end_id => {
widget.handle_action(tk, Action::Activate)
}
ev @ _ => Response::Unhandled(ev),
}
}
}