using System.Diagnostics; using System.Runtime.Versioning; namespace DevDrive; /// /// Dialog that prompts the user to select which startup apps to launch. /// [SupportedOSPlatform("windows")] public class StartupDialog : Form { private readonly List _apps; private readonly List _checkboxes = []; private readonly Button _launchButton; private readonly Button _cancelButton; /// /// Creates a new startup dialog for the given apps. /// /// List of startup apps to display. /// Name of the drive for the title. public StartupDialog(List apps, string driveName) { _apps = apps; Text = $"{driveName} - Startup Apps"; FormBorderStyle = FormBorderStyle.FixedDialog; StartPosition = FormStartPosition.CenterScreen; MaximizeBox = false; MinimizeBox = false; AutoScaleMode = AutoScaleMode.Dpi; var padding = 16; var checkboxHeight = 28; var buttonHeight = 32; var buttonWidth = 100; var contentHeight = padding + (_apps.Count * checkboxHeight) + padding + buttonHeight + padding; var contentWidth = 350; ClientSize = new Size(contentWidth, contentHeight); var yPos = padding; foreach (var app in _apps) { var checkbox = new CheckBox { Text = app.Name, Tag = app, Checked = app.CheckedByDefault, Location = new Point(padding, yPos), Size = new Size(contentWidth - (padding * 2), checkboxHeight), Font = new Font("Segoe UI", 10) }; _checkboxes.Add(checkbox); Controls.Add(checkbox); yPos += checkboxHeight; } yPos += padding; _launchButton = new Button { Text = "Launch", DialogResult = DialogResult.OK, Location = new Point(contentWidth - padding - buttonWidth - 8 - buttonWidth, yPos), Size = new Size(buttonWidth, buttonHeight), Font = new Font("Segoe UI", 9) }; _launchButton.Click += OnLaunchClick; Controls.Add(_launchButton); _cancelButton = new Button { Text = "Skip", DialogResult = DialogResult.Cancel, Location = new Point(contentWidth - padding - buttonWidth, yPos), Size = new Size(buttonWidth, buttonHeight), Font = new Font("Segoe UI", 9) }; Controls.Add(_cancelButton); AcceptButton = _launchButton; CancelButton = _cancelButton; } private void OnLaunchClick(object? sender, EventArgs e) { LaunchSelectedApps(); } /// /// Launches all selected apps. /// private void LaunchSelectedApps() { foreach (var checkbox in _checkboxes) { if (!checkbox.Checked) { continue; } if (checkbox.Tag is not StartupApp app) { continue; } try { var startInfo = new ProcessStartInfo { FileName = Environment.ExpandEnvironmentVariables(app.Path), UseShellExecute = true }; if (!string.IsNullOrWhiteSpace(app.Arguments)) { startInfo.Arguments = Environment.ExpandEnvironmentVariables(app.Arguments); } if (!string.IsNullOrWhiteSpace(app.WorkingDirectory)) { startInfo.WorkingDirectory = Environment.ExpandEnvironmentVariables(app.WorkingDirectory); } Process.Start(startInfo); Console.WriteLine($"[DevDrive] Launched: {app.Name}"); } catch (Exception ex) { Console.WriteLine($"[DevDrive] Failed to launch {app.Name}: {ex.Message}"); } } } /// /// Shows the startup dialog if there are apps to display. /// /// List of startup apps. /// Name of the drive. public static void ShowIfNeeded(List apps, string driveName) { if (apps.Count == 0) { return; } if (apps.Count == 1) { var app = apps[0]; var result = MessageBox.Show( $"Launch {app.Name}?", $"{driveName} - Startup", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if (result == DialogResult.Yes) { LaunchSingleApp(app); } return; } Application.EnableVisualStyles(); using var dialog = new StartupDialog(apps, driveName); dialog.ShowDialog(); } private static void LaunchSingleApp(StartupApp app) { try { var startInfo = new ProcessStartInfo { FileName = Environment.ExpandEnvironmentVariables(app.Path), UseShellExecute = true }; if (!string.IsNullOrWhiteSpace(app.Arguments)) { startInfo.Arguments = Environment.ExpandEnvironmentVariables(app.Arguments); } if (!string.IsNullOrWhiteSpace(app.WorkingDirectory)) { startInfo.WorkingDirectory = Environment.ExpandEnvironmentVariables(app.WorkingDirectory); } Process.Start(startInfo); Console.WriteLine($"[DevDrive] Launched: {app.Name}"); } catch (Exception ex) { Console.WriteLine($"[DevDrive] Failed to launch {app.Name}: {ex.Message}"); } } }