Browse Source

Added interface and implementation for application exiting. Will automatically close all windows when called.

pull/459/head
Jeremy Koritzinsky 11 years ago
parent
commit
bbe460c18f
  1. 31
      src/Perspex.Application/Application.cs
  2. 24
      src/Perspex.Controls/IApplicationLifecycle.cs
  3. 1
      src/Perspex.Controls/Perspex.Controls.csproj
  4. 17
      src/Perspex.Controls/TopLevel.cs
  5. 6
      src/Perspex.Controls/Window.cs

31
src/Perspex.Application/Application.cs

@ -32,7 +32,7 @@ namespace Perspex
/// method.
/// - Tracks the lifetime of the application.
/// </remarks>
public class Application : IGlobalDataTemplates, IGlobalStyles, IStyleRoot
public class Application : IGlobalDataTemplates, IGlobalStyles, IStyleRoot, IApplicationLifecycle
{
static Action _platformInitializationCallback;
@ -60,6 +60,7 @@ namespace Perspex
}
PerspexLocator.CurrentMutable.BindToSelf(this);
OnExit += OnExiting;
}
/// <summary>
@ -146,10 +147,35 @@ namespace Perspex
public void Run(ICloseable closable)
{
var source = new CancellationTokenSource();
closable.Closed += OnExiting;
closable.Closed += (s, e) => source.Cancel();
Dispatcher.UIThread.MainLoop(source.Token);
}
/// <summary>
/// Exits the application
/// </summary>
public void Exit()
{
OnExit?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Sent when the application is exiting.
/// </summary>
public event EventHandler OnExit;
/// <summary>
/// Called when the application is exiting.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnExiting(object sender, EventArgs e)
{
}
/// <summary>
/// Register's the services needed by Perspex.
/// </summary>
@ -168,7 +194,8 @@ namespace Perspex
.Bind<IKeyboardNavigationHandler>().ToTransient<KeyboardNavigationHandler>()
.Bind<IStyler>().ToConstant(_styler)
.Bind<ILayoutManager>().ToTransient<LayoutManager>()
.Bind<IRenderQueueManager>().ToTransient<RenderQueueManager>();
.Bind<IRenderQueueManager>().ToTransient<RenderQueueManager>()
.Bind<IApplicationLifecycle>().ToConstant(this);
}
/// <summary>

24
src/Perspex.Controls/IApplicationLifecycle.cs

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Perspex.Controls
{
/// <summary>
/// Sends events about the application lifecycle.
/// </summary>
public interface IApplicationLifecycle
{
/// <summary>
/// Sent when the application is exiting.
/// </summary>
event EventHandler OnExit;
/// <summary>
/// Exits the application.
/// </summary>
void Exit();
}
}

1
src/Perspex.Controls/Perspex.Controls.csproj

@ -50,6 +50,7 @@
<Compile Include="Generators\ItemContainer.cs" />
<Compile Include="Generators\TreeContainerIndex.cs" />
<Compile Include="HotkeyManager.cs" />
<Compile Include="IApplicationLifecycle.cs" />
<Compile Include="INameScope.cs" />
<Compile Include="IPseudoClasses.cs" />
<Compile Include="DropDownItem.cs" />

17
src/Perspex.Controls/TopLevel.cs

@ -48,6 +48,7 @@ namespace Perspex.Controls
private readonly IInputManager _inputManager;
private readonly IAccessKeyHandler _accessKeyHandler;
private readonly IKeyboardNavigationHandler _keyboardNavigationHandler;
private readonly IApplicationLifecycle _applicationLifecycle;
private Size _clientSize;
private bool _isActive;
@ -92,6 +93,7 @@ namespace Perspex.Controls
_inputManager = TryGetService<IInputManager>(dependencyResolver);
_keyboardNavigationHandler = TryGetService<IKeyboardNavigationHandler>(dependencyResolver);
_renderQueueManager = TryGetService<IRenderQueueManager>(dependencyResolver);
_applicationLifecycle = TryGetService<IApplicationLifecycle>(dependencyResolver);
(TryGetService<ITopLevelRenderer>(dependencyResolver) ?? new DefaultTopLevelRenderer()).Attach(this);
PlatformImpl.SetInputRoot(this);
@ -112,6 +114,7 @@ namespace Perspex.Controls
.Select(
x => (x as InputElement)?.GetObservable(CursorProperty) ?? Observable.Empty<Cursor>())
.Switch().Subscribe(cursor => PlatformImpl.SetCursor(cursor?.PlatformCursor));
_applicationLifecycle.OnExit += HandleApplicationExiting;
}
/// <summary>
@ -347,6 +350,20 @@ namespace Perspex.Controls
private void HandleClosed()
{
Closed?.Invoke(this, EventArgs.Empty);
_applicationLifecycle.OnExit -= OnApplicationExiting;
}
private void OnApplicationExiting(object sender, EventArgs args)
{
}
/// <summary>
/// Handles the application exiting, either from the last window closing, or a call to <see cref="IApplicationLifecycle.Exit"/>.
/// </summary>
protected virtual void HandleApplicationExiting()
{
}
/// <summary>

6
src/Perspex.Controls/Window.cs

@ -156,6 +156,12 @@ namespace Perspex.Controls
PlatformImpl.Dispose();
}
protected override void HandleApplicationExiting()
{
base.HandleApplicationExiting();
Close();
}
/// <summary>
/// Closes a dialog window with the specified result.
/// </summary>

Loading…
Cancel
Save