From 9aca0022de64cca887ef8ff4aff92c80371ba9c9 Mon Sep 17 00:00:00 2001 From: Owen McGirr Date: Tue, 28 Jul 2026 12:20:57 +0100 Subject: [PATCH] feat: add switch profile entry points --- src/SwitchifyPc.App/App.xaml.cs | 21 +++--- src/SwitchifyPc.App/IndependentWindowHost.cs | 39 ++++++++++ src/SwitchifyPc.App/MainWindow.xaml | 6 ++ src/SwitchifyPc.App/MainWindow.xaml.cs | 10 ++- src/SwitchifyPc.App/NativeTrayIcon.cs | 45 ++++++++---- .../IndependentWindowHostTests.cs | 61 ++++++++++++++++ src/SwitchifyPc.Tests/MainWindowTests.cs | 29 ++++++++ src/SwitchifyPc.Tests/NativeTrayIconTests.cs | 71 +++++++++++++++++++ 8 files changed, 257 insertions(+), 25 deletions(-) create mode 100644 src/SwitchifyPc.App/IndependentWindowHost.cs create mode 100644 src/SwitchifyPc.Tests/IndependentWindowHostTests.cs create mode 100644 src/SwitchifyPc.Tests/NativeTrayIconTests.cs diff --git a/src/SwitchifyPc.App/App.xaml.cs b/src/SwitchifyPc.App/App.xaml.cs index 5726dd1..d249532 100644 --- a/src/SwitchifyPc.App/App.xaml.cs +++ b/src/SwitchifyPc.App/App.xaml.cs @@ -37,7 +37,7 @@ public partial class App : System.Windows.Application private WindowsExistingInstanceSignal? existingInstanceSignal; private NativeTrayIcon? trayIcon; private SettingsWindow? settingsWindow; - private SwitchControlProfileWindow? switchControlProfileWindow; + private IndependentWindowHost? switchControlProfileWindowHost; private SetupGuideWindow? setupGuideWindow; private MainWindowViewModel mainWindowViewModel = new(); private SetupGuideViewModel setupGuideViewModel = new(); @@ -114,6 +114,7 @@ protected override void OnStartup(StartupEventArgs e) trayIcon = new NativeTrayIcon( ShowMainWindow, ShowSettingsWindow, + ShowSwitchControlProfiles, TrayStatusText, CanDisconnectBluetoothDevices, DisconnectBluetoothDevices, @@ -171,7 +172,7 @@ protected override void OnExit(ExitEventArgs e) trayIcon?.Dispose(); trayIcon = null; settingsWindow = null; - switchControlProfileWindow = null; + switchControlProfileWindowHost = null; setupGuideWindow = null; telemetryReporter?.Dispose(); telemetryReporter = null; @@ -201,7 +202,8 @@ private Window CreateMainWindow() InstallAvailableUpdateAsync, DisconnectBluetoothDevices, AcceptPairingApprovalAsync, - RejectPairingApproval); + RejectPairingApproval, + ShowSwitchControlProfiles); window.Closing += (_, eventArgs) => { if (isQuitting) return; @@ -296,16 +298,11 @@ private void ShowSwitchControlProfiles() { switchControlProfileStore ??= new JsonSwitchControlProfileStore( Path.Combine(UserDataDirectory(), "switch-control-profiles.json")); - if (switchControlProfileWindow is null) - { - switchControlProfileWindow = new SwitchControlProfileWindow( + switchControlProfileWindowHost ??= new IndependentWindowHost( + () => new SwitchControlProfileWindow( switchControlProfileStore, - () => switchControlSessionManager?.ActiveProfileId); - switchControlProfileWindow.Closed += (_, _) => switchControlProfileWindow = null; - } - switchControlProfileWindow.Owner = settingsWindow; - switchControlProfileWindow.Show(); - switchControlProfileWindow.Activate(); + () => switchControlSessionManager?.ActiveProfileId)); + switchControlProfileWindowHost.Show(); } private static void CenterWindow(Window window) diff --git a/src/SwitchifyPc.App/IndependentWindowHost.cs b/src/SwitchifyPc.App/IndependentWindowHost.cs new file mode 100644 index 0000000..bb63b91 --- /dev/null +++ b/src/SwitchifyPc.App/IndependentWindowHost.cs @@ -0,0 +1,39 @@ +using System.Windows; + +namespace SwitchifyPc.App; + +internal sealed class IndependentWindowHost + where TWindow : Window +{ + private readonly Func createWindow; + private TWindow? window; + + public IndependentWindowHost(Func createWindow) + { + this.createWindow = createWindow; + } + + public TWindow Show() + { + TWindow current = window ?? CreateWindow(); + current.Owner = null; + current.Show(); + current.WindowState = WindowState.Normal; + current.Activate(); + return current; + } + + private TWindow CreateWindow() + { + TWindow created = createWindow(); + window = created; + created.Closed += (_, _) => + { + if (ReferenceEquals(window, created)) + { + window = null; + } + }; + return created; + } +} diff --git a/src/SwitchifyPc.App/MainWindow.xaml b/src/SwitchifyPc.App/MainWindow.xaml index 63f5be0..7cfe7c2 100644 --- a/src/SwitchifyPc.App/MainWindow.xaml +++ b/src/SwitchifyPc.App/MainWindow.xaml @@ -271,6 +271,12 @@ FontWeight="SemiBold" AutomationProperties.LiveSetting="Polite" Visibility="{Binding HasActiveSwitchControlProfile, Converter={StaticResource BooleanToVisibilityConverter}}" /> +