Files
Bedtimer/App.xaml.cs
2025-10-26 10:40:21 -05:00

56 lines
1.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
}
}