-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
237 lines (209 loc) · 9.16 KB
/
App.xaml.cs
File metadata and controls
237 lines (209 loc) · 9.16 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Linq;
using System.Threading;
using System.Windows;
using Microsoft.Toolkit.Uwp.Notifications;
using ScreenshotSweeper.Helpers;
using ScreenshotSweeper.Models;
using ScreenshotSweeper.Services;
using ScreenshotSweeper.Views;
namespace ScreenshotSweeper
{
public partial class App : Application
{
// Static references to services for access from views
public static ConfigService? ConfigService { get; private set; }
public static FileMonitorService? FileMonitorService { get; private set; }
public static CleanupService? CleanupService { get; private set; }
public static TrayIconService? TrayService { get; private set; }
public static NotificationService? NotificationService { get; private set; }
// Command line flags
private bool _setupMode = false; // --setup: Run wizard only (installer mode)
private bool _startMinimized = false; // --minimized: Start minimized to tray
// Single-instance mutex
private static Mutex? _instanceMutex;
private const string MutexName = "ScreenshotSweeper_SingleInstance_8F7A3C2E";
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Prevent app from shutting down when main window closes —
// we'll control shutdown explicitly (tray Exit menu calls Shutdown()).
ShutdownMode = ShutdownMode.OnExplicitShutdown;
// Single-instance check: if another instance is running, bring it to front and exit
_instanceMutex = new Mutex(true, MutexName, out bool createdNew);
if (!createdNew)
{
// Another instance is already running — signal it to show window and exit
System.Console.WriteLine("[App] Another instance is already running. Exiting.");
BringExistingInstanceToFront();
Shutdown(0);
return;
}
// Parse command-line arguments
_setupMode = e.Args.Contains("--setup", StringComparer.OrdinalIgnoreCase);
_startMinimized = e.Args.Contains("--minimized", StringComparer.OrdinalIgnoreCase);
// Initialize config service
ConfigService = new ConfigService();
var config = ConfigService.LoadConfig();
// Setup mode: show wizard and exit (used by installer)
if (_setupMode)
{
var wizard = new SetupWizard();
wizard.ShowDialog();
Shutdown(0);
return;
}
// Normal startup: show wizard only if not configured
if (string.IsNullOrEmpty(config.ScreenshotFolderPath))
{
var wizard = new SetupWizard();
if (wizard.ShowDialog() != true)
{
// User cancelled - exit app
Shutdown(0);
return;
}
// Reload config after user completes wizard
config = ConfigService.LoadConfig();
}
// Initialize other services with the (possibly updated) config
NotificationService = new NotificationService(config);
FileMonitorService = new FileMonitorService(config);
CleanupService = new CleanupService(FileMonitorService, NotificationService);
TrayService = new TrayIconService(ConfigService);
// Start monitoring if folder is configured
if (!string.IsNullOrEmpty(config.ScreenshotFolderPath))
{
FileMonitorService.StartMonitoring();
CleanupService.Start();
System.Console.WriteLine("[App] Services started successfully");
}
else
{
System.Console.WriteLine("[App] Screenshot folder not configured - monitoring disabled");
}
// Create main window
if (MainWindow == null)
{
MainWindow = new MainWindow();
}
// Determine if we should start minimized to tray
bool shouldMinimize = _startMinimized || config.Startup.StartMinimized;
if (shouldMinimize)
{
// Start minimized to system tray — must Show first then Hide for proper initialization
MainWindow.WindowState = WindowState.Minimized;
MainWindow.ShowInTaskbar = false;
MainWindow.Show(); // Show first to initialize the window
MainWindow.Hide(); // Then hide
System.Console.WriteLine("[App] Started minimized to system tray (hidden)");
}
else
{
// Show window normally
try
{
MainWindow.WindowState = WindowState.Normal;
MainWindow.ShowInTaskbar = true;
MainWindow.Show();
MainWindow.Activate();
MainWindow.Topmost = true;
MainWindow.Topmost = false;
}
catch { /* Non-fatal */ }
}
// Wire cleanup events to update tray
CleanupService.StatusUpdated += (files) =>
{
long totalBytes = 0;
foreach (var f in files)
totalBytes += f.FileSizeBytes;
TrayService.UpdateStatus(files.Count, totalBytes, true);
};
// Handle notification actions
ToastNotificationManagerCompat.OnActivated += toastArgs =>
{
// Parse arguments from notification button clicks
ToastArguments args = ToastArguments.Parse(toastArgs.Argument);
string? filePath = null;
if (args.TryGetValue("path", out var path))
filePath = path;
if (string.IsNullOrEmpty(filePath))
return;
// Handle different actions
if (args.TryGetValue(Constants.ACTION_SET_TIMER, out var timerValue))
{
int minutes = timerValue switch
{
"15_min" => 15,
"30_min" => 30,
"1_hour" => 60,
_ => 30
};
CleanupService?.UpdateDeleteTime(filePath, minutes, TimeUnit.Minutes);
System.Console.WriteLine($"[App] Timer set to {minutes} minutes for: {filePath}");
}
else if (args.TryGetValue(Constants.ACTION_KEEP, out _))
{
var config = ConfigService?.LoadConfig();
if (config != null)
{
CleanupService?.MoveToKeep(filePath, config.KeepFolderPath);
System.Console.WriteLine($"[App] File moved to Keep folder: {filePath}");
}
}
};
}
protected override void OnExit(ExitEventArgs e)
{
FileMonitorService?.StopMonitoring();
CleanupService?.Stop();
TrayService?.Dispose();
// Release single-instance mutex
if (_instanceMutex != null)
{
_instanceMutex.ReleaseMutex();
_instanceMutex.Dispose();
_instanceMutex = null;
}
base.OnExit(e);
}
/// <summary>
/// Attempts to bring an existing instance's window to the foreground.
/// </summary>
private static void BringExistingInstanceToFront()
{
try
{
// Find existing window by process name and bring to front
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
var processes = System.Diagnostics.Process.GetProcessesByName(currentProcess.ProcessName);
foreach (var proc in processes)
{
if (proc.Id != currentProcess.Id && proc.MainWindowHandle != IntPtr.Zero)
{
// Use Windows API to restore and bring window to front
NativeMethods.ShowWindow(proc.MainWindowHandle, NativeMethods.SW_RESTORE);
NativeMethods.SetForegroundWindow(proc.MainWindowHandle);
break;
}
}
}
catch (Exception ex)
{
System.Console.WriteLine($"[App] Failed to bring existing instance to front: {ex.Message}");
}
}
}
/// <summary>
/// Native Windows API methods for window management.
/// </summary>
internal static class NativeMethods
{
public const int SW_RESTORE = 9;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
}