Skip to content

Commit

Permalink
[main] Merge SR4 to main (#21936)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Apr 19, 2024
2 parents 345bb16 + e94eec6 commit ebd35ca
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue21846"
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues">
<ScrollView>
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Button
x:Name="TestButton"
AutomationId="OpenModalButton"
Text="Open"
Clicked="OnButtonClicked" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 21846, "Fix crash closing Popup with WebView", PlatformAffected.iOS)]
public partial class Issue21846 : ContentPage
{
public Issue21846()
{
InitializeComponent();
}

async void OnButtonClicked(object sender, System.EventArgs e)
{
await Navigation.PushModalAsync(new Issue21846Modal());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue21846Modal"
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues">
<ScrollView>
<Grid
RowDefinitions="*, Auto"
Padding="30,0">
<WebView
x:Name="TestWebView"
Source="https://www.microsoft.com"/>
<Button
Grid.Row="1"
AutomationId="CloseModalButton"
Text="Close"
Clicked="OnButtonClicked" />
</Grid>
</ScrollView>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Issue21846Modal : ContentPage
{
public Issue21846Modal()
{
InitializeComponent();

BindingContext = this;
}

async void OnButtonClicked(object sender, System.EventArgs e)
{
if (TestWebView is IElement visualElement)
{
if (visualElement.Handler != null)
{
if (visualElement.Handler is IDisposable disposableHandler)
disposableHandler.Dispose();

visualElement.Handler.DisconnectHandler();
}
}

await Navigation.PopModalAsync();
}
}
}
2 changes: 1 addition & 1 deletion src/Controls/src/Build.Tasks/XamlCTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ bool TryCoreCompile(MethodDefinition initComp, ILRootNode rootnode, string xamlF
rootnode.Accept(new CreateObjectVisitor(visitorContext), null);
rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null);
rootnode.Accept(new SetFieldVisitor(visitorContext), null);
rootnode.Accept(new SetResourcesVisitor(visitorContext), null);
rootnode.Accept(new SimplifyTypeExtensionVisitor(), null);
rootnode.Accept(new SetResourcesVisitor(visitorContext), null);
rootnode.Accept(new SetPropertiesVisitor(visitorContext, true), null);

il.Emit(Ret);
Expand Down
3 changes: 1 addition & 2 deletions src/Controls/src/Core/Application/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public IAppLinks AppLinks

if (value is not null)
{
OnParentResourcesChanged(this.GetMergedResources());
OnParentResourcesChanged([new KeyValuePair<string, object>(AppThemeBinding.AppThemeResource, _lastAppTheme)]);
value.OnResourcesChanged(this.GetMergedResources());
((IElementDefinition)this).AddResourcesChangedListener(value.OnParentResourcesChanged);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/Element/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ internal Element ParentOverride
/// <inheritdoc/>
void IElementDefinition.AddResourcesChangedListener(Action<object, ResourcesChangedEventArgs> onchanged)
{
_changeHandlers = _changeHandlers ?? new List<Action<object, ResourcesChangedEventArgs>>(2);
_changeHandlers ??= new List<Action<object, ResourcesChangedEventArgs>>(2);
_changeHandlers.Add(onchanged);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Controls/src/Core/ResourcesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ static class ResourcesExtensions
resources[res.Key] = mergedClassStyles;
}
}
if (app != null)
{
resources = resources ?? new(StringComparer.Ordinal);
resources[AppThemeBinding.AppThemeResource] = app.RequestedTheme;
}

element = element.Parent;
}
return resources;
Expand Down
4 changes: 2 additions & 2 deletions src/Controls/src/Xaml/XamlLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ public static IResourceDictionary LoadResources(string xaml, IResourcesProvider
resources.Accept(new NamescopingVisitor(visitorContext), null); //set namescopes for {x:Reference}
resources.Accept(new CreateValuesVisitor(visitorContext), null);
resources.Accept(new RegisterXNamesVisitor(visitorContext), null);
resources.Accept(new FillResourceDictionariesVisitor(visitorContext), null);
resources.Accept(new SimplifyTypeExtensionVisitor(), null);
resources.Accept(new FillResourceDictionariesVisitor(visitorContext), null);
resources.Accept(new ApplyPropertiesVisitor(visitorContext, true), null);

return visitorContext.Values[resources] as IResourceDictionary;
Expand All @@ -207,8 +207,8 @@ static void Visit(RootNode rootnode, HydrationContext visitorContext, bool useDe
rootnode.Accept(new NamescopingVisitor(visitorContext), null); //set namescopes for {x:Reference}
rootnode.Accept(new CreateValuesVisitor(visitorContext), null);
rootnode.Accept(new RegisterXNamesVisitor(visitorContext), null);
rootnode.Accept(new FillResourceDictionariesVisitor(visitorContext), null);
rootnode.Accept(new SimplifyTypeExtensionVisitor(), null);
rootnode.Accept(new FillResourceDictionariesVisitor(visitorContext), null);
rootnode.Accept(new ApplyPropertiesVisitor(visitorContext, true), null);
}

Expand Down
28 changes: 28 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue21846.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue21846 : _IssuesUITest
{
public Issue21846(TestDevice device) : base(device)
{
}

public override string Issue => "Fix crash closing Popup with WebView";

[Test]
[Category(UITestCategories.WebView)]
public void WebViewNoCrashPopup()
{
this.IgnoreIfPlatforms([TestDevice.Android, TestDevice.Windows]);

App.WaitForElement("OpenModalButton");
App.Click("OpenModalButton");

App.WaitForElement("CloseModalButton");
App.Click("CloseModalButton");
}
}
}
12 changes: 12 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui21757.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui21757">
<Color x:Key="Gray-200">#C8C8C8</Color>
<Style x:Key="A" TargetType="BoxView">
<Setter Property="Color" Value="{StaticResource Gray-200}" />
</Style>
<Style x:Key="B" TargetType="{x:Type BoxView}">
<Setter Property="Color" Value="{StaticResource Gray-200}" />
</Style>
</ResourceDictionary>
60 changes: 60 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui21757.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Dispatching;

