Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented TrayIcon mouse right button event (#17383) #17406

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Avalonia.Base/Input/MouseEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Avalonia.Input;

public class MouseEventArgs : EventArgs
{
public Point Position { get; }
public MouseButton Button { get; }

public MouseEventArgs(Point position, MouseButton button)
{
Position = position;
Button = button;
}

public static MouseEventArgs Empty => new(new Point(), MouseButton.None);
}
7 changes: 3 additions & 4 deletions src/Avalonia.Controls/Platform/ITrayIconImpl.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Avalonia.Controls.Platform;
using Avalonia.Input;
using Avalonia.Metadata;

namespace Avalonia.Platform
Expand Down Expand Up @@ -27,10 +28,8 @@ public interface ITrayIconImpl : IDisposable
/// </summary>
INativeMenuExporter? MenuExporter { get; }

/// <summary>
/// Gets or Sets the Action that is called when the TrayIcon is clicked.
/// </summary>
Action? OnClicked { get; set; }
event EventHandler<MouseEventArgs>? MouseLeftButtonDown;
event EventHandler<MouseEventArgs>? MouseRightButtonDown;
}

[Unstable]
Expand Down
14 changes: 11 additions & 3 deletions src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Avalonia.Controls.Platform;
using Avalonia.Input;
using Avalonia.Logging;
using Avalonia.Platform;
using Avalonia.Threading;
Expand Down Expand Up @@ -32,7 +33,8 @@ internal class DBusTrayIconImpl : ITrayIconImpl

public bool IsActive { get; private set; }
public INativeMenuExporter? MenuExporter { get; }
public Action? OnClicked { get; set; }
public event EventHandler<MouseEventArgs>? MouseLeftButtonDown;
public event EventHandler<MouseEventArgs>? MouseRightButtonDown;
public Func<IWindowIconImpl?, uint[]>? IconConverterDelegate { get; set; }

public DBusTrayIconImpl()
Expand Down Expand Up @@ -118,7 +120,8 @@ private async void CreateTrayIcon()

_statusNotifierItemDbusObj!.SetTitleAndTooltip(_tooltipText);
_statusNotifierItemDbusObj.SetIcon(_icon);
_statusNotifierItemDbusObj.ActivationDelegate += OnClicked;
_statusNotifierItemDbusObj.ActivationDelegate += () => MouseLeftButtonDown?.Invoke(null, new MouseEventArgs(new Point(), MouseButton.Left));
_statusNotifierItemDbusObj.ContextMenuDelegate += () => MouseRightButtonDown?.Invoke(null, new MouseEventArgs(new Point(), MouseButton.Right));
}

private void DestroyTrayIcon()
Expand Down Expand Up @@ -224,8 +227,13 @@ public StatusNotifierItemDbusObj(Connection connection, ObjectPath dbusMenuPath)
public override Connection Connection { get; }

public event Action? ActivationDelegate;
public event Action? ContextMenuDelegate;

protected override ValueTask OnContextMenuAsync(int x, int y) => new();
protected override ValueTask OnContextMenuAsync(int x, int y)
{
ContextMenuDelegate?.Invoke();
return new ValueTask();
}

protected override ValueTask OnActivateAsync(int x, int y)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Avalonia.Native/TrayIconImpl.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using Avalonia.Controls.Platform;
using Avalonia.Input;
using Avalonia.Native.Interop;
using Avalonia.Platform;

Expand All @@ -19,8 +20,6 @@ public TrayIconImpl(IAvaloniaNativeFactory factory)
MenuExporter = new AvaloniaNativeMenuExporter(_native, factory);
}

public Action? OnClicked { get; set; }

public void Dispose()
{
_native.Dispose();
Expand Down Expand Up @@ -64,5 +63,7 @@ public void SetIsTemplateIcon(bool isTemplateIcon)
}

public INativeMenuExporter? MenuExporter { get; }
public event EventHandler<MouseEventArgs>? MouseLeftButtonDown;
public event EventHandler<MouseEventArgs>? MouseRightButtonDown;
}
}
4 changes: 3 additions & 1 deletion src/Avalonia.X11/XEmbedTrayIconImpl.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Avalonia.Controls.Platform;
using Avalonia.Input;
using Avalonia.Logging;
using Avalonia.Platform;

Expand Down Expand Up @@ -42,6 +43,7 @@ public void SetIsVisible(bool visible)
}

public INativeMenuExporter MenuExporter { get; }
public Action OnClicked { get; set; }
public event EventHandler<MouseEventArgs>? MouseLeftButtonDown;
public event EventHandler<MouseEventArgs>? MouseRightButtonDown;
}
}
16 changes: 13 additions & 3 deletions src/Windows/Avalonia.Win32/TrayIconImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Avalonia.Controls;
using Avalonia.Controls.Platform;
using Avalonia.Controls.Primitives.PopupPositioning;
using Avalonia.Input;
using Avalonia.LogicalTree;
using Avalonia.Media.Imaging;
using Avalonia.Metadata;
Expand Down Expand Up @@ -48,8 +49,9 @@ public TrayIconImpl()

s_trayIcons.Add(_uniqueId, this);
}

public Action? OnClicked { get; set; }

public event EventHandler<MouseEventArgs>? MouseLeftButtonDown;
public event EventHandler<MouseEventArgs>? MouseRightButtonDown;

public INativeMenuExporter MenuExporter => _exporter;

Expand Down Expand Up @@ -192,14 +194,22 @@ private IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
if (msg == (uint)CustomWindowsMessage.WM_TRAYMOUSE)
{
if (!GetCursorPos(out var cursorPoint))
{
return IntPtr.Zero;
}

var position = new Point(cursorPoint.X, cursorPoint.Y);

// Determine the type of message and call the matching event handlers
switch (lParam.ToInt32())
{
case (int)WindowsMessage.WM_LBUTTONUP:
OnClicked?.Invoke();
MouseLeftButtonDown?.Invoke(null, new MouseEventArgs(position, MouseButton.Left));
break;

case (int)WindowsMessage.WM_RBUTTONUP:
MouseRightButtonDown?.Invoke(null, new MouseEventArgs(position, MouseButton.Right));
OnRightClicked();
break;
}
Expand Down
Loading