Add manual-loop methods

is_running() and step()
This commit is contained in:
Alexandre Bury 2016-12-15 20:24:48 +01:00
parent 858067ef6b
commit 2bacfcb11e

View File

@ -518,56 +518,77 @@ impl Cursive {
} }
/// Returns `true` until [`quit(&mut self)`] is called.
///
/// [`quit(&mut self)`]: #method.quit
pub fn is_running(&self) -> bool {
self.running
}
/// Runs the event loop. /// Runs the event loop.
/// ///
/// It will wait for user input (key presses) /// It will wait for user input (key presses)
/// and trigger callbacks accordingly. /// and trigger callbacks accordingly.
/// ///
/// Blocks until `quit()` is called. /// Calls [`step(&mut self)`] until [`quit(&mut self)`] is called.
///
/// [`step(&mut self)`]: #method.step
/// [`quit(&mut self)`]: #method.quit
pub fn run(&mut self) { pub fn run(&mut self) {
// And the big event loop begins! // And the big event loop begins!
while self.running { while self.running {
if let Ok(cb) = self.cb_source.try_recv() { self.step();
cb(self); }
} }
// Do we need to redraw everytime? /// Performs a single step from the event loop.
// Probably, actually. ///
// TODO: Do we need to re-layout everytime? /// Useful if you need tighter control on the event loop.
self.layout(); /// Otherwise, [`run(&mut self)`] might be more convenient.
///
/// [`run(&mut self)`]: #method.run
pub fn step(&mut self) {
if let Ok(cb) = self.cb_source.try_recv() {
cb(self);
}
// TODO: Do we need to redraw every view every time? // Do we need to redraw everytime?
// (Is this getting repetitive? :p) // Probably, actually.
self.draw(); // TODO: Do we need to re-layout everytime?
self.backend.refresh(); self.layout();
// Wait for next event. // TODO: Do we need to redraw every view every time?
// (If set_fps was called, this returns -1 now and then) // (Is this getting repetitive? :p)
let event = self.backend.poll_event(); self.draw();
if event == Event::Exit { self.backend.refresh();
self.quit();
}
if event == Event::WindowResize { // Wait for next event.
self.backend.clear(); // (If set_fps was called, this returns -1 now and then)
} let event = self.backend.poll_event();
if event == Event::Exit {
self.quit();
}
// Event dispatch order: if event == Event::WindowResize {
// * Focused element: self.backend.clear();
// * Menubar (if active) }
// * Current screen (top layer)
// * Global callbacks // Event dispatch order:
if self.menubar.receive_events() { // * Focused element:
self.menubar.on_event(event).process(self); // * Menubar (if active)
} else { // * Current screen (top layer)
match self.screen_mut().on_event(event) { // * Global callbacks
// If the event was ignored, if self.menubar.receive_events() {
// it is our turn to play with it. self.menubar.on_event(event).process(self);
EventResult::Ignored => self.on_event(event), } else {
EventResult::Consumed(None) => (), match self.screen_mut().on_event(event) {
EventResult::Consumed(Some(cb)) => cb(self), // If the event was ignored,
} // it is our turn to play with it.
EventResult::Ignored => self.on_event(event),
EventResult::Consumed(None) => (),
EventResult::Consumed(Some(cb)) => cb(self),
} }
} }
} }