-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nigel Sampson
committed
Jun 4, 2020
1 parent
69d5a09
commit 113f412
Showing
2 changed files
with
118 additions
and
110 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
src/Caliburn.Micro.Platform/Platforms/net46-netcore/WindowConductor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters