Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1480 from surban/revert-1469-master
Browse files Browse the repository at this point in the history
Revert "Fixes to make sure that VFPT color scheme updates properly when VS theme is changed"
  • Loading branch information
vasily-kirichenko authored Nov 2, 2016
2 parents 346917a + 13030c6 commit 7af929e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ public PrintfSpecifiersUsageTaggerProvider(
_printfColorManager = printfColorManager;

VSColorTheme.ThemeChanged += UpdateTheme;
_printfColorManager.UpdateColors(force: true);
}

void UpdateTheme(EventArgs e)
{
_printfColorManager.UpdateColors(force: false);
_printfColorManager.UpdateColors();
}

public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag
Expand Down Expand Up @@ -80,7 +79,6 @@ public class PrintfColorManager
static readonly Color DarkThemeColor = Color.FromRgb(0, 77, 77);

VisualStudioTheme _currentTheme;
DateTime _lastThemeChange;
ThemeManager _themeManager;
IEditorFormatMapService _editorFormatMapService;

Expand All @@ -91,27 +89,22 @@ public PrintfColorManager(ThemeManager themeManager, IEditorFormatMapService edi
_editorFormatMapService = editorFormatMapService;

_currentTheme = _themeManager.GetCurrentTheme();
_lastThemeChange = DateTime.MinValue;
}

public Color GetDefaultColor()
{
return _currentTheme == VisualStudioTheme.Dark ? DarkThemeColor : LightThemeColor;
}

