Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Explorer.exe monitoring feature. #945

Merged
merged 14 commits into from
Nov 15, 2024
58 changes: 58 additions & 0 deletions RetroBar/Utilities/ExplorerMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using ManagedShell.Common.Logging;
using ManagedShell.Interop;
using System;
using System.Windows.Forms;

namespace RetroBar.Utilities
{
public class ExplorerMonitor : IDisposable
{
private ExplorerMonitorWindow _explorerMonitorWindow;

public void ExplorerMonitorStart(WindowManager windowManagerRef)
{
if (_explorerMonitorWindow != null) { return; } // Prevent multiple monitors.

_explorerMonitorWindow = new ExplorerMonitorWindow(windowManagerRef); // Start monitoring.
}

public void Dispose()
{
_explorerMonitorWindow?.Dispose();
}

private class ExplorerMonitorWindow : NativeWindow, IDisposable
{
private readonly WindowManager _windowManagerRef;
private static readonly int WM_TASKBARCREATEDMESSAGE = NativeMethods.RegisterWindowMessage("TaskbarCreated");

public ExplorerMonitorWindow(WindowManager windowManagerRef)
{
_windowManagerRef = windowManagerRef;
CreateHandle(new CreateParams());
}

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_TASKBARCREATEDMESSAGE)
{
try
{
_windowManagerRef.ReopenTaskbars(); // Reopen taskbars if explorer.exe is restarted.
}
catch (Exception ex)
{
ShellLogger.Warning($"Error handling TaskbarCreated message on ExplorerMonitor: {ex.Message}");
}
}

base.WndProc(ref m); // Call the base class to process other messages so we dont accidentally cause crashes or bugs.
}

public void Dispose()
{
DestroyHandle();
}
}
}
}
7 changes: 6 additions & 1 deletion RetroBar/Utilities/WindowManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ManagedShell;
using ManagedShell;
using ManagedShell.AppBar;
using ManagedShell.Common.Logging;
using System;
Expand All @@ -18,12 +18,16 @@ public class WindowManager : IDisposable
private readonly ShellManager _shellManager;
private readonly Updater _updater;

private readonly ExplorerMonitor _explorerMonitor = new();

public WindowManager(ShellManager shellManager, StartMenuMonitor startMenuMonitor, Updater updater)
{
_shellManager = shellManager;
_startMenuMonitor = startMenuMonitor;
_updater = updater;

_explorerMonitor.ExplorerMonitorStart(this);

_shellManager.ExplorerHelper.HideExplorerTaskbar = true;

openTaskbars();
Expand Down Expand Up @@ -183,6 +187,7 @@ private void resetScreenCache()

public void Dispose()
{
_explorerMonitor?.Dispose();
_shellManager.ExplorerHelper.HideExplorerTaskbar = false;
Settings.Instance.PropertyChanged -= Settings_PropertyChanged;
}
Expand Down