From 56a9856796fea557f722e5b1cd40539c10f235f2 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Fri, 24 Dec 2021 16:52:12 -0500 Subject: [PATCH] Quit functionality --- src/app.rs | 26 ++++++++++++++++++++++++-- src/tuine/runtime.rs | 3 +++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/app.rs b/src/app.rs index 8ec79085..ab328452 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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, ) { 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) => {} } } } diff --git a/src/tuine/runtime.rs b/src/tuine/runtime.rs index 452f8adc..6d74051c 100644 --- a/src/tuine/runtime.rs +++ b/src/tuine/runtime.rs @@ -73,6 +73,7 @@ where Ok(()) } +/// Handles a [`Event`]. fn on_event( 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( } } +/// Creates a new [`Element`] representing the root of the user interface. fn new_user_interface( 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( user_interface: &mut Element<'_, M>, terminal: &mut Terminal, app_data: &mut AppData, layout: &mut LayoutNode,