Skip to content

Commit

Permalink
Set BackgroundColor for Label (#10184) Fixes #10177
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen authored Sep 19, 2022
1 parent eea488d commit 3595cb1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/Core/src/Graphics/PaintExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,13 @@ public static bool IsNullOrEmpty(this Paint? paint)

return paint == null;
}

internal static bool IsTransparent(this Paint? paint)
{
if (paint is SolidPaint solidPaint)
return solidPaint.Color == Colors.Transparent;

return false;
}
}
}
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Label/LabelHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public override void PlatformArrange(Rect frame)
base.PlatformArrange(frame);
}

internal static void MapBackground(ILabelHandler handler, ILabel label)
{
handler.PlatformView?.UpdateBackground(label);
}

public static void MapText(ILabelHandler handler, ILabel label)
{
handler.PlatformView?.UpdateTextPlainText(label);
Expand Down
5 changes: 4 additions & 1 deletion src/Core/src/Handlers/Label/LabelHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class LabelHandler : ILabelHandler
{
public static IPropertyMapper<ILabel, ILabelHandler> Mapper = new PropertyMapper<ILabel, ILabelHandler>(ViewHandler.ViewMapper)
{
#if __IOS__ || TIZEN
#if IOS || TIZEN
[nameof(ILabel.Background)] = MapBackground,
[nameof(ILabel.Opacity)] = MapOpacity,
#elif WINDOWS
Expand All @@ -37,6 +37,9 @@ public partial class LabelHandler : ILabelHandler
[nameof(ILabel.Text)] = MapText,
[nameof(ITextStyle.TextColor)] = MapTextColor,
[nameof(ILabel.TextDecorations)] = MapTextDecorations,
#if ANDROID
[nameof(ILabel.Background)] = MapBackground,
#endif
};

public static CommandMapper<ILabel, ILabelHandler> CommandMapper = new(ViewCommandMapper)
Expand Down
22 changes: 19 additions & 3 deletions src/Core/src/Platform/Android/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ public static void UpdateBackground(this ContentViewGroup platformView, IBorderS
}

public static void UpdateBackground(this AView platformView, IView view) =>
platformView.UpdateBackground(view.Background);
platformView.UpdateBackground(view, false);

internal static void UpdateBackground(this AView platformView, IView view, bool treatTransparentAsNull) =>
platformView.UpdateBackground(view.Background, treatTransparentAsNull);

internal static void UpdateBackground(this TextView platformView, IView view) =>
UpdateBackground(platformView, view, true);

// TODO: NET7 make this public for net7.0
internal static void UpdateBackground(this EditText platformView, IView view)
Expand All @@ -190,7 +196,10 @@ internal static void UpdateBackground(this EditText platformView, IView view)
platformView.Background = layer;
}

public static void UpdateBackground(this AView platformView, Paint? background)
public static void UpdateBackground(this AView platformView, Paint? background) =>
UpdateBackground(platformView, background, false);

internal static void UpdateBackground(this AView platformView, Paint? background, bool treatTransparentAsNull)
{
var paint = background;

Expand All @@ -203,7 +212,14 @@ public static void UpdateBackground(this AView platformView, Paint? background)
mauiDrawable.Dispose();
}

if (paint is SolidPaint solidPaint)
if (treatTransparentAsNull && paint.IsTransparent())
{
// For controls where android treats transparent as null it's more
// performant to just set the background to null instead of
// giving it a transparent color/drawable
platformView.Background = null;
}
else if (paint is SolidPaint solidPaint)
{
if (solidPaint.Color is Color backgroundColor)
platformView.SetBackgroundColor(backgroundColor.ToPlatform());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@

<Style TargetType="Label">
<Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource White}}" />
<Setter Property="BackgroundColor" Value="Transparent" />
<Setter Property="FontFamily" Value="OpenSansRegular" />
<Setter Property="FontSize" Value="14" />
<Setter Property="VisualStateManager.VisualStateGroups">
Expand Down Expand Up @@ -206,7 +207,7 @@
</Style>

<Style TargetType="RadioButton">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BackgroundColor" Value="Transparent"/>
<Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Black}, Dark={StaticResource White}}" />
<Setter Property="FontFamily" Value="OpenSansRegular"/>
<Setter Property="FontSize" Value="14"/>
Expand Down Expand Up @@ -333,7 +334,7 @@

<Style TargetType="TimePicker">
<Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource White}}" />
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BackgroundColor" Value="Transparent"/>
<Setter Property="FontFamily" Value="OpenSansRegular"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="VisualStateManager.VisualStateGroups">
Expand Down

0 comments on commit 3595cb1

Please sign in to comment.