AbstractCodeStyleOptionViewModel.cs 3.7 KB
Newer Older
B
Balaji Krishnan 已提交
1 2 3
// 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.Collections.Generic;
4
using System.Linq;
B
Balaji Krishnan 已提交
5 6 7 8 9 10 11
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Options;
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.LanguageServices.Implementation.Utilities;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.Options
{
12 13 14 15 16 17 18 19 20 21 22 23 24
    /// <summary>
    /// This class acts as a base for any view model that 
    /// binds to the codestyle options UI.
    /// </summary>
    /// <remarks>
    /// This supports databinding of: 
    /// Description
    /// list of CodeStyle preferences
    /// list of Notification preferences 
    /// selected code style preference
    /// selected notification preference
    /// plus, styling for visual elements.
    /// </remarks>
B
Balaji Krishnan 已提交
25 26 27 28 29 30 31 32 33 34
    internal abstract class AbstractCodeStyleOptionViewModel : AbstractNotifyPropertyChanged
    {
        private readonly string _truePreview;
        private readonly string _falsePreview;
        protected AbstractOptionPreviewViewModel Info { get; }
        public IOption Option { get; }

        public string Description { get; set; }
        public double DescriptionMargin { get; set; }
        public string GroupName { get; set; }
35
        public abstract CodeStylePreference SelectedPreference { get; set; }
B
Balaji Krishnan 已提交
36 37
        public List<CodeStylePreference> Preferences { get; set; }
        public List<NotificationOptionViewModel> NotificationPreferences { get; set; }
38
        public abstract bool NotificationsAvailable { get; }
B
Balaji Krishnan 已提交
39

B
Balaji Krishnan 已提交
40
        public virtual string GetPreview() => SelectedPreference.IsChecked ? _truePreview : _falsePreview;
41 42 43 44 45
        public virtual NotificationOptionViewModel SelectedNotificationPreference
        {
            get { return NotificationPreferences.First(); }
            set { }
        }
B
Balaji Krishnan 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

        public AbstractCodeStyleOptionViewModel(
            IOption option,
            string description,
            string truePreview,
            string falsePreview,
            AbstractOptionPreviewViewModel info,
            OptionSet options,
            string groupName,
            List<CodeStylePreference> preferences = null,
            List<NotificationOptionViewModel> notificationPreferences = null)
        {
            _truePreview = truePreview;
            _falsePreview = falsePreview;

            Info = info;
            Option = option;
            Description = description;
            Preferences = preferences ?? GetDefaultPreferences();
            NotificationPreferences = notificationPreferences ?? GetDefaultNotifications();
            DescriptionMargin = 12d;
            GroupName = groupName;
        }

        private static List<NotificationOptionViewModel> GetDefaultNotifications()
        {
            return new List<NotificationOptionViewModel>
            {
                new NotificationOptionViewModel(NotificationOption.None, KnownMonikers.None),
75
                new NotificationOptionViewModel(NotificationOption.Suggestion, KnownMonikers.StatusInformation),
B
Balaji Krishnan 已提交
76 77 78 79 80 81 82 83 84
                new NotificationOptionViewModel(NotificationOption.Warning, KnownMonikers.StatusWarning),
                new NotificationOptionViewModel(NotificationOption.Error, KnownMonikers.StatusError)
            };
        }

        private static List<CodeStylePreference> GetDefaultPreferences()
        {
            return new List<CodeStylePreference>
            {
B
Balaji Krishnan 已提交
85 86
                new CodeStylePreference(ServicesVSResources.Yes, isChecked: true),
                new CodeStylePreference(ServicesVSResources.No, isChecked: false),
B
Balaji Krishnan 已提交
87 88 89 90 91
            };
        }

    }
}