5

In the Visual Studio extension I'm developing, I provide a custom tool window.

I'm trying to find out if the user has the tool window docked to a side (left/right/top/bottom) or if it is floating. Is there a way to calculate the current position of my tool window and is there an event to know after a user has completed moving it?

Some background: I'm developing the CodeStream extension for Visual Studio. There is functionality that when a user selects code we show three circle buttons in the CodeStream tool window (it's actually a webview) to provide some actions. When the CodeStream panel is docked to the right, the 3 buttons are on the left side of the panel and this is correct.

But, if a user moves the CodeStream panel to the left side of screen or elsewhere, I'd like to be able to know that so I can direct our webview to move the 3 buttons to the right side of the screen. I've provided two screenshots below.

We also have a VSCode extension and I've implemented similar logic there.

enter image description here

enter image description here

brianc
  • 1,547
  • 3
  • 16
  • 30
  • 2
    Please refer to [this official document](https://learn.microsoft.com/en-us/visualstudio/extensibility/adding-a-tool-window?view=vs-2019#set-the-default-position-for-the-tool-window) and [this one](https://social.msdn.microsoft.com/Forums/vstudio/en-US/b400b696-744c-4090-bd04-4a4ef8cd41d0/vs-custom-tool-window-how-to-set-currently-active-document?forum=vsx). –  Oct 15 '20 at 10:00
  • thanks @HiGuy, I know about setting its default position/style, but I'm more looking for these things: 1) an event that is raised when a user moves it 2) how to figure out where it is in the editor (left/right/top/bottom/floating) – brianc Oct 15 '20 at 13:48
  • I am afraid that you can not get what you want so far. And I will give you any feedback if I find any ideas. –  Oct 20 '20 at 01:46
  • Once you had the answer, what would you do with it? There's a few different options you may have depending on your final goal. – Jason Malinowski Oct 22 '20 at 22:19
  • @JasonMalinowski I've added some additional background (and screenshots) – brianc Oct 28 '20 at 14:58
  • Maybe it is quite difficultly so far. The easiest way is that when you install this extension, manually adjust the position. – Mr Qian Nov 02 '20 at 08:38

1 Answers1

0

Take a look at the IVsWindowFrame.GetFramePos method:

using Microsoft.VisualStudio.Shell;
...
ThreadHelper.ThrowIfNotOnUIThread();

if (Package.GetGlobalService(typeof(Interop.IVsUIShell)) is Interop.IVsUIShell shellService)
{
    shellService.GetToolWindowEnum(out var toolWindows);

    foreach (Interop.IVsWindowFrame frame in ComUtilities.EnumerableFrom(toolWindows))
    {
        frame.GetProperty((int)VsFramePropID.DocView, out var docView);
        if (docView?.GetType().FullName == "MyNamespace.MyToolWindow")
        {
            frame.GetFramePos(out var pos, out var guid, out int px, out int py, out int pcx, out int pcy);

            // Do something

            break;
        }
    }
}

I have not explored the usage of the values provided by GetFramePos. The docs say "Returns the position of the window," so presumably they are useful for that purpose.

(There may be a more expedient way to obtain the target Interop.IVsWindowFrame than enumerating all the tool windows as in the above example... that's just what I was doing when I discovered this method.)

MikeTV
  • 645
  • 6
  • 14