Skip to content

Commit

Permalink
Implement native events in WinUI SearchBarHandler (#875)
Browse files Browse the repository at this point in the history
* Implement native events in WinUI SearchBarHandler

* Enable nullable in WinUI SearchBarHandler

Co-authored-by: Rui Marinho <me@ruimarinho.net>
  • Loading branch information
jsuarezruiz and rmarinho authored May 7, 2021
1 parent 1063b9c commit 3e63574
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Compatibility/Core/src/WinUI/SearchBarRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ protected override void SetAutomationId(string id)
_queryTextBox.SetAutomationPropertiesAutomationId($"{id}_AutoSuggestBox");
}

[PortHandler]
void OnQuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs e)
{
// Modifies the text of the control if it does not match the query.
Expand All @@ -147,6 +148,7 @@ void OnQuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventAr
Element.OnSearchButtonPressed();
}

[PortHandler]
void OnTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e)
{
if (e.Reason == AutoSuggestionBoxTextChangeReason.ProgrammaticChange)
Expand Down
5 changes: 5 additions & 0 deletions src/Controls/src/Core/HandlerImpl/SearchBar.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ public partial class SearchBar : ISearchBar
Font ITextStyle.Font => _font ??= Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes);

bool ITextInput.IsTextPredictionEnabled => true;

void ISearchBar.SearchButtonPressed()
{
(this as ISearchBarController).OnSearchButtonPressed();
}
}
}
5 changes: 4 additions & 1 deletion src/Core/src/Core/ISearchBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
/// </summary>
public interface ISearchBar : IView, ITextInput, ITextAlignment
{

/// <summary>
/// Notify when the user presses the Search button.
/// </summary>
void SearchButtonPressed();
}
}
40 changes: 39 additions & 1 deletion src/Core/src/Handlers/SearchBar/SearchBarHandler.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.UI.Xaml.Controls;
#nullable enable
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.Maui.Handlers
{
Expand All @@ -10,6 +11,18 @@ public partial class SearchBarHandler : ViewHandler<ISearchBar, AutoSuggestBox>
QueryIcon = new SymbolIcon(Symbol.Find)
};

protected override void ConnectHandler(AutoSuggestBox nativeView)
{
nativeView.QuerySubmitted += OnQuerySubmitted;
nativeView.TextChanged += OnTextChanged;
}

protected override void DisconnectHandler(AutoSuggestBox nativeView)
{
nativeView.QuerySubmitted -= OnQuerySubmitted;
nativeView.TextChanged -= OnTextChanged;
}

public static void MapText(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdateText(searchBar);
Expand Down Expand Up @@ -42,5 +55,30 @@ public static void MapMaxLength(IViewHandler handler, ISearchBar searchBar) { }

[MissingMapper]
public static void MapIsReadOnly(IViewHandler handler, ISearchBar searchBar) { }

void OnQuerySubmitted(AutoSuggestBox? sender, AutoSuggestBoxQuerySubmittedEventArgs e)
{
if (VirtualView == null)
return;

// Modifies the text of the control if it does not match the query.
// This is possible because OnTextChanged is fired with a delay
if (e.QueryText != VirtualView.Text)
VirtualView.Text = e.QueryText;

VirtualView.SearchButtonPressed();
}

[PortHandler]
void OnTextChanged(AutoSuggestBox? sender, AutoSuggestBoxTextChangedEventArgs e)
{
if (e.Reason == AutoSuggestionBoxTextChangeReason.ProgrammaticChange)
return;

if (VirtualView == null || sender == null)
return;

VirtualView.Text = sender.Text;
}
}
}
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 @@ -25,5 +25,7 @@ public partial class SearchBarStub : StubBase, ISearchBar
public int MaxLength { get; set; }

public Keyboard Keyboard { get; set; }

public void SearchButtonPressed() { }
}
}

0 comments on commit 3e63574

Please sign in to comment.