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
2 changes: 1 addition & 1 deletion .github/scripts/RunTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ $UnitTestProjects = [ordered]@{
"Graphics" = "src/Graphics/tests/Graphics.Tests/Graphics.Tests.csproj"
"Resizetizer" = "src/SingleProject/Resizetizer/test/UnitTests/Resizetizer.UnitTests.csproj"
"Compatibility" = "src/Compatibility/Core/tests/Compatibility.UnitTests/Compatibility.Core.UnitTests.csproj"
"DualScreen" = "src/Controls/Foldable/test/Controls.DualScreen.UnitTests.csproj"
"DualScreen" = "src/Controls/Foldable/test/Controls.Foldable.UnitTests.csproj"
}

# Device test projects: Key → matches Run-DeviceTests.ps1 -Project parameter
Expand Down
20 changes: 20 additions & 0 deletions .github/scripts/shared/Detect-TestsInDiff.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ public class QualifiedHelpers
}
}

Describe 'Detect-TestsInDiff Foldable unit tests' {
It 'maps Foldable tests to the current unit test project' {
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..' '..')
$changedFile = 'src/Controls/Foldable/test/FoldableRegionHelperTests.cs'

Push-Location $repoRoot
try {
$tests = @(& $scriptPath -ChangedFiles $changedFile)
} finally {
Pop-Location
}

$test = $tests | Select-Object -First 1
$test.Type | Should -Be 'UnitTest'
$test.Project | Should -Be 'Controls.Foldable.UnitTests'
$test.ProjectPath | Should -Be 'src/Controls/Foldable/test/Controls.Foldable.UnitTests.csproj'
$test.Runner | Should -Be 'dotnet-test'
}
}

