Skip to content

Commit cc0b15c

Browse files
Merge pull request #528 from stackify/issue/527
Issue/527
2 parents 41fa8e9 + 8a0276d commit cc0b15c

File tree

20 files changed

+480
-33
lines changed

20 files changed

+480
-33
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,6 @@ paket-files/
260260
# Python Tools for Visual Studio (PTVS)
261261
__pycache__/
262262
*.pyc
263+
264+
# Mac Only settings file
265+
.DS_Store

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/Dock.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
using System.Threading;
1+
using System.Collections.Generic;
2+
using System.Threading;
23
using System.Threading.Tasks;
34
using ElectronNET.API.Entities;
45
using ElectronNET.API.Extensions;
6+
using Newtonsoft.Json;
57
using Newtonsoft.Json.Linq;
8+
using Newtonsoft.Json.Serialization;
69

710
namespace ElectronNET.API
811
{
@@ -162,12 +165,29 @@ public async Task<bool> IsVisibleAsync(CancellationToken cancellationToken = def
162165
}
163166

164167
/// <summary>
165-
/// TODO: Menu (macOS) still to be implemented
168+
/// Gets the dock menu items.
169+
/// </summary>
170+
/// <value>
171+
/// The menu items.
172+
/// </value>
173+
public IReadOnlyCollection<MenuItem> MenuItems { get { return _items.AsReadOnly(); } }
174+
private List<MenuItem> _items = new List<MenuItem>();
175+
176+
/// <summary>
166177
/// Sets the application's dock menu.
167178
/// </summary>
168-
public void SetMenu()
179+
public void SetMenu(MenuItem[] menuItems)
169180
{
170-
BridgeConnector.Socket.Emit("dock-setMenu");
181+
menuItems.AddMenuItemsId();
182+
BridgeConnector.Socket.Emit("dock-setMenu", JArray.FromObject(menuItems, _jsonSerializer));
183+
_items.AddRange(menuItems);
184+
185+
BridgeConnector.Socket.Off("dockMenuItemClicked");
186+
BridgeConnector.Socket.On("dockMenuItemClicked", (id) => {
187+
MenuItem menuItem = _items.GetMenuItem(id.ToString());
188+
menuItem?.Click();
189+
});
190+
171191
}
172192

173193
/// <summary>
@@ -202,5 +222,11 @@ public void SetIcon(string image)
202222
{
203223
BridgeConnector.Socket.Emit("dock-setIcon", image);
204224
}
225+
226+
private JsonSerializer _jsonSerializer = new JsonSerializer()
227+
{
228+
ContractResolver = new CamelCasePropertyNamesContractResolver(),
229+
NullValueHandling = NullValueHandling.Ignore
230+
};
205231
}
206232
}

ElectronNET.API/Entities/WebPreferences.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.ComponentModel;
1+
using System.ComponentModel;
22

33
namespace ElectronNET.API.Entities
44
{
@@ -206,5 +206,11 @@ public class WebPreferences
206206
/// </value>
207207
[DefaultValue(false)]
208208
public bool WebviewTag { get; set; } = false;
209+
210+
/// <summary>
211+
/// Whether to enable the remote module. Defaults to false.
212+
/// </summary>
213+
[DefaultValue(false)]
214+
public bool EnableRemoteModule { get; set; } = false;
209215
}
210216
}

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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
namespace ElectronNET.API
4+
{
5+
/// <summary>
6+
///
7+
/// </summary>
8+
public static class ServiceCollectionExtensions
9+
{
10+
/// <summary>
11+
/// Adds the <see cref="Electron"/> Members to the Service Collection
12+
/// </summary>
13+
public static IServiceCollection AddElectron(this IServiceCollection services)
14+
=> services
15+
// adding in this manner to ensure late binding.
16+
.AddSingleton(provider => IpcMain.Instance)
17+
.AddSingleton(provider => App.Instance)
18+
.AddSingleton(provider => AutoUpdater.Instance)
19+
.AddSingleton(provider => WindowManager.Instance)
20+
.AddSingleton(provider => Menu.Instance)
21+
.AddSingleton(provider => Dialog.Instance)
22+
.AddSingleton(provider => Notification.Instance)
23+
.AddSingleton(provider => Tray.Instance)
24+
.AddSingleton(provider => GlobalShortcut.Instance)
25+
.AddSingleton(provider => Shell.Instance)
26+
.AddSingleton(provider => Screen.Instance)
27+
.AddSingleton(provider => Clipboard.Instance)
28+
.AddSingleton(provider => HostHook.Instance)
29+
.AddSingleton(provider => PowerMonitor.Instance)
30+
.AddSingleton(provider => NativeTheme.Instance)
31+
.AddSingleton(provider => Dock.Instance);
32+
}
33+
}

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.

0 commit comments

Comments
 (0)