Quit functionality

This commit is contained in:
ClementTsang 2021-12-24 16:52:12 -05:00
parent b304db6a2f
commit 56a9856796
2 changed files with 27 additions and 2 deletions

View File

@ -203,6 +203,10 @@ impl AppState {
// TODO: Redraw
}
}
fn quit(&mut self) {
self.terminator.store(true, SeqCst);
}
}
impl Application for AppState {
@ -264,9 +268,27 @@ impl Application for AppState {
&mut self, event: crate::tuine::Event, _messages: &mut Vec<Self::Message>,
) {
use crate::tuine::Event;
use crossterm::event::{KeyCode, KeyModifiers};
match event {
Event::Keyboard(_) => {}
Event::Mouse(_) => {}
Event::Keyboard(event) => {
if event.modifiers.is_empty() {
match event.code {
KeyCode::Char('q') | KeyCode::Char('Q') => {
self.quit();
}
_ => {}
}
} else if let KeyModifiers::CONTROL = event.modifiers {
match event.code {
KeyCode::Char('c') | KeyCode::Char('C') => {
self.quit();
}
_ => {}
}
}
}
Event::Mouse(event) => {}
}
}
}

View File

@ -73,6 +73,7 @@ where
Ok(())
}
/// Handles a [`Event`].
fn on_event<A>(
application: &mut A, user_interface: &mut Element<'_, A::Message>, app_data: &mut AppData,
layout: &mut LayoutNode, event: Event,
@ -98,6 +99,7 @@ fn on_event<A>(
}
}
/// Creates a new [`Element`] representing the root of the user interface.
fn new_user_interface<A>(
application: &mut A, app_data: &mut AppData,
) -> Element<'static, A::Message>
@ -108,6 +110,7 @@ where
application.view(&mut ctx)
}
/// Updates the layout, and draws the given user interface.
fn draw<M, B>(
user_interface: &mut Element<'_, M>, terminal: &mut Terminal<B>, app_data: &mut AppData,
layout: &mut LayoutNode,