Files
Electron.NET/src/ElectronNET.API/API/Notification.cs

139 lines
4.8 KiB
C#
Raw Normal View History

using ElectronNET.API.Entities;
2017-10-18 03:49:34 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
2017-10-18 03:49:34 +02:00
using System.Threading.Tasks;
namespace ElectronNET.API
{
/// <summary>
/// Create OS desktop notifications
/// </summary>
public sealed class Notification
{
private static Notification _notification;
private static object _syncRoot = new object();
2025-11-09 03:50:24 +01:00
internal Notification()
{
}
internal static Notification Instance
{
get
{
if (_notification == null)
{
2017-11-04 00:16:14 +01:00
lock (_syncRoot)
{
if (_notification == null)
{
_notification = new Notification();
}
}
}
return _notification;
}
}
2017-10-18 03:49:34 +02:00
private static List<NotificationOptions> _notificationOptions = new List<NotificationOptions>();
/// <summary>
/// Create OS desktop notifications
/// </summary>
/// <param name="notificationOptions"></param>
public void Show(NotificationOptions notificationOptions)
{
2017-10-18 03:49:34 +02:00
GenerateIDsForDefinedActions(notificationOptions);
BridgeConnector.Socket.Emit("createNotification", notificationOptions);
}
2017-10-18 03:49:34 +02:00
private static void GenerateIDsForDefinedActions(NotificationOptions notificationOptions)
{
bool isActionDefined = false;
if (notificationOptions.OnShow != null)
{
notificationOptions.ShowID = Guid.NewGuid().ToString();
isActionDefined = true;
BridgeConnector.Socket.Off("NotificationEventShow");
BridgeConnector.Socket.On<JsonElement>("NotificationEventShow", (id) => { _notificationOptions.Single(x => x.ShowID == id.GetString()).OnShow(); });
2017-10-18 03:49:34 +02:00
}
if (notificationOptions.OnClick != null)
{
notificationOptions.ClickID = Guid.NewGuid().ToString();
isActionDefined = true;
BridgeConnector.Socket.Off("NotificationEventClick");
BridgeConnector.Socket.On<JsonElement>("NotificationEventClick", (id) => { _notificationOptions.Single(x => x.ClickID == id.GetString()).OnClick(); });
2017-10-18 03:49:34 +02:00
}
if (notificationOptions.OnClose != null)
{
notificationOptions.CloseID = Guid.NewGuid().ToString();
isActionDefined = true;
BridgeConnector.Socket.Off("NotificationEventClose");
BridgeConnector.Socket.On<JsonElement>("NotificationEventClose", (id) => { _notificationOptions.Single(x => x.CloseID == id.GetString()).OnClose(); });
2017-10-18 03:49:34 +02:00
}
if (notificationOptions.OnReply != null)
{
notificationOptions.ReplyID = Guid.NewGuid().ToString();
isActionDefined = true;
BridgeConnector.Socket.Off("NotificationEventReply");
BridgeConnector.Socket.On<JsonElement>("NotificationEventReply", (args) =>
2025-11-09 03:50:24 +01:00
{
var arguments = args.Deserialize<string[]>(Serialization.ElectronJson.Options);
_notificationOptions.Single(x => x.ReplyID == arguments[0]).OnReply(arguments[1]);
2017-10-18 03:49:34 +02:00
});
}
if (notificationOptions.OnAction != null)
{
notificationOptions.ActionID = Guid.NewGuid().ToString();
isActionDefined = true;
BridgeConnector.Socket.Off("NotificationEventAction");
BridgeConnector.Socket.On<JsonElement>("NotificationEventAction", (args) =>
2025-11-09 03:50:24 +01:00
{
var arguments = JsonSerializer.Deserialize<string[]>(args, Serialization.ElectronJson.Options);
_notificationOptions.Single(x => x.ReplyID == arguments[0]).OnAction(arguments[1]);
2017-10-18 03:49:34 +02:00
});
}
if (isActionDefined)
{
_notificationOptions.Add(notificationOptions);
}
}
/// <summary>
/// Whether or not desktop notifications are supported on the current system.
/// </summary>
/// <returns></returns>
public Task<bool> IsSupportedAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
BridgeConnector.Socket.On<JsonElement>("notificationIsSupportedComplete", (isSupported) =>
2017-10-18 03:49:34 +02:00
{
BridgeConnector.Socket.Off("notificationIsSupportedComplete");
taskCompletionSource.SetResult(isSupported.GetBoolean());
2017-10-18 03:49:34 +02:00
});
BridgeConnector.Socket.Emit("notificationIsSupported");
return taskCompletionSource.Task;
}
}
}