Skip to content

Commit e941965

Browse files
DanDan
authored andcommitted
Support subscribing to unmapped events on App and Tray modules
`Electron.App.On("eventName", () => {});` or `Electron.App.On("eventName", obj => {});` fix #527
1 parent 6076c72 commit e941965

File tree

10 files changed

+308
-2
lines changed

10 files changed

+308
-2
lines changed

ElectronNET.API/App.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,5 +1608,35 @@ internal void PreventQuit()
16081608
}
16091609

16101610
private bool _preventQuit = false;
1611+
1612+
private const string ModuleName = "app";
1613+
/// <summary>
1614+
/// Subscribe to an unmapped event on the <see cref="App"/> module.
1615+
/// </summary>
1616+
/// <param name="eventName">The event name</param>
1617+
/// <param name="fn">The handler</param>
1618+
public void On(string eventName, Action fn)
1619+
=> Events.Instance.On(ModuleName, eventName, fn);
1620+
/// <summary>
1621+
/// Subscribe to an unmapped event on the <see cref="App"/> module.
1622+
/// </summary>
1623+
/// <param name="eventName">The event name</param>
1624+
/// <param name="fn">The handler</param>
1625+
public void On(string eventName, Action<object> fn)
1626+
=> Events.Instance.On(ModuleName, eventName, fn);
1627+
/// <summary>
1628+
/// Subscribe to an unmapped event on the <see cref="App"/> module once.
1629+
/// </summary>
1630+
/// <param name="eventName">The event name</param>
1631+
/// <param name="fn">The handler</param>
1632+
public void Once(string eventName, Action fn)
1633+
=> Events.Instance.Once(ModuleName, eventName, fn);
1634+
/// <summary>
1635+
/// Subscribe to an unmapped event on the <see cref="App"/> module once.
1636+
/// </summary>
1637+
/// <param name="eventName">The event name</param>
1638+
/// <param name="fn">The handler</param>
1639+
public void Once(string eventName, Action<object> fn)
1640+
=> Events.Instance.Once(ModuleName, eventName, fn);
16111641
}
16121642
}

ElectronNET.API/Events.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.Globalization;
3+
using Quobject.EngineIoClientDotNet.ComponentEmitter;
4+
5+
namespace ElectronNET.API
6+
{
7+
/// <summary>
8+
/// Generic Event Consumers for Electron Modules
9+
/// </summary>
10+
internal class Events
11+
{
12+
private static Events _events;
13+
private static object _syncRoot = new object();
14+
private TextInfo _ti = new CultureInfo("en-US", false).TextInfo;
15+
private Events()
16+
{
17+
18+
}
19+
20+
public static Events Instance
21+
{
22+
get
23+
{
24+
if (_events == null)
25+
{
26+
lock (_syncRoot)
27+
{
28+
if (_events == null)
29+
{
30+
_events = new Events();
31+
}
32+
}
33+
}
34+
35+
return _events;
36+
}
37+
}
38+
39+
/// <summary>
40+
/// Subscribe to an unmapped electron event.
41+
/// </summary>
42+
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
43+
/// <param name="eventName">The name of the event</param>
44+
/// <param name="fn">The event handler</param>
45+
public void On(string moduleName, string eventName, Action fn)
46+
=> On(moduleName, eventName, new ListenerImpl(fn));
47+
48+
/// <summary>
49+
/// Subscribe to an unmapped electron event.
50+
/// </summary>
51+
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
52+
/// <param name="eventName">The name of the event</param>
53+
/// <param name="fn">The event handler</param>
54+
public void On(string moduleName, string eventName, Action<object> fn)
55+
=> On(moduleName, eventName, new ListenerImpl(fn));
56+
57+
/// <summary>
58+
/// Subscribe to an unmapped electron event.
59+
/// </summary>
60+
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
61+
/// <param name="eventName">The name of the event</param>
62+
/// <param name="fn">The event handler</param>
63+
private void On(string moduleName, string eventName, IListener fn)
64+
{
65+
var listener = $"{moduleName}{_ti.ToTitleCase(eventName)}Completed";
66+
var subscriber = $"register-{moduleName}-on-event";
67+
68+
BridgeConnector.Socket.On(listener, fn);
69+
BridgeConnector.Socket.Emit(subscriber, eventName, listener);
70+
}
71+
72+
/// <summary>
73+
/// Subscribe to an unmapped electron event.
74+
/// </summary>
75+
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
76+
/// <param name="eventName">The name of the event</param>
77+
/// <param name="fn">The event handler</param>
78+
public void Once(string moduleName, string eventName, Action fn)
79+
=> Once(moduleName, eventName, new ListenerImpl(fn));
80+
81+
/// <summary>
82+
/// Subscribe to an unmapped electron event.
83+
/// </summary>
84+
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
85+
/// <param name="eventName">The name of the event</param>
86+
/// <param name="fn">The event handler</param>
87+
public void Once(string moduleName, string eventName, Action<object> fn)
88+
=> Once(moduleName, eventName, new ListenerImpl(fn));
89+
90+
/// <summary>
91+
/// Subscribe to an unmapped electron event.
92+
/// </summary>
93+
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
94+
/// <param name="eventName">The name of the event</param>
95+
/// <param name="fn">The event handler</param>
96+
private void Once(string moduleName, string eventName, IListener fn)
97+
{
98+
var listener = $"{moduleName}{_ti.ToTitleCase(eventName)}Completed";
99+
var subscriber = $"register-{moduleName}-once-event";
100+
BridgeConnector.Socket.Once(listener, fn);
101+
BridgeConnector.Socket.Emit(subscriber, eventName, listener);
102+
}
103+
104+
}
105+
}

