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 TitleColor property in PickerHandlers #602

Merged
merged 8 commits into from
Jun 1, 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 @@ -109,7 +109,7 @@ protected override void OnFocusChangeRequested(object sender, VisualElement.Focu
}
}

[PortHandler("Partially ported, still missing code related to TitleColor, etc.")]
[PortHandler("Partially ported, still missing code related to Focus, etc.")]
void IPickerRenderer.OnClick()
{
Picker model = Element;
Expand Down Expand Up @@ -207,6 +207,7 @@ protected override EditText CreateNativeControl()

protected override EditText EditText => Control;

[PortHandler]
protected override void UpdateTitleColor()
{
_hintColorSwitcher = _hintColorSwitcher ?? new TextColorSwitcher(EditText.HintTextColors, Element.UseLegacyColorManagement());
Expand Down
3 changes: 1 addition & 2 deletions src/Controls/samples/Controls.Sample/Pages/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ void SetupMauiLayout()
verticalStack.Add(new Editor { Text = "Lorem ipsum dolor sit amet", FontSize = 10, FontFamily = "Dokdo" });
verticalStack.Add(new Editor { Text = "ReadOnly Editor", IsReadOnly = true });


var entry = new Entry();
entry.TextChanged += (sender, e) =>
{
Expand Down Expand Up @@ -267,7 +266,7 @@ void SetupMauiLayout()
"Japanese Macaque"
};

var picker = new Picker { Title = "Select a monkey", FontFamily = "Dokdo", HorizontalTextAlignment = TextAlignment.Center };
var picker = new Picker { Title = "Select a monkey", TitleColor = Colors.Red, FontFamily = "Dokdo", HorizontalTextAlignment = TextAlignment.Center };

picker.ItemsSource = monkeyList;
verticalStack.Add(picker);
Expand Down
8 changes: 6 additions & 2 deletions src/Core/src/Core/IPicker.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#nullable enable
using System.Collections;
using System.Collections.Generic;
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui
{
Expand All @@ -14,6 +13,11 @@ public interface IPicker : IView, ITextStyle, ITextAlignment, IItemDelegate<stri
/// </summary>
string Title { get; }

/// <summary>
/// Gets the color for the Picker title.
/// </summary>
Color TitleColor { get; }
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a Brush.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree. However, can we make it progressive? Start by merging this and other PRs related to Color, in order to complete Handlers and allow in Previews to work and port code in a simple way, and as we have to do with other important properties such as convert TextColor to Foreground, update little by little .

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we can do that.


/// <summary>
/// Gets the index of the selected item of the picker.
/// </summary>
Expand Down
31 changes: 26 additions & 5 deletions src/Core/src/Handlers/Picker/PickerHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
using System.Collections.Specialized;
using System.Linq;
using Android.App;
using Android.Content.Res;
using Android.Text;
using Android.Text.Style;
using Android.Graphics.Drawables;
using AResource = Android.Resource;
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui.Handlers
{
Expand All @@ -13,6 +17,8 @@ public partial class PickerHandler : ViewHandler<IPicker, MauiPicker>

AlertDialog? _dialog;

static ColorStateList? DefaultTitleColors { get; set; }

protected override MauiPicker CreateNativeView() =>
new MauiPicker(Context);

Expand All @@ -34,9 +40,10 @@ protected override void DisconnectHandler(MauiPicker nativeView)

protected override void SetupDefaults(MauiPicker nativeView)
{
DefaultBackground = nativeView.Background;

base.SetupDefaults(nativeView);

DefaultBackground = nativeView.Background;
DefaultTitleColors = nativeView.HintTextColors;
}

// This is a Android-specific mapping
Expand All @@ -59,6 +66,11 @@ public static void MapTitle(PickerHandler handler, IPicker picker)
handler.NativeView?.UpdateTitle(picker);
}

public static void MapTitleColor(PickerHandler handler, IPicker picker)
{
handler.NativeView?.UpdateTitleColor(picker, DefaultTitleColors);
}

public static void MapSelectedIndex(PickerHandler handler, IPicker picker)
{
handler.NativeView?.UpdateSelectedIndex(picker);
Expand Down Expand Up @@ -112,16 +124,25 @@ void OnClick(object? sender, EventArgs e)
{
using (var builder = new AlertDialog.Builder(Context))
{
builder.SetTitle(VirtualView.Title ?? string.Empty);
if (VirtualView.TitleColor == null)
{
builder.SetTitle(VirtualView.Title ?? string.Empty);
}
else
{
var title = new SpannableString(VirtualView.Title ?? string.Empty);
title.SetSpan(new ForegroundColorSpan(VirtualView.TitleColor.ToNative()), 0, title.Length(), SpanTypes.ExclusiveExclusive);
builder.SetTitle(title);
}

string[] items = VirtualView.GetItemsAsArray();

builder.SetItems(items, (EventHandler<Android.Content.DialogClickEventArgs>)((s, e) =>
builder.SetItems(items, (s, e) =>
{
var selectedIndex = e.Which;
VirtualView.SelectedIndex = selectedIndex;
base.NativeView?.UpdatePicker(VirtualView);
}));
});

builder.SetNegativeButton(AResource.String.Cancel, (o, args) => { });

Expand Down
1 change: 1 addition & 0 deletions src/Core/src/Handlers/Picker/PickerHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public partial class PickerHandler : ViewHandler<IPicker, object>

public static void MapReload(PickerHandler handler, IPicker picker) { }
public static void MapTitle(PickerHandler handler, IPicker view) { }
public static void MapTitleColor(PickerHandler handler, IPicker view) { }
public static void MapSelectedIndex(PickerHandler handler, IPicker view) { }
public static void MapCharacterSpacing(PickerHandler handler, IPicker view) { }
public static void MapFont(PickerHandler handler, IPicker view) { }
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Picker/PickerHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static void MapTitle(PickerHandler handler, IPicker picker)
handler.NativeView?.UpdateTitle(picker);
}

public static void MapTitleColor(PickerHandler handler, IPicker picker)
{
handler.NativeView?.UpdateTitle(picker);
}

public static void MapSelectedIndex(PickerHandler handler, IPicker picker)
{
handler.NativeView?.UpdateSelectedIndex(picker);
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/Handlers/Picker/PickerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public partial class PickerHandler
[nameof(IPicker.SelectedIndex)] = MapSelectedIndex,
[nameof(IPicker.TextColor)] = MapTextColor,
[nameof(IPicker.Title)] = MapTitle,
[nameof(IPicker.TitleColor)] = MapTitleColor,
[nameof(IPicker.HorizontalTextAlignment)] = MapHorizontalTextAlignment,
Actions =
{
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Picker/PickerHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public static void MapTitle(PickerHandler handler, IPicker picker)
handler.NativeView?.UpdateTitle(picker);
}

public static void MapTitleColor(PickerHandler handler, IPicker picker)
{
handler.NativeView?.UpdateTitleColor(picker);
}

public static void MapSelectedIndex(PickerHandler handler, IPicker picker)
{
handler.NativeView?.UpdateSelectedIndex(picker);
Expand Down
30 changes: 29 additions & 1 deletion src/Core/src/Platform/Android/PickerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
namespace Microsoft.Maui
using Android.Content.Res;

namespace Microsoft.Maui
{
public static class PickerExtensions
{
static readonly int[][] ColorStates =
{
new[] { Android.Resource.Attribute.StateEnabled },
new[] { -Android.Resource.Attribute.StateEnabled }
};

public static void UpdateTitle(this MauiPicker nativePicker, IPicker picker) =>
UpdatePicker(nativePicker, picker);

public static void UpdateTitleColor(this MauiPicker nativePicker, IPicker picker, ColorStateList? defaultColor)
{
var titleColor = picker.TitleColor;

if (titleColor == null)
{
nativePicker.SetHintTextColor(defaultColor);
}
else
{
var androidColor = titleColor.ToNative();

if (!nativePicker.TextColors.IsOneColor(ColorStates, androidColor))
{
var acolor = androidColor.ToArgb();
nativePicker.SetHintTextColor(new ColorStateList(ColorStates, new[] { acolor, acolor }));
}
}
}

public static void UpdateSelectedIndex(this MauiPicker nativePicker, IPicker picker) =>
UpdatePicker(nativePicker, picker);

Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Platform/Windows/PickerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ public static void UpdateHorizontalTextAlignment(this MauiComboBox nativeComboBo
nativeComboBox.HorizontalContentAlignment = picker.HorizontalTextAlignment.ToNativeHorizontalAlignment();
}
}
}
}
25 changes: 24 additions & 1 deletion src/Core/src/Platform/iOS/PickerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
using System;
using Foundation;
using Microsoft.Maui.Handlers;

namespace Microsoft.Maui
Expand All @@ -9,9 +10,32 @@ public static class PickerExtensions
public static void UpdateTitle(this MauiPicker nativePicker, IPicker picker) =>
nativePicker.UpdatePicker(picker);

public static void UpdateTitleColor(this MauiPicker nativePicker, IPicker picker) =>
nativePicker.SetTitleColor(picker);

public static void UpdateSelectedIndex(this MauiPicker nativePicker, IPicker picker) =>
nativePicker.SetSelectedIndex(picker, picker.SelectedIndex);

internal static void SetTitleColor(this MauiPicker nativePicker, IPicker picker)
{
var title = picker.Title;

if (string.IsNullOrEmpty(title))
return;

var titleColor = picker.TitleColor;

if (titleColor == null)
return;

nativePicker.UpdateAttributedPlaceholder(new NSAttributedString(title, null, titleColor.ToNative()));
}

internal static void UpdateAttributedPlaceholder(this MauiPicker nativePicker, NSAttributedString nsAttributedString)
{
nativePicker.AttributedPlaceholder = nsAttributedString;
}

internal static void UpdatePicker(this MauiPicker nativePicker, IPicker picker)
{
var selectedIndex = picker.SelectedIndex;
Expand Down Expand Up @@ -40,6 +64,5 @@ internal static void SetSelectedIndex(this MauiPicker nativePicker, IPicker pick

pickerView?.Select(Math.Max(selectedIndex, 0), 0, true);
}

}
}
4 changes: 2 additions & 2 deletions src/Core/src/Primitives/IItemDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
namespace Microsoft.Maui
namespace Microsoft.Maui
{
public interface IItemDelegate<T>
{
int GetCount();

T GetItem(int index);
}
}
18 changes: 18 additions & 0 deletions src/Core/tests/DeviceTests/AssertionExtensions.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,24 @@ public static void AssertHasUnderline(this NSAttributedString attributedString)
}
}

public static UIColor GetForegroundColor(this NSAttributedString text)
{
if (text == null)
return UIColor.Clear;

var value = text.GetAttribute(UIStringAttributeKey.ForegroundColor, 0, out var range);

if (value == null)
return UIColor.Clear;

Assert.Equal(0, range.Location);
Assert.Equal(text.Length, range.Length);

var kerning = Assert.IsType<UIColor>(value);

return kerning;
}

public static void AssertEqual(this CATransform3D expected, CATransform3D actual, int precision = 4)
{
Assert.Equal((double)expected.m11, (double)actual.m11, precision);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Xunit;
using AColor = Android.Graphics.Color;
using ATextAlignment = Android.Views.TextAlignment;

namespace Microsoft.Maui.DeviceTests
Expand All @@ -20,6 +22,18 @@ public async Task TitleInitializesCorrectly()
await ValidatePropertyInitValue(picker, () => picker.Title, GetNativeTitle, picker.Title);
}

[Fact(DisplayName = "Title Color Initializes Correctly")]
public async Task TitleColorInitializesCorrectly()
{
var picker = new PickerStub
{
Title = "Select an Item",
TitleColor = Colors.CadetBlue
};

await ValidatePropertyInitValue(picker, () => picker.TitleColor, GetNativeTitleColor, picker.TitleColor);
}

[Fact(DisplayName = "CharacterSpacing Initializes Correctly")]
public async Task CharacterSpacingInitializesCorrectly()
{
Expand Down Expand Up @@ -57,7 +71,7 @@ public async Task CharacterSpacingInitializesCorrectly()
}

MauiPicker GetNativePicker(PickerHandler pickerHandler) =>
(MauiPicker)pickerHandler.NativeView;
pickerHandler.NativeView;

string GetNativeTitle(PickerHandler pickerHandler) =>
GetNativePicker(pickerHandler).Hint;
Expand Down Expand Up @@ -88,5 +102,12 @@ bool GetNativeIsItalic(PickerHandler pickerHandler) =>

ATextAlignment GetNativeHorizontalTextAlignment(PickerHandler pickerHandler) =>
GetNativePicker(pickerHandler).TextAlignment;

Color GetNativeTitleColor(PickerHandler pickerHandler)
{
var currentTextColorInt = GetNativePicker(pickerHandler).CurrentHintTextColor;
var currentTextColor = new AColor(currentTextColorInt);
return currentTextColor.ToColor();
}
}
}
Loading