Browse Source

Remove ListServicesAsync usage

- Uses Reflection underneath
- It's faster to just fail than receiving all available services first
pull/9810/head
affederaffe 3 years ago
parent
commit
dff80c8ded
  1. 25
      src/Avalonia.FreeDesktop/DBusMenuExporter.cs
  2. 24
      src/Avalonia.FreeDesktop/DBusSystemDialog.cs
  3. 24
      src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs
  4. 2
      src/tools/Tmds.DBus.SourceGenerator

25
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<string, DBusVariantItem> GetProperties((NativeMenuItemBase? item, NativeMenu? menu) i, string[] names)
private static Dictionary<string, DBusVariantItem> GetProperties((NativeMenuItemBase? item, NativeMenu? menu) i, string[] names)
{
if (names.Length == 0)
names = s_allProperties;

24
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<DBusItem>());
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;
}
}
}

24
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);

2
src/tools/Tmds.DBus.SourceGenerator

@ -1 +1 @@
Subproject commit 86e0ded07fc8622e216f93f8fc01e8c1b3ef29b1
Subproject commit 309159609e09f3f0ae77f0abd761360049761700
Loading…
Cancel
Save