Skip to content

Commit db6fd05

Browse files
committed
support pressing enter key whilst text edit has focus for saving changes
1 parent 687ff73 commit db6fd05

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

examples/demo/src/apps/editor.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::str::FromStr;
22

33
use egui::{
4-
CursorIcon, Margin, TextEdit, Ui,
4+
CursorIcon, Key, Margin, TextEdit, Ui,
55
text::{CCursor, CCursorRange},
66
vec2,
77
};
@@ -69,7 +69,7 @@ impl Editor {
6969
.map(|parent| parent.to_json_pointer_string())
7070
.is_some_and(|object_pointer| object_pointer == state.object_pointer)
7171
{
72-
Self::show_text_edit_with_focus(
72+
let enter_was_pressed_with_focus = Self::show_text_edit(
7373
ui,
7474
&mut state.new_key_input,
7575
&mut state.request_focus,
@@ -84,7 +84,7 @@ impl Editor {
8484
.is_some_and(|obj| !obj.contains_key(&state.new_key_input));
8585

8686
ui.add_enabled_ui(valid_key, |ui| {
87-
if ui.small_button("✅").clicked() {
87+
if ui.small_button("✅").clicked() || enter_was_pressed_with_focus {
8888
edit_events.push(EditEvent::SaveObjectKeyEdit);
8989
}
9090
});
@@ -115,15 +115,12 @@ impl Editor {
115115
) {
116116
if let RenderContext::BaseValue(context) = &context {
117117
if state.pointer == context.pointer.to_json_pointer_string() {
118-
Self::show_text_edit_with_focus(
119-
ui,
120-
&mut state.new_value_input,
121-
&mut state.request_focus,
122-
);
118+
let enter_was_pressed_with_focus =
119+
Self::show_text_edit(ui, &mut state.new_value_input, &mut state.request_focus);
123120

124121
ui.add_space(5.0);
125122

126-
if ui.small_button("✅").clicked() {
123+
if ui.small_button("✅").clicked() || enter_was_pressed_with_focus {
127124
edit_events.push(EditEvent::SaveValueEdit);
128125
}
129126

@@ -289,7 +286,8 @@ impl Editor {
289286
};
290287
}
291288

292-
fn show_text_edit_with_focus(ui: &mut Ui, input: &mut String, request_focus: &mut bool) {
289+
/// Returns `bool` indicating whether the Enter key was pressed whilst the text edit had focus.
290+
fn show_text_edit(ui: &mut Ui, input: &mut String, request_focus: &mut bool) -> bool {
293291
// Wrap in horizontal to prevent jitters when typing when children are expanded (due to use of horizontal_wrapped when rendering properties).
294292
let text_edit_output = ui
295293
.horizontal(|ui| {
@@ -299,6 +297,7 @@ impl Editor {
299297
.clip_text(false)
300298
.desired_width(0.0)
301299
.min_size(vec2(10.0, 2.0))
300+
.return_key(None) // Disable return key so we can capture Enter key press for submission.
302301
.show(ui)
303302
})
304303
.inner;
@@ -317,6 +316,8 @@ impl Editor {
317316
ui.ctx().memory_mut(|mem| mem.request_focus(text_edit_id));
318317
}
319318
}
319+
320+
text_edit_output.response.has_focus() && ui.input(|i| i.key_pressed(Key::Enter))
320321
}
321322

322323
fn apply_events(&mut self, document: &mut Value) {

0 commit comments

Comments
 (0)