forked from rubberduck-vba/SlimDucky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnect.cs
101 lines (86 loc) · 2.94 KB
/
Connect.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Extensibility;
using Microsoft.Vbe.Interop;
namespace SlimDucky
{
[ComVisible(true)]
[Guid(Guid)]
[ProgId(ProgId)]
[EditorBrowsable(EditorBrowsableState.Never)]
public class Connect : IDTExtensibility2
{
public const string Guid = "3971959D-003F-4D83-8747-3CE434E7553D";
public const string ProgId = "SlimDucky.Connect";
private VBE _vbe;
private AddIn _addIn;
private App _app;
private bool _isInitialized;
private bool _isBeginShutdownInvoked;
public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
{
_vbe = Application as VBE;
_addIn = AddInInst as AddIn;
_isBeginShutdownInvoked = false;
switch(ConnectMode)
{
case ext_ConnectMode.ext_cm_Startup:
// normal execution path - don't initialize just yet, wait for OnStartupComplete to be called by the host.
break;
case ext_ConnectMode.ext_cm_AfterStartup:
InitializeAddIn();
break;
}
}
public void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
{
switch(RemoveMode)
{
case ext_DisconnectMode.ext_dm_UserClosed:
ShutdownAddIn();
break;
case ext_DisconnectMode.ext_dm_HostShutdown:
if(_isBeginShutdownInvoked)
{
// this is the normal case: nothing to do here, we already ran ShutdownAddIn.
}
else
{
// some hosts do not call OnBeginShutdown: this mitigates it.
ShutdownAddIn();
}
break;
}
}
public void OnAddInsUpdate(ref Array custom) { }
public void OnStartupComplete(ref Array custom)
{
InitializeAddIn();
}
public void OnBeginShutdown(ref Array custom)
{
_isBeginShutdownInvoked = true;
ShutdownAddIn();
}
private void InitializeAddIn()
{
if(_isInitialized)
{
// The add-in is already initialized. See:
// The strange case of the add-in initialized twice
// http://msmvps.com/blogs/carlosq/archive/2013/02/14/the-strange-case-of-the-add-in-initialized-twice.aspx
return;
}
_app = new App(_vbe, _addIn);
_app.Startup();
_isInitialized = true;
}
private void ShutdownAddIn()
{
_app.Shutdown();
_app = null;
_isInitialized = false;
}
}
}