-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
174 lines (146 loc) · 5.27 KB
/
Program.cs
File metadata and controls
174 lines (146 loc) · 5.27 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
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
167
168
169
170
171
172
173
174
namespace MonitorWindowsRestore;
static class Program
{
private static NotifyIcon? _trayIcon;
private static WindowTracker? _tracker;
private static MonitorWatcher? _watcher;
private static WindowRestorer? _restorer;
private static readonly List<string> _logs = [];
private static readonly object _logLock = new();
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
// Ensure single instance
using var mutex = new Mutex(true, "MonitorWindowsRestore_SingleInstance", out bool createdNew);
if (!createdNew)
{
MessageBox.Show("Monitor Windows Restore is already running.", "Already Running",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
var config = Config.Load();
var state = WindowState.Load();
_watcher = new MonitorWatcher(config);
_tracker = new WindowTracker(config, state, _watcher);
_restorer = new WindowRestorer(config, state);
// Wire up events
_tracker.OnLog += Log;
_restorer.OnLog += Log;
_watcher.OnLog += Log;
_watcher.OnMonitorsRestored += () =>
{
try
{
_restorer.RestoreAll();
}
catch (Exception ex)
{
Log($"Error during auto-restore: {ex.Message}");
}
};
// Create system tray icon
var iconPath = Path.Combine(AppContext.BaseDirectory, "app.ico");
_trayIcon = new NotifyIcon
{
Icon = File.Exists(iconPath) ? new Icon(iconPath) : SystemIcons.Application,
Visible = true,
Text = "Monitor Windows Restore",
ContextMenuStrip = CreateContextMenu(config)
};
// Start services
_tracker.Start();
_watcher.Start();
Log($"Started - tracking {config.Programs.Count} programs");
Log($"Current monitors: {Screen.AllScreens.Length}");
Application.Run();
}
private static ContextMenuStrip CreateContextMenu(Config config)
{
var menu = new ContextMenuStrip();
var statusItem = new ToolStripMenuItem($"Monitors: {Screen.AllScreens.Length}")
{
Enabled = false
};
menu.Items.Add(statusItem);
menu.Items.Add(new ToolStripSeparator());
var trackNowItem = new ToolStripMenuItem("Track Windows Now");
trackNowItem.Click += (_, _) =>
{
_tracker?.TrackWindows();
ShowBalloon("Tracked window positions");
};
menu.Items.Add(trackNowItem);
var restoreNowItem = new ToolStripMenuItem("Restore Windows Now");
restoreNowItem.Click += (_, _) =>
{
try
{
_restorer?.RestoreAll();
ShowBalloon("Restored window positions");
}
catch (Exception ex)
{
Log($"Error during restore: {ex.Message}");
ShowBalloon($"Restore failed: {ex.Message}");
}
};
menu.Items.Add(restoreNowItem);
menu.Items.Add(new ToolStripSeparator());
var showLogsItem = new ToolStripMenuItem("Show Recent Logs");
showLogsItem.Click += (_, _) => ShowLogs();
menu.Items.Add(showLogsItem);
var openConfigItem = new ToolStripMenuItem("Open Config Folder");
openConfigItem.Click += (_, _) =>
{
System.Diagnostics.Process.Start("explorer.exe", AppContext.BaseDirectory);
};
menu.Items.Add(openConfigItem);
menu.Items.Add(new ToolStripSeparator());
var programsMenu = new ToolStripMenuItem("Tracked Programs");
foreach (var prog in config.Programs)
{
programsMenu.DropDownItems.Add(new ToolStripMenuItem(prog) { Enabled = false });
}
menu.Items.Add(programsMenu);
menu.Items.Add(new ToolStripSeparator());
var exitItem = new ToolStripMenuItem("Exit");
exitItem.Click += (_, _) =>
{
_tracker?.Stop();
_watcher?.Stop();
_trayIcon?.Dispose();
Application.Exit();
};
menu.Items.Add(exitItem);
// Update monitor count when menu opens
menu.Opening += (_, _) =>
{
statusItem.Text = $"Monitors: {Screen.AllScreens.Length}";
};
return menu;
}
private static void Log(string message)
{
var entry = $"[{DateTime.Now:HH:mm:ss}] {message}";
lock (_logLock)
{
_logs.Add(entry);
if (_logs.Count > 100) _logs.RemoveAt(0);
}
Console.WriteLine(entry);
}
private static void ShowLogs()
{
string logs;
lock (_logLock)
{
logs = string.Join(Environment.NewLine, _logs.TakeLast(20));
}
MessageBox.Show(logs, "Recent Logs", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private static void ShowBalloon(string message)
{
_trayIcon?.ShowBalloonTip(2000, "Monitor Windows Restore", message, ToolTipIcon.Info);
}
}