From 255708de24a20e6356fcdadadc6a07b253e965b4 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 14 Sep 2014 23:33:47 +0200 Subject: [PATCH] Make app shutdown when main window closed. --- Perspex.Windows/Window.cs | 18 ++++++++++++++---- Perspex/Application.cs | 9 +++++++++ Perspex/ICloseable.cs | 15 +++++++++++++++ Perspex/Perspex.csproj | 1 + TestApplication/Program.cs | 2 +- 5 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 Perspex/ICloseable.cs diff --git a/Perspex.Windows/Window.cs b/Perspex.Windows/Window.cs index 41f24927e2..ba6ccf6bc5 100644 --- a/Perspex.Windows/Window.cs +++ b/Perspex.Windows/Window.cs @@ -23,7 +23,7 @@ namespace Perspex.Windows using Perspex.Windows.Threading; using Splat; - public class Window : ContentControl, ILayoutRoot, IRendered + public class Window : ContentControl, ILayoutRoot, IRendered, ICloseable { private UnmanagedMethods.WndProc wndProcDelegate; @@ -75,6 +75,8 @@ namespace Perspex.Windows }); } + public event EventHandler Closed; + public Size ClientSize { get @@ -166,6 +168,14 @@ namespace Perspex.Windows } } + private void OnClosed() + { + if (this.Closed != null) + { + this.Closed(this, EventArgs.Empty); + } + } + [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Using Win32 naming for consistency.")] private IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) { @@ -175,9 +185,9 @@ namespace Perspex.Windows switch ((UnmanagedMethods.WindowsMessage)msg) { - ////case UnmanagedMethods.WindowsMessage.WM_DESTROY: - //// this.OnClosed(); - //// break; + case UnmanagedMethods.WindowsMessage.WM_DESTROY: + this.OnClosed(); + break; case UnmanagedMethods.WindowsMessage.WM_KEYDOWN: WindowsKeyboardDevice.Instance.UpdateKeyStates(); diff --git a/Perspex/Application.cs b/Perspex/Application.cs index 9e7127c46e..13dcfe0c7d 100644 --- a/Perspex/Application.cs +++ b/Perspex/Application.cs @@ -6,9 +6,11 @@ namespace Perspex { + using System.Threading; using Perspex.Controls; using Perspex.Input; using Perspex.Styling; + using Perspex.Threading; using Splat; public class Application @@ -85,5 +87,12 @@ namespace Perspex Locator.CurrentMutable.Register(() => this.InputManager, typeof(IInputManager)); Locator.CurrentMutable.Register(() => styler, typeof(IStyler)); } + + public void Run(ICloseable closable) + { + DispatcherFrame frame = new DispatcherFrame(); + closable.Closed += (s, e) => frame.Continue = false; + Dispatcher.PushFrame(frame); + } } } diff --git a/Perspex/ICloseable.cs b/Perspex/ICloseable.cs new file mode 100644 index 0000000000..23ec938f4f --- /dev/null +++ b/Perspex/ICloseable.cs @@ -0,0 +1,15 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex +{ + using System; + + public interface ICloseable + { + event EventHandler Closed; + } +} diff --git a/Perspex/Perspex.csproj b/Perspex/Perspex.csproj index a045aeac5a..831f718420 100644 --- a/Perspex/Perspex.csproj +++ b/Perspex/Perspex.csproj @@ -101,6 +101,7 @@ + diff --git a/TestApplication/Program.cs b/TestApplication/Program.cs index e01e484606..d5ec5d53a2 100644 --- a/TestApplication/Program.cs +++ b/TestApplication/Program.cs @@ -234,7 +234,7 @@ namespace TestApplication }; window.Show(); - Dispatcher.Run(); + Application.Current.Run(window); } } }