Skip to content

Commit 13eb65a

Browse files
committed
Decouple the can/on execute conditions
1 parent bf8409c commit 13eb65a

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

Rubberduck.Core/UI/Command/CommandBase.cs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,42 @@ protected CommandBase(ILogger logger = null)
1616
{
1717
Logger = logger ?? LogManager.GetLogger(GetType().FullName);
1818
CanExecuteCondition = (parameter => true);
19+
OnExecuteCondition = (parameter => true);
1920
}
2021

2122
protected ILogger Logger { get; }
2223
protected abstract void OnExecute(object parameter);
2324

2425
protected Func<object, bool> CanExecuteCondition { get; private set; }
2526
protected Func<object, bool> OnExecuteCondition { get; private set; }
26-
private bool RequireReEvaluationOnExecute => OnExecuteCondition != null;
2727

28-
protected void AddToCanExecuteEvaluation(Func<object, bool> furtherCanExecuteEvaluation, bool requireReevaluation = false)
28+
protected void AddToCanExecuteEvaluation(Func<object, bool> furtherCanExecuteEvaluation, bool requireReevaluationAlso = false)
2929
{
3030
if (furtherCanExecuteEvaluation == null)
3131
{
3232
return;
3333
}
3434

35-
AddToCanExecuteEvaluation(furtherCanExecuteEvaluation);
35+
var currentCanExecuteCondition = CanExecuteCondition;
36+
CanExecuteCondition = (parameter) =>
37+
currentCanExecuteCondition(parameter) && furtherCanExecuteEvaluation(parameter);
3638

37-
if (requireReevaluation)
39+
if (requireReevaluationAlso)
3840
{
3941
AddToOnExecuteEvaluation(furtherCanExecuteEvaluation);
4042
}
4143
}
4244

43-
private void AddToCanExecuteEvaluation(Func<object, bool> furtherCanExecuteEvaluation)
44-
{
45-
var currentCanExecuteCondition = CanExecuteCondition;
46-
CanExecuteCondition = (parameter) =>
47-
currentCanExecuteCondition(parameter) && furtherCanExecuteEvaluation(parameter);
48-
}
49-
50-
private void AddToOnExecuteEvaluation(Func<object, bool> furtherCanExecuteEvaluation)
45+
protected void AddToOnExecuteEvaluation(Func<object, bool> furtherCanExecuteEvaluation)
5146
{
52-
if (OnExecuteCondition == null)
53-
{
54-
OnExecuteCondition = furtherCanExecuteEvaluation;
55-
}
56-
else
47+
if (furtherCanExecuteEvaluation == null)
5748
{
58-
var currentOnExecute = OnExecuteCondition;
59-
OnExecuteCondition = (parameter) => currentOnExecute(parameter) && furtherCanExecuteEvaluation(parameter);
49+
return;
6050
}
51+
52+
var currentOnExecute = OnExecuteCondition;
53+
OnExecuteCondition = (parameter) =>
54+
currentOnExecute(parameter) && furtherCanExecuteEvaluation(parameter);
6155
}
6256

6357
public bool CanExecute(object parameter)
@@ -83,12 +77,9 @@ public void Execute(object parameter)
8377
{
8478
try
8579
{
86-
if (RequireReEvaluationOnExecute)
80+
if (!OnExecuteCondition(parameter))
8781
{
88-
if (!OnExecuteCondition(parameter))
89-
{
90-
return;
91-
}
82+
return;
9283
}
9384

9485
OnExecute(parameter);

0 commit comments

Comments
 (0)