1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Merge pull request #592 from Cheerpipe/try_icon

Fixed tray icon color not matching the system theme but the app theme
This commit is contained in:
Robert Beekman 2021-05-17 09:27:34 +02:00 committed by GitHub
commit 4ab8459927
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 31 deletions

View File

@ -71,7 +71,7 @@ namespace Artemis.UI.Providers
Execute.OnUIThreadSync(() =>
{
using FileStream stream = File.OpenWrite(imagePath);
GetEncoderForIcon(icon, _themeWatcher.GetWindowsTheme() == ThemeWatcher.WindowsTheme.Dark ? Colors.White : Colors.Black).Save(stream);
GetEncoderForIcon(icon, _themeWatcher.GetSystemTheme() == ThemeWatcher.WindowsTheme.Dark ? Colors.White : Colors.Black).Save(stream);
});
new ToastContentBuilder()

View File

@ -51,9 +51,11 @@ namespace Artemis.UI.Screens
_themeWatcher = new ThemeWatcher();
_colorScheme = settingsService.GetSetting("UI.ColorScheme", ApplicationColorScheme.Automatic);
_colorScheme.SettingChanged += ColorSchemeOnSettingChanged;
_themeWatcher.ThemeChanged += ThemeWatcherOnThemeChanged;
_themeWatcher.SystemThemeChanged += _themeWatcher_SystemThemeChanged;
_themeWatcher.AppsThemeChanged += _themeWatcher_AppsThemeChanged;
ApplyColorSchemeSetting();
ApplyTryIconTheme(_themeWatcher.GetSystemTheme());
windowService.ConfigureMainWindowProvider(this);
bool autoRunning = Bootstrapper.StartupArguments.Contains("--autorun");
@ -170,28 +172,31 @@ namespace Artemis.UI.Screens
private void ApplyColorSchemeSetting()
{
if (_colorScheme.Value == ApplicationColorScheme.Automatic)
ApplyWindowsTheme(_themeWatcher.GetWindowsTheme());
ApplyUITheme(_themeWatcher.GetAppsTheme());
else
ChangeMaterialColors(_colorScheme.Value);
}
private void ApplyWindowsTheme(ThemeWatcher.WindowsTheme windowsTheme)
private void ApplyUITheme(ThemeWatcher.WindowsTheme theme)
{
Execute.PostToUIThread(() =>
{
Icon = windowsTheme == ThemeWatcher.WindowsTheme.Dark
? new BitmapImage(new Uri("pack://application:,,,/Artemis.UI;component/Resources/Images/Logo/bow-white.ico"))
: new BitmapImage(new Uri("pack://application:,,,/Artemis.UI;component/Resources/Images/Logo/bow-black.ico"));
});
if (_colorScheme.Value != ApplicationColorScheme.Automatic)
return;
if (windowsTheme == ThemeWatcher.WindowsTheme.Dark)
if (theme == ThemeWatcher.WindowsTheme.Dark)
ChangeMaterialColors(ApplicationColorScheme.Dark);
else
ChangeMaterialColors(ApplicationColorScheme.Light);
}
private void ApplyTryIconTheme(ThemeWatcher.WindowsTheme theme)
{
Execute.PostToUIThread(() =>
{
Icon = theme == ThemeWatcher.WindowsTheme.Dark
? new BitmapImage(new Uri("pack://application:,,,/Artemis.UI;component/Resources/Images/Logo/bow-white.ico"))
: new BitmapImage(new Uri("pack://application:,,,/Artemis.UI;component/Resources/Images/Logo/bow-black.ico"));
});
}
private void ChangeMaterialColors(ApplicationColorScheme colorScheme)
{
PaletteHelper paletteHelper = new();
@ -203,9 +208,14 @@ namespace Artemis.UI.Screens
extensionsPaletteHelper.SetLightDark(colorScheme == ApplicationColorScheme.Dark);
}
private void ThemeWatcherOnThemeChanged(object sender, WindowsThemeEventArgs e)
private void _themeWatcher_AppsThemeChanged(object sender, WindowsThemeEventArgs e)
{
ApplyWindowsTheme(e.Theme);
ApplyUITheme(e.Theme);
}
private void _themeWatcher_SystemThemeChanged(object sender, WindowsThemeEventArgs e)
{
ApplyTryIconTheme(e.Theme);
}
private void ColorSchemeOnSettingChanged(object sender, EventArgs e)

