diff --git a/crates/yakui-widgets/src/widgets/textbox.rs b/crates/yakui-widgets/src/widgets/textbox.rs index d317f295..837da0d7 100644 --- a/crates/yakui-widgets/src/widgets/textbox.rs +++ b/crates/yakui-widgets/src/widgets/textbox.rs @@ -1,5 +1,6 @@ use std::cell::RefCell; use std::f32::INFINITY; +use std::mem; use std::rc::Rc; use fontdue::layout::{Layout, LinePosition}; @@ -53,10 +54,13 @@ pub struct TextBoxWidget { selected: bool, cursor: usize, text_layout: Option>>>, + activated: bool, } pub struct TextBoxResponse { pub text: Option, + /// Whether the user pressed "Enter" in this box + pub activated: bool, } impl Widget for TextBoxWidget { @@ -70,6 +74,7 @@ impl Widget for TextBoxWidget { selected: false, cursor: 0, text_layout: None, + activated: false, } } @@ -90,6 +95,7 @@ impl Widget for TextBoxWidget { Self::Response { text: self.updated_text.take(), + activated: mem::take(&mut self.activated), } } @@ -212,6 +218,7 @@ impl Widget for TextBoxWidget { KeyCode::Enter | KeyCode::NumpadEnter => { if *down { ctx.input.set_selection(None); + self.activated = true; } EventResponse::Sink } diff --git a/crates/yakui/examples/textbox.rs b/crates/yakui/examples/textbox.rs index a38b9436..a388d54b 100644 --- a/crates/yakui/examples/textbox.rs +++ b/crates/yakui/examples/textbox.rs @@ -9,9 +9,13 @@ pub fn run() { my_box.style.font_size = 60.0; my_box.padding = Pad::all(50.0); - if let Some(new_text) = my_box.show().into_inner().text { + let response = my_box.show().into_inner(); + if let Some(new_text) = response.text { text.set(new_text); } + if response.activated { + println!("{}", text.borrow()); + } }); }