Browse Source

add command to trayicon.

pull/6779/head
Dan Walmsley 5 years ago
parent
commit
dff7efbec8
  1. 3
      src/Avalonia.Controls/NativeMenuItem.cs
  2. 49
      src/Avalonia.Controls/TrayIcon.cs

3
src/Avalonia.Controls/NativeMenuItem.cs

@ -16,6 +16,7 @@ namespace Avalonia.Controls
private bool _isChecked = false; private bool _isChecked = false;
private NativeMenuItemToggleType _toggleType; private NativeMenuItemToggleType _toggleType;
private IBitmap _icon; private IBitmap _icon;
private readonly CanExecuteChangedSubscriber _canExecuteChangedSubscriber;
private NativeMenu _menu; private NativeMenu _menu;
@ -47,8 +48,6 @@ namespace Avalonia.Controls
} }
} }
private readonly CanExecuteChangedSubscriber _canExecuteChangedSubscriber;
public NativeMenuItem() public NativeMenuItem()
{ {

49
src/Avalonia.Controls/TrayIcon.cs

@ -1,10 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Windows.Input;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Platform; using Avalonia.Controls.Platform;
using Avalonia.Platform; using Avalonia.Platform;
using Avalonia.Utilities;
#nullable enable #nullable enable
@ -13,10 +15,13 @@ namespace Avalonia.Controls
public sealed class TrayIcons : AvaloniaList<TrayIcon> public sealed class TrayIcons : AvaloniaList<TrayIcon>
{ {
} }
public class TrayIcon : AvaloniaObject, INativeMenuExporterProvider, IDisposable public class TrayIcon : AvaloniaObject, INativeMenuExporterProvider, IDisposable
{ {
private readonly ITrayIconImpl? _impl; private readonly ITrayIconImpl? _impl;
private ICommand? _command;
private TrayIcon(ITrayIconImpl? impl) private TrayIcon(ITrayIconImpl? impl)
{ {
@ -26,7 +31,15 @@ namespace Avalonia.Controls
_impl.SetIsVisible(IsVisible); _impl.SetIsVisible(IsVisible);
_impl.OnClicked = () => Clicked?.Invoke(this, EventArgs.Empty); _impl.OnClicked = () =>
{
Clicked?.Invoke(this, EventArgs.Empty);
if (Command?.CanExecute(CommandParameter) == true)
{
Command.Execute(CommandParameter);
}
};
} }
} }
@ -64,6 +77,21 @@ namespace Avalonia.Controls
/// on OSX this event is not raised. /// on OSX this event is not raised.
/// </summary> /// </summary>
public event EventHandler? Clicked; public event EventHandler? Clicked;
/// <summary>
/// Defines the <see cref="Command"/> property.
/// </summary>
public static readonly DirectProperty<TrayIcon, ICommand?> CommandProperty =
Button.CommandProperty.AddOwner<TrayIcon>(
trayIcon => trayIcon.Command,
(trayIcon, command) => trayIcon.Command = command,
enableDataValidation: true);
/// <summary>
/// Defines the <see cref="CommandParameter"/> property.
/// </summary>
public static readonly StyledProperty<object?> CommandParameterProperty =
Button.CommandParameterProperty.AddOwner<MenuItem>();
/// <summary> /// <summary>
/// Defines the <see cref="TrayIcons"/> attached property. /// Defines the <see cref="TrayIcons"/> attached property.
@ -98,6 +126,25 @@ namespace Avalonia.Controls
public static void SetIcons(AvaloniaObject o, TrayIcons trayIcons) => o.SetValue(IconsProperty, trayIcons); public static void SetIcons(AvaloniaObject o, TrayIcons trayIcons) => o.SetValue(IconsProperty, trayIcons);
public static TrayIcons GetIcons(AvaloniaObject o) => o.GetValue(IconsProperty); public static TrayIcons GetIcons(AvaloniaObject o) => o.GetValue(IconsProperty);
/// <summary>
/// Gets or sets the <see cref="Command"/> property of a TrayIcon.
/// </summary>
public ICommand? Command
{
get => _command;
set => SetAndRaise(CommandProperty, ref _command, value);
}
/// <summary>
/// Gets or sets the parameter to pass to the <see cref="Command"/> property of a
/// <see cref="TrayIcon"/>.
/// </summary>
public object CommandParameter
{
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
/// <summary> /// <summary>
/// Gets or sets the Menu of the TrayIcon. /// Gets or sets the Menu of the TrayIcon.

Loading…
Cancel
Save