This commit is contained in:
2025-10-26 10:40:21 -05:00
commit 45b9d70c90
14 changed files with 1043 additions and 0 deletions

55
App.xaml.cs Normal file
View File

@@ -0,0 +1,55 @@
using System;
using System.Linq;
using System.Threading;
using System.Windows;
namespace Bedtimer
{
public partial class App : Application
{
private Mutex? _single;
private TrayHost? _tray;
private void OnStartup(object sender, StartupEventArgs e)
{
var args = Environment.GetCommandLineArgs();
// 1) Elevated branches (exit immediately after doing the thing)
if (args.Length > 1 && args[1] == "--save-admin")
{
Config.SaveFromArgs(args);
Shutdown();
return;
}
// Handle elevated autostart toggle: Bedtimer.exe --autostart-admin on|off
AutoStart.HandleAdminArgs(args); // returns normally when not matching
// 2) Single-instance guard
_single = new Mutex(true, @"Global\Bedtimer_Singleton", out bool isNew);
if (!isNew)
{
Shutdown();
return;
}
// 3) Optional bootstrap: enable autostart only if current process is admin
// (Non-admin users won't be prompted on first run; they can toggle from UI later.)
if (!AutoStart.IsEnabled() && Config.IsProcessElevated())
{
// Will set HKLM\...\Run directly since were already elevated
AutoStart.SetEnabled(true);
}
// 4) Start tray + overlay
_tray = new TrayHost();
_tray.Init();
}
private void OnExit(object sender, ExitEventArgs e)
{
_tray?.Dispose();
_single?.Dispose();
}
}
}