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

[iOS/Catalyst] Correctly render ContentPage brushes background #11721

Merged
merged 6 commits into from
Feb 1, 2023
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
@@ -0,0 +1,24 @@
<views:BasePage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Pages.ContentPageBackgroundGallery"
xmlns:views="clr-namespace:Maui.Controls.Sample.Pages.Base"
Title="ContentPage Background">
<ContentPage.Background>
<!-- StartPoint defaults to (0,0) -->
<LinearGradientBrush EndPoint="1,0">
<GradientStop Color="Yellow"
Offset="0.1" />
<GradientStop Color="Green"
Offset="1.0" />
</LinearGradientBrush>
</ContentPage.Background>
<views:BasePage.Content>
<StackLayout>
<Label
Text="ContentPage"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</views:BasePage.Content>
</views:BasePage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Maui.Controls.Sample.Pages
{
public partial class ContentPageBackgroundGallery
{
public ContentPageBackgroundGallery()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<views:BasePage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Pages.ContentPageGallery"
x:Class="Maui.Controls.Sample.Pages.ContentPageBackgroundImageGallery"
xmlns:views="clr-namespace:Maui.Controls.Sample.Pages.Base"
Title="ContentPage Gallery"
Title="ContentPage BackgroundImage"
BackgroundImageSource="oasis.jpg">
<views:BasePage.Content>
<StackLayout>
<Label
Text="ContentPage"
VerticalOptions="Center"
HorizontalOptions="Center" />
Text="ContentPage"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</views:BasePage.Content>
</views:BasePage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Maui.Controls.Sample.Pages
{
public partial class ContentPageBackgroundImageGallery
{
public ContentPageBackgroundImageGallery()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Internals;

namespace Maui.Controls.Sample.Pages
{
[Preserve(AllMembers = true)]
public class ContentPageGallery : ContentPage
{
public ContentPageGallery()
{
var descriptionLabel =
new Label { Text = "ContentPage Galleries", Margin = new Thickness(2, 2, 2, 2) };

Title = "ContentPage Galleries";

Content = new ScrollView
{
Content = new StackLayout
{
Children =
{
descriptionLabel,
GalleryBuilder.NavButton("ContentPage BackgroundImage", () =>
new ContentPageBackgroundImageGallery(), Navigation),
GalleryBuilder.NavButton("ContentPage Background", () =>
new ContentPageBackgroundGallery(), Navigation),
}
}
};
}
}
}

This file was deleted.

14 changes: 11 additions & 3 deletions src/Core/src/Platform/iOS/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
using CoreAnimation;
using CoreGraphics;
using Foundation;
using Microsoft.Maui.Devices;
Expand Down Expand Up @@ -264,16 +265,23 @@ public static void UpdateBackgroundLayerFrame(this UIView view)

var layer = view.Layer;

UpdateBackgroundLayerFrame(layer, view.Bounds);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not in 100% of the cases we will have the background layer as a sublayer of the main one. The main change is here, use recursion to get the background layer.

}

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

foreach (var sublayer in layer.Sublayers)
jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved
{
if (sublayer.Name == BackgroundLayerName && sublayer.Frame != view.Bounds)
if (sublayer.Name == BackgroundLayerName && sublayer.Frame != bounds)
{
sublayer.Frame = view.Bounds;
sublayer.Frame = bounds;
break;
}

UpdateBackgroundLayerFrame(sublayer, bounds);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using ObjCRuntime;
using UIKit;
using UserNotificationsUI;
using Xunit;

namespace Microsoft.Maui.DeviceTests.Handlers.ContentView
{
[Category(TestCategory.ContentView)]
public partial class ContentViewTests
{
[Theory(DisplayName = "Background Updates Correctly")]
[InlineData(0xFF0000)]
[InlineData(0x00FF00)]
[InlineData(0x0000FF)]
public async Task BackgroundUpdatesCorrectly(uint color)
{
var expected = Color.FromUint(color);

var contentView = new ContentViewStub()
{
Content = new LabelStub { Text = "Background", TextColor = Colors.White },
Background = new LinearGradientPaintStub(Colors.Red, Colors.Blue),
};

await ValidateHasColor(contentView, expected);
}

[Fact, Category(TestCategory.FlowDirection)]
public async Task FlowDirectionPropagatesToContent()
{
Expand Down Expand Up @@ -114,5 +123,18 @@ public async Task DoesNotPropagateToContentWithExplicitFlowDirection()

Assert.Equal(UIUserInterfaceLayoutDirection.LeftToRight, labelFlowDirection);
}

Platform.ContentView GetNativeContentView(ContentViewHandler contentViewHandler) =>
contentViewHandler.PlatformView;

Task ValidateHasColor(IContentView contentView, Color color, Action action = null)
{
return InvokeOnMainThreadAsync(() =>
{
var nativeContentView = GetNativeContentView(CreateHandler(contentView));
action?.Invoke();
nativeContentView.AssertContainsColor(color);
});
}
}
}