Repository: veler/DevToys
File: src/app/dev/platforms/desktop/DevToys.Linux/Core/ThemeListener.cs (C#)

Code before:

        ApplyDesiredColorTheme();
    }

    private AvailableApplicationTheme GetCurrentSystemTheme()
    {
        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,
                _ => FallBack()
            };
        }
        catch (Exception ex)
        {
            LogGetLinuxThemeFailed(ex);
            return FallBack();
        }

        AvailableApplicationTheme FallBack()
        {
            return _gtkSettings.GtkThemeName?.Contains("Dark", StringComparison.OrdinalIgnoreCase) ?? false
                ? AvailableApplicationTheme.Dark
                : AvailableApplicationTheme.Light;
        }
    }

    [LoggerMessage(0, LogLevel.Error, "Failed to detect Linux theme.")]
    partial void LogGetLinuxThemeFailed(Exception ex);
}

Review comment (suggestion):
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. 

Code after:

        ApplyDesiredColorTheme();
    }

    private AvailableApplicationTheme GetCurrentSystemTheme()
    {
        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,
                _ => AvailableApplicationTheme.Light
            };
        }
        catch (Exception ex)
        {
            LogGetLinuxThemeFailed(ex);
            return FallBack();
        }

        AvailableApplicationTheme FallBack()
        {
            return _gtkSettings.GtkThemeName?.Contains("Dark", StringComparison.OrdinalIgnoreCase) ?? false
                ? AvailableApplicationTheme.Dark
                : AvailableApplicationTheme.Light;
        }
    }

    [LoggerMessage(0, LogLevel.Error, "Failed to detect Linux theme.")]
    partial void LogGetLinuxThemeFailed(Exception ex);
}