using Microsoft.Maui.Graphics;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui21757
{
public Maui21757()
{
InitializeComponent();
}

public Maui21757(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Test
{
[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}

[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void TypeLiteralAndXTypeCanBeUsedInterchangeably([Values(false, true)] bool useCompiledXaml)
{
var resourceDictionary = new Maui21757(useCompiledXaml);

var styleA = resourceDictionary["A"] as Style;
Assert.NotNull(styleA);
Assert.That(styleA.TargetType, Is.EqualTo(typeof(BoxView)));
Assert.That(styleA.Setters[0].Property, Is.EqualTo(BoxView.ColorProperty));
Assert.That(styleA.Setters[0].Value, Is.EqualTo(Color.FromArgb("#C8C8C8")));

var styleB = resourceDictionary["B"] as Style;
Assert.NotNull(styleB);
Assert.That(styleB.TargetType, Is.EqualTo(typeof(BoxView)));
Assert.That(styleB.Setters[0].Property, Is.EqualTo(BoxView.ColorProperty));
Assert.That(styleB.Setters[0].Value, Is.EqualTo(Color.FromArgb("#C8C8C8")));
}
}
}
9 changes: 9 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui21774.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui21774">
<StackLayout>
<Label x:Name="label0" TextColor="{DynamicResource labelColor}"/>
<Label x:Name="label1" TextColor="{AppThemeBinding Light=LimeGreen, Dark=HotPink}"/>
</StackLayout>
</ContentPage>
69 changes: 69 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui21774.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Dispatching;

using Microsoft.Maui.Graphics;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui21774
{
public Maui21774()
{
InitializeComponent();
}

public Maui21774(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Test
{
[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}

[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void AppThemeChangeOnUnparentedPage([Values(false, true)] bool useCompiledXaml)
{
Application.Current.Resources.Add("labelColor", Colors.LimeGreen);
Application.Current.UserAppTheme = AppTheme.Light;
var page = new Maui21774(useCompiledXaml);
Application.Current.MainPage = page;

Assert.That(page.label0.TextColor, Is.EqualTo(Colors.LimeGreen));
Assert.That(page.label1.TextColor, Is.EqualTo(Colors.LimeGreen));

//unparent the page, change the resource and the theme
Application.Current.MainPage = null;
Application.Current.Resources["labelColor"] = Colors.HotPink;
Application.Current.UserAppTheme = AppTheme.Dark;
//labels should not change
Assert.That(page.label0.TextColor, Is.EqualTo(Colors.LimeGreen));
Assert.That(page.label1.TextColor, Is.EqualTo(Colors.LimeGreen));

//reparent the page
Application.Current.MainPage = page;
//labels should change
Assert.That(page.label0.TextColor, Is.EqualTo(Colors.HotPink));
Assert.That(page.label1.TextColor, Is.EqualTo(Colors.HotPink));
}
}
}
6 changes: 6 additions & 0 deletions src/Core/src/Platform/iOS/KeyboardAutoManagerScroll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ internal static void AdjustPosition()

if (View.FindResponder<UINavigationController>() is UINavigationController navigationController)
{
if (View.IsDescendantOfView(navigationController.NavigationBar))
{
IsKeyboardAutoScrollHandling = false;
return;
}

navigationBarAreaHeight = navigationController.NavigationBar.Frame.GetMaxY();
}
else
Expand Down

0 comments on commit ebd35ca

Please sign in to comment.