forked from ElectronNET/Electron.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotification.cs
More file actions
executable file
·139 lines (116 loc) · 4.69 KB
/
Notification.cs
File metadata and controls
executable file
·139 lines (116 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ElectronNET.API.Interfaces;
namespace ElectronNET.API
{
/// <summary>
/// Create OS desktop notifications
/// </summary>
public sealed class Notification : INotification
{
private static Notification _notification;
private static object _syncRoot = new object();
internal Notification() { }
internal static Notification Instance
{
get
{
if (_notification == null)
{
lock (_syncRoot)
{
if (_notification == null)
{
_notification = new Notification();
}
}
}
return _notification;
}
}
private static List<NotificationOptions> _notificationOptions = new List<NotificationOptions>();
/// <summary>
/// Create OS desktop notifications
/// </summary>
/// <param name="notificationOptions"></param>
public void Show(NotificationOptions notificationOptions)
{
GenerateIDsForDefinedActions(notificationOptions);
BridgeConnector.Emit("createNotification", notificationOptions);
}
private static void GenerateIDsForDefinedActions(NotificationOptions notificationOptions)
{
bool isActionDefined = false;
if (notificationOptions.OnShow != null)
{
notificationOptions.ShowID = Guid.NewGuid().ToString();
isActionDefined = true;
BridgeConnector.Off("NotificationEventShow");
BridgeConnector.On<string>("NotificationEventShow", (id) => {
_notificationOptions.Single(x => x.ShowID == id).OnShow();
});
}
if (notificationOptions.OnClick != null)
{
notificationOptions.ClickID = Guid.NewGuid().ToString();
isActionDefined = true;
BridgeConnector.Off("NotificationEventClick");
BridgeConnector.On<string>("NotificationEventClick", (id) => {
_notificationOptions.Single(x => x.ClickID == id).OnClick();
});
}
if (notificationOptions.OnClose != null)
{
notificationOptions.CloseID = Guid.NewGuid().ToString();
isActionDefined = true;
BridgeConnector.Off("NotificationEventClose");
BridgeConnector.On<string>("NotificationEventClose", (id) => {
_notificationOptions.Single(x => x.CloseID == id.ToString()).OnClose();
});
}
if (notificationOptions.OnReply != null)
{
notificationOptions.ReplyID = Guid.NewGuid().ToString();
isActionDefined = true;
BridgeConnector.Off("NotificationEventReply");
BridgeConnector.On<string[]>("NotificationEventReply", (args) => {
_notificationOptions.Single(x => x.ReplyID == args[0].ToString()).OnReply(args[1].ToString());
});
}
if (notificationOptions.OnAction != null)
{
notificationOptions.ActionID = Guid.NewGuid().ToString();
isActionDefined = true;
BridgeConnector.Off("NotificationEventAction");
BridgeConnector.On<string[]>("NotificationEventAction", (args) => {
_notificationOptions.Single(x => x.ReplyID == args[0].ToString()).OnAction(args[1].ToString());
});
}
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>(TaskCreationOptions.RunContinuationsAsynchronously);
BridgeConnector.On<bool>("notificationIsSupportedComplete", (isSupported) =>
{
BridgeConnector.Off("notificationIsSupportedComplete");
taskCompletionSource.SetResult(isSupported);
});
BridgeConnector.Emit("notificationIsSupported");
return taskCompletionSource.Task;
}
}
}