Skip to content

Commit

Permalink
fix(responsive): layout breakpoint calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Dec 14, 2023
1 parent 775b466 commit 3690ecc
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/Uno.Toolkit.UI/Helpers/ResponsiveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#if IS_WINUI
using Microsoft.UI.Xaml;
using System.Linq;

#else
using Windows.UI.Xaml;
using Windows.UI.Core;
Expand Down Expand Up @@ -191,21 +193,34 @@ internal void Register(IResponsiveCallback host)
internal (ResponsiveLayout Layout, Size Size, Layout? Result) ResolveLayout(ResponsiveLayout? layout, IEnumerable<Layout> options)
{
layout ??= Layout;
var result =
options.FirstOrNull(SatisfyLayoutThreshold) ??
options.LastOrNull();
var result = ResolveLayoutCore(layout, WindowSize.Width, options);

return (layout, WindowSize, result);
}

bool SatisfyLayoutThreshold(Layout x) => x switch
internal static Layout? ResolveLayoutCore(ResponsiveLayout layout, double width, IEnumerable<Layout> options)
{
return options.Append((Layout)int.MaxValue)
.ZipSkipOne()
.Select(x => new
{
Layout = x.Previous,
InclusiveLBound = GetThreshold(x.Previous),
ExclusiveUBound = GetThreshold(x.Current),
})
.FirstOrDefault(x => x.InclusiveLBound <= width && width < x.ExclusiveUBound)
?.Layout ?? options.FirstOrNull();

double GetThreshold(Layout x) => x switch
{
UI.Layout.Narrowest => layout.Narrowest,
UI.Layout.Narrow => layout.Narrow,
UI.Layout.Normal => layout.Normal,
UI.Layout.Wide => layout.Wide,
UI.Layout.Widest => layout.Widest,
_ => double.NaN,
} >= WindowSize.Width;

_ => double.PositiveInfinity,
};
}

internal static IDisposable UsingDebuggableInstance()
Expand Down

0 comments on commit 3690ecc

Please sign in to comment.