Skip to content
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
40 changes: 40 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue19197.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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.Issue19197">
<ContentPage.Resources>
<ResourceDictionary>

<Style TargetType="Label">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup>
<VisualState x:Name="Vertical">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Property="Text" Value="Vertical" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Horizontal">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="800" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Property="Text" Value="Horizontal" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<Grid>
<Label
AutomationId="WaitForStubControl"
HorizontalOptions="Center"
VerticalOptions="Center" />
</Grid>
</ContentPage>
15 changes: 15 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue19197.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 19197, "AdaptiveTrigger does not work", PlatformAffected.iOS)]
public partial class Issue19197 : ContentPage
{
public Issue19197()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#if TEST_FAILS_ON_ANDROID && TEST_FAILS_ON_CATALYST && TEST_FAILS_ON_WINDOWS
// Test failing on Android.
// The test fails on Windows and MacCatalyst because the SetOrientation method, which is intended to change the device orientation, is only supported on mobile platforms Android and iOS.
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue19197 : _IssuesUITest
{
public Issue19197(TestDevice device) : base(device)
{
}

public override string Issue => "AdaptiveTrigger does not work";

[Test]
[Category(UITestCategories.Page)]
public void AdaptiveTriggerWorks()
{
App.WaitForElement("WaitForStubControl");
App.SetOrientationLandscape();
var text = App.FindElement("WaitForStubControl").GetText();
Assert.That(text, Is.EqualTo("Horizontal"));
App.SetOrientationPortrait();
}
}
}
#endif
48 changes: 45 additions & 3 deletions src/Core/src/Handlers/Window/WindowHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ namespace Microsoft.Maui.Handlers
{
public partial class WindowHandler : ElementHandler<IWindow, UIWindow>
{
readonly WindowProxy _proxy = new();
readonly WindowProxy _windowProxy = new();
readonly FrameObserverProxy _frameObserverProxy = new();

protected override void ConnectHandler(UIWindow platformView)
{
base.ConnectHandler(platformView);

_frameObserverProxy.Connect(VirtualView, platformView);

// For newer Mac Catalyst versions, we want to wait until we get effective window dimensions from the platform.
if (OperatingSystem.IsMacCatalystVersionAtLeast(16))
{
_proxy.Connect(VirtualView, platformView);
_windowProxy.Connect(VirtualView, platformView);
}
else
{
Expand All @@ -27,7 +30,8 @@ protected override void DisconnectHandler(UIWindow platformView)
{
if (OperatingSystem.IsMacCatalystVersionAtLeast(16))
{
_proxy.Disconnect();
_windowProxy.Disconnect();
_frameObserverProxy.Disconnect(platformView);
}

base.DisconnectHandler(platformView);
Expand Down Expand Up @@ -167,5 +171,43 @@ void HandleEffectiveGeometryObserved(NSObservedChange obj)
}
}
}

class FrameObserverProxy
{
WeakReference<IWindow>? _virtualView;
WeakReference<UIWindow>? _platformView;

IDisposable? _frameObserver;

IWindow? VirtualView => _virtualView is not null && _virtualView.TryGetTarget(out var v) ? v : null;

UIWindow? PlatformView => _platformView is not null && _platformView.TryGetTarget(out var v) ? v : null;

public void Connect(IWindow virtualView, UIWindow platformView)
{
_virtualView = new(virtualView);
_platformView = new(platformView);

_frameObserver = platformView.AddObserver("frame", Foundation.NSKeyValueObservingOptions.New, FrameAction);
}

public void Disconnect(UIWindow platformView)
{
_virtualView = null;
_platformView = null;

_frameObserver?.Dispose();
}

public void Update()
{
if (VirtualView is IWindow virtualView && PlatformView is UIWindow platformView)
{
virtualView.FrameChanged(platformView.Frame.ToRectangle());
}
}

void FrameAction(Foundation.NSObservedChange obj) => Update();
}
}
}
Loading