Browse Source

implement visible changed and tooltip.

pull/6610/head
Dan Walmsley 5 years ago
parent
commit
30f6145d73
  1. 11
      samples/ControlCatalog/App.xaml
  2. 10
      samples/ControlCatalog/App.xaml.cs
  3. 5
      src/Avalonia.Controls/Platform/ITrayIconImpl.cs
  4. 55
      src/Avalonia.Controls/TrayIcon.cs
  5. 21
      src/Windows/Avalonia.Win32/TrayIconImpl.cs

11
samples/ControlCatalog/App.xaml

@ -1,6 +1,17 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ControlCatalog.App">
<TrayIcon.TrayIcons>
<TrayIcons>
<TrayIcon Icon="/Assets/test_icon.ico" ToolTipText="Avalonia Tray Icon ToolTip">
<NativeMenu.Menu>
<NativeMenu>
<NativeMenuItem Header="About" />
</NativeMenu>
</NativeMenu.Menu>
</TrayIcon>
</TrayIcons>
</TrayIcon.TrayIcons>
<Application.Styles>
<Style Selector="TextBlock.h1">
<Setter Property="FontSize" Value="16" />

10
samples/ControlCatalog/App.xaml.cs

@ -93,8 +93,6 @@ namespace ControlCatalog
Styles.Insert(0, FluentLight);
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
@ -102,14 +100,6 @@ namespace ControlCatalog
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
{
desktopLifetime.MainWindow = new MainWindow();
var trayIcon1 = new TrayIcon();
trayIcon1.Icon = desktopLifetime.MainWindow.Icon;
var trayIcon2 = new TrayIcon();
trayIcon2.Icon = desktopLifetime.MainWindow.Icon;
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
singleViewLifetime.MainView = new MainView();

5
src/Avalonia.Controls/Platform/ITrayIconImpl.cs

@ -1,4 +1,7 @@
using System;
using Avalonia.Controls;
#nullable enable
namespace Avalonia.Platform
{
@ -7,7 +10,7 @@ namespace Avalonia.Platform
/// <summary>
/// Sets the icon of this tray icon.
/// </summary>
void SetIcon(IWindowIconImpl icon);
void SetIcon(IWindowIconImpl? icon);
/// <summary>
/// Sets the icon of this tray icon.

55
src/Avalonia.Controls/TrayIcon.cs

@ -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?>());
}
}
}
}

21
src/Windows/Avalonia.Win32/TrayIconImpl.cs

@ -10,6 +10,8 @@ using Avalonia.Threading;
using Avalonia.Win32.Interop;
using static Avalonia.Win32.Interop.UnmanagedMethods;
#nullable enable
namespace Avalonia.Win32
{
public class TrayIconImpl : ITrayIconImpl
@ -17,7 +19,8 @@ namespace Avalonia.Win32
private readonly int _uniqueId = 0;
private static int _nextUniqueId = 0;
private bool _iconAdded;
private IconImpl _icon;
private IconImpl? _icon;
private string? _tooltipText;
private static Dictionary<int, TrayIconImpl> s_trayIcons = new Dictionary<int, TrayIconImpl>();
@ -34,8 +37,6 @@ namespace Avalonia.Win32
_uniqueId = ++_nextUniqueId;
s_trayIcons.Add(_uniqueId, this);
UpdateIcon();
}
@ -44,7 +45,7 @@ namespace Avalonia.Win32
UpdateIcon(false);
}
public void SetIcon(IWindowIconImpl icon)
public void SetIcon(IWindowIconImpl? icon)
{
_icon = icon as IconImpl;
UpdateIcon();
@ -52,15 +53,13 @@ namespace Avalonia.Win32
public void SetIsVisible(bool visible)
{
if (visible)
{
}
UpdateIcon(!visible);
}
public void SetToolTipText(string text)
public void SetToolTipText(string? text)
{
throw new NotImplementedException();
_tooltipText = text;
UpdateIcon(!_iconAdded);
}
@ -73,7 +72,7 @@ namespace Avalonia.Win32
uFlags = NIF.TIP | NIF.MESSAGE,
uCallbackMessage = (int)CustomWindowsMessage.WM_TRAYMOUSE,
hIcon = _icon?.HIcon ?? new IconImpl(new System.Drawing.Bitmap(32, 32)).HIcon,
szTip = "Tool tip text here."
szTip = _tooltipText ?? ""
};
if (!remove)

Loading…
Cancel
Save