Skip to content

Commit

Permalink
#624 Make WindowConductor public
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel Sampson committed Jun 4, 2020
1 parent 69d5a09 commit 113f412
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 110 deletions.
118 changes: 118 additions & 0 deletions src/Caliburn.Micro.Platform/Platforms/net46-netcore/WindowConductor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace Caliburn.Micro
{
public class WindowConductor
{
private bool deactivatingFromView;
private bool deactivateFromViewModel;
private bool actuallyClosing;
private readonly Window view;
private readonly object model;

public WindowConductor(object model, Window view)
{
this.model = model;
this.view = view;
}

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

if (model is IDeactivate deactivatable)
{
view.Closed += Closed;
deactivatable.Deactivated += Deactivated;
}

if (model is IGuardClose guard)
{
view.Closing += Closing;
}
}

private async void Closed(object sender, EventArgs e)
{
view.Closed -= Closed;
view.Closing -= Closing;

if (deactivateFromViewModel)
{
return;
}

var deactivatable = (IDeactivate)model;

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

private void Deactivated(object sender, DeactivationEventArgs e)
{
if (!e.WasClosed)
{
return;
}

((IDeactivate)model).Deactivated -= Deactivated;

if (deactivatingFromView)
{
return;
}

deactivateFromViewModel = true;
actuallyClosing = true;
view.Close();
actuallyClosing = false;
deactivateFromViewModel = false;
}

private async void Closing(object sender, CancelEventArgs e)
{
if (e.Cancel)
{
return;
}

var guard = (IGuardClose)model;

if (actuallyClosing)
{
actuallyClosing = false;
return;
}

var cachedDialogResult = view.DialogResult;

e.Cancel = true;

await Task.Yield();

var canClose = await guard.CanCloseAsync(CancellationToken.None);

if (!canClose)
return;

actuallyClosing = true;

if (cachedDialogResult == null)
{
view.Close();
}
else if (view.DialogResult != cachedDialogResult)
{
view.DialogResult = cachedDialogResult;
}
}
}
}
110 changes: 0 additions & 110 deletions src/Caliburn.Micro.Platform/Platforms/net46-netcore/WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,115 +317,5 @@ private bool ApplySettings(object target, IEnumerable<KeyValuePair<string, objec

return false;
}

private class WindowConductor
{
private bool deactivatingFromView;
private bool deactivateFromViewModel;
private bool actuallyClosing;
private readonly Window view;
private readonly object model;

public WindowConductor(object model, Window view)
{
this.model = model;
this.view = view;
}

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

if (model is IDeactivate deactivatable)
{
view.Closed += Closed;
deactivatable.Deactivated += Deactivated;
}

if (model is IGuardClose guard)
{
view.Closing += Closing;
}
}

private async void Closed(object sender, EventArgs e)
{
view.Closed -= Closed;
view.Closing -= Closing;

if (deactivateFromViewModel)
{
return;
}

var deactivatable = (IDeactivate)model;

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

private void Deactivated(object sender, DeactivationEventArgs e)
{
if (!e.WasClosed)
{
return;
}

((IDeactivate)model).Deactivated -= Deactivated;

if (deactivatingFromView)
{
return;
}

deactivateFromViewModel = true;
actuallyClosing = true;
view.Close();
actuallyClosing = false;
deactivateFromViewModel = false;
}

private async void Closing(object sender, CancelEventArgs e)
{
if (e.Cancel)
{
return;
}

var guard = (IGuardClose)model;

if (actuallyClosing)
{
actuallyClosing = false;
return;
}

var cachedDialogResult = view.DialogResult;

e.Cancel = true;

await Task.Yield();

var canClose = await guard.CanCloseAsync(CancellationToken.None);

if (!canClose)
return;

actuallyClosing = true;

if (cachedDialogResult == null)
{
view.Close();
}
else if (view.DialogResult != cachedDialogResult)
{
view.DialogResult = cachedDialogResult;
}
}
}
}
}

0 comments on commit 113f412

Please sign in to comment.