Skip to content

Commit

Permalink
Rename TryClose to RequestClose, and put in IRequestClose
Browse files Browse the repository at this point in the history
  • Loading branch information
canton7 committed Jan 14, 2015
1 parent be88aed commit b4b2444
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
14 changes: 13 additions & 1 deletion Stylet/IScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,22 @@ public interface IGuardClose
Task<bool> CanCloseAsync();
}

/// <summary>
/// Get the object to request that its parent close it
/// </summary>
public interface IRequestClose
{
/// <summary>
/// Request that the conductor responsible for this screen close it
/// </summary>
/// <param name="dialogResult">DialogResult to return, if this is a dialog</param>
void RequestClose(bool? dialogResult = null);
}

/// <summary>
/// Generalised 'screen' composing all the behaviours expected of a screen
/// </summary>
public interface IScreen : IViewAware, IHaveDisplayName, IActivate, IDeactivate, IChild, IClose, IGuardClose
public interface IScreen : IViewAware, IHaveDisplayName, IActivate, IDeactivate, IChild, IClose, IGuardClose, IRequestClose
{
}

Expand Down
2 changes: 1 addition & 1 deletion Stylet/MessageBoxViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ protected override void OnViewLoaded()
public void ButtonClicked(MessageBoxResult button)
{
this.ClickedButton = button;
this.TryClose(true);
this.RequestClose(true);
}
}
}
20 changes: 17 additions & 3 deletions Stylet/Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ protected virtual void OnViewLoaded() { }
private object _parent;

/// <summary>
/// Gets or sets the parent conductor of this screen. Used to TryClose to request a closure
/// Gets or sets the parent conductor of this screen. Used to RequestClose to request a closure
/// </summary>
public object Parent
{
Expand Down Expand Up @@ -241,16 +241,18 @@ protected virtual bool CanClose()

#endregion

#region IRequestClose

/// <summary>
/// Request that the conductor responsible for this screen close it
/// </summary>
/// <param name="dialogResult">DialogResult to return, if this is a dialog</param>
public virtual void TryClose(bool? dialogResult = null)
public virtual void RequestClose(bool? dialogResult = null)
{
var conductor = this.Parent as IChildDelegate;
if (conductor != null)
{
this.logger.Info("TryClose called. Conductor: {0}; DialogResult: {1}", conductor, dialogResult);
this.logger.Info("RequstClose called. Conductor: {0}; DialogResult: {1}", conductor, dialogResult);
conductor.CloseItem(this, dialogResult);
}
else
Expand All @@ -260,5 +262,17 @@ public virtual void TryClose(bool? dialogResult = null)
throw e;
}
}

#endregion

/// <summary>
/// Obselete - use RequestClose
/// </summary>
/// <param name="dialogResult">DialogResult to return, if this is a dialog</param>
[Obsolete("Obseleted by RequestClose", true)]
public virtual void TryClose(bool? dialogResult = null)
{
this.RequestClose(dialogResult);
}
}
}
2 changes: 1 addition & 1 deletion Stylet/WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ async void IChildDelegate.CloseItem(object item, bool? dialogResult)
return;
}

logger.Info("ViewModel {0} close requested with DialogResult {1} because it called TryClose", this.viewModel, dialogResult);
logger.Info("ViewModel {0} close requested with DialogResult {1} because it called RequestClose", this.viewModel, dialogResult);

this.window.StateChanged -= this.WindowStateChanged;
this.window.Closed -= this.WindowClosed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public DialogViewModel()

public void Close()
{
this.TryClose(this.SelectedDesiredResult.Value);
this.RequestClose(this.SelectedDesiredResult.Value);
}
}
}
8 changes: 4 additions & 4 deletions StyletUnitTests/ScreenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,18 @@ public void CanCloseAsyncReturnsResultOfCanClose()
}

[Test]
public void TryCloseThrowsIfParentIsNotIChildDelegate()
public void RequestCloseThrowsIfParentIsNotIChildDelegate()
{
this.screen.Parent = new object();
Assert.Throws<InvalidOperationException>(() => this.screen.TryClose());
Assert.Throws<InvalidOperationException>(() => this.screen.RequestClose());
}

[Test]
public void TryCloseCallsParentCloseItemPassingDialogResult()
public void RequestCloseCallsParentCloseItemPassingDialogResult()
{
var parent = new Mock<IChildDelegate>();
screen.Parent = parent.Object;
this.screen.TryClose(true);
this.screen.RequestClose(true);
parent.Verify(x => x.CloseItem(this.screen, true));
}

Expand Down

0 comments on commit b4b2444

Please sign in to comment.