-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPluginMain.cs
278 lines (232 loc) · 8.32 KB
/
PluginMain.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using PluginCore.Localization;
using PluginCore.Utilities;
using PluginCore.Managers;
using PluginCore;
using System.Drawing;
using System.Runtime.InteropServices;
using EditorMiniMap.Helpers;
using System.Linq;
using ScintillaNet;
namespace EditorMiniMap
{
public class PluginMain : IPlugin
{
private const int API_KEY = 1;
private const String NAME = "EditorMiniMap";
private const String GUID = "1F4B503B-E512-4a2d-B80D-15C87DD6D5FA";
private const String HELP = "www.flashdevelop.org/community/viewtopic.php?f=4&t=9397";
private const String DESCRIPTION = "Adds a minimap to code documents.";
private const String AUTHOR = "Joey Robichaud";
private String _settingsFilename = "";
private Settings _settings = null;
private ToolStripMenuItem _toggleMenuItem = null;
#region Required Properties
/// <summary>
/// Api level of the plugin
/// </summary>
public Int32 Api
{
get { return API_KEY; }
}
/// <summary>
/// Name of the plugin
/// </summary>
public String Name
{
get { return NAME; }
}
/// <summary>
/// GUID of the plugin
/// </summary>
public String Guid
{
get { return GUID; }
}
/// <summary>
/// Author of the plugin
/// </summary>
public String Author
{
get { return AUTHOR; }
}
/// <summary>
/// Description of the plugin
/// </summary>
public String Description
{
get { return DESCRIPTION; }
}
/// <summary>
/// Web address for help
/// </summary>
public String Help
{
get { return HELP; }
}
/// <summary>
/// Object that contains the settings
/// </summary>
[Browsable(false)]
public Object Settings
{
get { return this._settings; }
}
#endregion
#region Required Methods
/// <summary>
/// Initializes the plugin
/// </summary>
public void Initialize()
{
this.InitBasics();
this.LoadSettings();
this.CreateMenuItems();
this.AddEventHandlers();
}
/// <summary>
/// Disposes the plugin
/// </summary>
public void Dispose()
{
this.SaveSettings();
}
/// <summary>
/// Handles the incoming events
/// </summary>
public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority prority)
{
if (e.Type == EventType.FileOpen)
{
TryAddMiniMapPanels(PluginBase.MainForm.CurrentDocument);
}
else if (e.Type == EventType.FileSwitch)
{
// Scintilla control Visible property does not seem to respect getting set when
// the control is not visible. So when switching to a new document, we have to
// refresh the settings.
RefreshMiniMapPanels(PluginBase.MainForm.CurrentDocument);
}
}
#endregion
#region Plugin Methods
private void TryAddMiniMapPanels(ITabbedDocument document)
{
if (document == null)
return;
TryAddMiniMapPanel(document, document.SplitSci1);
TryAddMiniMapPanel(document, document.SplitSci2);
}
private MiniMapPanel TryAddMiniMapPanel(ITabbedDocument document, ScintillaControl sci)
{
if (document == null || sci == null)
return null;
MiniMapPanel miniMapPanel = TryGetMiniMapPanel(document, sci);
if (miniMapPanel != null)
return null;
var panel = new MiniMapPanel(document, sci, _settings);
var splitter = document.Controls.OfType<SplitContainer>().FirstOrDefault();
if (splitter == null)
return null;
if (document.SplitSci1 == sci)
splitter.Panel1.Controls.Add(panel);
else if (document.SplitSci2 == sci)
splitter.Panel2.Controls.Add(panel);
return panel;
}
private void RefreshMiniMapPanels(ITabbedDocument document)
{
RefreshMiniMapPanel(document, document.SplitSci1);
RefreshMiniMapPanel(document, document.SplitSci2);
}
private void RefreshMiniMapPanel(ITabbedDocument document, ScintillaControl sci)
{
var miniMapPanel = TryGetMiniMapPanel(document, sci)
?? TryAddMiniMapPanel(document, sci);
if (miniMapPanel != null)
miniMapPanel.RefreshSettings();
}
private MiniMapPanel TryGetMiniMapPanel(ITabbedDocument document, ScintillaControl sci)
{
if (document == null || sci == null)
return null;
var splitter = document.Controls.OfType<SplitContainer>().FirstOrDefault();
if (splitter == null)
return null;
if (document.SplitSci1 == sci)
return splitter.Panel1.Controls.OfType<MiniMapPanel>().FirstOrDefault();
else if (document.SplitSci2 == sci)
return splitter.Panel2.Controls.OfType<MiniMapPanel>().FirstOrDefault();
return null;
}
private void ToggleMiniMap(object sender, EventArgs e)
{
_settings.IsVisible = !_settings.IsVisible;
}
void settingObject_OnSettingsChanged()
{
if (_settings.IsVisible != _toggleMenuItem.Checked)
_toggleMenuItem.Checked = _settings.IsVisible;
}
#endregion
#region Custom Methods
/// <summary>
/// Initializes important variables
/// </summary>
public void InitBasics()
{
String dataPath = Path.Combine(PluginCore.Helpers.PathHelper.DataDir, NAME);
if (!Directory.Exists(dataPath)) Directory.CreateDirectory(dataPath);
_settingsFilename = Path.Combine(dataPath, "Settings.fdb");
}
/// <summary>
/// Adds the required event handlers
/// </summary>
public void AddEventHandlers()
{
// Set events you want to listen (combine as flags)
EventManager.AddEventHandler(this, EventType.FileOpen | EventType.FileSwitch);
_settings.OnSettingsChanged += new SettingsChangesEvent(settingObject_OnSettingsChanged);
}
/// <summary>
/// Adds shortcuts for manipulating the mini map
/// </summary>
public void CreateMenuItems()
{
ToolStripMenuItem menu = (ToolStripMenuItem)PluginBase.MainForm.FindMenuItem("ViewMenu");
_toggleMenuItem = new ToolStripMenuItem(ResourceHelper.GetString("EditorMiniMap.Label.ToggleMiniMap"), ResourceHelper.GetImage("MiniMap"), new EventHandler(ToggleMiniMap));
_toggleMenuItem.Checked = _settings.IsVisible;
PluginBase.MainForm.RegisterShortcutItem("EditorMiniMap.ShowMiniMap", _toggleMenuItem);
int index = menu.DropDownItems.IndexOfKey("FullScreen") + 1;
index = index > -1 ? index : 0;
if (!(menu.DropDownItems[index] is ToolStripSeparator))
menu.DropDownItems.Insert(index, new ToolStripSeparator());
menu.DropDownItems.Insert(index + 1, _toggleMenuItem);
}
/// <summary>
/// Loads the plugin settings
/// </summary>
public void LoadSettings()
{
_settings = new Settings();
if (!File.Exists(_settingsFilename)) this.SaveSettings();
else
{
Object obj = ObjectSerializer.Deserialize(_settingsFilename, _settings);
_settings = (Settings)obj;
_settings.MaxLineLimit = _settings.MaxLineLimit;
}
}
/// <summary>
/// Saves the plugin settings
/// </summary>
public void SaveSettings()
{
ObjectSerializer.Serialize(_settingsFilename, _settings);
}
#endregion
}
}