-
Notifications
You must be signed in to change notification settings - Fork 492
Add SoftInputKeyboardPopup and integrate keyboard handling for popups #3117
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
Open
FabriBertani
wants to merge
3
commits into
CommunityToolkit:main
Choose a base branch
from
FabriBertani:bug/keybpard_overlapping_on_popup-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
d8323fb
Add SoftInputKeyboardPopup and integrate keyboard handling for popups
FabriBertani 512d9c9
Implement keyboard handling for popups on iOS and Android
FabriBertani 8beb2d2
Add debug logging for view controller retrieval failure in iOS popup …
FabriBertani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
samples/CommunityToolkit.Maui.Sample/Views/Popups/SoftInputKeyboardPopup.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <mct:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" | ||
| x:Class="CommunityToolkit.Maui.Sample.Views.Popups.SoftInputKeyboardPopup" | ||
| CanBeDismissedByTappingOutsideOfPopup="True"> | ||
|
|
||
| <VerticalStackLayout Spacing="12"> | ||
|
|
||
| <Label | ||
| Text="Keyboard for Entry" | ||
| FontSize="18" | ||
| HorizontalTextAlignment="Center" | ||
| VerticalTextAlignment="Center" | ||
| VerticalOptions="Center" | ||
| HorizontalOptions="Center" | ||
| FontAttributes="Bold" /> | ||
|
|
||
| <Entry | ||
| Placeholder="Click here to enter text..." | ||
| TextColor="Black" | ||
| HorizontalTextAlignment="Start" | ||
| VerticalTextAlignment="Center" | ||
| VerticalOptions="Center" | ||
| HorizontalOptions="Center" /> | ||
|
|
||
| <Label | ||
| x:Name="PickerForIOSLabel" | ||
| Text="Keyboard for Picker (iOS only)" | ||
| FontSize="18" | ||
| HorizontalTextAlignment="Center" | ||
| VerticalTextAlignment="Center" | ||
| VerticalOptions="Center" | ||
| HorizontalOptions="Center" | ||
| FontAttributes="Bold" | ||
| IsVisible="False" /> | ||
|
|
||
| <Picker | ||
| x:Name="PickerForIOS" | ||
| Title="Select a monkey" | ||
| HorizontalTextAlignment="Center" | ||
| VerticalTextAlignment="Center" | ||
| VerticalOptions="Center" | ||
| HorizontalOptions="Center" | ||
| IsVisible="False"> | ||
| <Picker.ItemsSource> | ||
| <x:Array Type="{x:Type x:String}"> | ||
| <x:String>Baboon</x:String> | ||
| <x:String>Capuchin Monkey</x:String> | ||
| <x:String>Blue Monkey</x:String> | ||
| <x:String>Squirrel Monkey</x:String> | ||
| <x:String>Golden Lion Tamarin</x:String> | ||
| <x:String>Howler Monkey</x:String> | ||
| <x:String>Japanese Macaque</x:String> | ||
| </x:Array> | ||
| </Picker.ItemsSource> | ||
| </Picker> | ||
|
|
||
| <Label | ||
| x:Name="DatePickerForIOSLabel" | ||
| Text="Keyboard for DatePicker (iOS only)" | ||
| FontSize="18" | ||
| HorizontalTextAlignment="Center" | ||
| VerticalTextAlignment="Center" | ||
| VerticalOptions="Center" | ||
| HorizontalOptions="Center" | ||
| FontAttributes="Bold" | ||
| IsVisible="False" /> | ||
|
|
||
| <DatePicker | ||
| x:Name="DatePickerForIOS" | ||
| MinimumDate="01/01/2020" | ||
| MaximumDate="12/31/2030" | ||
| Date="02/23/2026" | ||
| VerticalOptions="Center" | ||
| HorizontalOptions="Center" | ||
| IsVisible="False" /> | ||
|
|
||
| <Button | ||
| Text="Close" | ||
| HorizontalOptions="Center" | ||
| VerticalOptions="Center" | ||
| Clicked="ClosePopup" /> | ||
|
|
||
| </VerticalStackLayout> | ||
| </mct:Popup> |
23 changes: 23 additions & 0 deletions
23
samples/CommunityToolkit.Maui.Sample/Views/Popups/SoftInputKeyboardPopup.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using CommunityToolkit.Maui.Views; | ||
|
|
||
| namespace CommunityToolkit.Maui.Sample.Views.Popups; | ||
|
|
||
| public partial class SoftInputKeyboardPopup : Popup | ||
| { | ||
| public SoftInputKeyboardPopup() | ||
| { | ||
| InitializeComponent(); | ||
|
|
||
| #if IOS | ||
| PickerForIOSLabel.IsVisible = true; | ||
| PickerForIOS.IsVisible = true; | ||
| DatePickerForIOSLabel.IsVisible = true; | ||
| DatePickerForIOS.IsVisible = true; | ||
| #endif | ||
| } | ||
|
|
||
| async void ClosePopup(object? sender, EventArgs eventArgs) | ||
| { | ||
| await CloseAsync(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific; | ||
|
|
||
| namespace CommunityToolkit.Maui.Views; | ||
|
|
||
| public partial class Popup | ||
| { | ||
| partial void OnPlatformPopupOpened() | ||
| { | ||
| Microsoft.Maui.Controls.Application.Current?.On<Microsoft.Maui.Controls.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize); | ||
| } | ||
|
|
||
| partial void OnPlatformPopupClosed() | ||
| { | ||
| Microsoft.Maui.Controls.Application.Current?.On<Microsoft.Maui.Controls.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Unspecified); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| using System.Diagnostics; | ||
| using Foundation; | ||
| using UIKit; | ||
|
|
||
| namespace CommunityToolkit.Maui.Views; | ||
|
|
||
| public partial class Popup | ||
| { | ||
| /// <summary> | ||
| /// Stores the keyboard will show notification observer to manage keyboard lifecycle. | ||
| /// </summary> | ||
| NSObject? willShow; | ||
|
|
||
| /// <summary> | ||
| /// Stores the keyboard will hide notification observer to manage keyboard lifecycle. | ||
| /// </summary> | ||
| NSObject? willHide; | ||
|
|
||
| /// <summary> | ||
| /// Stores the native platform view to adjust safe area insets when keyboard appears. | ||
| /// </summary> | ||
| UIView? popupNativeView; | ||
|
|
||
| /// <summary> | ||
| /// Stores the view controller associated with the popup to adjust safe area insets. | ||
| /// </summary> | ||
| UIViewController? viewController; | ||
|
|
||
| partial void OnPlatformPopupOpened() | ||
| { | ||
| if (Handler?.PlatformView is UIView view) | ||
| { | ||
| popupNativeView = view; | ||
| } | ||
|
|
||
| willShow = UIKeyboard.Notifications.ObserveWillShow((_, args) => HandleKeyboard(args)); | ||
|
|
||
| willHide = UIKeyboard.Notifications.ObserveWillHide((_, args) => ResetSafeArea()); | ||
| } | ||
|
|
||
| partial void OnPlatformPopupClosed() | ||
| { | ||
| willShow?.Dispose(); | ||
| willHide?.Dispose(); | ||
|
|
||
| willShow = willHide = null; | ||
|
|
||
| popupNativeView = null; | ||
| viewController = null; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adjusts the safe area insets when the keyboard appears on iOS. | ||
| /// </summary> | ||
| /// <param name="args">The keyboard event arguments containing the keyboard frame.</param> | ||
| void HandleKeyboard(UIKeyboardEventArgs args) | ||
| { | ||
| if (popupNativeView is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| viewController ??= popupNativeView.Window?.RootViewController?.PresentedViewController; | ||
|
|
||
| if (viewController is null) | ||
| { | ||
| Trace.WriteLine("Failed to retrieve the presented view controller."); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| viewController.AdditionalSafeAreaInsets = new UIEdgeInsets(0, 0, args.FrameEnd.Height, 0); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Resets the safe area insets when the keyboard is hidden on iOS. | ||
| /// </summary> | ||
| void ResetSafeArea() | ||
| { | ||
| viewController?.AdditionalSafeAreaInsets = UIEdgeInsets.Zero; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.