Added the possibility to configure the single & double-click action of the NotifyIcon.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2432 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2013-01-18 10:26:14 +00:00
commit 6fadd33549
2 changed files with 76 additions and 35 deletions

View file

@ -251,7 +251,7 @@ namespace Greenshot {
// //
this.notifyIcon.ContextMenuStrip = this.contextMenu; this.notifyIcon.ContextMenuStrip = this.contextMenu;
this.notifyIcon.Text = "Greenshot"; this.notifyIcon.Text = "Greenshot";
this.notifyIcon.MouseUp += new System.Windows.Forms.MouseEventHandler(this.NotifyIconClick); this.notifyIcon.MouseUp += new System.Windows.Forms.MouseEventHandler(this.NotifyIconClickTest);
// //
// backgroundWorkerTimer // backgroundWorkerTimer
// //

View file

@ -310,7 +310,9 @@ namespace Greenshot {
private SettingsForm settingsForm = null; private SettingsForm settingsForm = null;
// Make sure we have only one about form // Make sure we have only one about form
private AboutForm aboutForm = null; private AboutForm aboutForm = null;
// Timer for the double click test
private System.Timers.Timer doubleClickTimer = new System.Timers.Timer();
public NotifyIcon NotifyIcon { public NotifyIcon NotifyIcon {
get { get {
return notifyIcon; return notifyIcon;
@ -1143,41 +1145,80 @@ namespace Greenshot {
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void NotifyIconClick(object sender, MouseEventArgs e) { private void NotifyIconClickTest(object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left) {
return;
}
// The right button will automatically be handled with the context menu, here we only check the left. // The right button will automatically be handled with the context menu, here we only check the left.
if (e.Button == MouseButtons.Left) { if (conf.DoubleClickAction == ClickActions.NOTHING) {
switch (conf.LeftClickAction) { // As there isn't a double-click we can start the Left click
case LeftClickActions.OPEN_LAST_IN_EXPLORER: NotifyIconClick(conf.LeftClickAction);
string path = null; // ready with the test
string configPath = FilenameHelper.FillVariables(conf.OutputFilePath, false); return;
string lastFilePath = Path.GetDirectoryName(conf.OutputFileAsFullpath); }
if (Directory.Exists(lastFilePath)) { // If the timer is enabled we are waiting for a double click...
path = lastFilePath; if (doubleClickTimer.Enabled) {
} else if (Directory.Exists(configPath)) { // User clicked a second time before the timer tick: Double-click!
path = configPath; doubleClickTimer.Elapsed -= NotifyIconSingleClickTest;
} doubleClickTimer.Stop();
NotifyIconClick(conf.DoubleClickAction);
} else {
// User clicked without a timer, set the timer and if it ticks it was a single click
// Create timer, if it ticks before the NotifyIconClickTest is called again we have a single click
doubleClickTimer.Elapsed += NotifyIconSingleClickTest;
doubleClickTimer.Interval = SystemInformation.DoubleClickTime;
doubleClickTimer.Start();
}
}
try { /// <summary>
System.Diagnostics.Process.Start(path); /// Called by the doubleClickTimer, this means a single click was used on the tray icon
} catch (Exception ex) { /// </summary>
// Make sure we show what we tried to open in the exception /// <param name="sender"></param>
ex.Data.Add("path", path); /// <param name="e"></param>
throw ex; private void NotifyIconSingleClickTest(object sender, EventArgs e) {
} doubleClickTimer.Elapsed -= NotifyIconSingleClickTest;
break; doubleClickTimer.Stop();
case LeftClickActions.OPEN_LAST_IN_EDITOR: BeginInvoke((MethodInvoker)delegate {
if (File.Exists(conf.OutputFileAsFullpath)) { NotifyIconClick(conf.LeftClickAction);
CaptureHelper.CaptureFile(conf.OutputFileAsFullpath, DestinationHelper.GetDestination(EditorDestination.DESIGNATION)); });
} }
break;
case LeftClickActions.CONTEXT_MENU: /// <summary>
MethodInfo oMethodInfo = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic); /// Handle the notify icon click
oMethodInfo.Invoke(notifyIcon, null); /// </summary>
break; private void NotifyIconClick(ClickActions clickAction) {
default: switch (clickAction) {
// Do nothing case ClickActions.OPEN_LAST_IN_EXPLORER:
break; string path = null;
} string configPath = FilenameHelper.FillVariables(conf.OutputFilePath, false);
string lastFilePath = Path.GetDirectoryName(conf.OutputFileAsFullpath);
if (Directory.Exists(lastFilePath)) {
path = lastFilePath;
} else if (Directory.Exists(configPath)) {
path = configPath;
}
try {
System.Diagnostics.Process.Start(path);
} catch (Exception ex) {
// Make sure we show what we tried to open in the exception
ex.Data.Add("path", path);
throw ex;
}
break;
case ClickActions.OPEN_LAST_IN_EDITOR:
if (File.Exists(conf.OutputFileAsFullpath)) {
CaptureHelper.CaptureFile(conf.OutputFileAsFullpath, DestinationHelper.GetDestination(EditorDestination.DESIGNATION));
}
break;
case ClickActions.CONTEXT_MENU:
MethodInfo oMethodInfo = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
oMethodInfo.Invoke(notifyIcon, null);
break;
default:
// Do nothing
break;
} }
} }