public
Description: A Visual Studio 2010 extension to make the selection display as a gradient in rich-client mode.
Homepage: http://blogs.msdn.com/noahric
Clone URL: git://github.com/NoahRic/GradientSelection.git
GradientSelection / ViewCreationListener.cs
100644 73 lines (66 sloc) 3.104 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
 
namespace GradientSelection
{
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("any")]
[TextViewRole(PredefinedTextViewRoles.Document)]
    internal sealed class ViewCreationListener : IWpfTextViewCreationListener
    {
[Import]
        IEditorFormatMapService formatMapService = null;
 
        /// <summary>
        /// When a text view is created, update its format map.
        /// </summary>
        public void TextViewCreated(IWpfTextView textView)
        {
            var formatMap = formatMapService.GetEditorFormatMap(textView);
 
            // Set the gradient brush only if the rich client experience is enabled
            if (!textView.Options.GetOptionValue(DefaultWpfViewOptions.EnableSimpleGraphicsId))
            {
                SetGradientBrush(formatMap);
            }
 
            // Track the rich client option changing, to set and clear the gradient brush
            textView.Options.OptionChanged += (sender, args) =>
                {
                    if (args.OptionId == DefaultWpfViewOptions.EnableSimpleGraphicsId.Name)
                    {
                        if (!textView.Options.GetOptionValue(DefaultWpfViewOptions.EnableSimpleGraphicsId))
                        {
                            SetGradientBrush(formatMap);
                        }
                        else
                        {
                            ClearGradientBrush(formatMap);
                        }
                    }
                };
        }
 
        void SetGradientBrush(IEditorFormatMap formatMap)
        {
            Color highlight = SystemColors.HighlightColor;
            Color darkColor = Color.FromArgb(96, highlight.R, highlight.G, highlight.B);
            Color lightColor = Color.FromArgb(48, highlight.R, highlight.G, highlight.B);
 
            // Change the selected text properties to use a gradient brush and an outline pen
            var properties = formatMap.GetProperties("Selected Text");
            properties[EditorFormatDefinition.BackgroundBrushId] = new LinearGradientBrush(
                new GradientStopCollection() { new GradientStop(darkColor, 0.0), new GradientStop(lightColor, 0.5), new GradientStop(darkColor, 1.0) },
                90.0);
            properties["BackgroundPen"] = new Pen(SystemColors.HighlightBrush, 1) { LineJoin = PenLineJoin.Round };
            formatMap.SetProperties("Selected Text", properties);
        }
 
        void ClearGradientBrush(IEditorFormatMap formatMap)
        {
            // Clear out the gradient brush and outline pen
            var properties = formatMap.GetProperties("Selected Text");
            properties.Remove(EditorFormatDefinition.BackgroundBrushId);
            properties.Remove("BackgroundPen");
            formatMap.SetProperties("Selected Text", properties);
        }
    }
}