Creating tool windows

Register tool window

Tool window system in ReSharper provides easy way to display content to user without going through glitches of Visual Studio COM interfaces. All you need to do is put an assembly-level attribute describing tool window properties. This will create appropriate Tool Window and entry in the ReSharper > Windows menu.

[assembly: ToolWindowDescriptor(
  ProgramConfigurations.VS_ADDIN, 
  Id = "MyToolWindowID", 
  Text = "My Tool Window",
  Guid = "19f1d48b-c9b3-4740-9bbf-d5f3db675717", 
  ToolWindowVisibilityPersistenceScope = ToolWindowVisibilityPersistenceScope.SOLUTION, 
  IconResourceID = 302, 
  ShortcutString = "Control+Alt+M")]

Unfortunately, unmanaged nature of Visual Studio doesn't allow plugin developers to specify custom icons for their tool windows. We are trying to find the solution to this problem, but currently you can use only IconResourceID of 302, which is generic ReSharper icon.

Populate tool window

When you want to populate tool window with content, you can do this my creating ToolWindowContent instance and calling into WindowManager to send content.

Control control = new MyToolControl();
WindowManager.Instance.GetToolWindowFrame("MyToolWindowID").Content = new ToolWindowContent(control, "Content title");

Providing content on demand

At times, it is needed to provide content only when user requests window to be displayed. This can be achieved by subscribing to an event:

IToolWindowFrame frame = WindowManager.Instance.GetToolWindowFrame("MyToolWindowID");
frame.ContentRequired += ContentRequired;

private void ContentRequired(object sender, ToolWindowFrameEventArgs e)
{
  e.Frame.Content = new ToolWindowContent(new MyToolControl(), "Content title");
}

Creating tabbed tool windows

When it is needed to display multiply contents in a single tool window, like it is done in Find Results View, it can be done with ToolWindowContentWithTabs.

Here is how it is done for searchResults, where most of the code is for building placeholder text which is displayed, when there is no tabs.

ToolWindowContentWithTabs searchResults = new ToolWindowContentWithTabs();
        emptyLabel = new RichTextLabel();
        emptyLabel.BackColor = SystemColors.Control;
        emptyLabel.RichTextBlock.AddLine(new RichText("No search results available.", new TextStyle(FontStyle.Bold)));
        emptyLabel.RichTextBlock.AddLine(new RichText(string.Format("Use ReSharper > Search > Find Usages {0}", GetShortcutText("FindUsages")), TextStyle.Default));
        emptyLabel.RichTextBlock.Parameters = new RichTextBlockParameters(8, ContentAlignment.MiddleCenter);
        searchResults.EmptyControl = emptyLabel;
        WindowManager.Instance.GetToolWindowFrame(SearchResultsID).Content = searchResults;

When you need to add content to the tabbed tool window, you use ToolWindowTabContent:

// ToolWindowContentWithTabs searchResults;
ToolWindowTabContent content = new ToolWindowTabContent(contentControl, "Tab title", true);
searchResults.Tabs.Add(content);

Other content tasks

To activate specific content, use WindowManager. It will activate both tool window and tab if needed.

WindowManager.Instance.ActivateContent(content);

You can also use HideContent or CloseContent to get rid of the tool window or tab.

References

To.Be.Specified.dll – description

Samples and power toys

No samples

Labels

 
(None)