Skip to content
Closed
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 @@ -57,6 +57,26 @@
</LinearGradientBrush>
</Button.Background>
</Button>
<Label
Text="Shadow"
Style="{StaticResource Headline}" />
<Button
x:Name="ShadowButton"
Text="Shadow"
HorizontalOptions="Center">
<Button.Background>
<LinearGradientBrush EndPoint="1,0">
<GradientStop Color="Yellow"
Offset="0.1" />
<GradientStop Color="Green"
Offset="1.0" />
</LinearGradientBrush>
</Button.Background>
<Button.Shadow>
<Shadow
Brush="Green" />
</Button.Shadow>
</Button>
<Label
Text="TextColor"
Style="{StaticResource Headline}"/>
Expand Down
4 changes: 3 additions & 1 deletion src/Core/src/Handlers/View/ViewHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ internal static void MapContextFlyout(IElementHandler handler, IContextFlyoutEle
static partial void MappingFrame(IViewHandler handler, IView view)
{
UpdateTransformation(handler, view);
handler.ToPlatform().UpdateBackgroundLayerFrame();

if (view.Background is not null)
handler.ToPlatform().UpdateBackgroundLayerFrame();
}

public static void MapTranslationX(IViewHandler handler, IView view)
Expand Down
28 changes: 24 additions & 4 deletions src/Core/src/Platform/iOS/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,23 +268,43 @@ public static void UpdateBackgroundLayerFrame(this UIView view)

var layer = view.Layer;

UpdateBackgroundLayerFrame(layer, view.Bounds);
UpdateBackgroundLayerFrame(layer, view.Bounds, view is WrapperView);
}

static void UpdateBackgroundLayerFrame(CALayer layer, CGRect bounds)
static void UpdateBackgroundLayerFrame(CALayer layer, CGRect bounds, bool getAllSubLayers)
{
if (layer == null || layer.Sublayers == null || layer.Sublayers.Length == 0)
return;

foreach (var sublayer in layer.Sublayers)
IEnumerable<CALayer> subLayers = getAllSubLayers ? layer.GetLayers() : layer.Sublayers;

foreach (var sublayer in subLayers)
{
if (sublayer.Name == BackgroundLayerName && sublayer.Frame != bounds)
{
sublayer.Frame = bounds;
break;
}

UpdateBackgroundLayerFrame(sublayer, bounds);
UpdateBackgroundLayerFrame(sublayer, bounds, getAllSubLayers);
}
}

static IEnumerable<CALayer> GetLayers(this CALayer root)
{
var stack = new Stack<CALayer>();
stack.Push(root);

while (stack.Count > 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the use of the stack to build the iterator.

{
var current = stack.Pop();
yield return current;

if (current.Sublayers is not null)
{
foreach (var child in current.Sublayers)
stack.Push(child);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,42 @@ public async Task ValidateDefaultAccessibilityTraits()
Assert.Equal(UIAccessibilityTrait.Button, trait);
}

[Theory]
[InlineData(null)]
[InlineData("#FF0000")]
[InlineData("#00FF00")]
[InlineData("#0000FF")]
[InlineData("#000000")]
public async Task BackgroundWithShadowRenderCorrectly(string colorHex)
{
var expectedColor = Color.FromArgb(colorHex);

var button = new ButtonStub
{
Text = "Test",
Background = new SolidPaintStub(expectedColor),
Shadow = new ShadowStub
{
Paint = new SolidPaintStub(Colors.Black),
Offset = new Point(10, 10),
Opacity = 1.0f,
Radius = 2.0f
}
};

var handler = await CreateHandlerAsync(button);

await InvokeOnMainThreadAsync(async () =>
{
await handler.PlatformView.AttachAndRun(async () =>
{
handler.UpdateValue(nameof(IButton.Background));

await handler.PlatformView.AssertContainsColor(expectedColor);
});
});
}

bool ImageSourceLoaded(ButtonHandler buttonHandler) =>
buttonHandler.PlatformView.ImageView.Image != null;

Expand Down