Skip to content

Commit

Permalink
Show existing window if minimized to tray
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Sep 29, 2024
1 parent fc0ac21 commit 87de11a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 20 deletions.
16 changes: 15 additions & 1 deletion Guard.Core/InstallationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,27 @@ public static string GetOsVersionString()

public static string GetAppEditionString()
{
return InstallationContext.GetInstallationType() switch
return GetInstallationType() switch
{
InstallationType.CLASSIC_PORTABLE => "portable",
InstallationType.CLASSIC_INSTALLER => "installer",
InstallationType.MICROSOFT_STORE => "store",
_ => "unknown"
};
}

public static string GetMutexName()
{
if (installationType == InstallationType.CLASSIC_PORTABLE)
{
return "2FAGuardPortable";
}
else if (installationType == InstallationType.MICROSOFT_STORE)
{
return "2FAGuardStore";
}

return "2FAGuard";
}
}
}
38 changes: 19 additions & 19 deletions Guard.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,11 @@ public partial class App : Application
protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
string mutexName = "2FAGuard";

var installationInfo = InstallationInfo.GetInstallationContext();
InstallationContext.Init(installationInfo.installationType, installationInfo.version);

if (installationInfo.installationType == InstallationType.CLASSIC_PORTABLE)
{
mutexName += "Portable";
}
else if (installationInfo.installationType == InstallationType.MICROSOFT_STORE)
{
mutexName += "Store";
}
string mutexName = InstallationContext.GetMutexName();

singleInstanceMutex = new Mutex(true, mutexName, out bool notAlreadyRunning);

Expand Down Expand Up @@ -65,23 +57,31 @@ Process process in Process.GetProcessesByName(
&& process.MainModule.FileName.Equals(
currentProcess.MainModule.FileName
)
&& !process.MainWindowHandle.Equals(IntPtr.Zero)
)
{
IntPtr existingWindowHandle = process.MainWindowHandle;

if (NativeWindow.IsIconic(existingWindowHandle))
if (existingWindowHandle != IntPtr.Zero)
{
NativeWindow.ShowWindow(
existingWindowHandle,
NativeWindow.ShowWindowCommand.Restore
);
if (NativeWindow.IsIconic(existingWindowHandle))
{
NativeWindow.ShowWindow(
existingWindowHandle,
NativeWindow.ShowWindowCommand.Restore
);
}

NativeWindow.SetForegroundWindow(existingWindowHandle);
focusedOtherProcess = true;
break;
}

NativeWindow.SetForegroundWindow(existingWindowHandle);

focusedOtherProcess = true;
break;
// If the process has no main window, try to bring it to the front using IPC
if (IPC.SendToFront())
{
focusedOtherProcess = true;
break;
}
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions Guard.WPF/Core/IPC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.IO.Pipes;
using System.Windows;
using Guard.Core;
using Guard.Core.Storage;

namespace Guard.WPF.Core
{
internal class IPC
{
internal static void InitPipeServer()
{
MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;

var pipeServerThread = new Thread(() =>
{
using var pipeServer = new NamedPipeServerStream(
$"{InstallationContext.GetMutexName()}-Start-Pipe"
);

while (true)
{
pipeServer.WaitForConnection();
// When a connection is received, bring the app to the front
Application.Current.Dispatcher.Invoke(() =>
{
MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;
mainWindow.Show();
mainWindow.WindowState = WindowState.Normal;
mainWindow.Activate();

// Re-add tray icon if enabled to ensure it's visible (e.g. after a explorer.exe restart)
if (SettingsManager.Settings.MinimizeToTray)
{
mainWindow.RemoveTrayIcon();
mainWindow.AddTrayIcon();
}
});
pipeServer.Disconnect();
}
})
{
IsBackground = true
};
pipeServerThread.Start();
}

internal static bool SendToFront()
{
try
{
using var pipeClient = new NamedPipeClientStream(
$"{InstallationContext.GetMutexName()}-Start-Pipe"
);
pipeClient.Connect(1000);
return true;
}
catch (Exception)
{
return false;
}
}
}
}
2 changes: 2 additions & 0 deletions Guard.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public MainWindow(bool autostart)
);

SessionSwitchEvent.Register(this);

IPC.InitPipeServer();
}

private void OnWindowLoaded()
Expand Down

0 comments on commit 87de11a

Please sign in to comment.