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

Implement native events in WinUI SearchBarHandler #875

Merged
merged 3 commits into from
May 7, 2021
Merged
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
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() { }
}
}