Skip to content

Commit ee8fef6

Browse files
authored
Merge pull request #48 from dmackdev/update-editor-demo
Update editor demo
2 parents 4215043 + db6fd05 commit ee8fef6

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

examples/demo/src/apps/editor.rs

Lines changed: 23 additions & 17 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,14 +286,21 @@ impl Editor {
289286
};
290287
}
291288

292-
fn show_text_edit_with_focus(ui: &mut Ui, input: &mut String, request_focus: &mut bool) {
293-
let text_edit_output = TextEdit::singleline(input)
294-
.code_editor()
295-
.margin(Margin::symmetric(2, 0))
296-
.clip_text(false)
297-
.desired_width(0.0)
298-
.min_size(vec2(10.0, 2.0))
299-
.show(ui);
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 {
291+
// Wrap in horizontal to prevent jitters when typing when children are expanded (due to use of horizontal_wrapped when rendering properties).
292+
let text_edit_output = ui
293+
.horizontal(|ui| {
294+
TextEdit::singleline(input)
295+
.code_editor()
296+
.margin(Margin::symmetric(2, 0))
297+
.clip_text(false)
298+
.desired_width(0.0)
299+
.min_size(vec2(10.0, 2.0))
300+
.return_key(None) // Disable return key so we can capture Enter key press for submission.
301+
.show(ui)
302+
})
303+
.inner;
300304

301305
if *request_focus {
302306
*request_focus = false;
@@ -312,6 +316,8 @@ impl Editor {
312316
ui.ctx().memory_mut(|mem| mem.request_focus(text_edit_id));
313317
}
314318
}
319+
320+
text_edit_output.response.has_focus() && ui.input(|i| i.key_pressed(Key::Enter))
315321
}
316322

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

0 commit comments

Comments
 (0)