From 0eeeed1a273fff1592cdda9a8624685f1befb5cc Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 29 Sep 2024 22:58:50 +0200 Subject: [PATCH] Added TextField.OnEnterPressed event --- CHANGELOG.md | 1 + MLEM.Ui/Elements/TextField.cs | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cab477..6b05068 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ Fixes ### MLEM.Ui Additions - Added Panel.IsVisible method to check if a child element is visible +- Added TextField.OnEnterPressed event ## 7.1.1 diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 40600aa..d2ec003 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -142,6 +142,13 @@ namespace MLEM.Ui.Elements { /// public string MobileDescription; /// + /// An event that is invoked if is pressed while this text field is active. + /// Note that, for text fields that are , this is ignored. + /// This also occurs once the text input window is successfully closed on a mobile device. + /// If another 's press behavior should be invoked when enter is pressed, can be used instead. + /// + public GenericCallback OnEnterPressed; + /// /// An element that should be pressed (using ) if is pressed while this text field is active. /// Note that, for text fields that are , this is ignored. /// This also occurs once the text input window is successfully closed on a mobile device. @@ -198,12 +205,12 @@ namespace MLEM.Ui.Elements { var result = await MlemPlatform.Current.OpenOnScreenKeyboard(title, this.MobileDescription, this.Text, false); if (result != null) { this.SetText(this.Multiline ? result : result.Replace('\n', ' '), true); - this.EnterReceiver?.Controls?.PressElement(this.EnterReceiver); + this.InvokeOnEnter(); } }; this.OnTextInput += (element, key, character) => { if (this.IsSelectedActive && !this.IsHidden && !this.textInput.OnTextInput(key, character) && key == Keys.Enter && !this.Multiline) - this.EnterReceiver?.Controls?.PressElement(this.EnterReceiver); + this.InvokeOnEnter(); }; } @@ -221,8 +228,8 @@ namespace MLEM.Ui.Elements { this.textInput.Update(time, this.Input); #if FNA // this occurs in OnTextInput outside FNA, where special keys are also counted as text input - if (this.EnterReceiver != null && !this.Multiline && this.Input.TryConsumePressed(Keys.Enter)) - this.EnterReceiver.Controls?.PressElement(this.EnterReceiver); + if ((this.OnEnterPressed != null || this.EnterReceiver != null) && !this.Multiline && this.Input.TryConsumePressed(Keys.Enter)) + this.InvokeOnEnter(); #endif } } @@ -276,6 +283,11 @@ namespace MLEM.Ui.Elements { this.CaretWidth = this.CaretWidth.OrStyle(style.TextFieldCaretWidth); } + private void InvokeOnEnter() { + this.OnEnterPressed?.Invoke(this); + this.EnterReceiver?.Controls?.PressElement(this.EnterReceiver); + } + /// /// A delegate method used for ///