4

I'm creating an add-in for Visual Studio 2008 that will allow me to switch between color schemes with a hotkey.

I got it to successfully load a color scheme and apply it, but it's very slow.

Here's the code that applies a scheme:

// The Theme class is a holder for a color scheme
private static void LoadTheme(Theme theme, DTE2 application)
{
    var items = GetItems(application);

    foreach (var item in items)
    {
        if (!theme.Properties.ContainsKey(item.Name)) continue;

        var prop = theme.Properties[item.Name];

        item.Background = prop.Background;
        item.Foreground = prop.Foreground;
        item.Bold = prop.Bold;
    }
}

private static IEnumerable<ColorableItems> GetItems(DTE2 application)
{
    var fontsAndColorsItems = (FontsAndColorsItems) application
                                                        .get_Properties("FontsAndColors", "TextEditor")
                                                        .Item("FontsAndColorsItems")
                                                        .Object;

    return fontsAndColorsItems.Cast<ColorableItems>();
}

Basically, GetItems gets the list of ColorableItems from Visual Studio's options. Setting a property on one of these items applies the change to VS immediately. Since a color scheme can contain over a hundred properties, this results in 300+ update operations and it takes a very long time (10+ seconds on my laptop).

I'd like to somehow tell VS that I don't want it to refresh while I'm updating properties, and when I'm done tell it to update, but I can't find a way of doing that.

Ideally the whole process would take 1 or 2 seconds, similar to running an Import/Export Settings with the VS wizard.

I'm also open to alternative approaches. I had an idea to simply overwrite the registry settings, but then I need a way of forcing VS to reload it's settings.

David Thibault
  • 8,638
  • 3
  • 37
  • 51

1 Answers1

3

Well, I found a solution that works very well. I can simply invoke the actual import/export settings functionality of Visual Studio, like this:

application.ExecuteCommand(
    "Tools.ImportandExportSettings", 
    string.Format("/import:\"{0}\"", file));

Where file is the full path to a vssettings file. This runs in about 1 second.

It will require some changes to the way my add-in works, but it's actually simpler this way.

David Thibault
  • 8,638
  • 3
  • 37
  • 51