Skip to content

Commit

Permalink
feat: Support for multiple XamlRoots
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jun 17, 2022
1 parent 3042db8 commit 09764b2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
28 changes: 12 additions & 16 deletions src/Uno.UI/UI/Xaml/Internal/VisualTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Uno.Foundation.Logging;
using Uno.UI.Extensions;
using Uno.UI.Xaml.Input;
using Uno.UI.Xaml.Islands;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
Expand Down Expand Up @@ -590,17 +591,15 @@ private void RemoveRoot(UIElement root)
[NotImplemented]
private static UIElement? GetXamlIslandRootForElement(DependencyObject? pObject)
{
return null;
//TODO Uno: XamlIslandRoot not needed for now.
//if (!pObject || !pObject->GetContext()->HasXamlIslands())
//{
// return nullptr;
//}
//if (VisualTree * visualTree = GetForElementNoRef(pObject))
//{
// return do_pointer_cast<CXamlIslandRoot>(visualTree->m_rootElement);
//}
//return null;
if (GetForElement(pObject) is VisualTree visualTree)
{
return visualTree.RootElement;
}
return null;
}

internal static VisualTree? GetForElement(DependencyObject? element, LookupOptions options = LookupOptions.WarningIfNotFound)
Expand Down Expand Up @@ -667,12 +666,11 @@ private void RemoveRoot(UIElement root)
}
}

//if (currentAncestor.GetTypeIndex() == KnownTypeIndex::XamlIsland)
//{
// return static_cast<CXamlIslandRoot*>(currentAncestor)->GetVisualTreeNoRef();
//}
//else
if (currentAncestor is RootVisual rootVisual)
if (currentAncestor is XamlIslandRoot xamlIslandRoot)
{
return xamlIslandRoot.ContentRoot.VisualTree;
}
else if (currentAncestor is RootVisual rootVisual)
{
return rootVisual.AssociatedVisualTree;
}
Expand Down Expand Up @@ -773,9 +771,7 @@ internal XamlRoot GetOrCreateXamlRoot()
{
if (XamlRoot == null)
{
//TODO Uno: Our current implementation has only a single XAML root.
//In the future we may want to support multiple.
XamlRoot = XamlRoot.Current;
XamlRoot = new XamlRoot(this);
}

return XamlRoot;
Expand Down
41 changes: 21 additions & 20 deletions src/Uno.UI/UI/Xaml/XamlRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,35 @@

using System;
using System.Collections.Generic;
using Uno.UI.Xaml.Core;
using Windows.Foundation;

namespace Windows.UI.Xaml
namespace Windows.UI.Xaml;

/// <summary>
/// Represents a tree of XAML content and information about the context in which it is hosted.
/// </summary>
/// <remarks>
/// Effectively a public API wrapper around VisualTree.
/// </remarks>
public sealed partial class XamlRoot
{
/// <summary>
/// Represents a tree of XAML content and information about the context in which it is hosted.
/// </summary>
/// <remarks>
/// Effectively a public API wrapper around VisualTree.
/// </remarks>
public sealed partial class XamlRoot
internal XamlRoot(VisualTree visualTree)
{
private XamlRoot() { }

internal static XamlRoot Current { get; } = new XamlRoot();
VisualTree = visualTree;
}

public event TypedEventHandler<XamlRoot, XamlRootChangedEventArgs>? Changed;
public event TypedEventHandler<XamlRoot, XamlRootChangedEventArgs>? Changed;

public UIElement? Content => Window.Current?.Content;
public UIElement? Content => Window.Current?.Content;

public Size Size => Content?.RenderSize ?? Size.Empty;
public Size Size => Content?.RenderSize ?? Size.Empty;

public double RasterizationScale
=> global::Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
public double RasterizationScale
=> global::Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;

internal void NotifyChanged()
{
Changed?.Invoke(this, new XamlRootChangedEventArgs());
}
internal void NotifyChanged()
{
Changed?.Invoke(this, new XamlRootChangedEventArgs());
}
}
14 changes: 6 additions & 8 deletions src/Uno.UI/UI/Xaml/XamlRoot.mux.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using DirectUI;
#nullable enable

using Uno.UI.Xaml.Core;

namespace Windows.UI.Xaml
namespace Windows.UI.Xaml;

public sealed partial class XamlRoot
{
public sealed partial class XamlRoot
{
//TODO Uno: This implementation does not match WinUI, but we currently support only
//a single XamlRoot and a single window. This will need to be adjusted later though.
internal VisualTree VisualTree => DXamlCore.Current.GetHandle().ContentRootCoordinator.CoreWindowContentRoot.VisualTree;
}
internal VisualTree VisualTree { get; set; }
}

0 comments on commit 09764b2

Please sign in to comment.