Skip to content

Commit

Permalink
Make TextEdit::return_key optional (#4543)
Browse files Browse the repository at this point in the history
I wanted to disable the return key functionality on my `TextEdit`, so
that the `TextEdit` doesn't get unfocused whenever the user submits a
command onto my developer console (which is also bound to <kbd>↵
Enter</kbd>).
  • Loading branch information
doonv authored May 27, 2024
1 parent cd45d18 commit 1ae2d28
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct TextEdit<'t> {
align: Align2,
clip_text: bool,
char_limit: usize,
return_key: KeyboardShortcut,
return_key: Option<KeyboardShortcut>,
}

impl<'t> WidgetWithState for TextEdit<'t> {
Expand Down Expand Up @@ -135,7 +135,7 @@ impl<'t> TextEdit<'t> {
align: Align2::LEFT_TOP,
clip_text: false,
char_limit: usize::MAX,
return_key: KeyboardShortcut::new(Modifiers::NONE, Key::Enter),
return_key: Some(KeyboardShortcut::new(Modifiers::NONE, Key::Enter)),
}
}

Expand Down Expand Up @@ -353,9 +353,11 @@ impl<'t> TextEdit<'t> {
///
/// This combination will cause a newline on multiline,
/// whereas on singleline it will cause the widget to lose focus.
///
/// This combination is optional and can be disabled by passing [`None`] into this function.
#[inline]
pub fn return_key(mut self, return_key: KeyboardShortcut) -> Self {
self.return_key = return_key;
pub fn return_key(mut self, return_key: impl Into<Option<KeyboardShortcut>>) -> Self {
self.return_key = return_key.into();
self
}
}
Expand Down Expand Up @@ -805,7 +807,7 @@ fn events(
default_cursor_range: CursorRange,
char_limit: usize,
event_filter: EventFilter,
return_key: KeyboardShortcut,
return_key: Option<KeyboardShortcut>,
) -> (bool, CursorRange) {
let os = ui.ctx().os();

Expand Down Expand Up @@ -892,8 +894,9 @@ fn events(
pressed: true,
modifiers,
..
} if *key == return_key.logical_key
&& modifiers.matches_logically(return_key.modifiers) =>
} if return_key.is_some_and(|return_key| {
*key == return_key.logical_key && modifiers.matches_logically(return_key.modifiers)
}) =>
{
if multiline {
let mut ccursor = text.delete_selected(&cursor_range);
Expand Down

0 comments on commit 1ae2d28

Please sign in to comment.