public virtual void UpdateColors(bool force)
public void UpdateColors()
{
var newTheme = _themeManager.GetCurrentTheme();

// Multiple theme change events are fired in rapid succession after the theme was changed.
// All of them must be processed to properly update the color scheme.
if (newTheme != VisualStudioTheme.Unknown &&
(newTheme != _currentTheme || (DateTime.Now - _lastThemeChange).TotalSeconds < 10 || force))
if (newTheme != VisualStudioTheme.Unknown && newTheme != _currentTheme)
{
_currentTheme = newTheme;
_lastThemeChange = DateTime.Now;

var formatMap = _editorFormatMapService.GetEditorFormatMap(category: "text");

try
{
formatMap.BeginBatchUpdate();
Expand Down
92 changes: 40 additions & 52 deletions src/FSharpVSPowerTools/Commands/SymbolClassifiersProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public class ClassificationColorManager
private IClassificationTypeRegistryService _classificationTypeRegistry;

private VisualStudioTheme _currentTheme;
private DateTime _lastThemeChange;

[ImportingConstructor]
public ClassificationColorManager(
Expand All @@ -137,11 +136,10 @@ public ClassificationColorManager(
_themeManager = themeManager;
_classificationFormatMapService = classificationFormatMapService;
_classificationTypeRegistry = classificationTypeRegistry;

// Theme changed event may fire even though the same theme is still in use.
// We save a current theme and skip color updates in these cases.
_currentTheme = _themeManager.GetCurrentTheme();
_lastThemeChange = DateTime.MinValue;
}

public FontColor GetDefaultColors(string category)
Expand All @@ -168,23 +166,17 @@ public FontColor GetDefaultColors(string category)
}
}

public virtual void UpdateColors(bool force)
public void UpdateColors()
{
var newTheme = _themeManager.GetCurrentTheme();

LoggingModule.logInfoMessage(() => String.Format("Color theme changed from {0} to {1}.", _currentTheme, newTheme));

// Multiple theme change events are fired in rapid succession after the theme was changed.
// All of them must be processed to properly update the color scheme.
if (newTheme != VisualStudioTheme.Unknown &&
(newTheme != _currentTheme || (DateTime.Now - _lastThemeChange).TotalSeconds < 10) || force)
if (newTheme != VisualStudioTheme.Unknown && newTheme != _currentTheme)
{
_currentTheme = newTheme;
_lastThemeChange = DateTime.Now;

var colors = newTheme == VisualStudioTheme.Dark ? DarkColors : LightAndBlueColors;
var formatMap = _classificationFormatMapService.GetClassificationFormatMap(category: "text");

try
{
formatMap.BeginBatchUpdate();
Expand All @@ -196,18 +188,18 @@ public virtual void UpdateColors(bool force)
var classificationType = _classificationTypeRegistry.GetClassificationType(type);
var oldProp = formatMap.GetTextProperties(classificationType);

var foregroundBrush =
color.Foreground == null
var foregroundBrush =
color.Foreground == null
? null
: new SolidColorBrush(color.Foreground.Value);

var backgroundBrush =
color.Background == null
var backgroundBrush =
color.Background == null
? null
: new SolidColorBrush(color.Background.Value);

var newProp = TextFormattingRunProperties.CreateTextFormattingRunProperties(
foregroundBrush, backgroundBrush, oldProp.Typeface, null, null, oldProp.TextDecorations,
foregroundBrush, backgroundBrush, oldProp.Typeface, null, null, oldProp.TextDecorations,
oldProp.TextEffects, oldProp.CultureInfo);

formatMap.SetTextProperties(classificationType, newProp);
Expand All @@ -227,7 +219,7 @@ static class ClassificationFormats
[ClassificationType(ClassificationTypeNames = ClassificationTypes.FSharpReferenceType)]
[Name(ClassificationTypes.FSharpReferenceType)]
[UserVisible(true)]
[Order(After = PredefinedClassificationTypeNames.String)]
[Order(After = PredefinedClassificationTypeNames.String)]
internal sealed class FSharpReferenceTypeFormat : ClassificationFormatDefinition
{
[ImportingConstructor]
Expand All @@ -244,7 +236,7 @@ public FSharpReferenceTypeFormat(ClassificationColorManager colorManager)
[ClassificationType(ClassificationTypeNames = ClassificationTypes.FSharpValueType)]
[Name(ClassificationTypes.FSharpValueType)]
[UserVisible(true)]
[Order(After = PredefinedClassificationTypeNames.String)]
[Order(After = PredefinedClassificationTypeNames.String)]
internal sealed class FSharpValueTypeFormat : ClassificationFormatDefinition
{
[ImportingConstructor]
Expand All @@ -261,7 +253,7 @@ public FSharpValueTypeFormat(ClassificationColorManager colorManager)
[ClassificationType(ClassificationTypeNames = ClassificationTypes.FSharpPatternCase)]
[Name(ClassificationTypes.FSharpPatternCase)]
[UserVisible(true)]
[Order(After = PredefinedClassificationTypeNames.String)]
[Order(After = PredefinedClassificationTypeNames.String)]
internal sealed class FSharpPatternCaseFormat : ClassificationFormatDefinition
{
[ImportingConstructor]
Expand All @@ -278,7 +270,7 @@ public FSharpPatternCaseFormat(ClassificationColorManager colorManager)
[ClassificationType(ClassificationTypeNames = ClassificationTypes.FSharpFunction)]
[Name(ClassificationTypes.FSharpFunction)]
[UserVisible(true)]
[Order(After = PredefinedClassificationTypeNames.String)]
[Order(After = PredefinedClassificationTypeNames.String)]
internal sealed class FSharpFunctionFormat : ClassificationFormatDefinition
{
[ImportingConstructor]
Expand All @@ -295,7 +287,7 @@ public FSharpFunctionFormat(ClassificationColorManager colorManager)
[ClassificationType(ClassificationTypeNames = ClassificationTypes.FSharpMutableVar)]
[Name(ClassificationTypes.FSharpMutableVar)]
[UserVisible(true)]
[Order(After = PredefinedClassificationTypeNames.String)]
[Order(After = PredefinedClassificationTypeNames.String)]
internal sealed class FSharpMutableVarFormat : ClassificationFormatDefinition
{
[ImportingConstructor]
Expand Down Expand Up @@ -367,14 +359,14 @@ public FSharpUnusedFormat(ClassificationColorManager colorManager)
[Order(After = PredefinedClassificationTypeNames.String)]
internal sealed class FSharpPrintfFormat : ClassificationFormatDefinition
{
[ImportingConstructor]
public FSharpPrintfFormat(ClassificationColorManager colorManager)
{
DisplayName = "F# Printf Format";
var colors = colorManager.GetDefaultColors(ClassificationTypes.FSharpPrintf);
ForegroundColor = colors.Foreground;
BackgroundColor = colors.Background;
}
[ImportingConstructor]
public FSharpPrintfFormat(ClassificationColorManager colorManager)
{
DisplayName = "F# Printf Format";
var colors = colorManager.GetDefaultColors(ClassificationTypes.FSharpPrintf);
ForegroundColor = colors.Foreground;
BackgroundColor = colors.Background;
}
}

[Export(typeof(EditorFormatDefinition))]
Expand All @@ -384,14 +376,14 @@ public FSharpPrintfFormat(ClassificationColorManager colorManager)
[Order(After = PredefinedClassificationTypeNames.String)]
internal sealed class FSharpEscapedFormat : ClassificationFormatDefinition
{
[ImportingConstructor]
public FSharpEscapedFormat(ClassificationColorManager colorManager)
{
DisplayName = "F# Escaped Characters";
var colors = colorManager.GetDefaultColors(ClassificationTypes.FSharpEscaped);
ForegroundColor = colors.Foreground;
BackgroundColor = colors.Background;
}
[ImportingConstructor]
public FSharpEscapedFormat(ClassificationColorManager colorManager)
{
DisplayName = "F# Escaped Characters";
var colors = colorManager.GetDefaultColors(ClassificationTypes.FSharpEscaped);
ForegroundColor = colors.Foreground;
BackgroundColor = colors.Background;
}
}

[Export(typeof(EditorFormatDefinition))]
Expand All @@ -401,14 +393,14 @@ public FSharpEscapedFormat(ClassificationColorManager colorManager)
[Order(After = PredefinedClassificationTypeNames.String)]
internal sealed class FSharpOperatorFormat : ClassificationFormatDefinition
{
[ImportingConstructor]
public FSharpOperatorFormat(ClassificationColorManager colorManager)
{
DisplayName = "F# Operators";
var colors = colorManager.GetDefaultColors(ClassificationTypes.FSharpOperator);
ForegroundColor = colors.Foreground;
BackgroundColor = colors.Background;
}
[ImportingConstructor]
public FSharpOperatorFormat(ClassificationColorManager colorManager)
{
DisplayName = "F# Operators";
var colors = colorManager.GetDefaultColors(ClassificationTypes.FSharpOperator);
ForegroundColor = colors.Foreground;
BackgroundColor = colors.Background;
}
}
}

Expand Down Expand Up @@ -443,14 +435,12 @@ public SymbolClassifierProvider(
_projectFactory = projectFactory;

// Receive notification for Visual Studio theme change
// and update colors at startup to prevent colors from before a theme-change to reappear.
VSColorTheme.ThemeChanged += UpdateTheme;
_classificationColorManager.UpdateColors(force: true);
}

private void UpdateTheme(EventArgs e)
{
_classificationColorManager.UpdateColors(force: false);
_classificationColorManager.UpdateColors();
}

public IClassifier GetClassifier(ITextBuffer buffer)
Expand Down Expand Up @@ -510,14 +500,12 @@ public UnusedSymbolClassifierProvider(
_projectFactory = projectFactory;

// Receive notification for Visual Studio theme change
// and update colors at startup to prevent colors from before a theme-change to reappear.
VSColorTheme.ThemeChanged += UpdateTheme;
_classificationColorManager.UpdateColors(force: true);
}

private void UpdateTheme(EventArgs e)
{
_classificationColorManager.UpdateColors(force: false);
_classificationColorManager.UpdateColors();
}

public IClassifier GetClassifier(ITextBuffer buffer)
Expand Down
16 changes: 1 addition & 15 deletions tests/FSharp.Editing.VisualStudio.Tests/Mocks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ open Microsoft.VisualStudio.Text.Editor
open Microsoft.VisualStudio.TextManager.Interop
open Microsoft.VisualStudio.OLE.Interop
open EnvDTE
open FSharpVSPowerTools
open FSharp.Editing
open FSharp.Editing.VisualStudio
open FSharp.Editing.VisualStudio.ProjectSystem
Expand Down Expand Up @@ -307,17 +306,4 @@ let createSVsRunningDocumentTable(dte: DTE) =
member __.UnadviseRunningDocTableEvents(dwCookie) = notimpl
member __.UnlockDocument(grfRDTLockType, dwCookie) = notimpl
member __.UnregisterDocumentLockHolder(dwLHCookie) = notimpl
}

let createClassificationColorManager() =
Mock<ClassificationColorManager>.With (fun x ->
<@
x.UpdateColors(any())
@>)

let createPrintfColorManager() =
Mock<PrintfColorManager>.With (fun x ->
<@
x.UpdateColors(any())
@>)

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type PrintfSpecifiersUsageTaggerHelper() =
serviceProvider = base.ServiceProvider,
projectFactory = base.ProjectFactory,
textDocumentFactoryService = base.DocumentFactoryService,
printfColorManager = base.PrintfColorManager)
printfColorManager = null)

member __.GetView buffer =
createMockTextView buffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type SymbolClassifierHelper() =
let classifierProvider =
new SymbolClassifierProvider(
serviceProvider = base.ServiceProvider,
classificationColorManager = base.ClassificationColorManager,
classificationColorManager = null,
projectFactory = base.ProjectFactory,
fsharpVsLanguageService = base.VsLanguageService,
classificationRegistry = base.ClassificationTypeRegistryService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type UnusedSymbolClassifierHelper() =
new UnusedSymbolClassifierProvider
(
serviceProvider = base.ServiceProvider,
classificationColorManager = base.ClassificationColorManager,
classificationColorManager = null,
projectFactory = base.ProjectFactory,
fsharpVsLanguageService = base.VsLanguageService,
classificationRegistry = base.ClassificationTypeRegistryService,
Expand Down
6 changes: 0 additions & 6 deletions tests/FSharp.Editing.VisualStudio.Tests/VsTestBase.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ type VsTestBase() =
let editorOperationsFactoryService = Mocks.createEditorOperationsFactoryService()
let textBufferUndoManagerProvider = Mocks.createTextBufferUndoManagerProvider()

let classificationColorManager = Mocks.createClassificationColorManager()
let printfColorManager = Mocks.createPrintfColorManager()

let fsharpLanguageService = FSharpLanguageService(serviceProvider)
let openDocumentsTracker = Mocks.OpenDocumentTrackerStub()
let fileSystem = FileSystem(openDocumentsTracker)
Expand Down Expand Up @@ -74,9 +71,6 @@ type VsTestBase() =
member __.EditorOperationsFactoryService = editorOperationsFactoryService
member __.TextBufferUndoManagerProvider = textBufferUndoManagerProvider
member __.ReferenceSourceProvider = referenceSourceProvider
member __.ClassificationColorManager = classificationColorManager
member __.PrintfColorManager = printfColorManager


member __.AddProject(project: IProjectProvider) =
dte.AddProject(project.Project.ProjectFile, project)
Expand Down

0 comments on commit 7af929e

Please sign in to comment.