Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal partial class LinuxProgram

internal LinuxProgram()
{
Gio.Module.Initialize();
Copy link
Contributor

@badcel badcel Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this line. This is part of the sample as the Gio.Module is not initialized automatically.

For Libadwaita and GTK applications this is done automatically via the Gtk.Application class for all GTK dependencies including Gio.

Application = Gtk.Application.New(null, Gio.ApplicationFlags.NonUnique);

GLib.Functions.SetPrgname("DevToys");
Expand Down