Skip to content

Commit

Permalink
improve input field events
Browse files Browse the repository at this point in the history
  • Loading branch information
Grokmoo committed Feb 27, 2024
1 parent 4f99c46 commit 8fd48dd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub fn build_ui(ui: &mut Frame, party: &mut Party) {
ui.scrollpane("pane", "character_content", |ui| {
ui.start("name_panel")
.children(|ui| {
if ui.input_field("name_input", "name_input", None).is_some() {
if ui.input_field("name_input", "name_input", None).keyboard.is_some() {
character.name = ui.text_for("name_input").unwrap_or_default();
}
});
Expand Down
3 changes: 3 additions & 0 deletions src/key_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub enum KeyEvent {
/// The escape key
Escape,

/// The tab key
Tab,

/// Function key 1
F1,

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ pub use context::{Context, PersistentState, InputModifiers, SavedContext};
pub use scrollpane::{ScrollpaneBuilder, ShowElement};
pub use theme_definition::{AnimStateKey, AnimState, Align, Color, Layout, WidthRelative, HeightRelative};
pub use window::WindowBuilder;
pub use recipes::InputFieldResult;
pub use recipes::{InputFieldResult, InputFieldKeyboard};
pub use winit_io::WinitIo;

pub use render::{IO, Renderer};
Expand Down
29 changes: 22 additions & 7 deletions src/recipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,11 @@ impl Frame {
}
```
*/
pub fn input_field(&mut self, theme: &str, id: &str, initial_value: Option<String>) -> Option<InputFieldResult> {
let mut output = None;
pub fn input_field(&mut self, theme: &str, id: &str, initial_value: Option<String>) -> InputFieldResult {
let mut output = InputFieldResult {
cursor: Point::default(),
keyboard: None,
};

self.modify(id, |state| {
let text = match state.text.as_mut() {
Expand All @@ -478,15 +481,15 @@ impl Frame {
'\x08' => { text.pop(); }, // backspace
'\r' => {}, // do nothing on enter, user will receive this as a key event as well
_ => {
output = Some(InputFieldResult::Char(c));
output.keyboard = Some(InputFieldKeyboard::Char(c));
text.push(c);
},
}
}

if output.is_none() {
if output.keyboard.is_none() {
if let Some(e) = state.key_events.pop() {
output = Some(InputFieldResult::KeyEvent(e));
output.keyboard = Some(InputFieldKeyboard::KeyEvent(e));
}
}
});
Expand All @@ -501,6 +504,8 @@ impl Frame {
}
});

output.cursor = text_pos;

if result.clicked {
self.focus_keyboard(id);
}
Expand Down Expand Up @@ -613,9 +618,19 @@ impl Frame {
}
}

/// A single frame of input that has been passed to an input field
/// Result struct returned from the creation of an input field
#[derive(Debug)]
pub struct InputFieldResult {
/// The current text cursor position for this input field
pub cursor: Point,

/// Any keyboard input for this input field this frame
pub keyboard: Option<InputFieldKeyboard>,
}

/// A single frame of keyboard input that has been passed to an input field
#[derive(Debug)]
pub enum InputFieldResult {
pub enum InputFieldKeyboard {
/// A keyboard character input
Char(char),

Expand Down
1 change: 1 addition & 0 deletions src/winit_io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ fn key_event(input: Option<VirtualKeyCode>) -> Option<KeyEvent> {
Return => KeyEvent::Return,
Space => KeyEvent::Space,
Escape => KeyEvent::Escape,
Tab => KeyEvent::Tab,
F1 => KeyEvent::F1,
F2 => KeyEvent::F2,
F3 => KeyEvent::F3,
Expand Down

0 comments on commit 8fd48dd

Please sign in to comment.