From dff80c8ded5cd28bb6b0f3535aee27f6394c1f4e Mon Sep 17 00:00:00 2001 From: affederaffe <68356204+affederaffe@users.noreply.github.com> Date: Sun, 5 Feb 2023 12:55:46 +0100 Subject: [PATCH] Remove ListServicesAsync usage - Uses Reflection underneath - It's faster to just fail than receiving all available services first --- src/Avalonia.FreeDesktop/DBusMenuExporter.cs | 25 ++++++++++++-------- src/Avalonia.FreeDesktop/DBusSystemDialog.cs | 24 ++++++++++++------- src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs | 24 +++++++++++-------- src/tools/Tmds.DBus.SourceGenerator | 2 +- 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/Avalonia.FreeDesktop/DBusMenuExporter.cs b/src/Avalonia.FreeDesktop/DBusMenuExporter.cs index deac7346cb..d86a6bf3a4 100644 --- a/src/Avalonia.FreeDesktop/DBusMenuExporter.cs +++ b/src/Avalonia.FreeDesktop/DBusMenuExporter.cs @@ -98,16 +98,21 @@ namespace Avalonia.FreeDesktop Connection.AddMethodHandler(this); if (!_appMenu) return; - var services = await Connection.ListServicesAsync(); - if (!services.Contains("com.canonical.AppMenu.Registrar")) - return; + _registrar = new ComCanonicalAppMenuRegistrar(Connection, "com.canonical.AppMenu.Registrar", "/com/canonical/AppMenu/Registrar"); - if (!_disposed) - await _registrar.RegisterWindowAsync(_xid, Path); - // It's not really important if this code succeeds, - // and it's not important to know if it succeeds - // since even if we register the window it's not guaranteed that - // menu will be actually exported + try + { + if (!_disposed) + await _registrar.RegisterWindowAsync(_xid, Path); + } + catch + { + // It's not really important if this code succeeds, + // and it's not important to know if it succeeds + // since even if we register the window it's not guaranteed that + // menu will be actually exported + _registrar = null; + } } public void Dispose() @@ -286,7 +291,7 @@ namespace Avalonia.FreeDesktop return null; } - private Dictionary GetProperties((NativeMenuItemBase? item, NativeMenu? menu) i, string[] names) + private static Dictionary GetProperties((NativeMenuItemBase? item, NativeMenu? menu) i, string[] names) { if (names.Length == 0) names = s_allProperties; diff --git a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs index 1c965a077e..fa36f7d1ab 100644 --- a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs +++ b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs @@ -17,20 +17,28 @@ namespace Avalonia.FreeDesktop { if (DBusHelper.Connection is null) return null; - var services = await DBusHelper.Connection.ListServicesAsync(); - return services.Contains("org.freedesktop.portal.Desktop", StringComparer.Ordinal) - ? new DBusSystemDialog(DBusHelper.Connection, handle) - : null; + + var dbusFileChooser = new OrgFreedesktopPortalFileChooser(DBusHelper.Connection, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop"); + try + { + await dbusFileChooser.GetVersionAsync(); + } + catch + { + return null; + } + + return new DBusSystemDialog(DBusHelper.Connection, handle, dbusFileChooser); } private readonly Connection _connection; private readonly OrgFreedesktopPortalFileChooser _fileChooser; private readonly IPlatformHandle _handle; - private DBusSystemDialog(Connection connection, IPlatformHandle handle) + private DBusSystemDialog(Connection connection, IPlatformHandle handle, OrgFreedesktopPortalFileChooser fileChooser) { _connection = connection; - _fileChooser = new OrgFreedesktopPortalFileChooser(connection, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop"); + _fileChooser = fileChooser; _handle = handle; } @@ -137,7 +145,6 @@ namespace Avalonia.FreeDesktop if (fileTypes is null) return null; - var any = false; var filters = new DBusArrayItem(DBusType.Struct, new List()); foreach (var fileType in fileTypes) @@ -154,7 +161,6 @@ namespace Avalonia.FreeDesktop else continue; - any = true; filters.Add(new DBusStructItem( new DBusItem[] { @@ -163,7 +169,7 @@ namespace Avalonia.FreeDesktop })); } - return any ? new DBusVariantItem("a(sa(us))", filters) : null; + return filters.Count > 0 ? new DBusVariantItem("a(sa(us))", filters) : null; } } } diff --git a/src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs b/src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs index f732cfef07..521fe68e90 100644 --- a/src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs +++ b/src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics; -using System.Linq; using Avalonia.Controls.Platform; using Avalonia.Logging; using Avalonia.Platform; @@ -66,13 +65,18 @@ namespace Avalonia.FreeDesktop private async void WatchAsync() { - var services = await _connection!.ListServicesAsync(); - if (!services.Contains("org.kde.StatusNotifierWatcher", StringComparer.Ordinal)) - return; - - _serviceWatchDisposable = await _dBus!.WatchNameOwnerChangedAsync((_, x) => OnNameChange(x.Item2) ); - var nameOwner = await _dBus.GetNameOwnerAsync("org.kde.StatusNotifierWatcher"); - OnNameChange(nameOwner); + try + { + _serviceWatchDisposable = await _dBus!.WatchNameOwnerChangedAsync((_, x) => OnNameChange(x.Item2)); + var nameOwner = await _dBus.GetNameOwnerAsync("org.kde.StatusNotifierWatcher"); + OnNameChange(nameOwner); + } + catch + { + _serviceWatchDisposable = null; + Logger.TryGet(LogEventLevel.Error, "DBUS") + ?.Log(this, "Interface 'org.kde.StatusNotifierWatcher' is unavailable."); + } } private void OnNameChange(string? newOwner) @@ -99,7 +103,7 @@ namespace Avalonia.FreeDesktop private async void CreateTrayIcon() { - if (_connection is null || !_serviceConnected || _isDisposed) + if (_connection is null || !_serviceConnected || _isDisposed || _statusNotifierWatcher is null) return; #if NET5_0_OR_GREATER @@ -114,7 +118,7 @@ namespace Avalonia.FreeDesktop _connection.AddMethodHandler(_statusNotifierItemDbusObj); await _dBus!.RequestNameAsync(_sysTrayServiceName, 0); - await _statusNotifierWatcher!.RegisterStatusNotifierItemAsync(_sysTrayServiceName); + await _statusNotifierWatcher.RegisterStatusNotifierItemAsync(_sysTrayServiceName); _statusNotifierItemDbusObj.SetTitleAndTooltip(_tooltipText); _statusNotifierItemDbusObj.SetIcon(_icon); diff --git a/src/tools/Tmds.DBus.SourceGenerator b/src/tools/Tmds.DBus.SourceGenerator index 86e0ded07f..309159609e 160000 --- a/src/tools/Tmds.DBus.SourceGenerator +++ b/src/tools/Tmds.DBus.SourceGenerator @@ -1 +1 @@ -Subproject commit 86e0ded07fc8622e216f93f8fc01e8c1b3ef29b1 +Subproject commit 309159609e09f3f0ae77f0abd761360049761700