Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit e5dde6f

Browse files
committed
Don't use lazy loading for built-in WPF commands.
1 parent af0cc60 commit e5dde6f

7 files changed

Lines changed: 19 additions & 27 deletions

File tree

src/Main/Core/Project/Src/Util/CommandWrapper.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public sealed class CommandWrapper : ICommand
4444
/// </summary>
4545
public static ICommand CreateLazyCommand(Codon codon, IReadOnlyCollection<ICondition> conditions)
4646
{
47+
if (codon.Properties["loadclasslazy"] == "false") {
48+
// if lazy loading was explicitly disabled, create the actual command now
49+
return CreateCommand(codon, conditions);
50+
}
51+
if (codon.Properties.Contains("command") && !codon.Properties.Contains("loadclasslazy")) {
52+
// If we're using the 'command=' syntax, this is most likely a built-in command
53+
// where lazy loading isn't useful (and hurts if CanExecute is used).
54+
// Don't use lazy loading unless loadclasslazy is set explicitly.
55+
return CreateCommand(codon, conditions);
56+
}
57+
// Create the wrapper that lazily loads the actual command.
4758
return new CommandWrapper(codon, conditions);
4859
}
4960

src/Main/ICSharpCode.Core.Presentation/Menu/MenuCommand.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ class MenuCommand : CoreMenuItem
3232
{
3333
readonly string ActivationMethod;
3434

35-
public MenuCommand(UIElement inputBindingOwner, Codon codon, object caller, bool createCommand, string activationMethod, IReadOnlyCollection<ICondition> conditions) : base(codon, caller, conditions)
35+
public MenuCommand(UIElement inputBindingOwner, Codon codon, object caller, string activationMethod, IReadOnlyCollection<ICondition> conditions) : base(codon, caller, conditions)
3636
{
3737
this.ActivationMethod = activationMethod;
38-
if (createCommand)
39-
this.Command = CommandWrapper.CreateCommand(codon, conditions);
40-
else
41-
this.Command = CommandWrapper.CreateLazyCommand(codon, conditions);
38+
this.Command = CommandWrapper.CreateLazyCommand(codon, conditions);
4239
this.CommandParameter = caller;
4340

4441
if (!string.IsNullOrEmpty(codon.Properties["shortcut"])) {

src/Main/ICSharpCode.Core.Presentation/Menu/MenuService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ static object CreateMenuItemFromDescriptor(MenuCreateContext context, MenuItemDe
211211
{
212212
Codon codon = descriptor.Codon;
213213
string type = codon.Properties.Contains("type") ? codon.Properties["type"] : "Command";
214-
bool createCommand = codon.Properties["loadclasslazy"] == "false";
215214

216215
switch (type) {
217216
case "Separator":
@@ -220,7 +219,7 @@ static object CreateMenuItemFromDescriptor(MenuCreateContext context, MenuItemDe
220219
return new MenuCheckBox(context.InputBindingOwner, codon, descriptor.Parameter, descriptor.Conditions);
221220
case "Item":
222221
case "Command":
223-
return new MenuCommand(context.InputBindingOwner, codon, descriptor.Parameter, createCommand, context.ActivationMethod, descriptor.Conditions);
222+
return new MenuCommand(context.InputBindingOwner, codon, descriptor.Parameter, context.ActivationMethod, descriptor.Conditions);
224223
case "Menu":
225224
var item = new CoreMenuItem(codon, descriptor.Parameter, descriptor.Conditions) {
226225
ItemsSource = new object[1],

src/Main/ICSharpCode.Core.Presentation/ToolBar/ToolBarButton.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@ sealed class ToolBarButton : Button, IStatusUpdate
3636
readonly string inputGestureText;
3737
readonly IEnumerable<ICondition> conditions;
3838

39-
public ToolBarButton(UIElement inputBindingOwner, Codon codon, object caller, bool createCommand, IReadOnlyCollection<ICondition> conditions)
39+
public ToolBarButton(UIElement inputBindingOwner, Codon codon, object caller, IReadOnlyCollection<ICondition> conditions)
4040
{
4141
ToolTipService.SetShowOnDisabled(this, true);
4242

4343
this.codon = codon;
4444
this.caller = caller;
45-
if (createCommand)
46-
this.Command = CommandWrapper.CreateCommand(codon, conditions);
47-
else
48-
this.Command = CommandWrapper.CreateLazyCommand(codon, conditions);
45+
this.Command = CommandWrapper.CreateLazyCommand(codon, conditions);
4946
this.CommandParameter = caller;
5047
this.Content = ToolBarService.CreateToolBarItemContent(codon);
5148
this.conditions = conditions;

src/Main/ICSharpCode.Core.Presentation/ToolBar/ToolBarService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,13 @@ static object CreateToolBarItemFromDescriptor(UIElement inputBindingOwner, Toolb
6565
object caller = descriptor.Parameter;
6666
string type = codon.Properties.Contains("type") ? codon.Properties["type"] : "Item";
6767

68-
bool createCommand = codon.Properties["loadclasslazy"] == "false";
69-
7068
switch (type) {
7169
case "Separator":
7270
return new ConditionalSeparator(codon, caller, true, descriptor.Conditions);
7371
case "CheckBox":
7472
return new ToolBarCheckBox(codon, caller, descriptor.Conditions);
7573
case "Item":
76-
return new ToolBarButton(inputBindingOwner, codon, caller, createCommand, descriptor.Conditions);
74+
return new ToolBarButton(inputBindingOwner, codon, caller, descriptor.Conditions);
7775
case "DropDownButton":
7876
return new ToolBarDropDownButton(
7977
codon, caller, MenuService.CreateUnexpandedMenuItems(

src/Main/ICSharpCode.Core.WinForms/Menu/MenuCommand.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,13 @@ public string Description {
4343
}
4444

4545
public MenuCommand(Codon codon, object caller, IReadOnlyCollection<ICondition> conditions)
46-
: this(codon, caller, false, conditions)
47-
{
48-
49-
}
50-
51-
public MenuCommand(Codon codon, object caller, bool createCommand, IReadOnlyCollection<ICondition> conditions)
5246
{
5347
this.RightToLeft = RightToLeft.Inherit;
5448
this.caller = caller;
5549
this.codon = codon;
5650
this.conditions = conditions;
5751

58-
if (createCommand) {
59-
this.command = CommandWrapper.CreateCommand(codon, conditions);
60-
} else {
61-
this.command = CommandWrapper.CreateLazyCommand(codon, conditions);
62-
}
52+
this.command = CommandWrapper.CreateLazyCommand(codon, conditions);
6353

6454
UpdateText();
6555
}

src/Main/ICSharpCode.Core.WinForms/Menu/MenuService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static object CreateMenuItemFromDescriptor(MenuItemDescriptor descriptor)
6262
return new MenuCheckBox(codon, descriptor.Parameter, descriptor.Conditions);
6363
case "Item":
6464
case "Command":
65-
return new MenuCommand(codon, descriptor.Parameter, createCommand, descriptor.Conditions);
65+
return new MenuCommand(codon, descriptor.Parameter, descriptor.Conditions);
6666
case "Menu":
6767
return new Menu(codon, descriptor.Parameter, ConvertSubItems(descriptor.SubItems), descriptor.Conditions);
6868
case "Builder":

0 commit comments

Comments
 (0)