Skip to content

Commit

Permalink
Merge pull request #17919 from unoplatform/dev/mazi/window-unload
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund authored Aug 17, 2024
2 parents 3d7fdf2 + 1c62c54 commit 77d9c05
Show file tree
Hide file tree
Showing 33 changed files with 567 additions and 199 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Page
x:Class="UITests.Microsoft_UI_Windowing.AppWindowClosing"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:UITests.Microsoft_UI_Windowing"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
x:DefaultBindMode="TwoWay"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">

<StackPanel Padding="8" Spacing="8">
<TextBlock Text="This target platform supports multiple windows" x:Name="LogTextBlock" />
<Button x:Name="OpenWindowButton" Content="Open secondary Window" Click="OnOpenWindow" />
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Threading.Tasks;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using SamplesApp;
using Uno.Disposables;
using Uno.UI.Common;
using Uno.UI.Samples.Controls;
using Uno.UI.Samples.UITests.Helpers;
using Windows.Graphics;

#if !WINDOWS_UWP && !WINAPPSDK
using Uno.UI.Xaml;
using Uno.UI.Xaml.Controls;
using WinUICoreServices = Uno.UI.Xaml.Core.CoreServices;
#endif

namespace UITests.Microsoft_UI_Windowing;

[Sample(
"Windowing",
IsManualTest = true,
Description =
"Click the button to open a secondary window. " +
"Try to close it via title bar close button - a dialog should appear and window will remain open. " +
"Close it via the 'Close' button - no dialog should appear.")]
public sealed partial class AppWindowClosing : Page
{
public AppWindowClosing()
{
this.InitializeComponent();
if (!SupportsMultipleWindows)
{
LogTextBlock.Text = "This platform does not support multiple windows.";
OpenWindowButton.IsEnabled = false;
}
}

private bool SupportsMultipleWindows =>
#if HAS_UNO
NativeWindowFactory.SupportsMultipleWindows;
#else
true;
#endif

private void OnOpenWindow(object sender, RoutedEventArgs args)
{
var secondaryWindow = new Window();
var content = new Border();
content.Child = new Button { Content = "Close", Command = new DelegateCommand(() => secondaryWindow.Close()), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
secondaryWindow.Content = content;
secondaryWindow.AppWindow.Closing += OnSecondaryWindowClosing;
secondaryWindow.Activate();

async void OnSecondaryWindowClosing(AppWindow sender, AppWindowClosingEventArgs args)
{
args.Cancel = true;
var dialog = new ContentDialog
{
Title = "Closing the window canceled.",
Content = "The secondary window should remain open.",
CloseButtonText = "OK",
XamlRoot = content.XamlRoot,
};

await dialog.ShowAsync();
}
}
}
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Microsoft_UI_Windowing\AppWindowClosing.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Microsoft_UI_Windowing\OverlappedPresenterTests.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -5466,6 +5470,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Microsoft_UI_Windowing\AppWindowPresenters.xaml.cs">
<DependentUpon>AppWindowPresenters.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Microsoft_UI_Windowing\AppWindowClosing.xaml.cs">
<DependentUpon>AppWindowClosing.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Microsoft_UI_Windowing\OverlappedPresenterTests.xaml.cs">
<DependentUpon>OverlappedPresenterTests.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal NativeWindowFactoryExtension()
{
}

public bool SupportsClosingCancellation => true;

public bool SupportsMultipleWindows => true;

public INativeWindowWrapper CreateWindow(Window window, XamlRoot xamlRoot)
Expand Down
19 changes: 1 addition & 18 deletions src/Uno.UI.Runtime.Skia.Gtk/UI/Controls/GtkWindowWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,11 @@ private void OnWindowShown(object? sender, EventArgs e)

public override void Close()
{
base.Close();
if (_wasShown)
{
_gtkWindow.Close();
}
else
{
// Simulate closing to be in line with other targets.
OnWindowClosed(null, EventArgs.Empty);
}
}

public override void ExtendContentIntoTitleBar(bool extend)
Expand All @@ -96,8 +92,6 @@ public override void ExtendContentIntoTitleBar(bool extend)

private void OnWindowClosed(object? sender, EventArgs e)
{
RaiseClosed();

var windows = global::Gtk.Window.ListToplevels();
if (!windows.Where(w => w is UnoGtkWindow && w != NativeWindow).Any())
{
Expand All @@ -114,17 +108,6 @@ private void OnWindowClosing(object sender, DeleteEventArgs args)
return;
}

var manager = SystemNavigationManagerPreview.GetForCurrentView();
if (!manager.HasConfirmedClose)
{
if (!manager.RequestAppClose())
{
// App closing was prevented, handle event
args.RetVal = true;
return;
}
}

// Closing should continue, perform suspension.
WinUIApplication.Current.RaiseSuspending();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Uno.Foundation.Logging;
using Uno.UI.Dispatching;
using Uno.UI.Hosting;
using Uno.UI.Runtime.Skia.Gtk.Extensions.UI.Xaml.Controls;
using Uno.UI.Xaml.Controls;
using Uno.UI.Xaml.Core;
using Uno.WinUI.Runtime.Skia.Linux.FrameBuffer;
Expand All @@ -16,6 +15,7 @@
using Uno.Helpers;
using WUX = Microsoft.UI.Xaml;
using System.Threading.Tasks;
using Uno.UI.Runtime.Skia.Linux.FrameBuffer.UI;

namespace Uno.UI.Runtime.Skia.Linux.FrameBuffer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ internal void RaiseNativeSizeChanged(Size newWindowSize)

internal void OnNativeActivated(CoreWindowActivationState state) => ActivationState = state;

internal void OnNativeClosed() => RaiseClosed();
internal void OnNativeClosed() => RaiseClosing();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Uno.WinUI.Runtime.Skia.Linux.FrameBuffer.UI;
using Microsoft.UI.Xaml;

namespace Uno.UI.Runtime.Skia.Gtk.Extensions.UI.Xaml.Controls;
namespace Uno.UI.Runtime.Skia.Linux.FrameBuffer.UI;

internal class NativeWindowFactoryExtension : INativeWindowFactoryExtension
{
Expand All @@ -20,6 +20,8 @@ internal NativeWindowFactoryExtension(IXamlRootHost host)
_host = host;
}

public bool SupportsClosingCancellation => false;

public bool SupportsMultipleWindows => false;

public INativeWindowWrapper CreateWindow(Window window, XamlRoot xamlRoot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ private MacOSNativeWindowFactoryExtension()
{
}

public bool SupportsClosingCancellation => true;

public bool SupportsMultipleWindows => true;

public INativeWindowWrapper CreateWindow(Window window, XamlRoot xamlRoot)
Expand Down
13 changes: 0 additions & 13 deletions src/Uno.UI.Runtime.Skia.MacOS/MacOSWindowWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public MacOSWindowWrapper(MacOSWindowNative nativeWindow, Window window, XamlRoo
_window = nativeWindow;

nativeWindow.Host.Closing += OnWindowClosing;
nativeWindow.Host.Closed += OnWindowClosed;
nativeWindow.Host.RasterizationScaleChanged += Host_RasterizationScaleChanged;
nativeWindow.Host.SizeChanged += (_, s) => OnHostSizeChanged(s);
OnHostSizeChanged(initialSize);
Expand Down Expand Up @@ -52,22 +51,10 @@ private void OnWindowClosing(object? sender, CancelEventArgs e)
e.Cancel = true;
}

var manager = SystemNavigationManagerPreview.GetForCurrentView();
if (!manager.HasConfirmedClose)
{
if (!manager.RequestAppClose())
{
e.Cancel = true;
return;
}
}

// All prerequisites passed, can safely close.
e.Cancel = false;
}

private void OnWindowClosed(object? sender, EventArgs e) => RaiseClosed();

protected override IDisposable ApplyFullScreenPresenter()
{
NativeUno.uno_window_enter_full_screen(_window.Handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal NativeWindowFactoryExtension()
{
}

public bool SupportsClosingCancellation => true;

public bool SupportsMultipleWindows => true;

public INativeWindowWrapper CreateWindow(Window window, XamlRoot xamlRoot)
Expand Down
19 changes: 5 additions & 14 deletions src/Uno.UI.Runtime.Skia.Wpf/UI/Controls/WpfWindowWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public WpfWindowWrapper(UnoWpfWindow wpfWindow, WinUIWindow window, XamlRoot xam
_wpfWindow.Deactivated += OnNativeDeactivated;
_wpfWindow.IsVisibleChanged += OnNativeIsVisibleChanged;
_wpfWindow.Closing += OnNativeClosing;
_wpfWindow.Closed += OnNativeClosed;
_wpfWindow.DpiChanged += OnNativeDpiChanged;
_wpfWindow.StateChanged += OnNativeStateChanged;
_wpfWindow.Host.SizeChanged += (_, e) => OnHostSizeChanged(e.NewSize);
Expand Down Expand Up @@ -81,7 +80,11 @@ protected override void ShowCore()

public override void Activate() => _wpfWindow.Activate();

public override void Close() => _wpfWindow.Close();
public override void Close()
{
base.Close();
_wpfWindow.Close();
}

public override void ExtendContentIntoTitleBar(bool extend)
{
Expand All @@ -95,8 +98,6 @@ private void OnHostSizeChanged(Size size)
VisibleBounds = Bounds;
}

private void OnNativeClosed(object? sender, EventArgs e) => RaiseClosed();

private void OnNativeClosing(object? sender, CancelEventArgs e)
{
var closingArgs = RaiseClosing();
Expand All @@ -106,16 +107,6 @@ private void OnNativeClosing(object? sender, CancelEventArgs e)
return;
}

var manager = SystemNavigationManagerPreview.GetForCurrentView();
if (!manager.HasConfirmedClose)
{
if (!manager.RequestAppClose())
{
e.Cancel = true;
return;
}
}

// Closing should continue, perform suspension.
WinUIApplication.Current.RaiseSuspending();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal X11NativeWindowFactoryExtension()
{
}

public bool SupportsClosingCancellation => true;

public bool SupportsMultipleWindows => true;

public INativeWindowWrapper CreateWindow(Window window, XamlRoot xamlRoot)
Expand Down
13 changes: 1 addition & 12 deletions src/Uno.UI.Runtime.Skia.X11/X11WindowWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public override void Activate()

public override void Close()
{
base.Close();
var x11Window = _host.RootX11Window;
if (this.Log().IsEnabled(LogLevel.Information))
{
Expand All @@ -95,8 +96,6 @@ public override void Close()
{
X11XamlRootHost.Close(x11Window);
}

RaiseClosed();
}

public override void ExtendContentIntoTitleBar(bool extend)
Expand All @@ -113,16 +112,6 @@ private void OnWindowClosing()
return;
}

var manager = SystemNavigationManagerPreview.GetForCurrentView();
if (!manager.HasConfirmedClose)
{
if (!manager.RequestAppClose())
{
// App closing was prevented
return;
}
}

// All prerequisites passed, can safely close.
Close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml.Controls.WindowClosed"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App70"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Closed="Window_Closed"
Title="Test title"
mc:Ignorable="d">

<Border Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Windows.Foundation;
using Windows.Foundation.Collections;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml.Controls;

public sealed partial class WindowClosed : Window
{
public WindowClosed()
{
this.InitializeComponent();
}

internal bool ClosedExecuted { get; private set; }

private void Window_Closed(object sender, WindowEventArgs args)
{
ClosedExecuted = true;
}
}
Loading

0 comments on commit 77d9c05

Please sign in to comment.