ElectronNET.API/Tray.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,5 +351,35 @@ public Task<bool> IsDestroyedAsync()
351351
ContractResolver = new CamelCasePropertyNamesContractResolver(),
352352
NullValueHandling = NullValueHandling.Ignore
353353
};
354+
355+
private const string ModuleName = "tray";
356+
/// <summary>
357+
/// Subscribe to an unmapped event on the <see cref="Tray"/> module.
358+
/// </summary>
359+
/// <param name="eventName">The event name</param>
360+
/// <param name="fn">The handler</param>
361+
public void On(string eventName, Action fn)
362+
=> Events.Instance.On(ModuleName, eventName, fn);
363+
/// <summary>
364+
/// Subscribe to an unmapped event on the <see cref="Tray"/> module.
365+
/// </summary>
366+
/// <param name="eventName">The event name</param>
367+
/// <param name="fn">The handler</param>
368+
public void On(string eventName, Action<object> fn)
369+
=> Events.Instance.On(ModuleName, eventName, fn);
370+
/// <summary>
371+
/// Subscribe to an unmapped event on the <see cref="Tray"/> module once.
372+
/// </summary>
373+
/// <param name="eventName">The event name</param>
374+
/// <param name="fn">The handler</param>
375+
public void Once(string eventName, Action fn)
376+
=> Events.Instance.Once(ModuleName, eventName, fn);
377+
/// <summary>
378+
/// Subscribe to an unmapped event on the <see cref="Tray"/> module once.
379+
/// </summary>
380+
/// <param name="eventName">The event name</param>
381+
/// <param name="fn">The handler</param>
382+
public void Once(string eventName, Action<object> fn)
383+
=> Events.Instance.Once(ModuleName, eventName, fn);
354384
}
355385
}

ElectronNET.Host/api/app.js

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/app.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/app.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,24 @@ export = (socket: SocketIO.Socket, app: Electron.App) => {
287287
socket.on('appSetUserAgentFallback', (userAgent) => {
288288
app.userAgentFallback = userAgent;
289289
});
290+
291+
socket.on('register-app-on-event', (eventName, listenerName) => {
292+
app.on(eventName, (...args) => {
293+
if (args.length > 1) {
294+
electronSocket.emit(listenerName, args[1]);
295+
} else {
296+
electronSocket.emit(listenerName);
297+
}
298+
});
299+
});
300+
301+
socket.on('register-app-once-event', (eventName, listenerName) => {
302+
app.once(eventName, (...args) => {
303+
if (args.length > 1) {
304+
electronSocket.emit(listenerName, args[1]);
305+
} else {
306+
electronSocket.emit(listenerName);
307+
}
308+
});
309+
});
290310
};

ElectronNET.Host/api/tray.js

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/tray.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/tray.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ export = (socket: SocketIO.Socket) => {
111111
}
112112
});
113113

114+
socket.on('register-tray-on-event', (eventName, listenerName) => {
115+
if (tray.value){
116+
tray.value.on(eventName, (...args) => {
117+
if (args.length > 1) {
118+
electronSocket.emit(listenerName, args[1]);
119+
} else {
120+
electronSocket.emit(listenerName);
121+
}
122+
});
123+
}
124+
});
125+
126+
socket.on('register-tray-once-event', (eventName, listenerName) => {
127+
if (tray.value){
128+
tray.value.once(eventName, (...args) => {
129+
if (args.length > 1) {
130+
electronSocket.emit(listenerName, args[1]);
131+
} else {
132+
electronSocket.emit(listenerName);
133+
}
134+
});
135+
}
136+
});
137+
114138
function addMenuItemClickConnector(menuItems, callback) {
115139
menuItems.forEach((item) => {
116140
if (item.submenu && item.submenu.items.length > 0) {

0 commit comments

Comments
 (0)