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

[Windows] Implement SearchBar CancelButtonColor on Windows #13622

Merged
merged 7 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -107,6 +107,11 @@
</LinearGradientBrush>
</SearchBar.Background>
</SearchBar>
<Label
Text="CancelButtonColor"
Style="{StaticResource Headline}"/>
<SearchBar
CancelButtonColor="Red"/>
</VerticalStackLayout>
</ScrollView>
</views:BasePage.Content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static void MapIsReadOnly(ISearchBarHandler handler, ISearchBar searchBar

public static void MapCancelButtonColor(ISearchBarHandler handler, ISearchBar searchBar)
{
// AutoSuggestBox does not support this property
handler.PlatformView?.UpdateCancelButtonColor(searchBar);
}

void OnLoaded(object sender, UI.Xaml.RoutedEventArgs e)
Expand All @@ -109,6 +109,7 @@ void OnLoaded(object sender, UI.Xaml.RoutedEventArgs e)
PlatformView?.UpdateMaxLength(VirtualView);
PlatformView?.UpdateIsReadOnly(VirtualView);
PlatformView?.UpdateIsTextPredictionEnabled(VirtualView);
PlatformView?.UpdateCancelButtonColor(VirtualView);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/Core/src/Platform/Windows/ButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,32 @@ public static void UpdateTextColor(this ButtonBase platformButton, ITextStyle bu
}

public static void UpdateTextColor(this ButtonBase platformButton, Color textColor)
{
platformButton.UpdateTextColor(textColor, TextColorResourceKeys);
}

internal static void UpdateTextColor(this ButtonBase platformButton, Color textColor, string[] resourceKeys)
{
var brush = textColor?.ToPlatform();

if (brush is null)
{
// Windows.Foundation.UniversalApiContract < 5
platformButton.Resources.RemoveKeys(TextColorResourceKeys);
platformButton.Resources.RemoveKeys(resourceKeys);
// Windows.Foundation.UniversalApiContract >= 5
platformButton.ClearValue(Button.ForegroundProperty);
}
else
{
// Windows.Foundation.UniversalApiContract < 5
platformButton.Resources.SetValueForAllKey(TextColorResourceKeys, brush);
platformButton.Resources.SetValueForAllKey(resourceKeys, brush);
// Windows.Foundation.UniversalApiContract >= 5
platformButton.Foreground = brush;
}

platformButton.RefreshThemeResources();
}

static readonly string[] TextColorResourceKeys =
{
"ButtonForeground",
Expand Down
19 changes: 18 additions & 1 deletion src/Core/src/Platform/Windows/SearchBarExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,22 @@ public static void UpdateIsTextPredictionEnabled(this AutoSuggestBox platformCon

textBox.UpdateIsTextPredictionEnabled(searchBar);
}

private static readonly string[] CancelButtonColorKeys =
{
"TextControlButtonForeground",
"TextControlButtonForegroundPointerOver",
"TextControlButtonForegroundPressed",
};

internal static void UpdateCancelButtonColor(this AutoSuggestBox platformControl, ISearchBar searchBar)
{
var cancelButton = platformControl.GetDescendantByName<Button>("DeleteButton");

if (cancelButton is null)
return;

cancelButton.UpdateTextColor(searchBar.CancelButtonColor, CancelButtonColorKeys);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ namespace Microsoft.Maui.DeviceTests
{
public partial class SearchBarHandlerTests
{
[Theory(DisplayName = "CancelButtonColor Initializes Correctly")]
[InlineData(0xFFFF0000)]
[InlineData(0xFF00FF00)]
[InlineData(0xFF0000FF)]
public async Task CancelButtonColorInitializesCorrectly(uint color)
{
var expected = Color.FromUint(color);

var searchBar = new SearchBarStub()
{
Text = "Test",
CancelButtonColor = expected
};

searchBar.Focus();

await InvokeOnMainThreadAsync(async () =>
{
var searchBarHandler = CreateHandler(searchBar);
await searchBarHandler.PlatformView.AttachAndRun(async () =>
{
await AssertionExtensions.Wait(() => searchBarHandler.PlatformView.FocusState != UI.Xaml.FocusState.Unfocused);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... what is the reason for checking this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CancelButton only appears when the SearchBar has text and the focus. For that reason, here, we wait until be sure that the native control is focused.

});
});

await ValidatePropertyInitValue(searchBar, () => searchBar.CancelButtonColor, GetNativeCancelButtonColor, expected);
}

[Theory(DisplayName = "Is Text Prediction Enabled")]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -69,5 +97,21 @@ bool GetNativeIsTextPredictionEnabled(SearchBarHandler searchBarHandler)

return false;
}

Color GetNativeCancelButtonColor(SearchBarHandler searchBarHandler)
{
var platformSearchBar = GetNativeSearchBar(searchBarHandler);
var cancelButton = platformSearchBar.GetDescendantByName<Button>("DeleteButton");

if (cancelButton is not null)
{
var foreground = cancelButton.Foreground;

if (foreground is SolidColorBrush solidColorBrush)
return solidColorBrush.Color.ToColor();
}

return Colors.Transparent;
}
}
}