initial
This commit is contained in:
55
App.xaml.cs
Normal file
55
App.xaml.cs
Normal 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 we’re 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user