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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! Event loop and handling
use log::{debug, error, trace};
use smallvec::SmallVec;
use std::collections::HashMap;
use std::time::Instant;
use winit::event::{Event, StartCause};
use winit::event_loop::{ControlFlow, EventLoopWindowTarget};
use winit::window as ww;
use kas::TkAction;
use kas_theme::Theme;
use crate::draw::{CustomPipe, DrawPipe, DrawWindow};
use crate::shared::{PendingAction, SharedState};
use crate::{ProxyAction, Window, WindowId};
/// Event-loop data structure (i.e. all run-time state)
pub(crate) struct Loop<C: CustomPipe + 'static, T: Theme<DrawPipe<C>>>
where
T::Window: kas_theme::Window<DrawWindow<C::Window>>,
{
/// Window states
windows: HashMap<ww::WindowId, Window<C::Window, T::Window>>,
/// Translates our WindowId to winit's
id_map: HashMap<WindowId, ww::WindowId>,
/// Shared data passed from Toolkit
shared: SharedState<C, T>,
/// Timer resumes: (time, window index)
resumes: Vec<(Instant, ww::WindowId)>,
}
impl<C: CustomPipe + 'static, T: Theme<DrawPipe<C>>> Loop<C, T>
where
T::Window: kas_theme::Window<DrawWindow<C::Window>>,
{
pub(crate) fn new(
mut windows: Vec<Window<C::Window, T::Window>>,
shared: SharedState<C, T>,
) -> Self {
let id_map = windows
.iter()
.map(|w| (w.window_id, w.window.id()))
.collect();
Loop {
windows: windows.drain(..).map(|w| (w.window.id(), w)).collect(),
id_map,
shared,
resumes: vec![],
}
}
pub(crate) fn handle(
&mut self,
event: Event<ProxyAction>,
elwt: &EventLoopWindowTarget<ProxyAction>,
control_flow: &mut ControlFlow,
) {
use Event::*;
match event {
WindowEvent { window_id, event } => {
if let Some(window) = self.windows.get_mut(&window_id) {
window.handle_event(&mut self.shared, event);
}
}
DeviceEvent { .. } => return, // windows handle local input; we do not handle global input
UserEvent(action) => match action {
ProxyAction::Close(id) => {
if let Some(id) = self.id_map.get(&id) {
if let Some(window) = self.windows.get_mut(&id) {
window.send_action(TkAction::Close);
}
}
}
ProxyAction::CloseAll => {
for window in self.windows.values_mut() {
window.send_action(TkAction::Close);
}
}
ProxyAction::Update(handle, payload) => {
self.shared
.pending
.push(PendingAction::Update(handle, payload));
}
},
NewEvents(cause) => {
// MainEventsCleared will reset control_flow (but not when it is Poll)
*control_flow = ControlFlow::Wait;
match cause {
StartCause::ResumeTimeReached {
requested_resume, ..
} => {
debug!("Wakeup: timer (requested: {:?})", requested_resume);
let item = self
.resumes
.first()
.cloned()
.unwrap_or_else(|| panic!("timer wakeup without resume"));
assert_eq!(item.0, requested_resume);
let resume = if let Some(w) = self.windows.get_mut(&item.1) {
w.update_timer(&mut self.shared)
} else {
// presumably, some window with active timers was removed
None
};
if let Some(instant) = resume {
self.resumes[0].0 = instant;
} else {
self.resumes.remove(0);
}
}
StartCause::WaitCancelled { .. } => {
// This event serves no purpose?
// debug!("Wakeup: WaitCancelled (ignoring)");
}
StartCause::Poll => (),
StartCause::Init => {
debug!("Wakeup: init");
}
}
}
MainEventsCleared => {
let mut close_all = false;
let mut to_close = SmallVec::<[ww::WindowId; 4]>::new();
for (window_id, window) in self.windows.iter_mut() {
let (action, resume) = window.update(&mut self.shared);
match action {
TkAction::None
| TkAction::Redraw
| TkAction::RegionMoved
| TkAction::Popup
| TkAction::Reconfigure => (),
TkAction::Close => to_close.push(*window_id),
TkAction::CloseAll => close_all = true,
}
if let Some(instant) = resume {
if let Some((i, _)) = self
.resumes
.iter()
.enumerate()
.find(|item| (item.1).1 == *window_id)
{
self.resumes[i].0 = instant;
} else {
self.resumes.push((instant, *window_id));
}
}
}
for window_id in &to_close {
if let Some(window) = self.windows.remove(window_id) {
self.id_map.remove(&window.window_id);
if window.handle_closure(&mut self.shared) == TkAction::CloseAll {
close_all = true;
}
// Wake immediately in order to close remaining windows:
*control_flow = ControlFlow::Poll;
}
}
if close_all {
for (_, window) in self.windows.drain() {
let _ = window.handle_closure(&mut self.shared);
}
}
self.resumes.sort_by_key(|item| item.0);
*control_flow = if *control_flow == ControlFlow::Exit || self.windows.is_empty() {
ControlFlow::Exit
} else if *control_flow == ControlFlow::Poll {
ControlFlow::Poll
} else if let Some((instant, _)) = self.resumes.first() {
trace!("Requesting resume at {:?}", *instant);
ControlFlow::WaitUntil(*instant)
} else {
ControlFlow::Wait
};
}
RedrawRequested(id) => {
if let Some(window) = self.windows.get_mut(&id) {
window.do_draw(&mut self.shared);
}
}
RedrawEventsCleared | LoopDestroyed | Suspended | Resumed => return,
};
// Create and init() any new windows.
while let Some(pending) = self.shared.pending.pop() {
match pending {
PendingAction::AddPopup(parent_id, id, popup) => {
debug!("Adding overlay");
// TODO: support pop-ups as a special window, where available
self.windows.get_mut(&parent_id).unwrap().add_popup(
&mut self.shared,
id,
popup,
);
self.id_map.insert(id, parent_id);
}
PendingAction::AddWindow(id, widget) => {
debug!("Adding window {}", widget.title());
match Window::new(&mut self.shared, elwt, id, widget) {
Ok(window) => {
let wid = window.window.id();
self.id_map.insert(id, wid);
self.windows.insert(wid, window);
}
Err(e) => {
error!("Unable to create window: {}", e);
}
};
}
PendingAction::CloseWindow(id) => {
if let Some(wwid) = self.id_map.get(&id) {
if let Some(window) = self.windows.get_mut(&wwid) {
window.send_close(&mut self.shared, id);
}
self.id_map.remove(&id);
}
}
PendingAction::ThemeResize => {
for (_, window) in self.windows.iter_mut() {
window.theme_resize(&self.shared);
}
}
PendingAction::RedrawAll => {
for (_, window) in self.windows.iter_mut() {
window.window.request_redraw();
}
}
PendingAction::Update(handle, payload) => {
for window in self.windows.values_mut() {
window.update_handle(&mut self.shared, handle, payload);
}
}
}
}
}
}