using ClassNotify.Shared.Enums; namespace ClassNotify.Shared.Policies; public sealed record ClientLaunchDecision( bool StartProcess, bool SendShowUi, bool BringToForeground); /// /// Pure-function launch policy governing whether to spawn processes, send wakeups, or foreground UI. /// Guaranteed Invariant: BackgroundRecovery NEVER foregrounds UI or sends SHOW_UI on duplicate instance. /// public static class ClientLaunchPolicy { public static ClientLaunchDecision DecideLaunch( ClientLaunchIntent intent, bool existingInstanceRunning, bool userDismissedInSession = false) { if (existingInstanceRunning) { // Another instance is already alive return intent switch { // Background recovery finding an existing instance: silent exit, DO NOT send SHOW_UI! ClientLaunchIntent.BackgroundRecovery => new ClientLaunchDecision( StartProcess: false, SendShowUi: false, BringToForeground: false), // Explicit human request finding existing instance: wake it up to foreground! ClientLaunchIntent.UserInteractive => new ClientLaunchDecision( StartProcess: false, SendShowUi: true, BringToForeground: true), // Installer first-run finding existing instance: wake it up to foreground ClientLaunchIntent.InstallerFirstRun => new ClientLaunchDecision( StartProcess: false, SendShowUi: true, BringToForeground: true), _ => new ClientLaunchDecision(false, false, false) }; } // No existing instance running; starting fresh instance return intent switch { // Background recovery: start process, keep in background, DO NOT foreground ClientLaunchIntent.BackgroundRecovery => new ClientLaunchDecision( StartProcess: true, SendShowUi: false, BringToForeground: false), // User interactive: start process and immediately show window ClientLaunchIntent.UserInteractive => new ClientLaunchDecision( StartProcess: true, SendShowUi: false, BringToForeground: true), // Installer first run: start process and immediately show window ClientLaunchIntent.InstallerFirstRun => new ClientLaunchDecision( StartProcess: true, SendShowUi: false, BringToForeground: true), _ => new ClientLaunchDecision(true, false, false) }; } }