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

[Improvement] Add ReturnType API for SearchBar #24826

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
Microsoft.Maui.Controls.SearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
Microsoft.Maui.Controls.SearchBar.ReturnType.set -> void
~static readonly Microsoft.Maui.Controls.SearchBar.ReturnTypeProperty -> Microsoft.Maui.Controls.BindableProperty
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#nullable enable
override Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.MovedToWindow() -> void
Microsoft.Maui.Controls.SearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
Microsoft.Maui.Controls.SearchBar.ReturnType.set -> void
~static readonly Microsoft.Maui.Controls.SearchBar.ReturnTypeProperty -> Microsoft.Maui.Controls.BindableProperty
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#nullable enable
override Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.MovedToWindow() -> void
override Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.MovedToWindow() -> void
Microsoft.Maui.Controls.SearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
Microsoft.Maui.Controls.SearchBar.ReturnType.set -> void
~static readonly Microsoft.Maui.Controls.SearchBar.ReturnTypeProperty -> Microsoft.Maui.Controls.BindableProperty
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
Microsoft.Maui.Controls.SearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
Microsoft.Maui.Controls.SearchBar.ReturnType.set -> void
~static readonly Microsoft.Maui.Controls.SearchBar.ReturnTypeProperty -> Microsoft.Maui.Controls.BindableProperty
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
Microsoft.Maui.Controls.SearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
Microsoft.Maui.Controls.SearchBar.ReturnType.set -> void
~static readonly Microsoft.Maui.Controls.SearchBar.ReturnTypeProperty -> Microsoft.Maui.Controls.BindableProperty
3 changes: 3 additions & 0 deletions src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
Microsoft.Maui.Controls.SearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
Microsoft.Maui.Controls.SearchBar.ReturnType.set -> void
~static readonly Microsoft.Maui.Controls.SearchBar.ReturnTypeProperty -> Microsoft.Maui.Controls.BindableProperty
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
Microsoft.Maui.Controls.SearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
Microsoft.Maui.Controls.SearchBar.ReturnType.set -> void
~static readonly Microsoft.Maui.Controls.SearchBar.ReturnTypeProperty -> Microsoft.Maui.Controls.BindableProperty
11 changes: 11 additions & 0 deletions src/Controls/src/Core/SearchBar/SearchBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace Microsoft.Maui.Controls
/// <include file="../../docs/Microsoft.Maui.Controls/SearchBar.xml" path="Type[@FullName='Microsoft.Maui.Controls.SearchBar']/Docs/*" />
public partial class SearchBar : InputView, ITextAlignmentElement, ISearchBarController, IElementConfiguration<SearchBar>, ICommandElement, ISearchBar
{
/// <summary>Bindable property for <see cref="ReturnType"/>.</summary>
public static readonly BindableProperty ReturnTypeProperty = BindableProperty.Create(
nameof(ReturnType), typeof(ReturnType), typeof(SearchBar), ReturnType.Search);

/// <summary>Bindable property for <see cref="SearchCommand"/>.</summary>
public static readonly BindableProperty SearchCommandProperty = BindableProperty.Create(
nameof(SearchCommand), typeof(ICommand), typeof(SearchBar), null,
Expand Down Expand Up @@ -69,6 +73,13 @@ public partial class SearchBar : InputView, ITextAlignmentElement, ISearchBarCon

readonly Lazy<PlatformConfigurationRegistry<SearchBar>> _platformConfigurationRegistry;

/// <include file="../../docs/Microsoft.Maui.Controls/SearchBar.xml" path="//Member[@MemberName='ReturnType']/Docs/*" />
public ReturnType ReturnType
{
get => (ReturnType)GetValue(ReturnTypeProperty);
set => SetValue(ReturnTypeProperty, value);
}

/// <include file="../../docs/Microsoft.Maui.Controls/SearchBar.xml" path="//Member[@MemberName='CancelButtonColor']/Docs/*" />
public Color CancelButtonColor
{
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Core/ISearchBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public interface ISearchBar : IView, ITextInput, ITextAlignment
/// </summary>
Color CancelButtonColor { get; }

/// <summary>
/// Gets an enumeration value that controls the appearance of the return button.
/// </summary>
ReturnType ReturnType { get; }

/// <summary>
/// Notify when the user presses the Search button.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public static void MapFocus(ISearchBarHandler handler, ISearchBar searchBar, obj
handler.QueryEditor?.Focus(request);
}

public static void MapReturnType(ISearchBarHandler handler, ISearchBar entry)
{
handler.PlatformView?.UpdateReturnType(entry);
}

void OnQueryTextSubmit(object? sender, QueryTextSubmitEventArgs e)
{
VirtualView.SearchButtonPressed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static void MapVerticalTextAlignment(IViewHandler handler, ISearchBar sea
public static void MapCharacterSpacing(IViewHandler handler, ISearchBar searchBar) { }
public static void MapTextColor(IViewHandler handler, ISearchBar searchBar) { }
public static void MapCancelButtonColor(IViewHandler handler, ISearchBar searchBar) { }
public static void MapReturnType(IViewHandler handler, ISearchBar searchBar) { }

/// <summary>
/// Maps the abstract <see cref="ITextInput.IsTextPredictionEnabled"/> property to the platform-specific implementations.
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public static void MapMaxLength(ISearchBarHandler handler, ISearchBar searchBar)
handler.PlatformView?.Entry.UpdateMaxLength(searchBar);
}

public static void MapReturnType(ISearchBarHandler handler, ISearchBar searchBar)
{
handler.PlatformView?.Entry.UpdateReturnType(searchBar);
}

public static void MapIsReadOnly(ISearchBarHandler handler, ISearchBar searchBar)
{
handler.PlatformView?.Entry.UpdateIsReadOnly(searchBar);
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public static void MapPlaceholderColor(ISearchBarHandler handler, ISearchBar sea
handler.PlatformView?.UpdatePlaceholderColor(searchBar);
}

public static void MapReturnType(ISearchBarHandler handler, ISearchBar searchBar)
{
handler.PlatformView?.UpdateReturnType(searchBar);
}

public static void MapHorizontalTextAlignment(ISearchBarHandler handler, ISearchBar searchBar)
{
handler.PlatformView?.UpdateHorizontalTextAlignment(searchBar);
Expand Down
3 changes: 2 additions & 1 deletion src/Core/src/Handlers/SearchBar/SearchBarHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public partial class SearchBarHandler : ISearchBarHandler
[nameof(ISearchBar.Text)] = MapText,
[nameof(ISearchBar.TextColor)] = MapTextColor,
[nameof(ISearchBar.CancelButtonColor)] = MapCancelButtonColor,
[nameof(ISearchBar.Keyboard)] = MapKeyboard
[nameof(ISearchBar.Keyboard)] = MapKeyboard,
[nameof(ISearchBar.ReturnType)] = MapReturnType,
};

public static CommandMapper<ISearchBar, ISearchBarHandler> CommandMapper = new(ViewCommandMapper)
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public static void MapFont(ISearchBarHandler handler, ISearchBar searchBar)
handler.QueryEditor?.UpdateFont(searchBar, fontManager);
}

public static void MapReturnType(ISearchBarHandler handler, ISearchBar searchBar)
{
handler.PlatformView?.UpdateReturnType(searchBar);
}

public static void MapHorizontalTextAlignment(ISearchBarHandler handler, ISearchBar searchBar)
{
handler.QueryEditor?.UpdateHorizontalTextAlignment(searchBar);
Expand Down
12 changes: 11 additions & 1 deletion src/Core/src/Platform/Android/SearchViewExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Android.Content.Res;
using Android.Content;
using Android.Content.Res;
using Android.Text;
using Android.Views.InputMethods;
using Android.Widget;
using SearchView = AndroidX.AppCompat.Widget.SearchView;

Expand Down Expand Up @@ -162,6 +164,14 @@ public static void UpdateKeyboard(this SearchView searchView, ISearchBar searchB
searchView.SetInputType(searchBar);
}

public static void UpdateReturnType(this SearchView searchView, ISearchBar searchBar)
{
searchView.ImeOptions = (int)searchBar.ReturnType.ToPlatform();

InputMethodManager? imm = (InputMethodManager?)searchView.Context?.GetSystemService(Context.InputMethodService);
imm?.RestartInput(searchView);
}

internal static void SetInputType(this SearchView searchView, ISearchBar searchBar, EditText? editText = null)
{
editText ??= searchView.GetFirstChildOfType<EditText>();
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Platform/Tizen/EntryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public static void UpdateReturnType(this Entry platformEntry, IEntry entry)
platformEntry.ReturnType = entry.ReturnType.ToPlatform();
}

public static void UpdateReturnType(this Entry platformEntry, ISearchBar searchBar)
{
platformEntry.ReturnType = searchBar.ReturnType.ToPlatform();
}

public static void UpdateFont(this Entry platformEntry, ITextStyle textStyle, IFontManager fontManager)
{
platformEntry.FontSize = textStyle.Font.Size > 0 ? textStyle.Font.Size.ToScaledPoint() : 25d.ToScaledPoint();
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Platform/Windows/SearchBarExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static void UpdatePlaceholderColor(this AutoSuggestBox platformControl, I
searchBar.PlaceholderColor?.ToPlatform());
}

public static void UpdateReturnType(this AutoSuggestBox platformControl, ISearchBar searchBar)
{
platformControl.UpdateKeyboard(searchBar);
}

public static void UpdateText(this AutoSuggestBox platformControl, ISearchBar searchBar)
{
platformControl.Text = searchBar.Text;
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Platform/iOS/SearchBarExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,10 @@ public static void UpdateKeyboard(this UISearchBar uiSearchBar, ISearchBar searc

uiSearchBar.ReloadInputViews();
}

public static void UpdateReturnType(this UISearchBar uiSearchBar, ISearchBar searchBar)
{
uiSearchBar.ReturnKeyType = searchBar.ReturnType.ToPlatform();
}
}
}
3 changes: 3 additions & 0 deletions src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
static Microsoft.Maui.Handlers.SearchBarHandler.MapReturnType(Microsoft.Maui.Handlers.ISearchBarHandler! handler, Microsoft.Maui.ISearchBar! entry) -> void
static Microsoft.Maui.Platform.SearchViewExtensions.UpdateReturnType(this AndroidX.AppCompat.Widget.SearchView! searchView, Microsoft.Maui.ISearchBar! searchBar) -> void
Microsoft.Maui.ISearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
5 changes: 4 additions & 1 deletion src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#nullable enable
override Microsoft.Maui.Platform.MauiCALayer.AddAnimation(CoreAnimation.CAAnimation! animation, string? key) -> void
*REMOVED*override Microsoft.Maui.Handlers.BorderHandler.ConnectHandler(Microsoft.Maui.Platform.ContentView! platformView) -> void
*REMOVED*override Microsoft.Maui.Handlers.BorderHandler.ConnectHandler(Microsoft.Maui.Platform.ContentView! platformView) -> void
static Microsoft.Maui.Handlers.SearchBarHandler.MapReturnType(Microsoft.Maui.Handlers.ISearchBarHandler! handler, Microsoft.Maui.ISearchBar! searchBar) -> void
static Microsoft.Maui.Platform.SearchBarExtensions.UpdateReturnType(this UIKit.UISearchBar! uiSearchBar, Microsoft.Maui.ISearchBar! searchBar) -> void
Microsoft.Maui.ISearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#nullable enable
override Microsoft.Maui.Platform.MauiCALayer.AddAnimation(CoreAnimation.CAAnimation! animation, string? key) -> void
*REMOVED*override Microsoft.Maui.Handlers.BorderHandler.ConnectHandler(Microsoft.Maui.Platform.ContentView! platformView) -> void
static Microsoft.Maui.Handlers.SearchBarHandler.MapReturnType(Microsoft.Maui.Handlers.ISearchBarHandler! handler, Microsoft.Maui.ISearchBar! searchBar) -> void
static Microsoft.Maui.Platform.SearchBarExtensions.UpdateReturnType(this UIKit.UISearchBar! uiSearchBar, Microsoft.Maui.ISearchBar! searchBar) -> void
Microsoft.Maui.ISearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
3 changes: 3 additions & 0 deletions src/Core/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
static Microsoft.Maui.Handlers.SearchBarHandler.MapReturnType(Microsoft.Maui.Handlers.ISearchBarHandler! handler, Microsoft.Maui.ISearchBar! searchBar) -> void
static Microsoft.Maui.Platform.EntryExtensions.UpdateReturnType(this Tizen.UIExtensions.NUI.Entry! platformEntry, Microsoft.Maui.ISearchBar! searchBar) -> void
Microsoft.Maui.ISearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
3 changes: 3 additions & 0 deletions src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
static Microsoft.Maui.Handlers.SearchBarHandler.MapReturnType(Microsoft.Maui.Handlers.ISearchBarHandler! handler, Microsoft.Maui.ISearchBar! searchBar) -> void
static Microsoft.Maui.Platform.SearchBarExtensions.UpdateReturnType(this Microsoft.UI.Xaml.Controls.AutoSuggestBox! platformControl, Microsoft.Maui.ISearchBar! searchBar) -> void
Microsoft.Maui.ISearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
2 changes: 2 additions & 0 deletions src/Core/src/PublicAPI/net/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
Microsoft.Maui.ISearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
static Microsoft.Maui.Handlers.SearchBarHandler.MapReturnType(Microsoft.Maui.IViewHandler! handler, Microsoft.Maui.ISearchBar! searchBar) -> void
2 changes: 2 additions & 0 deletions src/Core/src/PublicAPI/netstandard/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
Microsoft.Maui.ISearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
static Microsoft.Maui.Handlers.SearchBarHandler.MapReturnType(Microsoft.Maui.IViewHandler! handler, Microsoft.Maui.ISearchBar! searchBar) -> void
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
Microsoft.Maui.ISearchBar.ReturnType.get -> Microsoft.Maui.ReturnType
static Microsoft.Maui.Handlers.SearchBarHandler.MapReturnType(Microsoft.Maui.IViewHandler! handler, Microsoft.Maui.ISearchBar! searchBar) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using System.Threading.Tasks;
using Android.Text;
using Android.Text.Method;
using Android.Views.InputMethods;
using Android.Widget;
using AndroidX.AppCompat.Widget;
using Microsoft.Maui.DeviceTests.Stubs;
using Xunit;
using AColor = Android.Graphics.Color;
Expand Down Expand Up @@ -103,6 +105,31 @@ public async Task CharacterSpacingInitializesCorrectly()
Assert.Equal(expectedValue, values.PlatformViewValue, EmCoefficientPrecision);
}

[Fact(DisplayName = "ReturnType Initializes Correctly")]
public async Task ReturnTypeInitializesCorrectly()
{
var xplatReturnType = ReturnType.Next;
var entry = new SearchBarStub()
{
Text = "Test",
ReturnType = xplatReturnType
};

ImeAction expectedValue = ImeAction.Next;

var values = await GetValueAsync(entry, (handler) =>
{
return new
{
ViewValue = entry.ReturnType,
PlatformViewValue = GetNativeReturnType(handler)
};
});

Assert.Equal(xplatReturnType, values.ViewValue);
Assert.Equal(expectedValue, values.PlatformViewValue);
}

[Fact]
public async Task SearchViewHasEditTextChild()
{
Expand Down Expand Up @@ -145,14 +172,16 @@ await layoutHandler.PlatformView.AttachAndRun(() =>
});
}

ImeAction GetNativeReturnType(SearchBarHandler searchBarHandler) =>
(ImeAction)GetNativeSearchBar(searchBarHandler).ImeOptions;

double GetInputFieldHeight(SearchBarHandler searchBarHandler)
{
var control = GetNativeSearchBar(searchBarHandler);
var editText = control.GetChildrenOfType<EditText>().FirstOrDefault();
return MauiContext.Context.FromPixels((double)editText.MeasuredHeight);
}


static SearchView GetNativeSearchBar(SearchBarHandler searchBarHandler) =>
searchBarHandler.PlatformView;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,39 @@ public async Task CharacterSpacingInitializesCorrectly()
Assert.Equal(xplatCharacterSpacing, values.PlatformViewValue);
}

