Make `TextEdit::return_key` optional (#4543)

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>).
This commit is contained in:
Doonv 2024-05-27 17:43:48 +03:00 committed by GitHub
parent cd45d18615
commit 1ae2d2803a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 7 deletions

View File

@ -78,7 +78,7 @@ pub struct TextEdit<'t> {
align: Align2, align: Align2,
clip_text: bool, clip_text: bool,
char_limit: usize, char_limit: usize,
return_key: KeyboardShortcut, return_key: Option<KeyboardShortcut>,
} }
impl<'t> WidgetWithState for TextEdit<'t> { impl<'t> WidgetWithState for TextEdit<'t> {
@ -135,7 +135,7 @@ impl<'t> TextEdit<'t> {
align: Align2::LEFT_TOP, align: Align2::LEFT_TOP,
clip_text: false, clip_text: false,
char_limit: usize::MAX, char_limit: usize::MAX,
return_key: KeyboardShortcut::new(Modifiers::NONE, Key::Enter), return_key: Some(KeyboardShortcut::new(Modifiers::NONE, Key::Enter)),
} }
} }
@ -353,9 +353,11 @@ impl<'t> TextEdit<'t> {
/// ///
/// This combination will cause a newline on multiline, /// This combination will cause a newline on multiline,
/// whereas on singleline it will cause the widget to lose focus. /// 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] #[inline]
pub fn return_key(mut self, return_key: KeyboardShortcut) -> Self { pub fn return_key(mut self, return_key: impl Into<Option<KeyboardShortcut>>) -> Self {
self.return_key = return_key; self.return_key = return_key.into();
self self
} }
} }
@ -805,7 +807,7 @@ fn events(
default_cursor_range: CursorRange, default_cursor_range: CursorRange,
char_limit: usize, char_limit: usize,
event_filter: EventFilter, event_filter: EventFilter,
return_key: KeyboardShortcut, return_key: Option<KeyboardShortcut>,
) -> (bool, CursorRange) { ) -> (bool, CursorRange) {
let os = ui.ctx().os(); let os = ui.ctx().os();
@ -892,8 +894,9 @@ fn events(
pressed: true, pressed: true,
modifiers, modifiers,
.. ..
} if *key == return_key.logical_key } if return_key.is_some_and(|return_key| {
&& modifiers.matches_logically(return_key.modifiers) => *key == return_key.logical_key && modifiers.matches_logically(return_key.modifiers)
}) =>
{ {
if multiline { if multiline {
let mut ccursor = text.delete_selected(&cursor_range); let mut ccursor = text.delete_selected(&cursor_range);