View File

@ -11,7 +11,8 @@ namespace Artemis.UI.Utilities
{
private const string RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
private const string RegistryValueName = "AppsUseLightTheme";
private const string appsThemeRegistryValueName = "AppsUseLightTheme";
private const string systemThemeRegistryValueName = "SystemUsesLightTheme";
public ThemeWatcher()
{
@ -21,24 +22,44 @@ namespace Artemis.UI.Utilities
public void WatchTheme()
{
WindowsIdentity currentUser = WindowsIdentity.GetCurrent();
string query = string.Format(
string appsThemequery = string.Format(
CultureInfo.InvariantCulture,
@"SELECT * FROM RegistryValueChangeEvent WHERE Hive = 'HKEY_USERS' AND KeyPath = '{0}\\{1}' AND ValueName = '{2}'",
currentUser.User.Value,
RegistryKeyPath.Replace(@"\", @"\\"),
RegistryValueName);
appsThemeRegistryValueName);
string systemThemequery = string.Format(
CultureInfo.InvariantCulture,
@"SELECT * FROM RegistryValueChangeEvent WHERE Hive = 'HKEY_USERS' AND KeyPath = '{0}\\{1}' AND ValueName = '{2}'",
currentUser.User.Value,
RegistryKeyPath.Replace(@"\", @"\\"),
systemThemeRegistryValueName);
try
{
ManagementEventWatcher watcher = new(query);
watcher.EventArrived += (sender, args) =>
// For Apps theme
ManagementEventWatcher appsThemWatcher = new(appsThemequery);
appsThemWatcher.EventArrived += (_, _) =>
{
WindowsTheme newWindowsTheme = GetWindowsTheme();
OnThemeChanged(new WindowsThemeEventArgs(newWindowsTheme));
WindowsTheme newWindowsTheme = GetAppsTheme();
OnAppsThemeChanged(new WindowsThemeEventArgs(newWindowsTheme));
};
// Start listening for events
watcher.Start();
// Start listening for apps theme events
appsThemWatcher.Start();
// For System theme
ManagementEventWatcher systemThemWatcher = new(systemThemequery);
systemThemWatcher.EventArrived += (_, _) =>
{
WindowsTheme newWindowsTheme = GetSystemTheme();
OnSystemThemeChanged(new WindowsThemeEventArgs(newWindowsTheme));
};
// Start listening for system theme events
systemThemWatcher.Start();
}
catch (Exception)
{
@ -46,25 +67,40 @@ namespace Artemis.UI.Utilities
}
}
public WindowsTheme GetWindowsTheme()
private WindowsTheme GetTheme(string themeKeyName)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyPath))
{
object registryValueObject = key?.GetValue(RegistryValueName);
object registryValueObject = key?.GetValue(themeKeyName);
if (registryValueObject == null) return WindowsTheme.Light;
int registryValue = (int) registryValueObject;
int registryValue = (int)registryValueObject;
return registryValue > 0 ? WindowsTheme.Light : WindowsTheme.Dark;
}
}
public event EventHandler<WindowsThemeEventArgs> ThemeChanged;
protected virtual void OnThemeChanged(WindowsThemeEventArgs e)
public WindowsTheme GetAppsTheme()
{
ThemeChanged?.Invoke(this, e);
return GetTheme(appsThemeRegistryValueName);
}
public WindowsTheme GetSystemTheme()
{
return GetTheme(systemThemeRegistryValueName);
}
public event EventHandler<WindowsThemeEventArgs> AppsThemeChanged;
public event EventHandler<WindowsThemeEventArgs> SystemThemeChanged;
protected virtual void OnAppsThemeChanged(WindowsThemeEventArgs e)
{
AppsThemeChanged?.Invoke(this, e);
}
protected virtual void OnSystemThemeChanged(WindowsThemeEventArgs e)
{
SystemThemeChanged?.Invoke(this, e);
}
public enum WindowsTheme