Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property for tapping on page to close soft input keyboard #16530

Merged
merged 39 commits into from
Aug 22, 2023

Conversation

PureWeen
Copy link
Member

@PureWeen PureWeen commented Aug 3, 2023

Description of Change

When set to true on the ContentPage the soft input will hide whenever you tap anywhere on the screen. This behavior is most likely counter intuitive and not how typical platform app operates. If you're adding this to your app it will most likely confuse your user. This was the default behavior in Xamarin.Forms so we need a way to toggle this for users migrating.

Issues Fixed

Fixes #12003
Fixes #11703
Fixes #12002

@PureWeen PureWeen marked this pull request as ready for review August 4, 2023 01:17
@PureWeen PureWeen changed the title Add property for tapping on page Add property for tapping on page to close soft input keyboard Aug 4, 2023
@rmarinho
Copy link
Member

rmarinho commented Aug 4, 2023

/rebase

@github-actions github-actions bot force-pushed the add_property_for_tapping_on_page branch from 301d2be to 013b563 Compare August 4, 2023 21:20
@jsuarezruiz jsuarezruiz added the area-keyboard Keyboard, soft keyboard label Aug 7, 2023
@PureWeen PureWeen force-pushed the add_property_for_tapping_on_page branch from 9c60409 to 6539b09 Compare August 8, 2023 19:32
@PureWeen PureWeen requested a review from jknaudt21 August 8, 2023 22:13
jknaudt21
jknaudt21 previously approved these changes Aug 9, 2023
src/Controls/src/Core/ContentPage/ContentPage.cs Outdated Show resolved Hide resolved
@samhouts samhouts added this to the .NET 8 GA milestone Aug 9, 2023
rmarinho pushed a commit that referenced this pull request Aug 21, 2023
Context: https://github.com/jonathanpeppers/memory-analyzers
Context: https://www.nuget.org/packages/MemoryAnalyzers/0.1.0-beta.3

This adds a new Roslyn analyzer that warns about the following cases.

 ## MA0001

Don't define `public` events in `NSObject` subclasses:

```csharp
public class MyView : UIView
{
    // NOPE!
    public event EventHandler MyEvent;
}
```

 ## MA0002

Don't declare members in `NSObject` subclasses unless they are:

* `WeakReference` or `WeakReference<T>`
* Value types

```csharp
class MyView : UIView
{
    // NOPE!
    public UIView? Parent { get; set; }

    public void Add(MyView subview)
    {
        subview.Parent = this;
        AddSubview(subview);
    }
}
```

 ## MA0003

Don't subscribe to events inside `NSObject` subclasses unless:

* It's your event via `this.MyEvent` or from a base type.
* The method is `static`.

```csharp
class MyView : UIView
{
    public MyView()
    {
        var picker = new UIDatePicker();
        AddSubview(picker);
        picker.ValueChanged += OnValueChanged;
    }

    void OnValueChanged(object sender, EventArgs e) { }

    // Use this instead and it doesn't leak!
    //static void OnValueChanged(object sender, EventArgs e) { }
}
```

This is also on NuGet, but I just commited the package until we can get
it added to the `dotnet-public` feed.

Places with PRs in flight are marked with:

    [UnconditionalSuppressMessage("Memory", "MA0002", Justification = "FIXME: #16530")]

A few are marked as "not an issue" with an explanation. Others mention a
test with a proof they are OK.

A few places I could actually *remove* `UnconditionalSuppressMessage`
where I could improve the analyzer to ignore that case.
@samhouts
Copy link
Member

/rebase

samhouts and others added 3 commits August 21, 2023 14:27
# Conflicts:
#	src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt
#	src/Core/src/Platform/iOS/ResignFirstResponderTouchGestureRecognizer.cs
@PureWeen PureWeen merged commit 99253e7 into main Aug 22, 2023
@PureWeen PureWeen deleted the add_property_for_tapping_on_page branch August 22, 2023 18:42
@ghost
Copy link

ghost commented Aug 22, 2023

🚨 API change(s) detected @davidbritch FYI

@BaY1251
Copy link
Contributor

BaY1251 commented Oct 13, 2023

@PureWeen The Entry should have an event or command indicating that the soft keyboard has been turned off.
Simply turning off the soft keyboard does not truly solve the problem.

The key is that after the soft keyboard is turned off, the Entry needs to verify the input data.

Using the Unfocused event in Xamarin But how to implement it in MAUI

@BaY1251
Copy link
Contributor

BaY1251 commented Oct 17, 2023

@PureWeen In Xamarin, Entry did ultimately capture the focus, but after the soft keyboard disappeared, Unfocused was triggered for verification

The point is not the focus, but Entry needs to know that the soft keyboard is turned off in order to verify the data.

The core issue is that Entry needs to know that the keyboard has been turned off.
The handling event in Xamarin happens to be unfocused.
MAUI can have other events

Keyboard
assert

@Bruno2049
Copy link

The feature "HideSoftInputOnTapped" isn't working anymore in the version 8.0.100-rc.2.23502.2 for iOS & Android

Anyone confirm this too?

@davidbritch
Copy link
Contributor

@Bruno2049 In .NET 8 there's a SoftInputExtensions class, in the Microsoft.Maui namespace, that provides methods for showing/hiding the soft input keyboard on a text input control e.g. :

if (entry.IsSoftInputShowing())
    await entry.HideSoftInputAsync(System.Threading.CancellationToken.None);

@Bruno2049
Copy link

@davidbritch Ok, but to use that method I need to listen for the Tap gesture in Page.
Do you know how can I accomplish that?

@davidbritch
Copy link
Contributor

@Bruno2049 I've just tried this on Android and iOS in .NET 8 RC2 with an Entry and it's working fine for me.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ContentPageTest.MainPage"
             HideSoftInputOnTapped="True">
    <VerticalStackLayout Margin="20"
                         Spacing="6">
        <HorizontalStackLayout>
            <Label Text="Toggle HideSoftInputOnTapped"
                   VerticalTextAlignment="Center" />
            <CheckBox x:Name="checkBox"
                      IsChecked="True"
                      CheckedChanged="OnCheckBoxCheckedChanged" />
        </HorizontalStackLayout>
        <Label Text="Tap page and soft input keyboard should close." />
        <Entry x:Name="entry"
               Placeholder="Focus here" />
    </VerticalStackLayout>
</ContentPage>
    void OnCheckBoxCheckedChanged(System.Object sender, Microsoft.Maui.Controls.CheckedChangedEventArgs e)
    {
		HideSoftInputOnTapped = e.Value;
    }

@pulmuone
Copy link

HideSoftInputOnTapped

HideSoftInputOnTapped do not work.

@PureWeen
Copy link
Member Author

PureWeen commented Dec 1, 2023

HideSoftInputOnTapped

HideSoftInputOnTapped do not work.

Log an issue with a repro please

@github-actions github-actions bot locked and limited conversation to collaborators Dec 31, 2023
@samhouts samhouts added the fixed-in-8.0.0-rc.1.9171 Look for this fix in 8.0.0-rc.1.9171 label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
10 participants