Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a regression where Linux theme is not properly detected #1382

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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();
Comment on lines +175 to +177

Choose a reason for hiding this comment

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

Somewhere here you should, if I read the documentation right, check for an G_IO_ERROR_CLOSED error, in which case the fallback should be called.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks ! (and sorry for the late reply)
I will try to do this soon. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

If an error occurs, the bus.CallSync() call will throw an exception as gir.core interpretes the error argument by itself and raises an exception. As there is already a try/catch in place this should be fine.

So you probably only need to explicitly define in the switch expression which Theme DevToys should use if the user has no preference instead of calling the fallback.


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
Loading