Describe 'Detect-TestsInDiff device-test filtering' {
It 'sets class and category filters when a device-test diff has no added method signatures' {
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..' '..')
Expand Down
4 changes: 3 additions & 1 deletion .github/scripts/shared/Detect-TestsInDiff.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ $TestTypeRules = @(
}
@{
Type = "UnitTest"
PathPattern = "(?<!\w)UnitTests/|Graphics\.Tests/"
PathPattern = "(?<!\w)UnitTests/|Graphics\.Tests/|Controls/Foldable/test/"
Runner = "dotnet-test"
NeedsPlatform = $false
}
Expand All @@ -195,6 +195,7 @@ $UnitTestProjects = @{
"Graphics.Tests" = "src/Graphics/tests/Graphics.Tests/"
"Resizetizer.UnitTests" = "src/SingleProject/Resizetizer/test/UnitTests/"
"Compatibility.Core.UnitTests" = "src/Compatibility/Core/tests/Compatibility.UnitTests/"
"Controls.Foldable.UnitTests" = "src/Controls/Foldable/test/"
}

$UnitTestProjectPaths = @{
Expand All @@ -207,6 +208,7 @@ $UnitTestProjectPaths = @{
"Graphics.Tests" = "src/Graphics/tests/Graphics.Tests/Graphics.Tests.csproj"
"Resizetizer.UnitTests" = "src/SingleProject/Resizetizer/test/UnitTests/Resizetizer.UnitTests.csproj"
"Compatibility.Core.UnitTests" = "src/Compatibility/Core/tests/Compatibility.UnitTests/Compatibility.Core.UnitTests.csproj"
"Controls.Foldable.UnitTests" = "src/Controls/Foldable/test/Controls.Foldable.UnitTests.csproj"
}

# ============================================================
Expand Down
43 changes: 42 additions & 1 deletion src/Controls/Foldable/src/Android/FoldableService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,21 @@ public Task<int> GetHingeAngleAsync()

public Rect GetHinge() => _hingePx;

public Rect GetHinge(VisualElement visualElement) => GetHinge();

public bool IsLandscapeFor(VisualElement visualElement) => IsLandscape;

public Size GetScaledScreenSize(VisualElement visualElement) => ScaledScreenSize;

public bool StartMonitoring(VisualElement visualElement)
{
return true;
}

public void StopMonitoring(VisualElement visualElement)
{
}

/// <summary>
/// I question whether we should be basing anything on landscape-ness, and
/// instead should be using the orientation of the hinge
Expand Down Expand Up @@ -305,7 +320,7 @@ public bool IsLandscape
static int subscriberCount;
static object hingeAngleLock = new object();

public static event EventHandler<HingeSensor.HingeSensorChangedEventArgs> HingeAngleChanged
static event EventHandler<HingeSensor.HingeSensorChangedEventArgs> HingeSensorAngleChanged
{
add
{
Expand All @@ -325,6 +340,32 @@ public static event EventHandler<HingeSensor.HingeSensorChangedEventArgs> HingeA
}
}

EventHandler<FoldableHingeAngleChangedEventArgs> _hingeAngleChangedHandlers;
int _hingeAngleSubscriberCount;

public event EventHandler<FoldableHingeAngleChangedEventArgs> HingeAngleChanged
{
add
{
if (Interlocked.Increment(ref _hingeAngleSubscriberCount) == 1)
HingeSensorAngleChanged += OnHingeSensorAngleChanged;

_hingeAngleChangedHandlers += value;
}
remove
{
_hingeAngleChangedHandlers -= value;

if (Interlocked.Decrement(ref _hingeAngleSubscriberCount) == 0)
HingeSensorAngleChanged -= OnHingeSensorAngleChanged;
}
}

void OnHingeSensorAngleChanged(object sender, HingeSensor.HingeSensorChangedEventArgs e)
{
_hingeAngleChangedHandlers?.Invoke(this, new FoldableHingeAngleChangedEventArgs(e.HingeAngle));
}

static void ProcessHingeAngleSubscriberCount(int subscriberCount)
{
var sensor = DefaultHingeSensor;
Expand Down
11 changes: 10 additions & 1 deletion src/Controls/Foldable/src/Controls.Foldable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@
<EnableAotAnalyzer>true</EnableAotAnalyzer>
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
</PropertyGroup>
<Target Name="_SetIosDuoBindingsAvailability"
BeforeTargets="PrepareForBuild"
Condition="'$(_MauiTargetPlatformIsiOS)' == 'True'">
<PropertyGroup>
<IosDuoBindingsAvailable Condition="'$(IosDuoBindingsAvailable)' == '' and '$(TargetPlatformVersion)' != ''">$([MSBuild]::VersionGreaterThanOrEquals('$(TargetPlatformVersion)', '27.1'))</IosDuoBindingsAvailable>
<IosDuoBindingsAvailable Condition="'$(IosDuoBindingsAvailable)' == ''">false</IosDuoBindingsAvailable>
<DefineConstants Condition="'$(IosDuoBindingsAvailable)' == 'true'">$(DefineConstants);IOS_DUO_BINDINGS</DefineConstants>
</PropertyGroup>
</Target>
<PropertyGroup>
<!-- NuGet package information -->
<IsPackable>True</IsPackable>
<PackageTags>$(DefaultPackageTags);foldable;surface-duo</PackageTags>
<PackageTags>$(DefaultPackageTags);foldable;surface-duo;iphone-duo</PackageTags>
<Description>Foldable or multi-screen device support for .NET Multi-platform App UI (.NET MAUI) apps.</Description>
</PropertyGroup>
<Import Project="$(MauiSrcDirectory)MultiTargeting.targets" />
Expand Down
83 changes: 65 additions & 18 deletions src/Controls/Foldable/src/DualScreenInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Foldable;
using Microsoft.Maui.Graphics;
Expand Down Expand Up @@ -46,6 +47,8 @@ public partial class DualScreenInfo : INotifyPropertyChanged
TwoPaneViewMode _spanMode;
TwoPaneViewLayoutGuide _twoPaneViewLayoutGuide;
IFoldableService _dualScreenService;
IFoldableService _hingeAngleSubscriptionService;
bool _hasExplicitService;
IFoldableService FoldableService =>
_dualScreenService ?? Element?.Handler?.MauiContext?.Services?.GetService<IFoldableService>();

Expand Down Expand Up @@ -76,24 +79,44 @@ internal DualScreenInfo(VisualElement element, IFoldableService dualScreenServic
_spanningBounds = Array.Empty<Rect>();
Element = element;
_dualScreenService = dualScreenService;
_hasExplicitService = dualScreenService != null;

if (element == null)
{
_twoPaneViewLayoutGuide = TwoPaneViewLayoutGuide.Instance;
}
else
{
_twoPaneViewLayoutGuide = new TwoPaneViewLayoutGuide(element, FoldableService); // get if null
_twoPaneViewLayoutGuide = new TwoPaneViewLayoutGuide(element, dualScreenService);
_twoPaneViewLayoutGuide.PropertyChanged += OnTwoPaneViewLayoutGuideChanged;
}
}

internal void SetFoldableService(IFoldableService foldableService)
void BindFoldableService(IFoldableService foldableService)
{
if (ReferenceEquals(_dualScreenService, foldableService))
return;

_dualScreenService = foldableService;

if (subscriberCount > 0)
SetHingeAngleSubscription(foldableService);
}

internal void SetFoldableService(IFoldableService foldableService)
{
_hasExplicitService = foldableService != null;
BindFoldableService(foldableService);
_twoPaneViewLayoutGuide.SetFoldableService(foldableService);
}

internal void Dispose()
{
SetHingeAngleSubscription(null);
_twoPaneViewLayoutGuide.PropertyChanged -= OnTwoPaneViewLayoutGuideChanged;
_twoPaneViewLayoutGuide.Dispose();
}

EventHandler<HingeAngleChangedEventArgs> _hingeAngleChanged;
int subscriberCount = 0;
/// <summary>
Expand Down Expand Up @@ -188,11 +211,7 @@ Rect[] GetSpanningBounds()
if (guide.Pane2 == Rect.Zero)
return Array.Empty<Rect>();

//TODO: I think this should this be checking SpanMode==Wide
if (IsLandscape)
return new[] { guide.Pane1, new Rect(0, hinge.Height + guide.Pane1.Height, guide.Pane2.Width, guide.Pane2.Height) };
else
return new[] { guide.Pane1, new Rect(hinge.Width + guide.Pane1.Width, 0, guide.Pane2.Width, guide.Pane2.Height) };
return new[] { guide.Pane1, guide.Pane2 };
}

Rect GetHingeBounds()
Expand All @@ -214,6 +233,9 @@ static DualScreenInfo OnCreate()

void OnTwoPaneViewLayoutGuideChanged(object sender, PropertyChangedEventArgs e)
{
if (!_hasExplicitService)
BindFoldableService(Element?.Handler?.MauiContext?.Services?.GetService<IFoldableService>());

SpanningBounds = GetSpanningBounds();
IsLandscape = GetIsLandscape();
HingeBounds = GetHingeBounds();
Expand All @@ -233,38 +255,63 @@ bool SetProperty<T>(ref T backingStore, T value,
return true;
}

#if !ANDROID
public Task<int> GetHingeAngleAsync() => FoldableService?.GetHingeAngleAsync() ?? Task.FromResult(0);
void ProcessHingeAngleSubscriberCount(int newCount) { }
#else

static object hingeAngleLock = new object();
/// <summary>
/// Query the current hinge angle of the foldable device.
/// </summary>
/// <returns>Hinge angle between 0 and 360 degrees.</returns>
public Task<int> GetHingeAngleAsync() => FoldableService?.GetHingeAngleAsync() ?? Task.FromResult(0);
public async Task<int> GetHingeAngleAsync()
{
var service = FoldableService;
if (service == null)
return 0;

await MainThread.InvokeOnMainThreadAsync(_twoPaneViewLayoutGuide.ConnectToService);
return await service.GetHingeAngleAsync().ConfigureAwait(false);
}

void ProcessHingeAngleSubscriberCount(int newCount)
{
lock (hingeAngleLock)
{
if (newCount == 1)
{
Foldable.FoldableService.HingeAngleChanged += OnHingeAngleChanged;
MainThread.BeginInvokeOnMainThread(() =>
{
lock (hingeAngleLock)
{
if (Volatile.Read(ref subscriberCount) <= 0)
return;

_twoPaneViewLayoutGuide.ConnectToService();
var service = FoldableService;
SetHingeAngleSubscription(service);
}
});
}
else if (newCount == 0)
{
Foldable.FoldableService.HingeAngleChanged -= OnHingeAngleChanged;
SetHingeAngleSubscription(null);
}
}
}

void OnHingeAngleChanged(object sender, HingeSensor.HingeSensorChangedEventArgs e)
void SetHingeAngleSubscription(IFoldableService service)
{
if (ReferenceEquals(_hingeAngleSubscriptionService, service))
return;

_hingeAngleSubscriptionService?.HingeAngleChanged -= OnHingeAngleChanged;

_hingeAngleSubscriptionService = service;

_hingeAngleSubscriptionService?.HingeAngleChanged += OnHingeAngleChanged;
}

void OnHingeAngleChanged(object sender, FoldableHingeAngleChangedEventArgs e)
{
_hingeAngleChanged?.Invoke(this, new HingeAngleChangedEventArgs(e.HingeAngle));
_hingeAngleChanged?.Invoke(this, new HingeAngleChangedEventArgs(e.HingeAngleInDegrees));
}
#endif
}

/// <summary>
Expand Down
Loading
Loading