forked from HearthSim/Hearthstone-Deck-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrayIcon.cs
104 lines (86 loc) · 3.58 KB
/
TrayIcon.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
#region
using System;
using System.Drawing;
using System.Windows.Forms;
using Hearthstone_Deck_Tracker.Utility.Extensions;
#endregion
namespace Hearthstone_Deck_Tracker
{
public class TrayIcon
{
public const string ClassCardsFirstMenuItemName = "classCardsFirst";
public const string StartHearthstoneMenuItemName = "startHearthstone";
public const string AutoSelectDeckMenuItemName = "autoSelectDeck";
public const string UseNoDeckMenuItemName = "useNoDeck";
public const string CheckedProperty = "Checked";
private NotifyIcon _notifyIcon;
public NotifyIcon NotifyIcon
{
get
{
if(_notifyIcon == null)
Initialize();
return _notifyIcon;
}
}
public void Initialize()
{
_notifyIcon = new NotifyIcon
{
Icon = new Icon(@"Images/HearthstoneDeckTracker16.ico"),
Visible = true,
ContextMenu = new ContextMenu(),
Text = "Hearthstone Deck Tracker v" + (Helper.GetCurrentVersion() ?? new Version("0.0")).ToVersionString()
};
var startHearthstonMenuItem = new MenuItem("Start Launcher/Hearthstone", (sender, args) => Helper.StartHearthstoneAsync().Forget())
{
Name = StartHearthstoneMenuItemName
};
_notifyIcon.ContextMenu.MenuItems.Add(startHearthstonMenuItem);
var useNoDeckMenuItem = new MenuItem("No-deck mode", (sender, args) => UseNoDeckContextMenu()) {Name = UseNoDeckMenuItemName};
_notifyIcon.ContextMenu.MenuItems.Add(useNoDeckMenuItem);
var autoSelectDeckMenuItem = new MenuItem("Autoselect deck", (sender, args) => AutoDeckDetectionContextMenu())
{
Name = AutoSelectDeckMenuItemName
};
_notifyIcon.ContextMenu.MenuItems.Add(autoSelectDeckMenuItem);
var classCardsFirstMenuItem = new MenuItem("Class cards first", (sender, args) => SortClassCardsFirstContextMenu())
{
Name = ClassCardsFirstMenuItemName
};
_notifyIcon.ContextMenu.MenuItems.Add(classCardsFirstMenuItem);
_notifyIcon.ContextMenu.MenuItems.Add("Show", (sender, args) => Core.MainWindow.ActivateWindow());
_notifyIcon.ContextMenu.MenuItems.Add("Exit", (sender, args) => Core.MainWindow.Close());
_notifyIcon.MouseClick += (sender, args) =>
{
if(args.Button == MouseButtons.Left)
Core.MainWindow.ActivateWindow();
};
_notifyIcon.BalloonTipClicked += (sender1, e) => { Core.MainWindow.ActivateWindow(); };
}
private void AutoDeckDetectionContextMenu()
=> Core.MainWindow.AutoDeckDetection(!(bool)GetContextMenuProperty(AutoSelectDeckMenuItemName, CheckedProperty));
private void UseNoDeckContextMenu()
{
if((bool)GetContextMenuProperty(UseNoDeckMenuItemName, CheckedProperty))
Core.MainWindow.SelectLastUsedDeck();
else
Core.MainWindow.SelectDeck(null, true);
}
private int IndexOfKeyContextMenuItem(string key) => NotifyIcon.ContextMenu.MenuItems.IndexOfKey(key);
public void SetContextMenuProperty(string key, string property, object value)
{
var target = NotifyIcon.ContextMenu.MenuItems[IndexOfKeyContextMenuItem(key)];
target.GetType().GetProperty(property).SetValue(target, value);
}
private object GetContextMenuProperty(string key, string property)
{
var target = NotifyIcon.ContextMenu.MenuItems[IndexOfKeyContextMenuItem(key)];
return target.GetType().GetProperty(property).GetValue(target, null);
}
private void SortClassCardsFirstContextMenu()
=> Core.MainWindow.SortClassCardsFirst(!(bool)GetContextMenuProperty(ClassCardsFirstMenuItemName, CheckedProperty));
public void ShowMessage(string text, string title = "Hearthstone Deck Tracker", int duration = 5, ToolTipIcon icon = ToolTipIcon.Info)
=> _notifyIcon.ShowBalloonTip(duration, title, text, icon);
}
}