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