-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show existing window if minimized to tray
- Loading branch information
1 parent
fc0ac21
commit 87de11a
Showing
4 changed files
with
99 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters