Skip to content

Commit

Permalink
Make Window Manager & Bootstrapper properly async
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel Sampson committed Dec 9, 2018
1 parent e371911 commit fd65420
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
9 changes: 5 additions & 4 deletions src/Caliburn.Micro.Platform/Platforms/net46/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

Expand Down Expand Up @@ -202,20 +203,20 @@ protected virtual void OnUnhandledException(object sender, DispatcherUnhandledEx
/// </summary>
/// <param name="viewModelType">The view model type.</param>
/// <param name="settings">The optional window settings.</param>
protected void DisplayRootViewFor(Type viewModelType, IDictionary<string, object> settings = null)
protected async Task DisplayRootViewForAsync(Type viewModelType, IDictionary<string, object> settings = null)
{
var windowManager = IoC.Get<IWindowManager>();
windowManager.ShowWindow(IoC.GetInstance(viewModelType, null), null, settings);
await windowManager.ShowWindowAsync(IoC.GetInstance(viewModelType, null), null, settings);
}

/// <summary>
/// Locates the view model, locates the associate view, binds them and shows it as the root view.
/// </summary>
/// <typeparam name="TViewModel">The view model type.</typeparam>
/// <param name="settings">The optional window settings.</param>
protected void DisplayRootViewFor<TViewModel>(IDictionary<string, object> settings = null)
protected Task DisplayRootViewFor<TViewModel>(IDictionary<string, object> settings = null)
{
DisplayRootViewFor(typeof(TViewModel), settings);
return DisplayRootViewForAsync(typeof(TViewModel), settings);
}
}
}
42 changes: 26 additions & 16 deletions src/Caliburn.Micro.Platform/Platforms/net46/WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
Expand All @@ -23,23 +24,23 @@ public interface IWindowManager
/// <param name="context">The context.</param>
/// <param name="settings">The optional dialog settings.</param>
/// <returns>The dialog result.</returns>
bool? ShowDialog(object rootModel, object context = null, IDictionary<string, object> settings = null);
Task<bool?> ShowDialogAsync(object rootModel, object context = null, IDictionary<string, object> settings = null);

/// <summary>
/// Shows a non-modal window for the specified model.
/// </summary>
/// <param name="rootModel">The root model.</param>
/// <param name="context">The context.</param>
/// <param name="settings">The optional window settings.</param>
void ShowWindow(object rootModel, object context = null, IDictionary<string, object> settings = null);
Task ShowWindowAsync(object rootModel, object context = null, IDictionary<string, object> settings = null);

/// <summary>
/// Shows a popup at the current mouse position.
/// </summary>
/// <param name="rootModel">The root model.</param>
/// <param name="context">The view context.</param>
/// <param name="settings">The optional popup settings.</param>
void ShowPopup(object rootModel, object context = null, IDictionary<string, object> settings = null);
Task ShowPopupAsync(object rootModel, object context = null, IDictionary<string, object> settings = null);
}

/// <summary>
Expand All @@ -54,9 +55,11 @@ public class WindowManager : IWindowManager
/// <param name="context">The context.</param>
/// <param name="settings">The dialog popup settings.</param>
/// <returns>The dialog result.</returns>
public virtual bool? ShowDialog(object rootModel, object context = null, IDictionary<string, object> settings = null)
public virtual async Task<bool?> ShowDialogAsync(object rootModel, object context = null, IDictionary<string, object> settings = null)
{
return CreateWindow(rootModel, true, context, settings).ShowDialog();
var window = await CreateWindowAsync(rootModel, true, context, settings);

return window.ShowDialog();
}

/// <summary>
Expand All @@ -65,7 +68,7 @@ public class WindowManager : IWindowManager
/// <param name="rootModel">The root model.</param>
/// <param name="context">The context.</param>
/// <param name="settings">The optional window settings.</param>
public virtual void ShowWindow(object rootModel, object context = null, IDictionary<string, object> settings = null)
public virtual async Task ShowWindowAsync(object rootModel, object context = null, IDictionary<string, object> settings = null)
{
NavigationWindow navWindow = null;

Expand All @@ -77,12 +80,14 @@ public virtual void ShowWindow(object rootModel, object context = null, IDiction

if (navWindow != null)
{
var window = CreatePage(rootModel, context, settings);
var window = await CreatePageAsync(rootModel, context, settings);
navWindow.Navigate(window);
}
else
{
CreateWindow(rootModel, false, context, settings).Show();
var window = await CreateWindowAsync(rootModel, false, context, settings);

window.Show();
}
}

Expand All @@ -92,7 +97,7 @@ public virtual void ShowWindow(object rootModel, object context = null, IDiction
/// <param name="rootModel">The root model.</param>
/// <param name="context">The view context.</param>
/// <param name="settings">The optional popup settings.</param>
public virtual async void ShowPopup(object rootModel, object context = null, IDictionary<string, object> settings = null)
public virtual async Task ShowPopupAsync(object rootModel, object context = null, IDictionary<string, object> settings = null)
{
var popup = CreatePopup(rootModel, settings);
var view = ViewLocator.LocateForModel(rootModel, popup, context);
Expand Down Expand Up @@ -156,7 +161,7 @@ protected virtual Popup CreatePopup(object rootModel, IDictionary<string, object
/// <param name="context">The view context.</param>
/// <param name="settings">The optional popup settings.</param>
/// <returns>The window.</returns>
protected virtual Window CreateWindow(object rootModel, bool isDialog, object context, IDictionary<string, object> settings)
protected virtual async Task<Window> CreateWindowAsync(object rootModel, bool isDialog, object context, IDictionary<string, object> settings)
{
var view = EnsureWindow(rootModel, ViewLocator.LocateForModel(rootModel, null, context), isDialog);
ViewModelBinder.Bind(rootModel, view, context);
Expand All @@ -170,7 +175,9 @@ protected virtual Window CreateWindow(object rootModel, bool isDialog, object co

ApplySettings(view, settings);

new WindowConductor(rootModel, view);
var conductor = new WindowConductor(rootModel, view);

await conductor.InitialiseAysnc();

return view;
}
Expand Down Expand Up @@ -243,7 +250,7 @@ protected virtual Window InferOwnerOf(Window window)
/// <param name="context">The context.</param>
/// <param name="settings">The optional popup settings.</param>
/// <returns>The page.</returns>
public virtual Page CreatePage(object rootModel, object context, IDictionary<string, object> settings)
public virtual async Task<Page> CreatePageAsync(object rootModel, object context, IDictionary<string, object> settings)
{
var view = EnsurePage(rootModel, ViewLocator.LocateForModel(rootModel, null, context));
ViewModelBinder.Bind(rootModel, view, context);
Expand All @@ -259,7 +266,7 @@ public virtual Page CreatePage(object rootModel, object context, IDictionary<str

if (rootModel is IActivate activator)
{
activator.ActivateAsync();
await activator.ActivateAsync();
}

if (rootModel is IDeactivate deactivatable)
Expand Down Expand Up @@ -322,10 +329,13 @@ public WindowConductor(object model, Window view)
{
this.model = model;
this.view = view;
}

public async Task InitialiseAysnc()
{
if (model is IActivate activator)
{
activator.ActivateAsync();
await activator.ActivateAsync();
}

if (model is IDeactivate deactivatable)
Expand All @@ -340,7 +350,7 @@ public WindowConductor(object model, Window view)
}
}

private void Closed(object sender, EventArgs e)
private async void Closed(object sender, EventArgs e)
{
view.Closed -= Closed;
view.Closing -= Closing;
Expand All @@ -353,7 +363,7 @@ private void Closed(object sender, EventArgs e)
var deactivatable = (IDeactivate)model;

deactivatingFromView = true;
deactivatable.DeactivateAsync(true);
await deactivatable.DeactivateAsync(true);
deactivatingFromView = false;
}

Expand Down

0 comments on commit fd65420

Please sign in to comment.