Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ClassificationColorManager.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 //
4 // Theme Coloring Source: https://github.com/fsprojects/VisualFSharpPowerTools
5 //
6 // Copyright 2014 F# Software Foundation
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 using System;
21 using System.Collections.Generic;
22 using System.ComponentModel.Composition;
23 using System.Windows.Media;
24 using Microsoft.VisualStudio.Text.Classification;
25 using Microsoft.VisualStudio.Text.Formatting;
26 
27 namespace SiliconStudio.Paradox.VisualStudio.Classifiers
28 {
29  public class ClassificationColorManager : IDisposable
30  {
31  private VisualStudioTheme currentTheme = VisualStudioTheme.Unknown;
32  protected string ClassificationCategory = "text"; // DefGuidList.guidTextEditorFontCategory
33 
34  protected readonly Dictionary<VisualStudioTheme, IDictionary<string, ClassificationColor>> themeColors =
35  new Dictionary<VisualStudioTheme, IDictionary<string, ClassificationColor>>();
36 
37  private VisualStudioThemeEngine themeEngine;
38 
39  [Import]
40  private IClassificationFormatMapService classificationFormatMapService = null;
41 
42  [Import]
43  private IClassificationTypeRegistryService classificationTypeRegistry = null;
44 
45  protected ClassificationColorManager(IServiceProvider serviceProvider)
46  {
47  // Initialize theme engine
48  themeEngine = new VisualStudioThemeEngine(serviceProvider);
49  themeEngine.OnThemeChanged += themeEngine_OnThemeChanged;
50  }
51 
52  public void Dispose()
53  {
54  themeEngine.OnThemeChanged -= themeEngine_OnThemeChanged;
55  themeEngine.Dispose();
56  }
57 
59  {
60  return themeEngine.GetCurrentTheme();
61  }
62 
63  void themeEngine_OnThemeChanged(object sender, EventArgs e)
64  {
65  UpdateColors();
66  }
67 
68  private void UpdateColors()
69  {
70  var theme = themeEngine.GetCurrentTheme();
71 
72  // Did theme change?
73  if (theme != currentTheme)
74  {
75  currentTheme = theme;
76 
77  var colors = themeColors[theme];
78  var formatMap = classificationFormatMapService.GetClassificationFormatMap(ClassificationCategory);
79 
80  // TODO: It seems this approach doesn't update Fonts & Colors settings
81  try
82  {
83  formatMap.BeginBatchUpdate();
84  foreach (var pair in colors)
85  {
86  string type = pair.Key;
87  var color = pair.Value;
88 
89  var classificationType = classificationTypeRegistry.GetClassificationType(type);
90  var oldProp = formatMap.GetTextProperties(classificationType);
91 
92  var foregroundBrush =
93  color.ForegroundColor == null
94  ? null
95  : new SolidColorBrush(color.ForegroundColor.Value);
96 
97  var backgroundBrush =
98  color.BackgroundColor == null
99  ? null
100  : new SolidColorBrush(color.BackgroundColor.Value);
101 
102  var newProp = TextFormattingRunProperties.CreateTextFormattingRunProperties(
103  foregroundBrush, backgroundBrush, oldProp.Typeface, null, null, oldProp.TextDecorations,
104  oldProp.TextEffects, oldProp.CultureInfo);
105 
106  formatMap.SetTextProperties(classificationType, newProp);
107  }
108  }
109  finally
110  {
111  formatMap.EndBatchUpdate();
112  }
113  }
114  }
115 
116  public ClassificationColor GetClassificationColor(string classificationName)
117  {
118  var theme = GetCurrentTheme();
119 
120  ClassificationColor classificationColor;
121  themeColors[theme].TryGetValue(classificationName, out classificationColor);
122 
123  return classificationColor;
124  }
125  }
126 }