-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathCarnacTrayIcon.cs
More file actions
78 lines (66 loc) · 2.05 KB
/
CarnacTrayIcon.cs
File metadata and controls
78 lines (66 loc) · 2.05 KB
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
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Linq;
using System.Windows;
using Application = System.Windows.Application;
namespace Carnac
{
public class CarnacTrayIcon : IDisposable
{
readonly NotifyIcon trayIcon;
public CarnacTrayIcon()
{
var exitMenuItem = new MenuItem
{
Text = Properties.Resources.ShellView_Exit
};
var PreferencesMenuItem = new MenuItem
{
Text = Properties.Resources.ShellView_Preferences
};
var iconStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Carnac.icon.embedded.ico");
trayIcon = new NotifyIcon
{
Icon = new Icon(iconStream),
ContextMenu = new ContextMenu(new[] { PreferencesMenuItem, exitMenuItem })
};
exitMenuItem.Click += (sender, args) =>
{
trayIcon.Visible = false;
Application.Current.Shutdown();
};
PreferencesMenuItem.Click += (sender, args) =>
{
ShowPreferencesWindow();
};
trayIcon.MouseClick += NotifyIconClick;
trayIcon.Visible = true;
}
public event Action OpenPreferences = () => { };
void NotifyIconClick(object sender, MouseEventArgs mouseEventArgs)
{
if (mouseEventArgs.Button == MouseButtons.Left)
{
ShowPreferencesWindow();
}
}
public void ShowPreferencesWindow()
{
var preferencesWindow = Application.Current.Windows.Cast<Window>().FirstOrDefault(x => x.Name == "PreferencesViewWindow");
if (preferencesWindow != null)
{
preferencesWindow.Activate();
}
else
{
OpenPreferences();
}
}
public void Dispose()
{
trayIcon.Dispose();
}
}
}