From 44d12af346e8482756114db869cd9647a9e73d5d Mon Sep 17 00:00:00 2001 From: Etienne Baudoux Date: Fri, 16 Aug 2024 17:43:25 -0700 Subject: [PATCH 1/3] Fixed a regression where Linux theme is not properly detected --- .../DevToys.Linux/Core/ThemeListener.cs | 50 +++++++++++++++++-- .../desktop/DevToys.Linux/LinuxProgram.cs | 1 + 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs b/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs index b33f5704a0..e1e004b1af 100644 --- a/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs +++ b/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs @@ -2,14 +2,18 @@ using DevToys.Core.Settings; using DevToys.Blazor.Components; using DevToys.Blazor.Core.Services; +using Gio; +using GLib; +using Microsoft.Extensions.Logging; using Object = GObject.Object; using static GObject.Object; namespace DevToys.Linux.Core; [Export(typeof(IThemeListener))] -internal sealed class ThemeListener : IThemeListener +internal sealed partial class ThemeListener : IThemeListener { + private readonly ILogger _logger; private readonly ISettingsProvider _settingsProvider; private readonly Gtk.Settings _gtkSettings; @@ -19,6 +23,8 @@ internal sealed class ThemeListener : IThemeListener [ImportingConstructor] public ThemeListener(ISettingsProvider settingsProvider) { + _logger = this.Log(); + // Listen for app settings _settingsProvider = settingsProvider; _settingsProvider.SettingChanged += SettingsProvider_SettingChanged; @@ -147,8 +153,44 @@ private void UpdateSystemSettingsAndApplyTheme() private AvailableApplicationTheme GetCurrentSystemTheme() { - return _gtkSettings.GtkApplicationPreferDarkTheme || (_gtkSettings.GtkThemeName?.Contains("Dark", StringComparison.OrdinalIgnoreCase) ?? false) - ? AvailableApplicationTheme.Dark - : AvailableApplicationTheme.Light; + try + { + var bus = DBusConnection.Get(BusType.Session); + using var parameters = Variant.NewTuple([ + Variant.NewString("org.freedesktop.appearance"), Variant.NewString("color-scheme") + ]); + + using Variant ret = bus.CallSync( + busName: "org.freedesktop.portal.Desktop", + objectPath: "/org/freedesktop/portal/desktop", + interfaceName: "org.freedesktop.portal.Settings", + methodName: "Read", + parameters: parameters, + replyType: VariantType.New("(v)"), + flags: DBusCallFlags.None, + timeoutMsec: 2000, + cancellable: null + ); + + uint userThemePreference = ret.GetChildValue(0).GetVariant().GetVariant().GetUint32(); + + return userThemePreference switch + { + 1 => AvailableApplicationTheme.Dark, + 2 => AvailableApplicationTheme.Light, + _ => _gtkSettings.GtkApplicationPreferDarkTheme || + (_gtkSettings.GtkThemeName?.Contains("Dark", StringComparison.OrdinalIgnoreCase) ?? false) + ? AvailableApplicationTheme.Dark + : AvailableApplicationTheme.Light + }; + } + catch (Exception ex) + { + LogGetLinuxThemeFailed(ex); + return AvailableApplicationTheme.Light; + } } + + [LoggerMessage(0, LogLevel.Error, "Failed to detect Linux theme.")] + partial void LogGetLinuxThemeFailed(Exception ex); } diff --git a/src/app/dev/platforms/desktop/DevToys.Linux/LinuxProgram.cs b/src/app/dev/platforms/desktop/DevToys.Linux/LinuxProgram.cs index 57ca0d8d55..756a6edc31 100644 --- a/src/app/dev/platforms/desktop/DevToys.Linux/LinuxProgram.cs +++ b/src/app/dev/platforms/desktop/DevToys.Linux/LinuxProgram.cs @@ -31,6 +31,7 @@ internal partial class LinuxProgram internal LinuxProgram() { + Gio.Module.Initialize(); Application = Gtk.Application.New(null, Gio.ApplicationFlags.NonUnique); GLib.Functions.SetPrgname("DevToys"); From 970b908ceeb715d1ccf1b7f0f1040766e1e71da5 Mon Sep 17 00:00:00 2001 From: Etienne Baudoux Date: Sun, 18 Aug 2024 10:41:26 -0700 Subject: [PATCH 2/3] Addressed feedback --- .../desktop/DevToys.Linux/Core/ThemeListener.cs | 16 +++++++++++----- .../desktop/DevToys.Linux/LinuxProgram.cs | 1 - 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs b/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs index e1e004b1af..cb1e0a70a6 100644 --- a/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs +++ b/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs @@ -70,10 +70,12 @@ public void ApplyDesiredColorTheme() if (theme == AvailableApplicationTheme.Dark) { ActualAppTheme = ApplicationTheme.Dark; + _gtkSettings.GtkApplicationPreferDarkTheme = true; } else { ActualAppTheme = ApplicationTheme.Light; + _gtkSettings.GtkApplicationPreferDarkTheme = false; } _ignoreOperatingSystemSettingChanged = false; @@ -178,16 +180,20 @@ private AvailableApplicationTheme GetCurrentSystemTheme() { 1 => AvailableApplicationTheme.Dark, 2 => AvailableApplicationTheme.Light, - _ => _gtkSettings.GtkApplicationPreferDarkTheme || - (_gtkSettings.GtkThemeName?.Contains("Dark", StringComparison.OrdinalIgnoreCase) ?? false) - ? AvailableApplicationTheme.Dark - : AvailableApplicationTheme.Light + _ => FallBack() }; } catch (Exception ex) { LogGetLinuxThemeFailed(ex); - return AvailableApplicationTheme.Light; + return FallBack(); + } + + AvailableApplicationTheme FallBack() + { + return _gtkSettings.GtkThemeName?.Contains("Dark", StringComparison.OrdinalIgnoreCase) ?? false + ? AvailableApplicationTheme.Dark + : AvailableApplicationTheme.Light; } } diff --git a/src/app/dev/platforms/desktop/DevToys.Linux/LinuxProgram.cs b/src/app/dev/platforms/desktop/DevToys.Linux/LinuxProgram.cs index 756a6edc31..57ca0d8d55 100644 --- a/src/app/dev/platforms/desktop/DevToys.Linux/LinuxProgram.cs +++ b/src/app/dev/platforms/desktop/DevToys.Linux/LinuxProgram.cs @@ -31,7 +31,6 @@ internal partial class LinuxProgram internal LinuxProgram() { - Gio.Module.Initialize(); Application = Gtk.Application.New(null, Gio.ApplicationFlags.NonUnique); GLib.Functions.SetPrgname("DevToys"); From 06f11e716ac605f6368847c0196ea545976cced8 Mon Sep 17 00:00:00 2001 From: Etienne Baudoux Date: Sun, 20 Oct 2024 08:58:46 -0700 Subject: [PATCH 3/3] Addressed feedback --- .../dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs b/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs index cb1e0a70a6..5e978013e5 100644 --- a/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs +++ b/src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs @@ -180,7 +180,7 @@ private AvailableApplicationTheme GetCurrentSystemTheme() { 1 => AvailableApplicationTheme.Dark, 2 => AvailableApplicationTheme.Light, - _ => FallBack() + _ => AvailableApplicationTheme.Light }; } catch (Exception ex)