using ElectronNET.API.Entities;
using ElectronNET.API.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Versioning;
using System.Text.Json;
using System.Threading.Tasks;
using ElectronNET.API.Serialization;
// ReSharper disable InconsistentNaming
namespace ElectronNET.API
{
///
/// Add icons and context menus to the system's notification area.
///
public sealed class Tray : ApiBase
{
protected override SocketTaskEventNameTypes SocketTaskEventNameType => SocketTaskEventNameTypes.DashesLowerFirst;
protected override SocketEventNameTypes SocketEventNameType => SocketEventNameTypes.DashedLower;
///
/// Emitted when the tray icon is clicked.
///
public event Action OnClick
{
add
{
if (_click == null)
{
BridgeConnector.Socket.On("tray-click" + GetHashCode(), (result) =>
{
var array = result.EnumerateArray().ToArray();
var trayClickEventArgs = array[0].Deserialize(ElectronJsonContext.Default.TrayClickEventArgs);
var bounds = array[1].Deserialize(ElectronJsonContext.Default.Rectangle);
_click(trayClickEventArgs, bounds);
});
BridgeConnector.Socket.Emit("register-tray-click", GetHashCode());
}
_click += value;
}
remove
{
_click -= value;
if (_click == null)
{
BridgeConnector.Socket.Off("tray-click" + GetHashCode());
}
}
}
private event Action _click;
///
/// macOS, Windows: Emitted when the tray icon is right clicked.
///
[SupportedOSPlatform("macOS")]
[SupportedOSPlatform("Windows")]
public event Action OnRightClick
{
add
{
if (_rightClick == null)
{
BridgeConnector.Socket.On("tray-right-click" + GetHashCode(), (result) =>
{
var array = result.EnumerateArray().ToArray();
var trayClickEventArgs = array[0].Deserialize(ElectronJsonContext.Default.TrayClickEventArgs);
var bounds = array[1].Deserialize(ElectronJsonContext.Default.Rectangle);
_rightClick(trayClickEventArgs, bounds);
});
BridgeConnector.Socket.Emit("register-tray-right-click", GetHashCode());
}
_rightClick += value;
}
remove
{
_rightClick -= value;
if (_rightClick == null)
{
BridgeConnector.Socket.Off("tray-right-click" + GetHashCode());
}
}
}
private event Action _rightClick;
///
/// macOS, Windows: Emitted when the tray icon is double clicked.
///
[SupportedOSPlatform("macOS")]
[SupportedOSPlatform("Windows")]
public event Action OnDoubleClick
{
add
{
if (_doubleClick == null)
{
BridgeConnector.Socket.On("tray-double-click" + GetHashCode(), (result) =>
{
var array = result.EnumerateArray().ToArray();
var trayClickEventArgs = array[0].Deserialize(ElectronJsonContext.Default.TrayClickEventArgs);
var bounds = array[1].Deserialize(ElectronJsonContext.Default.Rectangle);
_doubleClick(trayClickEventArgs, bounds);
});
BridgeConnector.Socket.Emit("register-tray-double-click", GetHashCode());
}
_doubleClick += value;
}
remove
{
_doubleClick -= value;
if (_doubleClick == null)
{
BridgeConnector.Socket.Off("tray-double-click" + GetHashCode());
}
}
}
private event Action _doubleClick;
///
/// Windows: Emitted when the tray balloon shows.
///
[SupportedOSPlatform("Windows")]
public event Action OnBalloonShow
{
add => AddEvent(value, GetHashCode());
remove => RemoveEvent(value, GetHashCode());
}
///
/// Windows: Emitted when the tray balloon is clicked.
///
[SupportedOSPlatform("Windows")]
public event Action OnBalloonClick
{
add => AddEvent(value, GetHashCode());
remove => RemoveEvent(value, GetHashCode());
}
///
/// Windows: Emitted when the tray balloon is closed
/// because of timeout or user manually closes it.
///
[SupportedOSPlatform("Windows")]
public event Action OnBalloonClosed
{
add => AddEvent(value, GetHashCode());
remove => RemoveEvent(value, GetHashCode());
}
// TODO: Implement macOS Events
private static Tray _tray;
private static readonly object _syncRoot = new();
internal Tray()
{
}
internal static Tray Instance
{
get
{
if (_tray == null)
{
lock (_syncRoot)
{
if (_tray == null)
{
_tray = new Tray();
}
}
}
return _tray;
}
}
///
/// Gets the menu items.
///
///
/// The menu items.
///
public IReadOnlyCollection