-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDockableToolwindowPresenter.cs
83 lines (68 loc) · 2.45 KB
/
DockableToolwindowPresenter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Vbe.Interop;
namespace SlimDucky
{
public class DockableToolwindowPresenter
{
private readonly AddIn _addin;
private readonly Window _window;
private readonly VBE _vbe;
private object _userControlObject;
public DockableToolwindowPresenter(VBE vbe, AddIn addin, IDockableUserControl view)
{
_vbe = vbe;
_addin = addin;
UserControl = view;
_window = CreateToolWindow(view);
}
public IDockableUserControl UserControl { get; }
private Window CreateToolWindow(IDockableUserControl view)
{
Window toolWindow;
try
{
object control = null;
toolWindow = _vbe.Windows.CreateToolWindow(_addin, _DockableWindowHost.ProgId, view.Caption, view.ClassId, ref control);
_userControlObject = control;
}
catch(COMException exception)
{
Debug.WriteLine(exception);
throw;
}
var userControlHost = (_DockableWindowHost)_userControlObject;
toolWindow.Visible = true; //window resizing doesn't work without this
EnsureMinimumWindowSize(toolWindow);
toolWindow.Visible = true; // here Rubberduck checks window settings to show toolwindow at startup
userControlHost.AddUserControl(view as UserControl, new IntPtr(_vbe.MainWindow.HWnd));
return toolWindow;
}
private void EnsureMinimumWindowSize(Window window)
{
const int defaultWidth = 350;
const int defaultHeight = 200;
if(!window.Visible || window.LinkedWindows != null)
{
return;
}
if(window.Width < defaultWidth)
{
window.Width = defaultWidth;
}
if(window.Height < defaultHeight)
{
window.Height = defaultHeight;
}
}
public virtual void Show() => _window.Visible = true;
public virtual void Hide() => _window.Visible = false;
~DockableToolwindowPresenter()
{
// destructor for tracking purposes only - do not suppress unless
Debug.WriteLine("DockableToolwindowPresenter finalized.");
}
}
}