Skip to content

Commit

Permalink
Windows Accent Colour usage
Browse files Browse the repository at this point in the history
- Added feature to check, and use Windows Accent Colour as the 'accent colour' of the TcNo Account Switcher as well.
  • Loading branch information
TCNOco committed Jun 30, 2021
1 parent d088af6 commit 61ebde0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
101 changes: 96 additions & 5 deletions TcNo-Acc-Switcher-Server/Data/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ public static async System.Threading.Tasks.Task ShowPlatform(string platform)
private Dictionary<string, string> _stylesheet;
[JsonIgnore] public Dictionary<string, string> Stylesheet { get => _instance._stylesheet; set => _instance._stylesheet = value; }

private bool _windowsAccent;
public bool WindowsAccent { get => _instance._windowsAccent; set => _instance._windowsAccent = value; }

// Constants
[JsonIgnore] public readonly string SettingsFile = "WindowSettings.json";
[JsonIgnore] public readonly string StylesheetFile = "StyleSettings.yaml";
Expand Down Expand Up @@ -544,6 +547,8 @@ public void LoadStylesheetFromFile()

// Get name of current stylesheet
GetCurrentStylesheet();
if (WindowsAccent)
SetAccentColor();
}

/// <summary>
Expand Down Expand Up @@ -645,11 +650,97 @@ public void Protocol_Toggle()
}
}

/// <summary>
/// Create shortcuts in Start Menu
/// </summary>
/// <param name="platforms">true creates Platforms folder & drops shortcuts, otherwise only places main program & tray shortcut</param>
public void StartMenu_Toggle(bool platforms)
#region WindowsAccent
[SupportedOSPlatform("windows")]
public void WindowsAccent_Toggle()
{
if (!WindowsAccent)
SetAccentColor(true);
else
GeneralInvocableFuncs.ShowToast("info", "Please restart the program after clicking Close.", "Accent color disabled", "toastarea");
}

[SupportedOSPlatform("windows")]
private void SetAccentColor() => SetAccentColor(false);
[SupportedOSPlatform("windows")]
private void SetAccentColor(bool userInvoked)
{
var accent = GetAccentColorHexString();
_instance._stylesheet["selectionBackground"] = accent;
_instance._stylesheet["linkColor"] = accent;
_instance._stylesheet["linkColor-hover"] = accent; // TODO: Make this lighter somehow
_instance._stylesheet["linkColor-active"] = accent; // TODO: Make this darker somehow
_instance._stylesheet["borderedItemBorderColorBottom-focus"] = accent;
_instance._stylesheet["buttonBorder-active"] = accent;
_instance._stylesheet["checkboxBackground-checked"] = accent;
_instance._stylesheet["listBackgroundColor-checked"] = accent;
_instance._stylesheet["listTextColor-before"] = accent;
_instance._stylesheet["updateBarBackground"] = accent;
_instance._stylesheet["platformBorderColor"] = accent;

var accentColorIntString = GetAccentColorIntString();
_instance._stylesheet["platformTransform-HoverAnimation-boxShadow-0"] = $"0 0 0 0 rgba({accentColorIntString}, 0.7)";
_instance._stylesheet["platformTransform-HoverAnimation-boxShadow-70"] = $"0 0 0 10px rgba({accentColorIntString}, 0)";
_instance._stylesheet["platformTransform-HoverAnimation-boxShadow-100"] = $"0 0 0 10px rgba({accentColorIntString}, 0)";

if (userInvoked)
_ = AppData.ReloadPage();
}

[SupportedOSPlatform("windows")]
public static string GetAccentColorHexString()
{
byte r, g, b, a;
(r, g, b, a) = GetAccentColor();
byte[] rgb = {r, g, b};
return '#' + BitConverter.ToString(rgb).Replace("-", string.Empty);
}

[SupportedOSPlatform("windows")]
public static string GetAccentColorIntString()
{
byte r, g, b, a;
(r, g, b, a) = GetAccentColor();
byte[] rgb = { r, g, b };
return Convert.ToInt32(r) + ", " + Convert.ToInt32(g) + ", " + Convert.ToInt32(b);
}

[SupportedOSPlatform("windows")]
public static (byte r, byte g, byte b, byte a) GetAccentColor()
{
using var dwmKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\DWM", RegistryKeyPermissionCheck.ReadSubTree);
const string keyExMsg = "The \"HKCU\\Software\\Microsoft\\Windows\\DWM\" registry key does not exist.";
if (dwmKey is null) throw new InvalidOperationException(keyExMsg);

var accentColorObj = dwmKey.GetValue("AccentColor");
if (accentColorObj is int accentColorDWord)
{
return ParseDWordColor(accentColorDWord);
}
else
{
const string valueExMsg = "The \"HKCU\\Software\\Microsoft\\Windows\\DWM\\AccentColor\" registry key value could not be parsed as an ABGR color.";
throw new InvalidOperationException(valueExMsg);
}
}

private static (byte r, byte g, byte b, byte a) ParseDWordColor(int color)
{
byte
a = (byte)((color >> 24) & 0xFF),
b = (byte)((color >> 16) & 0xFF),
g = (byte)((color >> 8) & 0xFF),
r = (byte)((color >> 0) & 0xFF);

return (r, g, b, a);
}
#endregion

/// <summary>
/// Create shortcuts in Start Menu
/// </summary>
/// <param name="platforms">true creates Platforms folder & drops shortcuts, otherwise only places main program & tray shortcut</param>
public void StartMenu_Toggle(bool platforms)
{
Globals.DebugWriteLine(@"[Func:Data\Settings\Steam.StartMenu_Toggle]");
if (platforms)
Expand Down
1 change: 1 addition & 0 deletions TcNo-Acc-Switcher-Server/Resources/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Settings_Tray_StartWindows: Start Tray with Windows
Settings_Tray_StartNow: '[Start Tray now]'
Settings_CurrentStyle: Current Style
Settings_Protocol: 'Enable tcno:\\ protocol (Recommended)'
Settings_WindowsAccent: Use the Windows Accent Colour
Cleaning_Header_StartMenuShortcuts: 'Shortcuts for Start Menu:'
Cleaning_Header_General: General Cleaning
Cleaning_Header_LoginHistory: Clean my login history
Expand Down
2 changes: 2 additions & 0 deletions TcNo-Acc-Switcher-Server/Shared/SharedSettings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@
}
</ul>
</div>
</div><div class="rowSetting">
<div class="form-check"><input class="form-check-input" type="checkbox" id="AppSett_WinAccent" @bind="_appSett.WindowsAccent" @onclick="() => _appSett.WindowsAccent_Toggle()"><label class="form-check-label" for="AppSett_WinAccent"></label></div><label for="AppSett_WinAccent">@_locale["Settings_WindowsAccent"]<br></label>
</div>

0 comments on commit 61ebde0

Please sign in to comment.