From 8847e63d4e74968ab7c480526bc197b10c8e74f1 Mon Sep 17 00:00:00 2001 From: Phillip Tennen Date: Wed, 7 Feb 2024 19:33:52 +0000 Subject: [PATCH] [libgui - Rust] TextInputView supports a user-provided key-pressed callback --- rust_programs/libgui/src/text_input_view.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rust_programs/libgui/src/text_input_view.rs b/rust_programs/libgui/src/text_input_view.rs index 71ae6051..63530b13 100644 --- a/rust_programs/libgui/src/text_input_view.rs +++ b/rust_programs/libgui/src/text_input_view.rs @@ -20,6 +20,7 @@ use crate::window_events::KeyCode; pub struct TextInputView { pub view: Rc, is_shift_held: RefCell, + key_pressed_cb: RefCell>>, } impl TextInputView { @@ -39,6 +40,7 @@ impl TextInputView { Rc::new(Self { view, is_shift_held: RefCell::new(false), + key_pressed_cb: RefCell::new(None), }) } @@ -167,6 +169,10 @@ impl TextInputView { } println!(); } + + pub fn set_on_key_pressed(&self, f: F) { + *self.key_pressed_cb.borrow_mut() = Some(Box::new(f)); + } } // TODO(PT): Model keycodes in Rust @@ -258,6 +264,12 @@ impl UIElement for TextInputView { } fn handle_key_pressed(&self, key: KeyCode) { + // First, dispatch the user-provided callback, if set + let callback = self.key_pressed_cb.borrow(); + if let Some(callback) = callback.as_ref() { + callback(self, key); + } + if is_key_shift(key) { *self.is_shift_held.borrow_mut() = true; }