Browse Source

Reimplement new child window support.

pull/3833/head
Dariusz Komosinski 6 years ago
parent
commit
d69718ab61
  1. 2
      samples/ControlCatalog/Pages/DialogsPage.xaml
  2. 15
      samples/ControlCatalog/Pages/DialogsPage.xaml.cs
  3. 84
      src/Avalonia.Controls/Window.cs

2
samples/ControlCatalog/Pages/DialogsPage.xaml

@ -11,5 +11,7 @@
<Button Name="DecoratedWindowDialog">Decorated window (dialog)</Button>
<Button Name="Dialog">Dialog</Button>
<Button Name="DialogNoTaskbar">Dialog (No taskbar icon)</Button>
<Button Name="OwnedWindow">Owned window</Button>
<Button Name="OwnedWindowNoTaskbar">Owned window (No taskbar icon)</Button>
</StackPanel>
</UserControl>

15
samples/ControlCatalog/Pages/DialogsPage.xaml.cs

@ -93,6 +93,21 @@ namespace ControlCatalog.Pages
window.ShowInTaskbar = false;
window.ShowDialog(GetWindow());
};
this.FindControl<Button>("OwnedWindow").Click += delegate
{
var window = CreateSampleWindow();
window.Show(GetWindow());
};
this.FindControl<Button>("OwnedWindowNoTaskbar").Click += delegate
{
var window = CreateSampleWindow();
window.ShowInTaskbar = false;
window.Show(GetWindow());
};
}
private Window CreateSampleWindow()

84
src/Avalonia.Controls/Window.cs

@ -69,7 +69,7 @@ namespace Avalonia.Controls
/// </summary>
public class Window : WindowBase, IStyleable, IFocusScope, ILayoutRoot
{
private List<Window> _children = new List<Window>();
private readonly List<(Window child, bool isDialog)> _children = new List<(Window, bool)>();
/// <summary>
/// Defines the <see cref="SizeToContent"/> property.
@ -376,7 +376,7 @@ namespace Avalonia.Controls
private void CloseInternal()
{
foreach (var child in _children.ToList())
foreach (var (child, _) in _children.ToList())
{
// if we HandleClosing() before then there will be no children.
child.CloseInternal();
@ -399,7 +399,7 @@ namespace Avalonia.Controls
{
bool canClose = true;
foreach (var child in _children.ToList())
foreach (var (child, _) in _children.ToList())
{
if (!child.HandleClosing())
{
@ -472,6 +472,27 @@ namespace Avalonia.Controls
/// The window has already been closed.
/// </exception>
public override void Show()
{
ShowCore(null);
}
/// <summary>
/// Shows the window as a child of <paramref name="parent"/>.
/// </summary>
/// <exception cref="InvalidOperationException">
/// The window has already been closed.
/// </exception>
public void Show(Window parent)
{
if (parent is null)
{
throw new ArgumentNullException(nameof(parent), "Showing a child window requires valid parent.");
}
ShowCore(parent);
}
private void ShowCore(Window parent)
{
if (PlatformImpl == null)
{
@ -483,7 +504,7 @@ namespace Avalonia.Controls
return;
}
this.RaiseEvent(new RoutedEventArgs(WindowOpenedEvent));
RaiseEvent(new RoutedEventArgs(WindowOpenedEvent));
EnsureInitialized();
IsVisible = true;
@ -504,6 +525,14 @@ namespace Avalonia.Controls
using (BeginAutoSizing())
{
if (parent != null)
{
PlatformImpl?.SetParent(parent.PlatformImpl);
}
Owner = parent;
parent?.AddChild(this, false);
PlatformImpl?.Show();
Renderer?.Start();
}
@ -571,9 +600,9 @@ namespace Avalonia.Controls
using (BeginAutoSizing())
{
PlatformImpl.SetParent(owner.PlatformImpl);
PlatformImpl?.SetParent(owner.PlatformImpl);
Owner = owner;
owner.AddChild(this);
owner.AddChild(this, true);
PlatformImpl?.Show();
Renderer?.Start();
@ -598,28 +627,57 @@ namespace Avalonia.Controls
private void UpdateEnabled()
{
PlatformImpl.SetEnabled(_children.Count == 0);
bool isEnabled = true;
foreach (var (_, isDialog) in _children)
{
if (isDialog)
{
isEnabled = false;
break;
}
}
PlatformImpl.SetEnabled(isEnabled);
}
private void AddChild(Window window)
private void AddChild(Window window, bool isDialog)
{
_children.Add(window);
_children.Add((window, isDialog));
UpdateEnabled();
}
private void RemoveChild(Window window)
{
_children.Remove(window);
for (int i = _children.Count - 1; i >= 0; i--)
{
var (child, _) = _children[i];
if (ReferenceEquals(child, window))
{
_children.RemoveAt(i);
}
}
UpdateEnabled();
}
private void OnGotInputWhenDisabled()
{
var firstChild = _children.FirstOrDefault();
Window firstDialogChild = null;
foreach (var (child, isDialog) in _children)
{
if (isDialog)
{
firstDialogChild = child;
break;
}
}
if (firstChild != null)
if (firstDialogChild != null)
{
firstChild.OnGotInputWhenDisabled();
firstDialogChild.OnGotInputWhenDisabled();
}
else
{

Loading…
Cancel
Save