[Fact(DisplayName = "ReturnType Initializes Correctly")]
public async Task ReturnTypeInitializesCorrectly()
{
var xplatReturnType = ReturnType.Next;
var entry = new SearchBarStub()
{
Text = "Test",
ReturnType = xplatReturnType
};

UIReturnKeyType expectedValue = UIReturnKeyType.Next;

var values = await GetValueAsync(entry, (handler) =>
{
return new
{
ViewValue = entry.ReturnType,
PlatformViewValue = GetNativeReturnType(handler)
};
});

Assert.Equal(xplatReturnType, values.ViewValue);
Assert.Equal(expectedValue, values.PlatformViewValue);
}

double GetInputFieldHeight(SearchBarHandler searchBarHandler)
{
return GetNativeSearchBar(searchBarHandler).Bounds.Height;
}

UIReturnKeyType GetNativeReturnType(SearchBarHandler searchBarHandler) =>
GetNativeSearchBar(searchBarHandler).ReturnKeyType;

static UISearchBar GetNativeSearchBar(SearchBarHandler searchBarHandler) =>
(UISearchBar)searchBarHandler.PlatformView;

Expand Down
2 changes: 2 additions & 0 deletions src/Core/tests/DeviceTests/Stubs/SearchBarStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ void OnTextChanged(string oldValue, string newValue) =>

public Keyboard Keyboard { get; set; }

public ReturnType ReturnType { get; set; }

public void SearchButtonPressed() { }
}
}