Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose whether Enter was hit in a TextBox #106

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/yakui-widgets/src/widgets/textbox.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cell::RefCell;
use std::f32::INFINITY;
use std::mem;
use std::rc::Rc;

use fontdue::layout::{Layout, LinePosition};
Expand Down Expand Up @@ -53,10 +54,13 @@ pub struct TextBoxWidget {
selected: bool,
cursor: usize,
text_layout: Option<IgnoreDebug<Rc<RefCell<Layout>>>>,
activated: bool,
}

pub struct TextBoxResponse {
pub text: Option<String>,
/// Whether the user pressed "Enter" in this box
pub activated: bool,
}

impl Widget for TextBoxWidget {
Expand All @@ -70,6 +74,7 @@ impl Widget for TextBoxWidget {
selected: false,
cursor: 0,
text_layout: None,
activated: false,
}
}

Expand All @@ -90,6 +95,7 @@ impl Widget for TextBoxWidget {

Self::Response {
text: self.updated_text.take(),
activated: mem::take(&mut self.activated),
}
}

Expand Down Expand Up @@ -212,6 +218,7 @@ impl Widget for TextBoxWidget {
KeyCode::Enter | KeyCode::NumpadEnter => {
if *down {
ctx.input.set_selection(None);
self.activated = true;
}
EventResponse::Sink
}
Expand Down
6 changes: 5 additions & 1 deletion crates/yakui/examples/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
}

Expand Down
Loading