Skip to content

Commit

Permalink
Add EntryProperties extension
Browse files Browse the repository at this point in the history
  • Loading branch information
enisn committed Mar 20, 2024
1 parent a234b97 commit c8bc1ec
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/UraniumUI.Material/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[assembly: XmlnsDefinition(Constants.XamlNamespace, Constants.NamespacePrefix + nameof(UraniumUI.Material.Attachments))]
[assembly: XmlnsDefinition(Constants.XamlNamespace, Constants.NamespacePrefix + nameof(UraniumUI.Material.Controls))]
[assembly: XmlnsDefinition(Constants.XamlNamespace, Constants.NamespacePrefix + nameof(UraniumUI.Material.Extensions))]
[assembly: XmlnsDefinition(Constants.XamlNamespace, Constants.NamespacePrefix + nameof(UraniumUI.Material.Resources))]

[assembly: Microsoft.Maui.Controls.XmlnsPrefix(Constants.XamlNamespace, "material")]
Expand Down
57 changes: 57 additions & 0 deletions src/UraniumUI.Material/Extensions/EntryProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.Maui.Platform;
using UraniumUI.Resources;

namespace UraniumUI.Material.Extensions;
public static class EntryProperties
{
public static readonly BindableProperty SelectionHighlightColorProperty = BindableProperty.CreateAttached(
"SelectionHighlightColor",
typeof(Color),
typeof(Entry),
ColorResource.GetColor("Primary", "PrimaryDark", Colors.Purple),
propertyChanged: OnSelectionHighlightColorChanged);

private static void OnSelectionHighlightColorChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is not Entry entry)
{
return;
}

if (newValue is not Color color)
{
throw new InvalidOperationException($"The type of the parameter of {typeof(EntryProperties).FullName}.{nameof(SelectionHighlightColorProperty)} must be a {typeof(Color).FullName}.");
}

#if WINDOWS
if (entry.Handler.PlatformView is Microsoft.UI.Xaml.Controls.TextBox textBox)
{
textBox.SelectionHighlightColor = new Microsoft.UI.Xaml.Media.SolidColorBrush(color.ToWindowsColor());
textBox.BorderThickness = new Microsoft.UI.Xaml.Thickness(0);

textBox.Style = null;
}
#elif ANDROID
if (entry.Handler.PlatformView is AndroidX.AppCompat.Widget.AppCompatEditText editText)
{
editText.SetHighlightColor(color.ToPlatform());
}

#elif IOS || MACCATALYST
if (entry.Handler.PlatformView is UIKit.UITextField textField)
{
textField.TintColor = color.ToPlatform();
}
#endif
}

public static void SetSelectionHighlightColor(BindableObject bindable, Color value)
{
bindable.SetValue(SelectionHighlightColorProperty, value);
}

public static Color GetSelectionHighlightColor(BindableObject bindable)
{
return (Color)bindable.GetValue(SelectionHighlightColorProperty);
}
}

0 comments on commit c8bc1ec

Please sign in to comment.