Skip to content

Commit

Permalink
Merge pull request #11425 from unoplatform/dev/mazi/island-dialogs
Browse files Browse the repository at this point in the history
Add support for `ContentDialog` in Uno Islands
  • Loading branch information
MartinZikmund authored Feb 23, 2023
2 parents fe75d76 + 2041404 commit 82fc8e7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/SamplesApp/UnoIslands.Shared/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
<TextBlock Text="{Binding Phone}" />
<TextBox Text="{Binding Note, Mode=TwoWay}" PlaceholderText="Enter a note..." />
</StackPanel>
<Button Click="Button_Click">Test dialog</Button>
</Grid>
</Page>
12 changes: 12 additions & 0 deletions src/SamplesApp/UnoIslands.Shared/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,17 @@ public MainPage()
{
this.InitializeComponent();
}

private async void Button_Click(object sender, RoutedEventArgs e)
{
var dialog = new ContentDialog()
{
XamlRoot = XamlRoot,
Title = "Hello",
Content = "Hello World!",
CloseButtonText = "Close"
};
await dialog.ShowAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void SizeAndPositionContentInPopup()
double xOffset = 0;
double yOffset = 0;

var windowBounds = Xaml.Window.Current.Bounds;
var xamlRootSize = XamlRoot?.Size ?? Xaml.Window.Current.Bounds.Size;

var flowDirection = FlowDirection;

Expand All @@ -216,13 +216,13 @@ private void SizeAndPositionContentInPopup()

if (m_placementMode == PlacementMode.EntireControlInPopup)
{
Height = windowBounds.Height;
Width = windowBounds.Width;
Height = xamlRootSize.Height;
Width = xamlRootSize.Width;
}
else if (m_tpLayoutRootPart != null)
{
m_tpLayoutRootPart.Height = windowBounds.Height;
m_tpLayoutRootPart.Width = windowBounds.Width;
m_tpLayoutRootPart.Height = xamlRootSize.Height;
m_tpLayoutRootPart.Width = xamlRootSize.Width;
}

// Uno - omitted code related to display regions, which aren't currently supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Text;

using Uno.Extensions;
using Uno.Foundation.Logging;
using Uno.UI;
Expand All @@ -12,6 +11,7 @@
using Windows.UI.ViewManagement;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using WinUICoreServices = Uno.UI.Xaml.Core.CoreServices;

namespace Windows.UI.Xaml.Controls
{
Expand Down Expand Up @@ -57,6 +57,12 @@ protected override Size ArrangeOverride(Size finalSize)

private Size CalculateDialogAvailableSize(Size availableSize)
{
// Skip calculation if in the context of Uno Islands.
if (WinUICoreServices.Instance.ContentRootCoordinator.CoreWindowContentRoot is null)
{
return availableSize;
}

var visibleBounds = ApplicationView.GetForCurrentView().TrueVisibleBounds;

if (availableSize.Width > visibleBounds.Width)
Expand All @@ -73,7 +79,15 @@ private Size CalculateDialogAvailableSize(Size availableSize)

private Rect CalculateDialogPlacement(Size desiredSize, Size finalSize)
{
var visibleBounds = ApplicationView.GetForCurrentView().TrueVisibleBounds;
Rect visibleBounds;
if (WinUICoreServices.Instance.ContentRootCoordinator.CoreWindowContentRoot is null)
{
visibleBounds = XamlRoot?.Bounds ?? new Rect(0, 0, finalSize.Width, finalSize.Height);
}
else
{
visibleBounds = ApplicationView.GetForCurrentView().TrueVisibleBounds;
}

var maximumWidth = Math.Min(visibleBounds.Width, finalSize.Width);
var maximumHeight = Math.Min(visibleBounds.Height, finalSize.Height);
Expand Down
5 changes: 3 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/Popup/Popup.WithPopupRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Windows.UI.Xaml.Media;
using Uno.UI;
using Uno.UI.Xaml.Core;
using CoreServices = Uno.UI.Xaml.Core.CoreServices;

namespace Windows.UI.Xaml.Controls.Primitives;

Expand Down Expand Up @@ -85,7 +86,7 @@ partial void OnIsOpenChangedPartialNative(bool oldIsOpen, bool newIsOpen)
{
#if !HAS_UNO_WINUI
// In UWP, XamlRoot is set automatically to CoreWindow XamlRoot if not set beforehand.
if (XamlRoot is null)
if (XamlRoot is null && Child?.XamlRoot is null)
{
XamlRoot = CoreServices.Instance.ContentRootCoordinator.CoreWindowContentRoot.XamlRoot;
}
Expand All @@ -94,7 +95,7 @@ partial void OnIsOpenChangedPartialNative(bool oldIsOpen, bool newIsOpen)
#if !__SKIA__ // The OpenPopup method should be moved out of Window in general https://github.com/unoplatform/uno/issues/8978
_closePopup.Disposable = Window.Current.OpenPopup(this);
#else
var currentXamlRoot = XamlRoot ?? CoreServices.Instance.ContentRootCoordinator.CoreWindowContentRoot.XamlRoot;
var currentXamlRoot = XamlRoot ?? Child?.XamlRoot ?? CoreServices.Instance.ContentRootCoordinator.CoreWindowContentRoot.XamlRoot;
_closePopup.Disposable = currentXamlRoot?.OpenPopup(this);
#endif
PopupPanel.Visibility = Visibility.Visible;
Expand Down

0 comments on commit 82fc8e7

Please sign in to comment.