Skip to content

Commit

Permalink
Registry stuff for accent colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Reabstraction committed Aug 4, 2023
1 parent 61e4933 commit 2d96458
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 40 deletions.
6 changes: 4 additions & 2 deletions MicaForEveryone.Core/Models/ProcessRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ProcessRule : IRule
public ProcessRule(string processName)
{
ProcessName = processName;
ProcessNames = processName.Split("|").ToList();
}

public string Name => $"Process({ProcessName})";
Expand All @@ -21,6 +22,8 @@ public ProcessRule(string processName)
[XclParameter]
public string ProcessName { get; }

public List<string> ProcessNames { get; }

[XclField]
public TitlebarColorMode TitleBarColor { get; set; }

Expand All @@ -45,8 +48,7 @@ public ProcessRule(string processName)
[XclField]
public string BorderColor { get; set; } = string.Empty;

public bool IsApplicable(TargetWindow target) =>
target.ProcessName == ProcessName;
public bool IsApplicable(TargetWindow target) => ProcessNames.Contains(target.ProcessName);

public override string ToString() => Name;

Expand Down
69 changes: 31 additions & 38 deletions MicaForEveryone.Core/Models/TargetWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,28 @@ public void ApplyBorderColorRule(COLORREF borderColor)
DesktopWindowManager.SetBorderColor(WindowHandle, borderColor);
}

private static UInt32 HexToColorRef(string? value)
/**
* Converts a string, that may be a special value (i.e. Accent) to a COLORREF.
*/
private static UInt32 StringToColorRef(string? value)
{
if (value == null)
return 0;
// Special values
if (value == "Accent")
return AccentColor.GetAccentColor();

try
{
if (value == null)
return 0;

var rgb = Convert.ToUInt32(value, 16);
var bgr = ((rgb & 0xFF) << 16) | (rgb & 0xFF00) | ((rgb >> 16) & 0xFF);
return bgr;
var rgb = Convert.ToUInt32(value, 16);
var bgr = ((rgb & 0xFF) << 16) | (rgb & 0xFF00) | ((rgb >> 16) & 0xFF);
return bgr;
}
catch
{
return 0;
}
}

public void ApplyRule(IRule rule, TitlebarColorMode systemTitlebarColorMode)
Expand All @@ -129,44 +143,23 @@ public void ApplyRule(IRule rule, TitlebarColorMode systemTitlebarColorMode)
ApplyTitlebarColorRule(rule.TitleBarColor, systemTitlebarColorMode);
ApplyBackdropRule(rule.BackdropPreference);
ApplyCornerPreferenceRule(rule.CornerPreference);
if (rule.CaptionColor != null)

try
{
try
{
ApplyCaptionColorRule(HexToColorRef(rule.CaptionColor));
}
catch (FormatException)
{
}
catch (ArgumentOutOfRangeException)
{
}
if (rule.CaptionColor != null)
ApplyCaptionColorRule(StringToColorRef(rule.CaptionColor));
if (rule.CaptionTextColor != null)
ApplyCaptionTextColorRule(StringToColorRef(rule.CaptionTextColor));
if (rule.BorderColor != null)
ApplyBorderColorRule(StringToColorRef(rule.BorderColor));
}
if (rule.CaptionTextColor != null)
catch (FormatException)
{
try
{
ApplyCaptionTextColorRule(HexToColorRef(rule.CaptionTextColor));
}
catch (FormatException)
{
}
catch (ArgumentOutOfRangeException) {
}
}
if (rule.BorderColor != null)
catch (ArgumentOutOfRangeException)
{
try
{
ApplyBorderColorRule(HexToColorRef(rule.BorderColor));
}
catch (FormatException)
{
}
catch (ArgumentOutOfRangeException) {
}
}

if (rule.ExtendFrameIntoClientArea)
DesktopWindowManager.ExtendFrameIntoClientArea(WindowHandle);

Expand Down
7 changes: 7 additions & 0 deletions MicaForEveryone.Core/Services/RuleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public RuleService(ISettingsService settingsService)
_settingsService.RuleChanged += SettingsService_Changed;
_settingsService.ConfigFileReloaded += SettingsService_Changed;
_settingsService.ConfigFilePathChanged += SettingsService_Changed;
AccentColor.AccentColorChanged += Accent_Changed;
}

~RuleService()
Expand All @@ -28,6 +29,7 @@ public RuleService(ISettingsService settingsService)
_settingsService.RuleChanged -= SettingsService_Changed;
_settingsService.ConfigFileReloaded -= SettingsService_Changed;
_settingsService.ConfigFilePathChanged -= SettingsService_Changed;
AccentColor.AccentColorChanged -= Accent_Changed;
Dispose(false);
}

Expand Down Expand Up @@ -119,6 +121,11 @@ private void WinEvent_Handler(object sender, WindowOpenedEventArgs args)
MatchAndApplyRuleToWindow(target);
}

public void Accent_Changed(object? sender, EventArgs args)
{
SettingsService_Changed(sender, args);
}

private void Dispose(bool disposing)
{
if (!disposing) return;
Expand Down
87 changes: 87 additions & 0 deletions MicaForEveryone.Win32/AccentColor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using MicaForEveryone.Win32.PInvoke;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MicaForEveryone.Win32
{
public class AccentColor
{


private const int REG_NOTIFY_CHANGE_NAME = 0x1;
private const int REG_NOTIFY_CHANGE_ATTRIBUTES = 0x2;
private const int REG_NOTIFY_CHANGE_LAST_SET = 0x4;
private const int REG_NOTIFY_CHANGE_SECURITY = 0x8;

[DllImport("advapi32.dll", SetLastError = true)]
private static extern int RegNotifyChangeKeyValue(IntPtr hKey, bool bWatchSubtree, uint dwNotifyFilter, IntPtr hEvent, bool fAsynchronous);

private static COLORREF accentColor = new COLORREF(0x000000);

public static COLORREF GetAccentColor()
{
return accentColor;
}

private static RegistryKey accentKey;
public static EventHandler AccentColorChanged;

private static bool isInitialized = false;

public static void InitializeListeners()
{
if(isInitialized)
{
return;
}

// Get accent color
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\DWM");
// Event
EmptyEventCallback callback = new EmptyEventCallback(AccentChangedEventCall);

accentKey = key;

Task.Run(AccentChangedEventCall);

isInitialized = true;
}

delegate void EmptyEventCallback();

static void AccentChangedEventCall()
{
while (true)
{
object color = accentKey.GetValue("AccentColor");
if (color != null)
{
if (color.GetType() == typeof(Int32))
{
UInt32 uColor = (UInt32)(Int32)color;

byte[] bytes = BitConverter.GetBytes(uColor);

byte r = bytes[0];
byte g = bytes[1];
byte b = bytes[2];

accentColor = new COLORREF(r, g, b);
}
}

Console.WriteLine($"COLOR: {color}");


int hEvent = RegNotifyChangeKeyValue(accentKey.Handle.DangerousGetHandle(), true,
REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_ATTRIBUTES | REG_NOTIFY_CHANGE_LAST_SET | REG_NOTIFY_CHANGE_SECURITY,
IntPtr.Zero, false);
AccentColorChanged.Invoke(null, EventArgs.Empty);
}
}
}
}
2 changes: 2 additions & 0 deletions MicaForEveryone/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal static class Program
[STAThread]
public static async Task Main(string[] args)
{
AccentColor.InitializeListeners();

if (Environment.OSVersion.Version.Build < 18362)
{
Environment.Exit(1);
Expand Down

0 comments on commit 2d96458

Please sign in to comment.