diff --git a/src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs b/src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs index 1ea5d720ce..9095773d90 100644 --- a/src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs +++ b/src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs @@ -7,6 +7,7 @@ using System.Runtime.CompilerServices; using System.Threading.Tasks; using Avalonia.Controls.Platform; using Avalonia.Logging; +using Avalonia.Platform; using Tmds.DBus; [assembly: InternalsVisibleTo(Connection.DynamicAssemblyName)] @@ -19,7 +20,7 @@ using Tmds.DBus; namespace Avalonia.FreeDesktop { - internal class DBusTrayIconImpl + internal class DBusTrayIconImpl : ITrayIconImpl { private static int s_trayIconInstanceId; @@ -40,7 +41,8 @@ namespace Avalonia.FreeDesktop public bool IsActive { get; private set; } public INativeMenuExporter? MenuExporter { get; } public Action? OnClicked { get; set; } - + public Func IconConverterDelegate { get; set; } + public DBusTrayIconImpl() { _connection = DBusHelper.TryCreateNewConnection(); @@ -158,10 +160,15 @@ namespace Avalonia.FreeDesktop _serviceWatchDisposable?.Dispose(); } - public void SetIcon(UIntPtr[] x11iconData) + public void SetIcon(IWindowIconImpl icon) { if (_isDisposed) return; + + var x11iconData = IconConverterDelegate(icon); + + if(x11iconData.Length == 0) return; + var w = (int)x11iconData[0]; var h = (int)x11iconData[1]; @@ -171,7 +178,7 @@ namespace Avalonia.FreeDesktop for (var i = 0; i < pixLength; i++) { - var rawPixel = x11iconData[i + 2].ToUInt32(); + var rawPixel = x11iconData[i + 2]; pixByteArray[pixByteArrayCounter++] = (byte)((rawPixel & 0xFF000000) >> 24); pixByteArray[pixByteArrayCounter++] = (byte)((rawPixel & 0xFF0000) >> 16); pixByteArray[pixByteArrayCounter++] = (byte)((rawPixel & 0xFF00) >> 8); @@ -360,7 +367,7 @@ namespace Avalonia.FreeDesktop Task WatchNewOverlayIconAsync(Action handler, Action onError); Task WatchNewToolTipAsync(Action handler, Action onError); Task WatchNewStatusAsync(Action handler, Action onError); - Task GetAsync(string prop); + Task GetAsync(string prop); Task GetAllAsync(); Task SetAsync(string prop, object val); Task WatchPropertiesAsync(Action handler); diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index d3aeefd088..452a72bc83 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using Avalonia.Controls; @@ -106,7 +107,19 @@ namespace Avalonia.X11 public ITrayIconImpl CreateTrayIcon () { - return new X11TrayIconImpl(); + var dbusTrayIcon = new DBusTrayIconImpl(); + + if (!dbusTrayIcon.IsActive) return new XEmbedTrayIconImpl(); + + dbusTrayIcon.IconConverterDelegate = (icon) => + { + if (!(icon is X11IconData x11icon)) + return Array.Empty(); + + return x11icon.Data.Select(x=>x.ToUInt32()).ToArray(); + }; + + return dbusTrayIcon; } public IWindowImpl CreateWindow() diff --git a/src/Avalonia.X11/X11TrayIconImpl.cs b/src/Avalonia.X11/X11TrayIconImpl.cs deleted file mode 100644 index 93bf71b409..0000000000 --- a/src/Avalonia.X11/X11TrayIconImpl.cs +++ /dev/null @@ -1,114 +0,0 @@ -using System; -using Avalonia.Controls.Platform; -using Avalonia.FreeDesktop; -using Avalonia.Logging; -using Avalonia.Platform; - -namespace Avalonia.X11 -{ - internal class X11TrayIconImpl : ITrayIconImpl - { - public X11TrayIconImpl() - { - _xEmbedTrayIcon = new XEmbedTrayIconImpl(); - _dBusTrayIcon = new DBusTrayIconImpl(); - } - - private readonly DBusTrayIconImpl _dBusTrayIcon; - private readonly XEmbedTrayIconImpl _xEmbedTrayIcon; - private bool _isDisposed; - - public void Dispose() - { - _dBusTrayIcon?.Dispose(); - _xEmbedTrayIcon?.Dispose(); - _isDisposed = true; - } - - public void SetIcon(IWindowIconImpl? icon) - { - if (_isDisposed) return; - - if (_dBusTrayIcon?.IsActive ?? false) - { - if (!(icon is X11IconData x11icon)) - return; - - _dBusTrayIcon.SetIcon(x11icon.Data); - } - else - { - _xEmbedTrayIcon.SetIcon(icon); - } - } - - public void SetToolTipText(string? text) - { - if (_isDisposed) return; - - if (_dBusTrayIcon?.IsActive ?? false) - { - _dBusTrayIcon.SetToolTipText(text); - } - else - { - _xEmbedTrayIcon.SetToolTipText(text); - } - } - - public void SetIsVisible(bool visible) - { - if (_isDisposed) return; - - if (_dBusTrayIcon?.IsActive ?? false) - { - _dBusTrayIcon.SetIsVisible(visible); - } - else - { - _xEmbedTrayIcon.SetIsVisible(visible); - } - } - - public INativeMenuExporter? MenuExporter - { - get - { - if (_dBusTrayIcon?.IsActive ?? false) - { - return _dBusTrayIcon.MenuExporter; - } - else - { - return _xEmbedTrayIcon.MenuExporter; - } - } - } - - public Action? OnClicked - { - get - { - if (_dBusTrayIcon?.IsActive ?? false) - { - return _dBusTrayIcon.OnClicked; - } - else - { - return _xEmbedTrayIcon.OnClicked; - } - } - set - { - if (_dBusTrayIcon?.IsActive ?? false) - { - _dBusTrayIcon.OnClicked = value; - } - else - { - _xEmbedTrayIcon.OnClicked = value; - } - } - } - } -} diff --git a/src/Avalonia.X11/XEmbedTrayIconImpl.cs b/src/Avalonia.X11/XEmbedTrayIconImpl.cs index 3355182a60..8b9e882bba 100644 --- a/src/Avalonia.X11/XEmbedTrayIconImpl.cs +++ b/src/Avalonia.X11/XEmbedTrayIconImpl.cs @@ -5,10 +5,11 @@ using Avalonia.Platform; namespace Avalonia.X11 { - internal class XEmbedTrayIconImpl + internal class XEmbedTrayIconImpl : ITrayIconImpl { public XEmbedTrayIconImpl() { + MenuExporter = null; } private bool _isCalled; @@ -29,12 +30,12 @@ namespace Avalonia.X11 NotImplemented(); } - public void SetIcon(IWindowIconImpl? icon) + public void SetIcon(IWindowIconImpl icon) { NotImplemented(); } - public void SetToolTipText(string? text) + public void SetToolTipText(string text) { NotImplemented(); } @@ -44,7 +45,7 @@ namespace Avalonia.X11 NotImplemented(); } - public INativeMenuExporter? MenuExporter { get; } - public Action? OnClicked { get; set; } + public INativeMenuExporter MenuExporter { get; } + public Action OnClicked { get; set; } } }