Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom layout updates #420

Merged
merged 3 commits into from
Jan 23, 2024
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/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);

Check warning on line 12 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference argument for parameter 'layout' in 'CustomGridLayoutManager.CustomGridLayoutManager(IGridLayout layout)'.

Check warning on line 12 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference argument for parameter 'layout' in 'CustomGridLayoutManager.CustomGridLayoutManager(IGridLayout layout)'.

Check warning on line 12 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference argument for parameter 'layout' in 'CustomGridLayoutManager.CustomGridLayoutManager(IGridLayout layout)'.

Check warning on line 12 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Possible null reference argument for parameter 'layout' in 'CustomGridLayoutManager.CustomGridLayoutManager(IGridLayout layout)'.

Check warning on line 12 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Possible null reference argument for parameter 'layout' in 'CustomGridLayoutManager.CustomGridLayoutManager(IGridLayout layout)'.

Check warning on line 12 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Possible null reference argument for parameter 'layout' in 'CustomGridLayoutManager.CustomGridLayoutManager(IGridLayout layout)'.
}
return null;

Check warning on line 14 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference return.

Check warning on line 14 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference return.

Check warning on line 14 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference return.

Check warning on line 14 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Possible null reference return.

Check warning on line 14 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Possible null reference return.

Check warning on line 14 in 8.0/UserInterface/CustomLayoutDemos/CustomLayoutDemos/CustomLayoutManagerFactory.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Possible null reference return.
}
}
}

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>
Loading