-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
166 lines (135 loc) · 4.74 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
namespace QuickRotate;
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Console.WriteLine("Starting");
Application.Run(new TrayContext());
}
}
class TrayContext : ApplicationContext
{
List<uint> displayIDs;
ContextMenuStrip contextMenu;
NotifyIcon trayIcon;
public TrayContext()
{
displayIDs = new();
contextMenu = new();
contextMenu.Opened += RefreshDisplays;
contextMenu.Closing += OnClosing;
contextMenu.Items.AddRange(new ToolStripItem[]{
new ToolStripMenuItem("Refresh Displays", null, RefreshDisplays),
new ToolStripMenuItem("Select/Deselect All", null, ToggleAll),
new ToolStripSeparator(),
new ToolStripSeparator(),
new ToolStripMenuItem("Landscape", null, Landscape),
new ToolStripMenuItem("Portrait", null, Portrait),
new ToolStripMenuItem("Landscape (Flipped)", null, LandscapeFlipped),
new ToolStripMenuItem("Portrait (Flipped)", null, PortraitFlipped),
new ToolStripSeparator(),
new ToolStripMenuItem("Exit", null, Exit)
});
trayIcon = new();
trayIcon.Icon = SystemIcons.Application;
trayIcon.Text = "QuickRotate";
trayIcon.Visible = true;
trayIcon.ContextMenuStrip = contextMenu;
}
void Refresh()
{
var newDisplayIDs = DisplayUtils.GetDisplayIDs();
if (displayIDs.SequenceEqual(newDisplayIDs))
return;
var items = contextMenu.Items;
for (int i = 0; i < displayIDs.Count; ++i)
items.RemoveAt(3);
displayIDs = newDisplayIDs;
for (int i = 0; i < displayIDs.Count; ++i)
{
var item = new ToolStripMenuItem($"Display {displayIDs[i]}");
item.CheckOnClick = true;
items.Insert(3 + i, item);
}
if (displayIDs.Count == 1) {
var item = items[3] as ToolStripMenuItem;
item!.Checked = true;
}
}
void ApplyRotation(DisplayUtils.Orientation orientation)
{
for (int i = 0; i < displayIDs.Count; ++i) {
var item = contextMenu.Items[3 + i] as ToolStripMenuItem;
if (!item!.Checked)
continue;
var id = displayIDs[i];
if (!DisplayUtils.Rotate(id, orientation)) {
MessageBox.Show($"Couldn't rotate display {id}", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
void RefreshDisplays(object? sender, EventArgs e)
{
Refresh();
}
// Of course the simple act of not closing the whole form when a checkbox
// is clicked must have 10 different solutions each with their own UI or
// behavioral problems. This one requires buttons to call .Close()
// explicitly, but at least it does not require checking cursor bounds or
// setting tags on everything.
void OnClosing(object? sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = e.CloseReason != ToolStripDropDownCloseReason.AppFocusChange
&& e.CloseReason != ToolStripDropDownCloseReason.CloseCalled;
}
void ToggleAll(object? sender, EventArgs e)
{
var items = contextMenu.Items;
var checkAll = false;
for (int i = 0; i < displayIDs.Count; ++i) {
var item = items[3 + i] as ToolStripMenuItem;
if (!item!.Checked)
{
checkAll = true;
break;
}
}
for (int i = 0; i < displayIDs.Count; ++i) {
var item = items[3 + i] as ToolStripMenuItem;
item!.Checked = checkAll;
}
}
void Landscape(object? sender, EventArgs e)
{
ApplyRotation(DisplayUtils.Orientation.LANDSCAPE);
contextMenu.Close();
}
void Portrait(object? sender, EventArgs e)
{
ApplyRotation(DisplayUtils.Orientation.PORTRAIT);
contextMenu.Close();
}
void LandscapeFlipped(object? sender, EventArgs e)
{
ApplyRotation(DisplayUtils.Orientation.LANDSCAPE_FLIPPED);
contextMenu.Close();
}
void PortraitFlipped(object? sender, EventArgs e)
{
ApplyRotation(DisplayUtils.Orientation.PORTRAIT_FLIPPED);
contextMenu.Close();
}
void Exit(object? sender, EventArgs e)
{
// Icon lingers after closing if this is not set explicitly
trayIcon.Visible = false;
Application.Exit();
}
}