Skip to content

Commit

Permalink
feat(XamlRoot): Initial support for UIElement.XamlRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Oct 23, 2020
1 parent 5c53c2f commit 1cd99c5
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Uno.UI/Generated/3.0.0.0/Windows.UI.Xaml/UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public bool UseLayoutRounding
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.UI.Xaml.XamlRoot XamlRoot
{
Expand Down
8 changes: 4 additions & 4 deletions src/Uno.UI/Generated/3.0.0.0/Windows.UI.Xaml/XamlRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.UI.Xaml
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented]
#endif
public partial class XamlRoot
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.UI.Xaml.UIElement Content
{
Expand Down Expand Up @@ -37,7 +37,7 @@ public double RasterizationScale
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.Foundation.Size Size
{
Expand All @@ -64,7 +64,7 @@ public double RasterizationScale
// Forced skipping of method Windows.UI.Xaml.XamlRoot.UIContext.get
// Forced skipping of method Windows.UI.Xaml.XamlRoot.Changed.add
// Forced skipping of method Windows.UI.Xaml.XamlRoot.Changed.remove
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public event global::Windows.Foundation.TypedEventHandler<global::Windows.UI.Xaml.XamlRoot, global::Windows.UI.Xaml.XamlRootChangedEventArgs> Changed
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.UI.Xaml
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented]
#endif
public partial class XamlRootChangedEventArgs
Expand Down
17 changes: 17 additions & 0 deletions src/Uno.UI/UI/Xaml/UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace Windows.UI.Xaml
public partial class UIElement : DependencyObject, IXUidProvider
{
private readonly SerialDisposable _clipSubscription = new SerialDisposable();
private XamlRoot _xamlRoot = null;
private string _uid;

/// <summary>
Expand Down Expand Up @@ -158,6 +159,22 @@ private static void OnRenderTransformOriginChanged(object dependencyObject, Depe
}
#endregion

public XamlRoot XamlRoot
{
get
{
if (IsLoaded)
{
return _xamlRoot ?? XamlRoot.Current;
}
return null;
}
set
{
_xamlRoot = value;
}
}

public GeneralTransform TransformToVisual(UIElement visual)
=> new MatrixTransform { Matrix = new Matrix(GetTransform(from: this, to: visual)) };

Expand Down
18 changes: 17 additions & 1 deletion src/Uno.UI/UI/Xaml/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,31 @@ private void InitializeCommon()

public UIElement Content
{
get { return InternalGetContent(); }
get => InternalGetContent();
set
{
var oldContent = Content;
if (oldContent != null)
{
oldContent.IsWindowRoot = false;

if (oldContent is FrameworkElement oldRoot)
{
oldRoot.SizeChanged -= RootSizeChanged;
}
}
if (value != null)
{
value.IsWindowRoot = true;
}

InternalSetContent(value);

if (value is FrameworkElement newRoot)
{
newRoot.SizeChanged += RootSizeChanged;
}
XamlRoot.Current.NotifyChanged();
}
}

Expand Down Expand Up @@ -102,6 +113,11 @@ internal IDisposable RegisterSizeChangedEvent(Windows.UI.Xaml.WindowSizeChangedE
);
}

private void RootSizeChanged(object sender, SizeChangedEventArgs args)
{
XamlRoot.Current.NotifyChanged();
}

private void RaiseSizeChanged(Windows.UI.Core.WindowSizeChangedEventArgs windowSizeChangedEventArgs)
{
SizeChanged?.Invoke(this, windowSizeChangedEventArgs);
Expand Down
22 changes: 22 additions & 0 deletions src/Uno.UI/UI/Xaml/XamlRoot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#nullable enable

using Windows.Foundation;

namespace Windows.UI.Xaml
{
public partial class XamlRoot
{
internal static XamlRoot Current { get; } = new XamlRoot();

public event TypedEventHandler<XamlRoot, XamlRootChangedEventArgs>? Changed;

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

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

internal void NotifyChanged()
{
Changed?.Invoke(this, new XamlRootChangedEventArgs());
}
}
}
9 changes: 9 additions & 0 deletions src/Uno.UI/UI/Xaml/XamlRootChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Windows.UI.Xaml
{
public partial class XamlRootChangedEventArgs
{
internal XamlRootChangedEventArgs()
{
}
}
}

0 comments on commit 1cd99c5

Please sign in to comment.