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 HorizontalTextAlignment property in WinUI SearchBar #1176

Merged
merged 1 commit into from
Jun 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
17 changes: 4 additions & 13 deletions src/Controls/samples/Controls.Sample/Pages/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,10 @@ void SetupMauiLayout()
verticalStack.Add(new ProgressBar { Progress = 0.5, BackgroundColor = Colors.LightCoral });
verticalStack.Add(new ProgressBar { Progress = 0.5, ProgressColor = Colors.Purple });

var searchBar = new SearchBar
{
CharacterSpacing = 4,
Text = "A search query"
};
verticalStack.Add(searchBar);

var placeholderSearchBar = new SearchBar
{
Placeholder = "Placeholder"
};
verticalStack.Add(placeholderSearchBar);

verticalStack.Add(new SearchBar { CharacterSpacing = 4, Text = "A search query" });
verticalStack.Add(new SearchBar { Placeholder = "Placeholder" });
verticalStack.Add(new SearchBar { HorizontalTextAlignment = TextAlignment.End, Text = "SearchBar HorizontalTextAlignment" });

var monkeyList = new List<string>
{
"Baboon",
Expand Down
24 changes: 19 additions & 5 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ namespace Microsoft.Maui.Handlers
{
public partial class SearchBarHandler : ViewHandler<ISearchBar, AutoSuggestBox>
{
MauiTextBox? _queryTextBox;

protected override AutoSuggestBox CreateNativeView() => new AutoSuggestBox
{
AutoMaximizeSuggestionArea = false,
QueryIcon = new SymbolIcon(Symbol.Find)
QueryIcon = new SymbolIcon(Symbol.Find),
Style = UI.Xaml.Application.Current.Resources["MauiAutoSuggestBoxStyle"] as UI.Xaml.Style
};

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

protected override void DisconnectHandler(AutoSuggestBox nativeView)
{
nativeView.Loaded -= OnLoaded;
nativeView.QuerySubmitted -= OnQuerySubmitted;
nativeView.TextChanged -= OnTextChanged;
}
Expand All @@ -32,9 +37,11 @@ public static void MapPlaceholder(SearchBarHandler handler, ISearchBar searchBar
{
handler.NativeView?.UpdatePlaceholder(searchBar);
}

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

public static void MapHorizontalTextAlignment(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdateHorizontalTextAlignment(searchBar, handler._queryTextBox);
}

[MissingMapper]
public static void MapFont(IViewHandler handler, ISearchBar searchBar) { }
Expand All @@ -56,6 +63,14 @@ public static void MapMaxLength(IViewHandler handler, ISearchBar searchBar) { }
[MissingMapper]
public static void MapIsReadOnly(IViewHandler handler, ISearchBar searchBar) { }

void OnLoaded(object sender, UI.Xaml.RoutedEventArgs e)
{
_queryTextBox = NativeView?.GetFirstDescendant<MauiTextBox>();

if (VirtualView != null)
NativeView?.UpdateHorizontalTextAlignment(VirtualView, _queryTextBox);
}

void OnQuerySubmitted(AutoSuggestBox? sender, AutoSuggestBoxQuerySubmittedEventArgs e)
{
if (VirtualView == null)
Expand All @@ -69,7 +84,6 @@ void OnQuerySubmitted(AutoSuggestBox? sender, AutoSuggestBoxQuerySubmittedEventA
VirtualView.SearchButtonPressed();
}

[PortHandler]
void OnTextChanged(AutoSuggestBox? sender, AutoSuggestBoxTextChangedEventArgs e)
{
if (e.Reason == AutoSuggestionBoxTextChangeReason.ProgrammaticChange)
Expand Down
53 changes: 53 additions & 0 deletions src/Core/src/Platform/Windows/MauiCancelButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#nullable disable
using System;
using Microsoft.UI.Xaml.Controls;
using WBrush = Microsoft.UI.Xaml.Media.Brush;

namespace Microsoft.Maui
{
internal class MauiCancelButton : Button
{
TextBlock _cancelButtonGlyph;
Border _cancelButtonBackground;

public WBrush ForegroundBrush
{
get => _cancelButtonGlyph.Foreground;
set => _cancelButtonGlyph.Foreground = value;
}

public WBrush BackgroundBrush
{
get => _cancelButtonBackground.Background;
set => _cancelButtonBackground.Background = value;
}

public bool IsReady { get; private set; }

public event EventHandler ReadyChanged;

protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

_cancelButtonGlyph = (TextBlock)GetTemplateChild("GlyphElement");
_cancelButtonBackground = (Border)GetTemplateChild("BorderElement");

if (_cancelButtonGlyph != null && _cancelButtonBackground != null)
{
// The SearchBarRenderer needs to be able to check whether we're ready to have the colors set
// (we won't be until the first time the button actually appears, which requires the search bar
// to be focused and have text in it)
IsReady = true;

// And we need to inform the SearchBarHandler of this so it can run the button color update method
OnReadyChanged();
}
}

protected virtual void OnReadyChanged()
{
ReadyChanged?.Invoke(this, EventArgs.Empty);
}
}
}
8 changes: 8 additions & 0 deletions src/Core/src/Platform/Windows/SearchBarExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@ public static void UpdateText(this AutoSuggestBox nativeControl, ISearchBar sear
{
nativeControl.Text = searchBar.Text;
}

public static void UpdateHorizontalTextAlignment(this AutoSuggestBox nativeControl, ISearchBar searchBar, MauiTextBox? queryTextBox)
{
if (queryTextBox == null)
return;

queryTextBox.TextAlignment = searchBar.HorizontalTextAlignment.ToNative();
}
}
}
Loading