Skip to content

Commit

Permalink
Merge pull request #420 from dotnet/dabritch-customlayouts-update
Browse files Browse the repository at this point in the history
Custom layout updates
  • Loading branch information
jfversluis committed Jan 23, 2024
2 parents 81df36b + 64b0004 commit 9631125
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
# if: runner.os == 'macOS' # reenable when .NET 8 is default on hosted runners

- name: Select Xcode Version
run: sudo xcode-select -s /Applications/Xcode_15.0.1.app
run: sudo xcode-select -s /Applications/Xcode_15.2.app
if: runner.os == 'macOS' # Remove when Xcode 15+ is default on the hosted runners

- name: Find and build changed projects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

namespace CustomLayoutDemos
{
public class CustomLayoutManagerFactory : ILayoutManagerFactory
{
public ILayoutManager CreateLayoutManager(Layout layout)
{
if (layout is Grid)
{
return new CustomGridLayoutManager(layout as IGridLayout);
}
return null;
}
}
public class CustomLayoutManagerFactory : ILayoutManagerFactory
{
public ILayoutManager CreateLayoutManager(Layout layout)
{
if (layout is Grid)
{
return new CustomGridLayoutManager(layout as IGridLayout);
}
return null;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void EnsureRows()
maxRow = Math.Max(grid.GetRow(child), maxRow);
}

// Add more rows if we need them
// Add more rows if needed
for (int n = grid.RowDefinitions.Count; n <= maxRow; n++)
{
grid.RowDefinitions.Add(new RowDefinition(GridLength.Star));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace CustomLayoutDemos.Layouts
{
public class HorizontalWrapLayout : StackLayout
public class HorizontalWrapLayout : HorizontalStackLayout
{
protected override ILayoutManager CreateLayoutManager()
{
return new HorizontalWrapLayoutManager(this);
}
}
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.Maui.Layouts;
using StackLayoutManager = Microsoft.Maui.Layouts.StackLayoutManager;
using HorizontalStackLayoutManager = Microsoft.Maui.Layouts.HorizontalStackLayoutManager;

namespace CustomLayoutDemos.Layouts
{
public class HorizontalWrapLayoutManager : StackLayoutManager
public class HorizontalWrapLayoutManager : HorizontalStackLayoutManager
{
HorizontalWrapLayout _layout;

Expand All @@ -26,7 +26,6 @@ public override Size Measure(double widthConstraint, double heightConstraint)
for (int n = 0; n < _layout.Count; n++)
{
var child = _layout[n];

if (child.Visibility == Visibility.Collapsed)
{
continue;
Expand Down Expand Up @@ -65,13 +64,13 @@ public override Size Measure(double widthConstraint, double heightConstraint)
totalWidth += padding.HorizontalThickness;
totalHeight += padding.VerticalThickness;

// Ensure that the total size of the layout fits within its constraints
var finalWidth = ResolveConstraints(widthConstraint, Stack.Width, totalWidth, Stack.MinimumWidth, Stack.MaximumWidth);
var finalHeight = ResolveConstraints(heightConstraint, Stack.Height, totalHeight, Stack.MinimumHeight, Stack.MaximumHeight);

return new Size(finalWidth, finalHeight);
}


public override Size ArrangeChildren(Rect bounds)
{
var padding = Stack.Padding;
Expand All @@ -87,7 +86,6 @@ public override Size ArrangeChildren(Rect bounds)
for (int n = 0; n < _layout.Count; n++)
{
var child = _layout[n];

if (child.Visibility == Visibility.Collapsed)
{
continue;
Expand All @@ -113,6 +111,7 @@ public override Size ArrangeChildren(Rect bounds)

var actual = new Size(maxStackWidth, currentRowTop + currentRowHeight);

// Adjust the size if the layout is set to fill its container
return actual.AdjustForFill(bounds, Stack);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ namespace CustomLayoutDemos;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

// Setup a custom layout manager so the default manager for the Grid can be replaced.
builder.Services.Add(new ServiceDescriptor(typeof(ILayoutManagerFactory), new CustomLayoutManagerFactory()));
// Setup a custom layout manager so the default manager for the Grid can be replaced.
builder.Services.Add(new ServiceDescriptor(typeof(ILayoutManagerFactory), new CustomLayoutManagerFactory()));

#if DEBUG
builder.Logging.AddDebug();
builder.Logging.AddDebug();
#endif

return builder.Build();
}
return builder.Build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<Label Grid.Row="2"
Text="Notice that the Grid doesn't explicitly specify a RowDefinitions collection." />
<Label Grid.Row="3"
Text="In MauiProgram.cs, we've added an instance of an ILayoutManagerFactory which replaces the default GridLayoutManager. The custom manager will automatically add the necessary RowDefinitions at runtime." />
Text="In MauiProgram.cs, an instance of an ILayoutManagerFactory has been added that replaces the default GridLayoutManager. The custom manager will automatically add the necessary RowDefinitions at runtime." />
<Label Grid.Row="5"
Text="We can even skip some rows, and it will add the intervening ones for us. (Notice the gap between the previous label and this one.)" />
Text="We can even skip some rows, and it will add the intervening ones for us (notice the gap between the previous label and this one)." />
</Grid>
</ContentPage>

0 comments on commit 9631125

Please sign in to comment.