|
|
|
@ -1,4 +1,6 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using Avalonia.Collections; |
|
|
|
using Avalonia.Controls.Platform; |
|
|
|
using Avalonia.Platform; |
|
|
|
|
|
|
|
@ -6,6 +8,10 @@ using Avalonia.Platform; |
|
|
|
|
|
|
|
namespace Avalonia.Controls |
|
|
|
{ |
|
|
|
public sealed class TrayIcons : AvaloniaList<TrayIcon> |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
public class TrayIcon : AvaloniaObject, IDataContextProvider |
|
|
|
{ |
|
|
|
private readonly ITrayIconImpl _impl; |
|
|
|
@ -13,13 +19,50 @@ namespace Avalonia.Controls |
|
|
|
private TrayIcon(ITrayIconImpl impl) |
|
|
|
{ |
|
|
|
_impl = impl; |
|
|
|
|
|
|
|
_impl.SetIsVisible(IsVisible); |
|
|
|
} |
|
|
|
|
|
|
|
public TrayIcon () : this(PlatformManager.CreateTrayIcon()) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static TrayIcon () |
|
|
|
{ |
|
|
|
TrayIconsProperty.Changed.Subscribe(args => |
|
|
|
{ |
|
|
|
if (args.Sender is Application application) |
|
|
|
{ |
|
|
|
if(args.OldValue.Value != null) |
|
|
|
{ |
|
|
|
RemoveIcons(args.OldValue.Value); |
|
|
|
} |
|
|
|
|
|
|
|
if(args.NewValue.Value != null) |
|
|
|
{ |
|
|
|
args.NewValue.Value.CollectionChanged += Icons_CollectionChanged; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private static void Icons_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private static void RemoveIcons (IEnumerable<TrayIcon> icons) |
|
|
|
{ |
|
|
|
foreach(var icon in icons) |
|
|
|
{ |
|
|
|
icon.Remove(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static readonly AttachedProperty<TrayIcons> TrayIconsProperty |
|
|
|
= AvaloniaProperty.RegisterAttached<TrayIcon, Application, TrayIcons>("TrayIcons"); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Defines the <see cref="DataContext"/> property.
|
|
|
|
/// </summary>
|
|
|
|
@ -42,6 +85,10 @@ namespace Avalonia.Controls |
|
|
|
public static readonly StyledProperty<bool> IsVisibleProperty = |
|
|
|
Visual.IsVisibleProperty.AddOwner<TrayIcon>(); |
|
|
|
|
|
|
|
public static void SetTrayIcons(AvaloniaObject o, TrayIcons trayIcons) => o.SetValue(TrayIconsProperty, trayIcons); |
|
|
|
|
|
|
|
public static TrayIcons GetTrayIcons(AvaloniaObject o) => o.GetValue(TrayIconsProperty); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Removes the notify icon from the taskbar notification area.
|
|
|
|
/// </summary>
|
|
|
|
@ -102,6 +149,14 @@ namespace Avalonia.Controls |
|
|
|
{ |
|
|
|
_impl.SetIcon(Icon.PlatformImpl); |
|
|
|
} |
|
|
|
else if (change.Property == IsVisibleProperty) |
|
|
|
{ |
|
|
|
_impl.SetIsVisible(change.NewValue.GetValueOrDefault<bool>()); |
|
|
|
} |
|
|
|
else if (change.Property == ToolTipTextProperty) |
|
|
|
{ |
|
|
|
_impl.SetToolTipText(change.NewValue.GetValueOrDefault<string?>()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|