Skip to content

Commit

Permalink
Update for shadowsocks#2379
Browse files Browse the repository at this point in the history
Move GetWindows10SystemThemeSetting() to Util.cs
WindowsThemeMode use enum
Status var windowsThemeMode rename to windowsThemeMode
Used `if else` to replace `switch case`
Delete legacy commint (Some code).
Increase contrast for icon.
  • Loading branch information
TGSAN committed Jun 15, 2019
1 parent 6f71e6c commit 95b760c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
31 changes: 31 additions & 0 deletions shadowsocks-csharp/Util/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@ public static string GetTempPath()
return _tempPath;
}

public enum WindowsThemeMode { Dark, Light }

// Support on Windows 10 1903+
public static WindowsThemeMode GetWindows10SystemThemeSetting()
{
WindowsThemeMode registData = WindowsThemeMode.Dark;
try
{
RegistryKey reg_HKCU = Registry.CurrentUser;
RegistryKey reg_ThemesPersonalize = reg_HKCU.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", false);
if (reg_ThemesPersonalize.GetValue("SystemUsesLightTheme") != null)
{
if (Convert.ToInt32(reg_ThemesPersonalize.GetValue("SystemUsesLightTheme").ToString()) == 0) // 0:dark mode, 1:light mode
registData = WindowsThemeMode.Dark;
else
registData = WindowsThemeMode.Light;
//Console.WriteLine(registData);
}
else
{
throw new Exception("Reg-Value SystemUsesLightTheme not found.");
}
}
catch
{
Logging.Info(
$"Cannot get Windows 10 system theme mode, return default value 0 (dark mode).");
}
return registData;
}

// return a full path with filename combined which pointed to the temporary directory
public static string GetTempPath(string filename)
{
Expand Down
54 changes: 11 additions & 43 deletions shadowsocks-csharp/View/MenuViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class MenuViewController
private LogForm logForm;
private HotkeySettingsForm hotkeySettingsForm;
private string _urlToOpen;
private int windowsThemeMode;
private Utils.WindowsThemeMode currentWindowsThemeMode;

public MenuViewController(ShadowsocksController controller)
{
Expand Down Expand Up @@ -172,17 +172,11 @@ private void UpdateTrayIcon()
bool global = config.global;

// set Windows 10 Theme color (1903+)
windowsThemeMode = getWindows10SystemThemeSetting();
currentWindowsThemeMode = Utils.GetWindows10SystemThemeSetting();

switch (windowsThemeMode)
{
case 1:
if (!global || !enabled)
icon_baseBitmap = getDarkTrayIcon(icon_baseBitmap);
break;
default:
break;
}
if (currentWindowsThemeMode == Utils.WindowsThemeMode.Light)
if (!global || !enabled)
icon_baseBitmap = getDarkTrayIcon(icon_baseBitmap);

icon_baseBitmap = getTrayIconByState(icon_baseBitmap, enabled, global);

Expand Down Expand Up @@ -227,14 +221,10 @@ private Bitmap getDarkTrayIcon(Bitmap originIcon)
{
Color flyBlue = Color.FromArgb(192, 0, 0, 0);
// Multiply with flyBlue
//int red = (255 - color.R) * flyBlue.R / 255;
//int green = (255 - color.G) * flyBlue.G / 255;
//int blue = (255 - color.B) * flyBlue.B / 255;
//int alpha = color.A * flyBlue.A / 255;
int red = color.R * flyBlue.R / 255;
int green = color.G * flyBlue.G / 255;
int blue = color.B * flyBlue.B / 255;
int alpha = color.A * flyBlue.A / 255;
int alpha = color.A;
iconCopy.SetPixel(x, y, Color.FromArgb(alpha, red, green, blue));
}
else
Expand All @@ -258,8 +248,12 @@ private Bitmap getTrayIconByState(Bitmap originIcon, bool enabled, bool global)
{
if (!enabled)
{
Color flyBlue = Color.FromArgb(192, 192, 192, 192);
// Multiply with flyBlue
Color flyBlue;
if (currentWindowsThemeMode == Utils.WindowsThemeMode.Light)
flyBlue = Color.FromArgb(128, 192, 192, 192); // Dark icon more transparent
else
flyBlue = Color.FromArgb(192, 192, 192, 192); // Light icon less transparent
int red = color.R * flyBlue.R / 255;
int green = color.G * flyBlue.G / 255;
int blue = color.B * flyBlue.B / 255;
Expand All @@ -285,32 +279,6 @@ private Bitmap getTrayIconByState(Bitmap originIcon, bool enabled, bool global)
return iconCopy;
}

public int getWindows10SystemThemeSetting()
{
// Support on Windows 10 1903+
int registData = 0; // 0:dark mode, 1:light mode
try
{
RegistryKey reg_HKCU = Registry.CurrentUser;
RegistryKey reg_ThemesPersonalize = reg_HKCU.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", false);
if (reg_ThemesPersonalize.GetValue("SystemUsesLightTheme") != null)
{
registData = Convert.ToInt32(reg_ThemesPersonalize.GetValue("SystemUsesLightTheme").ToString());
//Console.WriteLine(registData);
}
else
{
throw new Exception("Reg-Value SystemUsesLightTheme not found.");
}
}
catch
{
Logging.Info(
$"Cannot get Windows 10 system theme mode, return default value 0 (dark mode).");
}
return registData;
}

private Bitmap AddBitmapOverlay(Bitmap original, params Bitmap[] overlays)
{
Bitmap bitmap = new Bitmap(original.Width, original.Height, PixelFormat.Format64bppArgb);
Expand Down

0 comments on commit 95b760c

Please sign in to comment.