Skip to content

Commit

Permalink
2.0.42 - Fix #241
Browse files Browse the repository at this point in the history
  • Loading branch information
adospace committed Jun 19, 2024
1 parent 169ffb6 commit 39e454f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
env:
Solution_Name: ./src/MauiReactor.Build.sln
TemplatePack_Name: ./src/MauiReactor.TemplatePack/MauiReactor.TemplatePack.csproj
Version: 2.0.41
Version: 2.0.42

steps:
- name: Checkout
Expand Down
103 changes: 77 additions & 26 deletions src/MauiReactor/Shell.partial.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MauiReactor.Internals;
using Microsoft.Maui.Platform;
using System.Collections;

namespace MauiReactor;
Expand Down Expand Up @@ -78,7 +79,7 @@ public ItemTemplateNode(VisualNode root, ItemTemplatePresenter presenter, Visual

private VisualNode _root;

private VisualNode Root
internal VisualNode Root
{
get => _root;
set
Expand All @@ -98,6 +99,7 @@ private VisualNode Root
{
return ((IVisualNode)_owner).GetPageHost();
}

protected sealed override void OnAddChild(VisualNode widget, BindableObject nativeControl)
{
Validate.EnsureNotNull(_presenter);
Expand All @@ -124,6 +126,12 @@ protected internal override void OnLayoutCycleRequested()
Layout();
base.OnLayoutCycleRequested();
}

internal void UpdateLayout()
{
Invalidate();
}

}

private class ItemTemplatePresenter : Microsoft.Maui.Controls.ContentView
Expand All @@ -140,56 +148,99 @@ public ItemTemplatePresenter(CustomDataTemplate template, bool useMenuItemTempla

protected override void OnBindingContextChanged()
{
if (BindingContext != null)
{
var item = BindingContext;

var owner = _template.Owner;
var ownerAsIShell = (IShell)owner;
var newRoot = _useMenuItemTemplate ?
//unfortunately seems there is not other way to get the underlying MenuItem, anyway shouldn't be a problem for performance as menu items are generally not so many
ownerAsIShell.MenuItemTemplate?.Invoke((Microsoft.Maui.Controls.MenuItem)(item.GetType().GetProperty("MenuItem").EnsureNotNull().GetValue(item).EnsureNotNull()))
:
ownerAsIShell.ItemTemplate?.Invoke((Microsoft.Maui.Controls.BaseShellItem)item);

if (newRoot != null)
{
_itemTemplateNode = new ItemTemplateNode(newRoot, this, _template.Owner);
_itemTemplateNode.Layout();
}
}

UpdateLayout();
base.OnBindingContextChanged();
}

internal void UpdateLayout()
{
var item = BindingContext;

if (item == null)
{
return;
}

var owner = _template.Owner;
var ownerAsIShell = (IShell)owner;
var newRoot = _useMenuItemTemplate ?
//unfortunately seems there is not other way to get the underlying MenuItem, anyway shouldn't be a problem for performance as menu items are generally not so many
ownerAsIShell.MenuItemTemplate?.Invoke((Microsoft.Maui.Controls.MenuItem)(item.GetType().GetProperty("MenuItem").EnsureNotNull().GetValue(item).EnsureNotNull()))
:
ownerAsIShell.ItemTemplate?.Invoke((Microsoft.Maui.Controls.BaseShellItem)item);

if (newRoot != null)
{
if (_itemTemplateNode == null)
{
_itemTemplateNode = new ItemTemplateNode(newRoot, this, _template.Owner);
}
else
{
_itemTemplateNode.Root = newRoot;
}

_itemTemplateNode.Layout();
}
}
}

private class CustomDataTemplate
{
public DataTemplate DataTemplate { get; }
public Shell<T> Owner { get; set; }

public List<ItemTemplatePresenter> ItemTemplatePresenters { get; } = [];

public CustomDataTemplate(Shell<T> owner, bool useMenuItemTemplate)
{
Owner = owner;
DataTemplate = new DataTemplate(() => new ItemTemplatePresenter(this, useMenuItemTemplate));
DataTemplate = new DataTemplate(() =>
{
var presenter = new ItemTemplatePresenter(this, useMenuItemTemplate);
ItemTemplatePresenters.Add(presenter);
return presenter;
});
}
}

internal void UpdateLayout()
{
foreach (var presenter in ItemTemplatePresenters)
{
presenter.UpdateLayout();
}
}
}

partial void OnBeginUpdate()
{
Validate.EnsureNotNull(NativeControl);
var thisAsIShell = (IShell)this;

if (thisAsIShell.ItemTemplate != null)
{
_customDataTemplate = new CustomDataTemplate(this, false);
NativeControl.ItemTemplate = _customDataTemplate.DataTemplate;
if (NativeControl.ItemTemplate == null || _customDataTemplate == null)
{
_customDataTemplate = new CustomDataTemplate(this, false);
NativeControl.ItemTemplate = _customDataTemplate.DataTemplate;
}
else
{
_customDataTemplate.UpdateLayout();
}
}

if (thisAsIShell.MenuItemTemplate != null)
{
_customMenuItemDataTemplate = new CustomDataTemplate(this, true);
NativeControl.MenuItemTemplate = _customMenuItemDataTemplate.DataTemplate;
if (NativeControl.MenuItemTemplate == null || _customMenuItemDataTemplate == null)
{
_customMenuItemDataTemplate = new CustomDataTemplate(this, false);
NativeControl.MenuItemTemplate = _customMenuItemDataTemplate.DataTemplate;
}
else
{
_customMenuItemDataTemplate.UpdateLayout();
}
}
}

Expand Down

0 comments on commit 39e454f

Please sign in to comment.