-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrayIcon.cs
119 lines (101 loc) · 2.9 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
namespace DesktopClock
{
public class TrayIcon
{
private readonly NotifyIcon notifyIcon = new()
{
Visible = true
};
private readonly Settings settings;
public TrayIcon(Settings settings)
{
this.settings = settings;
InitializeIcon();
InitializeContextMenu();
}
private void InitializeIcon()
{
// Get the WPF resource BitmapSource
var bitmapSource = (BitmapSource)System.Windows.Application.Current.FindResource("Icon");
// Convert to a WinForm Bitmap
using var stream = new MemoryStream();
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
encoder.Save(stream);
var bitmap = new Bitmap(stream);
// Convert to an Icon
using var icon = Icon.FromHandle(bitmap.GetHicon());
// Set the NotifyIcon icon
notifyIcon.Icon = icon;
}
private void InitializeContextMenu()
{
// Screen selection
var screenSelectionItem = new ToolStripMenuItem("Screen");
for (var i = 0; i < Screen.AllScreens.Length; i++)
screenSelectionItem.DropDownItems.Add(CreateScreenMenuItem(i));
// Clock position selection
var clockPositionSelectionItem = new ToolStripMenuItem("Position");
foreach (ClockPosition clockPosition in Enum.GetValues<ClockPosition>())
clockPositionSelectionItem.DropDownItems.Add(CreatePositionMenuItem(clockPosition));
// Create the menu
notifyIcon.ContextMenuStrip = new ContextMenuStrip
{
Items =
{
screenSelectionItem,
clockPositionSelectionItem,
new ToolStripMenuItem("Quit", null, Quit_Click),
}
};
}
private ToolStripMenuItem CreateScreenMenuItem(int screenIndex)
{
return new ToolStripMenuItem($"Screen {screenIndex + 1}", null, (sender, e) => ScreenSelected_Click(screenIndex))
{
Checked = screenIndex == settings.ScreenIndex
};
}
private ToolStripMenuItem CreatePositionMenuItem(ClockPosition clockPosition)
{
return new ToolStripMenuItem(clockPosition.GetDescription(), null, (sender, e) => PositionSelected_Click(clockPosition))
{
Checked = clockPosition == settings.ClockPosition
};
}
private void ScreenSelected_Click(int screenIndex)
{
settings.ScreenIndex = screenIndex;
settings.Save();
InitializeContextMenu();
OnItemClicked(TrayAction.ChangeScreen);
}
private void PositionSelected_Click(ClockPosition clockPosition)
{
settings.ClockPosition = clockPosition;
settings.Save();
InitializeContextMenu();
OnItemClicked(TrayAction.ChangePosition);
}
private void Quit_Click(object? sender, EventArgs e)
{
OnItemClicked(TrayAction.Quit);
}
public event EventHandler<TrayAction>? ItemClicked;
private void OnItemClicked(TrayAction action)
{
ItemClicked?.Invoke(this, action);
}
}
public enum TrayAction
{
ChangeScreen,
ChangePosition,
Quit
}
}