提交 5ee5392f 编写于 作者: B Balaji Krishnan

Add RelayCommand to CheckBoxViewModel

Add RelayCommand to CheckBoxViewModel to enable/disable checkboxes in
option preview control depending on the state of other controls. The
StyleViewModel now makes use of this for Use Var feature.
上级 03c1a914
......@@ -17,8 +17,8 @@ internal static class CSharpCodeStyleOptions
public static readonly Option<bool> UseVarWhenDeclaringLocals = new Option<bool>(FeatureName, "UseVarWhenDeclaringLocals", defaultValue: true);
public static readonly Option<TypeInferencePreferenceOptions> UseImplicitTypingForLocals = new Option<TypeInferencePreferenceOptions>(FeatureName, "UseImplicitTypingForLocals", defaultValue: TypeInferencePreferenceOptions.ExplicitTyping);
public static readonly Option<bool> UseVarWhenTypeIsApparent = new Option<bool>(FeatureName, "UseImplicitTypingWhereApparent", defaultValue: true);
public static readonly Option<bool> DoNotUseVarForIntrinsicTypes = new Option<bool>(FeatureName, "NoImplicitTypingForIntrinsics", defaultValue: true);
public static readonly Option<bool> UseVarWhenTypeIsApparent = new Option<bool>(FeatureName, "UseImplicitTypingWhereApparent", defaultValue: false);
public static readonly Option<bool> DoNotUseVarForIntrinsicTypes = new Option<bool>(FeatureName, "NoImplicitTypingForIntrinsics", defaultValue: false);
}
public enum TypeInferencePreferenceOptions
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.Options;
......@@ -155,13 +156,18 @@ internal StyleViewModel(OptionSet optionSet, IServiceProvider serviceProvider) :
// TODO (BalajiK): Localize all strings from here.
Items.Add(new HeaderItemViewModel() { Header = "Type Inference preference for local declarations everywhere:" });
Items.Add(new RadioButtonViewModel<TypeInferencePreferenceOptions>("use implicit typing", s_ImplicitTypingPreview, "usevar", TypeInferencePreferenceOptions.ImplicitTyping, CSharpCodeStyleOptions.UseImplicitTypingForLocals, this, optionSet));
Items.Add(new RadioButtonViewModel<TypeInferencePreferenceOptions>("use explicit typing", s_ExplicitTypingPreview, "usevar", TypeInferencePreferenceOptions.ExplicitTyping, CSharpCodeStyleOptions.UseImplicitTypingForLocals, this, optionSet));
var implicitTypingOption = new RadioButtonViewModel<TypeInferencePreferenceOptions>("use implicit typing", s_ImplicitTypingPreview, "usevar", TypeInferencePreferenceOptions.ImplicitTyping, CSharpCodeStyleOptions.UseImplicitTypingForLocals, this, optionSet);
Items.Add(implicitTypingOption);
var explicitTypingOption = new RadioButtonViewModel<TypeInferencePreferenceOptions>("use explicit typing", s_ExplicitTypingPreview, "usevar", TypeInferencePreferenceOptions.ExplicitTyping, CSharpCodeStyleOptions.UseImplicitTypingForLocals, this, optionSet);
Items.Add(explicitTypingOption);
Items.Add(new HeaderItemViewModel() { Header = "Type Inference preference for local declarations in special cases:" });
Items.Add(new CheckBoxOptionViewModel(CSharpCodeStyleOptions.UseVarWhenTypeIsApparent, "use var where typing is apparent", s_ImplicitTypingWhereApparentPreview, this, optionSet));
Items.Add(new CheckBoxOptionViewModel(CSharpCodeStyleOptions.DoNotUseVarForIntrinsicTypes, "use intrinsic types", s_ExplicitTypingForIntrinsicTypesPreview, this, optionSet));
var useVarWhereApparentChildControl = new CheckBoxOptionViewModel(CSharpCodeStyleOptions.UseVarWhenTypeIsApparent, "use var where typing is apparent", s_ImplicitTypingWhereApparentPreview, this, optionSet, _ => implicitTypingOption.IsChecked);
var useIntrinsicTypesChildControl = new CheckBoxOptionViewModel(CSharpCodeStyleOptions.DoNotUseVarForIntrinsicTypes, "use intrinsic types", s_ExplicitTypingForIntrinsicTypesPreview, this, optionSet, _ => implicitTypingOption.IsChecked);
Items.Add(useVarWhereApparentChildControl);
Items.Add(useIntrinsicTypesChildControl);
}
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Windows.Input;
using Microsoft.CodeAnalysis.Options;
using Microsoft.VisualStudio.LanguageServices.Implementation.Utilities;
......@@ -15,6 +17,9 @@ internal string GetPreview()
}
private bool _isChecked;
private RelayCommand _enableCommand;
private readonly Predicate<object> _canExecute;
private readonly string _truePreview;
private readonly string _falsePreview;
......@@ -28,6 +33,8 @@ public CheckBoxOptionViewModel(IOption option, string description, string previe
_truePreview = preview;
_falsePreview = preview;
_info = info;
_canExecute = null;
SetProperty(ref _isChecked, (bool)options.GetOption(new OptionKey(option, option.IsPerLanguage ? info.Language : null)));
}
......@@ -38,6 +45,20 @@ public CheckBoxOptionViewModel(IOption option, string description, string truePr
_truePreview = truePreview;
_falsePreview = falsePreview;
_info = info;
_canExecute = null;
SetProperty(ref _isChecked, (bool)options.GetOption(new OptionKey(option, option.IsPerLanguage ? info.Language : null)));
}
public CheckBoxOptionViewModel(IOption option, string description, string preview, AbstractOptionPreviewViewModel info, OptionSet options, Predicate<object> canExecute)
{
this.Option = option;
Description = description;
_truePreview = preview;
_falsePreview = preview;
_info = info;
_canExecute = canExecute;
SetProperty(ref _isChecked, (bool)options.GetOption(new OptionKey(option, option.IsPerLanguage ? info.Language : null)));
}
......@@ -54,5 +75,21 @@ public bool IsChecked
_info.SetOptionAndUpdatePreview(_isChecked, Option, GetPreview());
}
}
public ICommand EnableCommand
{
get
{
if (_enableCommand == null)
{
_enableCommand = _canExecute == null
? new RelayCommand(_ => { })
: new RelayCommand(_ => { }, _canExecute);
}
return _enableCommand;
}
}
}
}
......@@ -27,6 +27,7 @@
<StackPanel Orientation="Horizontal">
<CheckBox x:Uid="OptionCheckBox"
IsChecked="{Binding IsChecked, Mode=TwoWay}"
Command="{Binding EnableCommand}"
Width="Auto"
Focusable="False"
Margin="20, 0, 0, 0">
......@@ -42,7 +43,7 @@
<DataTemplate DataType="{x:Type options:AbstractRadioButtonViewModel}">
<StackPanel Orientation="Horizontal">
<RadioButton x:Uid="OptionRadioButton"
IsChecked="{Binding IsChecked, Mode=TwoWay}"
IsChecked="{Binding IsChecked, Mode=TwoWay}"
Width="Auto"
Focusable="False"
GroupName="{Binding GroupName, Mode=OneWay}"
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.Windows.Input;
namespace Microsoft.VisualStudio.LanguageServices.Implementation.Options
{
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
_canExecute = canExecute;
}
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
}
......@@ -322,6 +322,7 @@
<Compile Include="Options\InternalOptionSerializer.cs" />
<Compile Include="Options\PerLanguageOptionBinding.cs" />
<Compile Include="Options\RadioButtonViewModel.cs" />
<Compile Include="Options\RelayCommand.cs" />
<Compile Include="RoslynVisualStudioWorkspace.cs" />
</ItemGroup>
<ItemGroup>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册