Skip to content

Commit

Permalink
Merge pull request #212 from boxofrox/fix/textbox-input
Browse files Browse the repository at this point in the history
Fix input for TextBox and TextEdit widgets.
  • Loading branch information
sebcrozet authored Oct 26, 2020
2 parents c9d7214 + bbf9a20 commit 05d3c91
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
43 changes: 40 additions & 3 deletions examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ widget_ids! {
dialer_title,
number_dialer,
plot_path,
// TextBox and TextEdit
text_box,
text_edit,
// Scrollbar
canvas_scrollbar,
}
Expand All @@ -125,6 +128,8 @@ pub struct DemoApp {
ball_color: conrod::Color,
sine_frequency: f32,
cat: conrod::image::Id,
text_box: String,
text_edit: String,
}

#[cfg(feature = "conrod")]
Expand All @@ -136,14 +141,16 @@ impl DemoApp {
ball_color: conrod::color::WHITE,
sine_frequency: 1.0,
cat,
text_box: "Hello".to_string(),
text_edit: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\nUt enim ad minim veniam...".to_string(),
}
}
}

/// Instantiate a GUI demonstrating every widget available in conrod.
#[cfg(feature = "conrod")]
pub fn gui(ui: &mut conrod::UiCell, ids: &Ids, app: &mut DemoApp) {
use conrod::{widget, Colorable, Labelable, Positionable, Sizeable, Widget};
use conrod::{widget, Colorable, Labelable, Sizeable, Widget};
use std::iter::once;

const MARGIN: conrod::Scalar = 30.0;
Expand All @@ -157,8 +164,8 @@ pub fn gui(ui: &mut conrod::UiCell, ids: &Ids, app: &mut DemoApp) {
const TITLE: &'static str = "All Widgets";
widget::Canvas::new()
.pad(MARGIN)
.align_bottom()
.h(300.0)
.align_right()
.w(600.0)
.scroll_kids_vertically()
.set(ids.canvas, ui);

Expand Down Expand Up @@ -387,6 +394,36 @@ pub fn gui(ui: &mut conrod::UiCell, ids: &Ids, app: &mut DemoApp) {
.align_middle_x_of(ids.canvas)
.set(ids.plot_path, ui);

////////////////////////////////
///// TextBox and TextEdit /////
////////////////////////////////

for event in widget::TextBox::new(&app.text_box)
.down_from(ids.plot_path, 60.0)
.align_middle_x_of(ids.canvas)
.padded_w_of(ids.canvas, MARGIN)
.h(40.0)
.set(ids.text_box, ui)
{
use conrod::widget::text_box::Event;
match event {
Event::Enter => {}
Event::Update(s) => {
app.text_box = s;
}
}
}

for string in widget::TextEdit::new(&app.text_edit)
.down_from(ids.text_box, 60.0)
.align_middle_x_of(ids.canvas)
.padded_w_of(ids.canvas, MARGIN)
.h(100.0)
.set(ids.text_edit, ui)
{
app.text_edit = string;
}

/////////////////////
///// Scrollbar /////
/////////////////////
Expand Down
3 changes: 3 additions & 0 deletions src/window/gl_canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ impl AbstractCanvas for GLCanvas {
key_states[key as usize] = action;
let _ = out_events.send(WindowEvent::Key(key, action, modifiers));
}
glutin::WindowEvent::ReceivedCharacter(c) => {
let _ = out_events.send(WindowEvent::Char(c));
}
_ => {}
},
_ => {}
Expand Down
13 changes: 13 additions & 0 deletions src/window/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ impl Window {
Key::Up => CKey::Up,
Key::Right => CKey::Right,
Key::Down => CKey::Down,
Key::Back => CKey::Backspace,
Key::Return => CKey::Return,
Key::Space => CKey::Space,
Key::Caret => CKey::Caret,
Expand Down Expand Up @@ -839,6 +840,18 @@ impl Window {
Action::Release => Some(Input::Release(Button::Keyboard(key))),
}
}
WindowEvent::Char(ch) => {
// Shamelessly taken from kiss3d_conrod/backends/conrod_winit/src/macros.rs:175.
let string = match ch {
// Ignore control characters and return ascii for Text event (like sdl2).
'\u{7f}' | // Delete
'\u{1b}' | // Escape
'\u{8}' | // Backspace
'\r' | '\n' | '\t' => "".to_string(),
_ => ch.to_string()
};
Some(Input::Text(string))
}
_ => None,
}
}
Expand Down

0 comments on commit 05d3c91

Please sign in to comment.