-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
273 lines (216 loc) · 12.7 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using FMUtils.KeyboardHook;
using FMUtils.WinApi;
using ProSnap.ActionItems;
using ProSnap.Properties;
namespace ProSnap
{
static class Program
{
private static Hook KeyboardHook;
private static NotifyIcon TrayIcon;
internal static List<ExtendedScreenshot> History;
internal static PeekPreview Preview;
internal static RegionSelector Selector;
internal static bool isTakingScrollingScreenshot = false;
internal static List<ExtendedScreenshot> timelapse = new List<ExtendedScreenshot>();
private static BackgroundWorker IconAnimation = new BackgroundWorker();
private static int CurrentIcon = 0;
[STAThread]
private static void Main()
{
Trace.Listeners.Add(new ReportListener());
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.ThreadException += Application_ThreadException;
//todo: It's reccomended that this be set in the manifest, but I can't figure out how to do that successfully with clickonce
Windowing.SetProcessDPIAware();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
System.Threading.Thread.CurrentThread.Name = "Main UI " + System.Threading.Thread.CurrentThread.ManagedThreadId;
Update.CheckForUpdate();
History = new List<ExtendedScreenshot>();
Preview = new PeekPreview();
Selector = new RegionSelector();
Trace.WriteLine(string.Format("Forcing the creation of windows by accessing their handles on the Main UI thread: {0}, {1}", Preview.Handle, Selector.Handle), string.Format("Program [{0}]", System.Threading.Thread.CurrentThread.Name));
KeyboardHook = new Hook("Global Action Hook");
KeyboardHook.KeyDownEvent += KeyDown;
KeyboardHook.KeyUpEvent += KeyUp;
TrayIcon = new NotifyIcon()
{
Icon = ProSnap.Properties.Resources.camera_36x36_icon,
Text = "ProSnap",
Visible = true,
ContextMenuStrip = new ContextMenuStrip()
};
//TrayIcon.ContextMenuStrip.Items.Add("Take Screenshot", null, new EventHandler((o, e) => { Thread.Sleep(3000); SpawnActionChain(ActiveShortcutProfile.Shortcuts.FirstOrDefault(s => s.Actions.ActionItems.FirstOrDefault().ActionType == ActionTypes.TakeForegroundScreenshot)); }));
TrayIcon.ContextMenuStrip.Items.Add(new ToolStripMenuItem("History", null, (o, e) => (o as ToolStripMenuItem).ShowDropDown()) { Name = "tsmiHistory" });
TrayIcon.ContextMenuStrip.Items.Add("Options", null, (o, e) => Options.Options.ShowOrActivate());
TrayIcon.ContextMenuStrip.Items.Add("Exit", null, (o, e) =>
{
Application.Exit();
DumpTraceReport();
});
TrayIcon.MouseDown += TrayIcon_MouseDown;
IconAnimation.WorkerSupportsCancellation = true;
IconAnimation.DoWork += IconAnimation_DoWork;
Application.Run();
KeyboardHook.isPaused = true;
TrayIcon.Visible = false;
}
private static void IconAnimation_DoWork(object sender, DoWorkEventArgs e)
{
do
{
CurrentIcon++;
if (CurrentIcon > 10)
CurrentIcon = 0;
switch (CurrentIcon)
{
case 1:
TrayIcon.Icon = ProSnap.Properties.Resources.camera_36x36_flash_small_icon;
break;
case 2:
TrayIcon.Icon = ProSnap.Properties.Resources.camera_36x36_flash_large_icon;
break;
case 3:
case 4:
case 5:
TrayIcon.Icon = ProSnap.Properties.Resources.camera_36x36_flash_xlarge_icon;
break;
default:
TrayIcon.Icon = ProSnap.Properties.Resources.camera_36x36_icon;
break;
}
Thread.Sleep(50);
} while (!IconAnimation.CancellationPending);
CurrentIcon = 0;
TrayIcon.Icon = ProSnap.Properties.Resources.camera_36x36_icon;
}
private static void TrayIcon_MouseDown(object sender, MouseEventArgs e)
{
Trace.WriteLine(string.Format("{0} Button", e.Button), string.Format("TrayIcon.MouseDown [{0}]", System.Threading.Thread.CurrentThread.Name));
switch (e.Button)
{
case MouseButtons.Left:
Trace.WriteLine("Show & activate preview...", string.Format("TrayIcon.MouseDown [{0}]", System.Threading.Thread.CurrentThread.Name));
Preview.Show();
Preview.Activate();
break;
case MouseButtons.Right:
if (Configuration.UpdateRestartRequired && TrayIcon.ContextMenuStrip.Items[0].Name != "tsmiRestart")
{
TrayIcon.ContextMenuStrip.Items.Insert(0, new ToolStripMenuItem("Relaunch for update", null, (o, a) => Application.Restart()) { Name = "tsmiRestart" });
TrayIcon.ContextMenuStrip.Items.Insert(1, new ToolStripSeparator());
}
var DefaultFont = new ToolStripMenuItem().Font;
ToolStripMenuItem HistoryItem = TrayIcon.ContextMenuStrip.Items.Cast<ToolStripItem>().Where(tsi => tsi.Name == "tsmiHistory").FirstOrDefault() as ToolStripMenuItem;
HistoryItem.DropDownItems.Clear();
HistoryItem.DropDownItems.AddRange(History.Select(ess => new ToolStripMenuItem(ess.WindowTitle, null, (mi, e_mi) => Program.Preview.Show(ess))
{
Tag = ess,
Image = ess.isFlagged ? Resources.heart_fill_12x11 : null,
Font = Preview.CurrentScreenshot == ess ? new Font(DefaultFont, FontStyle.Bold) : DefaultFont
}).ToArray());
HistoryItem.Enabled = HistoryItem.DropDownItems.Count > 0;
break;
}
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
var InnermostException = e.ExceptionObject as Exception;
Trace.WriteLine(string.Format("Unhandled Exception at '{0}':\n{1}", InnermostException.TargetSite, InnermostException), string.Format("Program.CurrentDomain_UnhandledException [{0}]", System.Threading.Thread.CurrentThread.Name));
DumpTraceReport();
if (!Debugger.IsAttached)
Crash.SubmitCrashReport();
new CrashReportForm().ShowDialog();
Application.Exit();
}
catch (Exception ex)
{
Trace.WriteLine(string.Format("Unhandled Exception... Exception:\n{0}", ex), string.Format("Program.CurrentDomain_UnhandledException [{0}]", System.Threading.Thread.CurrentThread.Name));
}
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
CurrentDomain_UnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, true));
}
internal static void DumpTraceReport()
{
try
{
ReportListener reporter = Trace.Listeners.Cast<TraceListener>().Where(tl => tl is ReportListener).FirstOrDefault() as ReportListener;
File.WriteAllText(Path.Combine(Configuration.LocalPath, "report.txt"), string.Join("\n", reporter.Messages.Select(m => string.Format("<{0}> {1}: {2}", m.Timestamp, m.Category, m.Message)).ToArray()));
}
catch (Exception e)
{
}
}
private static void KeyDown(KeyboardHookEventArgs e)
{
//Trace.WriteLine(e.ToString(), "Program.KeyDown");
//Trace.WriteLine("Shortcut match: " + CurrentShortcutItem.ToString(), "Program.KeyDown");
//Trace.WriteLine("isTakingScrollingScreenshot: " + isTakingScrollingScreenshot, "Program.KeyDown");
var Shortcut = Configuration.Shortcuts.FirstOrDefault(S => S.Enabled && S.KeyCombo.Key == e.Key && S.KeyCombo.isAltPressed == e.isAltPressed && S.KeyCombo.isCtrlPressed == e.isCtrlPressed && S.KeyCombo.isShiftPressed == e.isShiftPressed && S.KeyCombo.isWinPressed == e.isWinPressed);
if (Configuration.IgnoreAllKeyHooks || Shortcut == null || Shortcut.RequirePreviewOpen && !Preview.Focused)
return;
SpawnActionChain(Shortcut);
}
private static void KeyUp(KeyboardHookEventArgs e)
{
//Trace.WriteLine(e.ToString(), "Program.KeyUp");
if (Configuration.IgnoreAllKeyHooks)
return;
//if (e.Key == Keys.RControlKey && isTakingScrollingScreenshot)
//{
// //Trace.WriteLine("isTakingScrollingScreenshot: " + isTakingScrollingScreenshot);
// SpawnActionChain(ActiveShortcutProfile.Shortcuts.LastOrDefault());
//}
}
private static void SpawnActionChain(ShortcutItem CurrentShortcutItem)
{
Trace.WriteLine("Beginning action chain for " + CurrentShortcutItem, string.Format("Program.SpawnActionChain [{0}]", System.Threading.Thread.CurrentThread.Name));
//Creating a STA thread manually here, because in order for SaveFileDialog to work it must be created on a STA thread. BackgroundWorker/ThreadPool is MTA.
//todo: Is the above still relevant now that we safely invoke to the UI thread?
var HookAction = new Thread(new ThreadStart(() => DoActionItem(CurrentShortcutItem, 0, Preview.CurrentScreenshot ?? History.LastOrDefault())));
HookAction.Name = "Hook Action " + HookAction.ManagedThreadId;
HookAction.SetApartmentState(ApartmentState.STA);
HookAction.Start();
}
private static void DoActionItem(ShortcutItem ActiveShortcutItem, int itemIndex, ExtendedScreenshot ActionItemScreenshot)
{
if (ActiveShortcutItem == null || itemIndex >= ActiveShortcutItem.ActionChain.ActionItems.Count)
{
Trace.WriteLine("No more action items to execute", string.Format("Program.DoActionItem [{0}]", System.Threading.Thread.CurrentThread.Name));
//todo: Why can't the hook be reinstated on the action thread?
Preview.BeginInvoke(new MethodInvoker(() => KeyboardHook.isPaused = false));
IconAnimation.CancelAsync();
return;
}
IActionItem CurrentActionItem = ActiveShortcutItem.ActionChain.ActionItems[itemIndex];
//don't try and invoke the scrolling screenshot actions if they're the first type in the action chain (eg, solo items for their shortcuts) and there's no active scrolling screenshot happening
//at least this solves flashing the icon whenever 'End' key is pressed during normal computer use
if (!Program.isTakingScrollingScreenshot && (CurrentActionItem.ActionType == ActionTypes.ContinueScrollingScreenshot || CurrentActionItem.ActionType == ActionTypes.EndScrollingScreenshot) && itemIndex == 0)
return;
KeyboardHook.isPaused = true;
Trace.WriteLine(CurrentActionItem.ActionType, string.Format("Program.DoActionItem [{0}]", System.Threading.Thread.CurrentThread.Name));
//todo: figure out a better way to do these exclusions rather than harcoding behavior for scrolling screenshots
if (!IconAnimation.IsBusy)
if ((new[] { ActionTypes.ContinueScrollingScreenshot, ActionTypes.EndScrollingScreenshot }.Contains(CurrentActionItem.ActionType) && isTakingScrollingScreenshot) || !new[] { ActionTypes.ContinueScrollingScreenshot, ActionTypes.EndScrollingScreenshot }.Contains(CurrentActionItem.ActionType))
IconAnimation.RunWorkerAsync();
Task.Factory.StartNew<ExtendedScreenshot>(() => CurrentActionItem.Invoke(ActionItemScreenshot))
.ContinueWith(t => DoActionItem(ActiveShortcutItem, itemIndex + 1, t.Result));
}
}
}