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 PlaceholderColor property in SearchBarHandlers #1512

Merged
merged 3 commits into from
Aug 31, 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
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ void UpdatePlaceholder()
Control.SetQueryHint(Element.Placeholder);
}

[PortHandler]
void UpdatePlaceholderColor()
{
_hintColorSwitcher?.UpdateTextColor(_editText, Element.PlaceholderColor, _editText.SetHintTextColor);
Expand Down
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/Windows/SearchBarRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ void UpdatePlaceholder()
Control.PlaceholderText = Element.Placeholder ?? string.Empty;
}

[PortHandler]
void UpdatePlaceholderColor()
{
if (_queryTextBox == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
Style="{StaticResource Headline}"/>
<SearchBar
TextColor="Green"/>
<Label
Text="With Placeholder"
Style="{StaticResource Headline}" />
<SearchBar
Placeholder="Placeholder"/>
<Label
Text="Using PlaceholderColor"
Style="{StaticResource Headline}" />
<SearchBar
PlaceholderColor="Pink"
Placeholder="Placeholder"/>
<Label
Text="Fonts"
Style="{StaticResource Headline}"/>
Expand Down
9 changes: 1 addition & 8 deletions src/Core/src/Core/IEditor.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui
namespace Microsoft.Maui
{
/// <summary>
/// Represents a View used to accept multi-line input.
/// </summary>
public interface IEditor : IView, ITextInput, ITextStyle
{
/// <summary>
/// Gets or sets the placeholder text color.
/// </summary>
Color PlaceholderColor { get; set; }

/// <summary>
/// Occurs when the user finalizes the text in an editor with the return key.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/Core/src/Core/IPlaceholder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Microsoft.Maui
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui
{
/// <summary>
/// Provides functionality to be able to use a Placeholder.
Expand All @@ -9,5 +11,10 @@ public interface IPlaceholder
/// Gets the placeholder or hint text.
/// </summary>
string Placeholder { get; }

/// <summary>
/// Gets or sets the placeholder text color.
/// </summary>
Color PlaceholderColor { get; set; }
}
}
9 changes: 7 additions & 2 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using Android.Content.Res;
using Android.Graphics.Drawables;
using Android.Widget;
using Microsoft.Extensions.DependencyInjection;
using SearchView = AndroidX.AppCompat.Widget.SearchView;

namespace Microsoft.Maui.Handlers
{
public partial class SearchBarHandler : ViewHandler<ISearchBar, SearchView>
{
static Drawable? DefaultBackground;
static ColorStateList? DefaultPlaceholderTextColors { get; set; }

EditText? _editText;
public EditText? QueryEditor => _editText;
Expand Down Expand Up @@ -45,6 +45,11 @@ public static void MapPlaceholder(SearchBarHandler handler, ISearchBar searchBar
handler.NativeView?.UpdatePlaceholder(searchBar);
}

public static void MapPlaceholderColor(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdatePlaceholderColor(searchBar, DefaultPlaceholderTextColors, handler._editText);
}

public static void MapFont(SearchBarHandler handler, ISearchBar searchBar)
{
var fontManager = handler.GetRequiredService<IFontManager>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public partial class SearchBarHandler : ViewHandler<ISearchBar, object>

public static void MapText(IViewHandler handler, ISearchBar searchBar) { }
public static void MapPlaceholder(IViewHandler handler, ISearchBar searchBar) { }
public static void MapPlaceholderColor(IViewHandler handler, ISearchBar searchBar) { }
public static void MapFont(IViewHandler handler, ISearchBar searchBar) { }
public static void MapHorizontalTextAlignment(IViewHandler handler, ISearchBar searchBar) { }
public static void MapCharacterSpacing(IViewHandler handler, ISearchBar searchBar) { }
Expand Down
15 changes: 13 additions & 2 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ namespace Microsoft.Maui.Handlers
{
public partial class SearchBarHandler : ViewHandler<ISearchBar, AutoSuggestBox>
{
Brush? _defaultTextColorBrush;
Brush? _defaultPlaceholderColorBrush;
Brush? _defaultPlaceholderColorFocusBrush;

Brush? _defaultTextColorBrush;
Brush? _defaultTextColorFocusBrush;

Brush? _defaultDeleteButtonForegroundColorBrush;
Brush? _defaultDeleteButtonBackgroundColorBrush;

Expand Down Expand Up @@ -46,6 +49,11 @@ public static void MapPlaceholder(SearchBarHandler handler, ISearchBar searchBar
handler.NativeView?.UpdatePlaceholder(searchBar);
}

public static void MapPlaceholderColor(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdatePlaceholderColor(searchBar, handler._defaultPlaceholderColorBrush, handler._defaultPlaceholderColorFocusBrush, handler._queryTextBox);
}

public static void MapHorizontalTextAlignment(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdateHorizontalTextAlignment(searchBar, handler._queryTextBox);
Expand Down Expand Up @@ -90,6 +98,9 @@ void OnLoaded(object sender, UI.Xaml.RoutedEventArgs e)

if(_queryTextBox != null)
{
_defaultPlaceholderColorBrush = _queryTextBox.PlaceholderForegroundBrush;
_defaultPlaceholderColorFocusBrush = _queryTextBox.PlaceholderForegroundFocusBrush;

_defaultTextColorBrush = _queryTextBox.Foreground;
_defaultTextColorFocusBrush = _queryTextBox.ForegroundFocusBrush;
}
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public partial class SearchBarHandler
[nameof(ISearchBar.IsTextPredictionEnabled)] = MapIsTextPredictionEnabled,
[nameof(ISearchBar.MaxLength)] = MapMaxLength,
[nameof(ISearchBar.Placeholder)] = MapPlaceholder,
[nameof(ISearchBar.PlaceholderColor)] = MapPlaceholderColor,
[nameof(ISearchBar.Text)] = MapText,
[nameof(ISearchBar.TextColor)] = MapTextColor,
[nameof(ISearchBar.CancelButtonColor)] = MapCancelButtonColor
Expand Down
7 changes: 6 additions & 1 deletion src/Core/src/Handlers/SearchBar/SearchBarHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ public static void MapText(SearchBarHandler handler, ISearchBar searchBar)

public static void MapPlaceholder(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdatePlaceholder(searchBar);
handler.NativeView?.UpdatePlaceholder(searchBar, handler._editor);
}

public static void MapPlaceholderColor(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdatePlaceholder(searchBar, handler._editor);
}

public static void MapFont(SearchBarHandler handler, ISearchBar searchBar)
Expand Down
28 changes: 26 additions & 2 deletions src/Core/src/Platform/Android/SearchViewExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using Android.Text;
using Android.Content.Res;
using Android.Widget;
using SearchView = AndroidX.AppCompat.Widget.SearchView;

Expand All @@ -17,6 +16,31 @@ public static void UpdatePlaceholder(this SearchView searchView, ISearchBar sear
searchView.QueryHint = searchBar.Placeholder;
}

public static void UpdatePlaceholderColor(this SearchView searchView, ISearchBar searchBar, ColorStateList? defaultPlaceholderColor, EditText? editText = null)
{
editText ??= searchView.GetFirstChildOfType<EditText>();

if (editText == null)
return;

var placeholderTextColor = searchBar.PlaceholderColor;

if (placeholderTextColor == null)
{
editText.SetHintTextColor(defaultPlaceholderColor);
}
else
{
var androidColor = placeholderTextColor.ToNative();

if (!editText.HintTextColors.IsOneColor(ColorExtensions.States, androidColor))
{
var acolor = androidColor.ToArgb();
editText.SetHintTextColor(new ColorStateList(ColorExtensions.States, new[] { acolor, acolor }));
}
}
}

public static void UpdateFont(this SearchView searchView, ISearchBar searchBar, IFontManager fontManager, EditText? editText = null)
{
editText ??= searchView.GetFirstChildOfType<EditText>();
Expand Down
14 changes: 14 additions & 0 deletions src/Core/src/Platform/Windows/SearchBarExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ public static void UpdatePlaceholder(this AutoSuggestBox nativeControl, ISearchB
nativeControl.PlaceholderText = searchBar.Placeholder ?? string.Empty;
}

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

Color placeholderColor = searchBar.PlaceholderColor;

BrushHelpers.UpdateColor(placeholderColor, ref defaultPlaceholderColorBrush,
() => queryTextBox.PlaceholderForegroundBrush, brush => queryTextBox.PlaceholderForegroundBrush = brush);

BrushHelpers.UpdateColor(placeholderColor, ref defaultPlaceholderColorFocusBrush,
() => queryTextBox.PlaceholderForegroundFocusBrush, brush => queryTextBox.PlaceholderForegroundFocusBrush = brush);
}

public static void UpdateText(this AutoSuggestBox nativeControl, ISearchBar searchBar)
{
nativeControl.Text = searchBar.Text;
Expand Down
20 changes: 17 additions & 3 deletions src/Core/src/Platform/iOS/SearchBarExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UIKit;
using Foundation;
using UIKit;

namespace Microsoft.Maui
{
Expand All @@ -9,9 +10,22 @@ public static void UpdateText(this UISearchBar uiSearchBar, ISearchBar searchBar
uiSearchBar.Text = searchBar.Text;
}

public static void UpdatePlaceholder(this UISearchBar uiSearchBar, ISearchBar searchBar)
public static void UpdatePlaceholder(this UISearchBar uiSearchBar, ISearchBar searchBar, UITextField? textField)
{
uiSearchBar.Placeholder = searchBar.Placeholder;
textField ??= uiSearchBar.FindDescendantView<UITextField>();

if (textField == null)
return;

var placeholder = searchBar.Placeholder ?? string.Empty;
var placeholderColor = searchBar.PlaceholderColor;
var foregroundColor = placeholderColor ?? ColorExtensions.PlaceholderColor.ToColor();

textField.AttributedPlaceholder = foregroundColor == null
? new NSAttributedString(placeholder)
: new NSAttributedString(str: placeholder, foregroundColor: foregroundColor.ToNative());

textField.AttributedPlaceholder.WithCharacterSpacing(searchBar.CharacterSpacing);
}

public static void UpdateFont(this UISearchBar uiSearchBar, ITextStyle textStyle, IFontManager fontManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Xunit;
using AColor = global::Android.Graphics.Color;
using AColor = Android.Graphics.Color;
using SearchView = AndroidX.AppCompat.Widget.SearchView;

namespace Microsoft.Maui.DeviceTests
{
public partial class SearchBarHandlerTests
{
[Fact(DisplayName = "PlaceholderColor Initializes Correctly")]
public async Task PlaceholderColorInitializesCorrectly()
{
var searchBar = new SearchBarStub()
{
Placeholder = "Test",
PlaceholderColor = Colors.Yellow
};

await ValidatePropertyInitValue(searchBar, () => searchBar.PlaceholderColor, GetNativePlaceholderColor, searchBar.PlaceholderColor);
}

[Fact(DisplayName = "Horizontal TextAlignment Initializes Correctly")]
public async Task HorizontalTextAlignmentInitializesCorrectly()
{
Expand Down Expand Up @@ -79,7 +91,7 @@ await InvokeOnMainThreadAsync(() =>
}

SearchView GetNativeSearchBar(SearchBarHandler searchBarHandler) =>
(SearchView)searchBarHandler.NativeView;
searchBarHandler.NativeView;

string GetNativeText(SearchBarHandler searchBarHandler) =>
GetNativeSearchBar(searchBarHandler).Query;
Expand All @@ -102,6 +114,22 @@ Color GetNativeTextColor(SearchBarHandler searchBarHandler)
string GetNativePlaceholder(SearchBarHandler searchBarHandler) =>
GetNativeSearchBar(searchBarHandler).QueryHint;

Color GetNativePlaceholderColor(SearchBarHandler searchBarHandler)

{
var searchView = GetNativeSearchBar(searchBarHandler);
var editText = searchView.GetChildrenOfType<EditText>().FirstOrDefault();

if (editText != null)
{
int currentHintTextColor = editText.CurrentHintTextColor;
AColor currentPlaceholderColorr = new AColor(currentHintTextColor);
return currentPlaceholderColorr.ToColor();
}

return Colors.Transparent;
}

double GetNativeCharacterSpacing(SearchBarHandler searchBarHandler)
{
var searchView = GetNativeSearchBar(searchBarHandler);
Expand Down
2 changes: 2 additions & 0 deletions src/Core/tests/DeviceTests/Stubs/EntryStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public string Text

public string Placeholder { get; set; }

public Color PlaceholderColor { get; set; }

public bool IsReadOnly { get; set; }

public Font Font { get; set; }
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 @@ -9,6 +9,8 @@ public partial class SearchBarStub : StubBase, ISearchBar
public string Text { get => _text; set => SetProperty(ref _text, value); }

public string Placeholder { get; set; }

public Color PlaceholderColor { get; set; }

public Color TextColor { get; set; }

Expand Down