1
1
use std:: str:: FromStr ;
2
2
3
3
use egui:: {
4
- CursorIcon , Margin , TextEdit , Ui ,
4
+ CursorIcon , Key , Margin , TextEdit , Ui ,
5
5
text:: { CCursor , CCursorRange } ,
6
6
vec2,
7
7
} ;
@@ -69,7 +69,7 @@ impl Editor {
69
69
. map ( |parent| parent. to_json_pointer_string ( ) )
70
70
. is_some_and ( |object_pointer| object_pointer == state. object_pointer )
71
71
{
72
- Self :: show_text_edit_with_focus (
72
+ let enter_was_pressed_with_focus = Self :: show_text_edit (
73
73
ui,
74
74
& mut state. new_key_input ,
75
75
& mut state. request_focus ,
@@ -84,7 +84,7 @@ impl Editor {
84
84
. is_some_and ( |obj| !obj. contains_key ( & state. new_key_input ) ) ;
85
85
86
86
ui. add_enabled_ui ( valid_key, |ui| {
87
- if ui. small_button ( "✅" ) . clicked ( ) {
87
+ if ui. small_button ( "✅" ) . clicked ( ) || enter_was_pressed_with_focus {
88
88
edit_events. push ( EditEvent :: SaveObjectKeyEdit ) ;
89
89
}
90
90
} ) ;
@@ -115,15 +115,12 @@ impl Editor {
115
115
) {
116
116
if let RenderContext :: BaseValue ( context) = & context {
117
117
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 ) ;
123
120
124
121
ui. add_space ( 5.0 ) ;
125
122
126
- if ui. small_button ( "✅" ) . clicked ( ) {
123
+ if ui. small_button ( "✅" ) . clicked ( ) || enter_was_pressed_with_focus {
127
124
edit_events. push ( EditEvent :: SaveValueEdit ) ;
128
125
}
129
126
@@ -289,14 +286,21 @@ impl Editor {
289
286
} ;
290
287
}
291
288
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 ;
300
304
301
305
if * request_focus {
302
306
* request_focus = false ;
@@ -312,6 +316,8 @@ impl Editor {
312
316
ui. ctx ( ) . memory_mut ( |mem| mem. request_focus ( text_edit_id) ) ;
313
317
}
314
318
}
319
+
320
+ text_edit_output. response . has_focus ( ) && ui. input ( |i| i. key_pressed ( Key :: Enter ) )
315
321
}
316
322
317
323
fn apply_events ( & mut self , document : & mut Value ) {
0 commit comments