Skip to content

Commit

Permalink
feat: Application.Exit on GTK and WPF
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed May 17, 2021
1 parent 101f35a commit 55bfe8a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/Uno.UI.Runtime.Skia.Gtk/GtkApplicationExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Uno.UI.Xaml;
using Windows.UI.Xaml;

namespace Uno.UI.Runtime.Skia
{
public class GtkApplicationExtension : IApplicationExtension
{
private readonly Application _owner;

public GtkApplicationExtension(Application owner)
{
_owner = owner ?? throw new ArgumentNullException(nameof(owner));
}

public bool CanExit => true;

public void Exit() => Gtk.Application.Default.Quit();
}
}
4 changes: 3 additions & 1 deletion src/Uno.UI.Runtime.Skia.Gtk/GtkHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
using Windows.UI.Xaml.Controls;
using Gtk;
using Uno.UI.Runtime.Skia.GTK.Extensions.Helpers;

using Uno.UI.Xaml;

namespace Uno.UI.Runtime.Skia
{
public class GtkHost : ISkiaHost
Expand Down Expand Up @@ -46,6 +47,7 @@ public void Run()
SetupTheme();

ApiExtensibility.Register(typeof(Windows.UI.Core.ICoreWindowExtension), o => new GtkCoreWindowExtension(o));
ApiExtensibility.Register<Windows.UI.Xaml.Application>(typeof(IApplicationExtension), o => new GtkApplicationExtension(o));
ApiExtensibility.Register(typeof(Windows.UI.ViewManagement.IApplicationViewExtension), o => new GtkApplicationViewExtension(o));
ApiExtensibility.Register(typeof(ISystemThemeHelperExtension), o => new GtkSystemThemeHelperExtension(o));
ApiExtensibility.Register(typeof(Windows.Graphics.Display.IDisplayInformationExtension), o => _displayInformationExtension ??= new GtkDisplayInformationExtension(o, _window));
Expand Down
20 changes: 20 additions & 0 deletions src/Uno.UI.Runtime.Skia.Wpf/WpfApplicationExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Uno.UI.Xaml;
using Windows.UI.Xaml;

namespace Uno.UI.Runtime.Skia.Wpf
{
public class WpfApplicationExtension : IApplicationExtension
{
private readonly Application _owner;

public WpfApplicationExtension(Application owner)
{
_owner = owner ?? throw new ArgumentNullException(nameof(owner));
}

public bool CanExit => true;

public void Exit() => System.Windows.Application.Current.Shutdown();
}
}
3 changes: 3 additions & 0 deletions src/Uno.UI.Runtime.Skia.Wpf/WpfHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using WpfCanvas = System.Windows.Controls.Canvas;
using WpfControl = System.Windows.Controls.Control;
using WpfFrameworkPropertyMetadata = System.Windows.FrameworkPropertyMetadata;
using Uno.UI.Xaml;
using Uno.UI.Runtime.Skia.Wpf;

namespace Uno.UI.Skia.Platform
{
Expand All @@ -43,6 +45,7 @@ static WpfHost()
DefaultStyleKeyProperty.OverrideMetadata(typeof(WpfHost), new WpfFrameworkPropertyMetadata(typeof(WpfHost)));

ApiExtensibility.Register(typeof(Windows.UI.Core.ICoreWindowExtension), o => new WpfCoreWindowExtension(o));
ApiExtensibility.Register<Windows.UI.Xaml.Application>(typeof(IApplicationExtension), o => new WpfApplicationExtension(o));
ApiExtensibility.Register(typeof(Windows.UI.ViewManagement.IApplicationViewExtension), o => new WpfApplicationViewExtension(o));
ApiExtensibility.Register(typeof(ISystemThemeHelperExtension), o => new WpfSystemThemeHelperExtension(o));
ApiExtensibility.Register(typeof(IDisplayInformationExtension), o => new WpfDisplayInformationExtension(o));
Expand Down
19 changes: 18 additions & 1 deletion src/Uno.UI/UI/Xaml/Application.Skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
using Uno.UI;
using Uno.UI.Xaml;
using Uno.Foundation.Extensibility;
using Microsoft.Extensions.Logging;

namespace Windows.UI.Xaml
{
public partial class Application : IApplicationEvents
{
private static bool _startInvoked = false;
private static string[] _args;
private readonly IApplicationExtension _applicationExtension;

internal ISkiaHost Host { get; set; }

Expand All @@ -32,7 +34,7 @@ public Application()
throw new InvalidOperationException("The application must be started using Application.Start first, e.g. Windows.UI.Xaml.Application.Start(_ => new App());");
}

CoreDispatcher.Main.RunAsync(CoreDispatcherPriority.Normal, Initialize);
ApiExtensibility.CreateInstance(this, out _applicationExtension);
}

internal static void Start(global::Windows.UI.Xaml.ApplicationInitializationCallback callback, string[] args)
Expand All @@ -41,6 +43,21 @@ internal static void Start(global::Windows.UI.Xaml.ApplicationInitializationCall
Start(callback);
}

public void Exit()
{
if (_applicationExtension != null && _applicationExtension.CanExit)
{
_applicationExtension.Exit();
}
else
{
if (this.Log().IsEnabled(LogLevel.Warning))
{
this.Log().LogWarning("This platform does not support application exit.");
}
}
}

static partial void StartPartial(ApplicationInitializationCallback callback)
{
_startInvoked = true;
Expand Down
8 changes: 6 additions & 2 deletions src/Uno.UI/UI/Xaml/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Uno.Helpers.Theming;
using Windows.UI.ViewManagement;
using Uno.Extensions;
using Microsoft.Extensions.Logging;

#if HAS_UNO_WINUI
using LaunchActivatedEventArgs = Microsoft.UI.Xaml.LaunchActivatedEventArgs;
Expand Down Expand Up @@ -182,11 +183,14 @@ public void OnSystemThemeChanged()
UISettings.OnColorValuesChanged();
}

#if !__ANDROID__ && !__MACOS__
#if !__ANDROID__ && !__MACOS__ && !__SKIA__
[NotImplemented]
public void Exit()
{

if (this.Log().IsEnabled(LogLevel.Warning))
{
this.Log().LogWarning("This platform does not support application exit.");
}
}
#endif

Expand Down
9 changes: 9 additions & 0 deletions src/Uno.UI/UI/Xaml/IApplicationExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Uno.UI.Xaml
{
internal interface IApplicationExtension
{
bool CanExit { get; }

void Exit();
}
}

0 comments on commit 55bfe8a

Please sign in to comment.