Skip to content

Commit

Permalink
fix: Improve system dark theme detection on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
manfromarce authored and ahmed605 committed Sep 13, 2023
1 parent a2c02ca commit c10924b
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ namespace Uno.UI.Runtime.Skia.Gtk.Extensions.Helpers.Theming
internal class GtkSystemThemeHelperExtension : ISystemThemeHelperExtension, IDisposable
{
private bool _disposedValue;
private string _previousThemeName;

internal GtkSystemThemeHelperExtension(object owner)
{
_previousThemeName = Settings.Default.ThemeName;
ObserveSystemTheme();
}

Expand All @@ -29,6 +31,7 @@ private void ObserveSystemTheme()
{
var settings = Settings.Default;
settings.AddNotification(nameof(settings.ApplicationPreferDarkTheme), ApplicationPreferDarkThemeHandler);
settings.AddNotification(nameof(settings.ThemeName), ApplicationPreferDarkThemeHandler);
}
}

Expand All @@ -42,11 +45,24 @@ private void UnobserveSystemTheme()
{
var settings = Settings.Default;
settings.RemoveNotification(nameof(settings.ApplicationPreferDarkTheme), ApplicationPreferDarkThemeHandler);
settings.RemoveNotification(nameof(settings.ThemeName), ApplicationPreferDarkThemeHandler);
}
}

private void ApplicationPreferDarkThemeHandler(object o, GLib.NotifyArgs args)
=> SystemThemeChanged?.Invoke(o, EventArgs.Empty);
{
var settings = Settings.Default;
if (args.Property == nameof(settings.ApplicationPreferDarkTheme) || IsGtkThemeDark(_previousThemeName) != IsGtkThemeDark(settings.ThemeName))
{
SystemThemeChanged?.Invoke(o, EventArgs.Empty);
}
_previousThemeName = settings.ThemeName;
}

private bool IsGtkThemeDark(string themeName)
{
return themeName.Contains("-dark", StringComparison.OrdinalIgnoreCase);
}

public SystemTheme GetSystemTheme()
{
Expand All @@ -56,7 +72,7 @@ public SystemTheme GetSystemTheme()
}
else
{
return Settings.Default.ApplicationPreferDarkTheme ? SystemTheme.Dark : SystemTheme.Light;
return (Settings.Default.ApplicationPreferDarkTheme || IsGtkThemeDark(Settings.Default.ThemeName)) ? SystemTheme.Dark : SystemTheme.Light;
}
}

Expand Down

0 comments on commit c10924b

Please sign in to comment.