diff --git a/ProxyForge/Interfaces/IWindowThemeService.cs b/ProxyForge/Interfaces/IWindowThemeService.cs new file mode 100644 index 0000000..35b706f --- /dev/null +++ b/ProxyForge/Interfaces/IWindowThemeService.cs @@ -0,0 +1,8 @@ +using System.Windows; + +namespace ProxyForge.Interfaces; + +internal interface IWindowThemeService +{ + void Bind(Window window); +} \ No newline at end of file diff --git a/ProxyForge/Plugin/PluginHost.cs b/ProxyForge/Plugin/PluginHost.cs index 06ac661..180be30 100644 --- a/ProxyForge/Plugin/PluginHost.cs +++ b/ProxyForge/Plugin/PluginHost.cs @@ -3,6 +3,7 @@ using ProxyForge.Core; using ProxyForge.Detection; using ProxyForge.Interfaces; using ProxyForge.Memory; +using ProxyForge.Services; using ProxyForge.Settings; using ProxyForge.Streaming; using ProxyForge.ViewModels; @@ -60,6 +61,7 @@ internal sealed class PluginHost : IDisposable var chunkAllocator = new ChunkAllocator(65536); MfSession.AddRef(); + WindowThemeRegistry.Register(new WindowThemeService()); IProxyEncoderFactory encoderFactory = new MfProxyEncoderFactory( budget, @@ -124,6 +126,7 @@ internal sealed class PluginHost : IDisposable { var vm = new GenerationPopupViewModel(CacheManager.ActiveGenerations); _popupView = new GenerationPopupView(vm); + WindowThemeRegistry.Bind(_popupView); var mainWindow = Application.Current?.MainWindow; if (mainWindow is { IsLoaded: true }) diff --git a/ProxyForge/Services/WindowThemeRegistry.cs b/ProxyForge/Services/WindowThemeRegistry.cs new file mode 100644 index 0000000..33085a8 --- /dev/null +++ b/ProxyForge/Services/WindowThemeRegistry.cs @@ -0,0 +1,22 @@ +using ProxyForge.Interfaces; +using System.Windows; + +namespace ProxyForge.Services; + +internal static class WindowThemeRegistry +{ + private static IWindowThemeService _service = NullWindowThemeService.Instance; + + internal static void Register(IWindowThemeService service) + => Interlocked.Exchange(ref _service, service ?? NullWindowThemeService.Instance); + + internal static void Bind(Window window) + => _service.Bind(window); + + private sealed class NullWindowThemeService : IWindowThemeService + { + internal static readonly NullWindowThemeService Instance = new(); + private NullWindowThemeService() { } + public void Bind(Window window) { } + } +} \ No newline at end of file diff --git a/ProxyForge/Services/WindowThemeService.cs b/ProxyForge/Services/WindowThemeService.cs new file mode 100644 index 0000000..731611b --- /dev/null +++ b/ProxyForge/Services/WindowThemeService.cs @@ -0,0 +1,47 @@ +using ProxyForge.Interfaces; +using System.Runtime.InteropServices; +using System.Windows; +using System.Windows.Interop; +using System.Windows.Media; + +namespace ProxyForge.Services; + +internal sealed class WindowThemeService : IWindowThemeService +{ + public void Bind(Window window) + { + if (window is null) return; + window.SourceInitialized += (_, _) => ApplyCurrentTheme(window); + window.Loaded += (_, _) => ApplyCurrentTheme(window); + } + + private static void ApplyCurrentTheme(Window window) + { + if (!OperatingSystem.IsWindowsVersionAtLeast(10, 0, 22000)) return; + var hwnd = new WindowInteropHelper(window).Handle; + if (hwnd == nint.Zero) return; + + var captionBrush = (window.TryFindResource(SystemColors.ControlBrushKey) as SolidColorBrush) ?? Brushes.White; + var textBrush = (window.TryFindResource(SystemColors.WindowTextBrushKey) as SolidColorBrush) ?? Brushes.Black; + + SetDwmColor(hwnd, NativeMethods.DwmwaCaption, captionBrush.Color); + SetDwmColor(hwnd, NativeMethods.DwmwaBorder, captionBrush.Color); + SetDwmColor(hwnd, NativeMethods.DwmwaText, textBrush.Color); + } + + private static void SetDwmColor(nint hwnd, uint attribute, Color color) + { + uint colorRef = color.R | ((uint)color.G << 8) | ((uint)color.B << 16); + NativeMethods.DwmSetWindowAttribute(hwnd, attribute, ref colorRef, sizeof(uint)); + } + + private static class NativeMethods + { + internal const uint DwmwaCaption = 35; + internal const uint DwmwaBorder = 34; + internal const uint DwmwaText = 36; + + [DllImport("dwmapi.dll")] + internal static extern int DwmSetWindowAttribute(nint hwnd, uint dwAttribute, ref uint pvAttribute, uint cbAttribute); + } +} \ No newline at end of file