Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,6 @@ paket-files/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc

# Mac Only settings file
.DS_Store
30 changes: 30 additions & 0 deletions ElectronNET.API/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,5 +1608,35 @@ internal void PreventQuit()
}

private bool _preventQuit = false;

private const string ModuleName = "app";
/// <summary>
/// Subscribe to an unmapped event on the <see cref="App"/> module.
/// </summary>
/// <param name="eventName">The event name</param>
/// <param name="fn">The handler</param>
public void On(string eventName, Action fn)
=> Events.Instance.On(ModuleName, eventName, fn);
/// <summary>
/// Subscribe to an unmapped event on the <see cref="App"/> module.
/// </summary>
/// <param name="eventName">The event name</param>
/// <param name="fn">The handler</param>
public void On(string eventName, Action<object> fn)
=> Events.Instance.On(ModuleName, eventName, fn);
/// <summary>
/// Subscribe to an unmapped event on the <see cref="App"/> module once.
/// </summary>
/// <param name="eventName">The event name</param>
/// <param name="fn">The handler</param>
public void Once(string eventName, Action fn)
=> Events.Instance.Once(ModuleName, eventName, fn);
/// <summary>
/// Subscribe to an unmapped event on the <see cref="App"/> module once.
/// </summary>
/// <param name="eventName">The event name</param>
/// <param name="fn">The handler</param>
public void Once(string eventName, Action<object> fn)
=> Events.Instance.Once(ModuleName, eventName, fn);
}
}
34 changes: 30 additions & 4 deletions ElectronNET.API/Dock.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Threading;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using ElectronNET.API.Entities;
using ElectronNET.API.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

namespace ElectronNET.API
{
Expand Down Expand Up @@ -162,12 +165,29 @@ public async Task<bool> IsVisibleAsync(CancellationToken cancellationToken = def
}

/// <summary>
/// TODO: Menu (macOS) still to be implemented
/// Gets the dock menu items.
/// </summary>
/// <value>
/// The menu items.
/// </value>
public IReadOnlyCollection<MenuItem> MenuItems { get { return _items.AsReadOnly(); } }
private List<MenuItem> _items = new List<MenuItem>();

/// <summary>
/// Sets the application's dock menu.
/// </summary>
public void SetMenu()
public void SetMenu(MenuItem[] menuItems)
{
BridgeConnector.Socket.Emit("dock-setMenu");
menuItems.AddMenuItemsId();
BridgeConnector.Socket.Emit("dock-setMenu", JArray.FromObject(menuItems, _jsonSerializer));
_items.AddRange(menuItems);

BridgeConnector.Socket.Off("dockMenuItemClicked");
BridgeConnector.Socket.On("dockMenuItemClicked", (id) => {
MenuItem menuItem = _items.GetMenuItem(id.ToString());
menuItem?.Click();
});

}

/// <summary>
Expand Down Expand Up @@ -202,5 +222,11 @@ public void SetIcon(string image)
{
BridgeConnector.Socket.Emit("dock-setIcon", image);
}

private JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
};
}
}
8 changes: 7 additions & 1 deletion ElectronNET.API/Entities/WebPreferences.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.ComponentModel;
using System.ComponentModel;

namespace ElectronNET.API.Entities
{
Expand Down Expand Up @@ -206,5 +206,11 @@ public class WebPreferences
/// </value>
[DefaultValue(false)]
public bool WebviewTag { get; set; } = false;

/// <summary>
/// Whether to enable the remote module. Defaults to false.
/// </summary>
[DefaultValue(false)]
public bool EnableRemoteModule { get; set; } = false;
}
}
105 changes: 105 additions & 0 deletions ElectronNET.API/Events.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Globalization;
using Quobject.EngineIoClientDotNet.ComponentEmitter;

namespace ElectronNET.API
{
/// <summary>
/// Generic Event Consumers for Electron Modules
/// </summary>
internal class Events
{
private static Events _events;
private static object _syncRoot = new object();
private TextInfo _ti = new CultureInfo("en-US", false).TextInfo;
private Events()
{

}

public static Events Instance
{
get
{
if (_events == null)
{
lock (_syncRoot)
{
if (_events == null)
{
_events = new Events();
}
}
}

return _events;
}
}

/// <summary>
/// Subscribe to an unmapped electron event.
/// </summary>
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
/// <param name="eventName">The name of the event</param>
/// <param name="fn">The event handler</param>
public void On(string moduleName, string eventName, Action fn)
=> On(moduleName, eventName, new ListenerImpl(fn));

/// <summary>
/// Subscribe to an unmapped electron event.
/// </summary>
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
/// <param name="eventName">The name of the event</param>
/// <param name="fn">The event handler</param>
public void On(string moduleName, string eventName, Action<object> fn)
=> On(moduleName, eventName, new ListenerImpl(fn));

/// <summary>
/// Subscribe to an unmapped electron event.
/// </summary>
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
/// <param name="eventName">The name of the event</param>
/// <param name="fn">The event handler</param>
private void On(string moduleName, string eventName, IListener fn)
{
var listener = $"{moduleName}{_ti.ToTitleCase(eventName)}Completed";
var subscriber = $"register-{moduleName}-on-event";

BridgeConnector.Socket.On(listener, fn);
BridgeConnector.Socket.Emit(subscriber, eventName, listener);
}

/// <summary>
/// Subscribe to an unmapped electron event.
/// </summary>
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
/// <param name="eventName">The name of the event</param>
/// <param name="fn">The event handler</param>
public void Once(string moduleName, string eventName, Action fn)
=> Once(moduleName, eventName, new ListenerImpl(fn));

/// <summary>
/// Subscribe to an unmapped electron event.
/// </summary>
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
/// <param name="eventName">The name of the event</param>
/// <param name="fn">The event handler</param>
public void Once(string moduleName, string eventName, Action<object> fn)
=> Once(moduleName, eventName, new ListenerImpl(fn));

/// <summary>
/// Subscribe to an unmapped electron event.
/// </summary>
/// <param name="moduleName">The name of the module, e.g. app, dock, etc...</param>
/// <param name="eventName">The name of the event</param>
/// <param name="fn">The event handler</param>
private void Once(string moduleName, string eventName, IListener fn)
{
var listener = $"{moduleName}{_ti.ToTitleCase(eventName)}Completed";
var subscriber = $"register-{moduleName}-once-event";
BridgeConnector.Socket.Once(listener, fn);
BridgeConnector.Socket.Emit(subscriber, eventName, listener);
}

}
}
33 changes: 33 additions & 0 deletions ElectronNET.API/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.DependencyInjection;

namespace ElectronNET.API
{
/// <summary>
///
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds the <see cref="Electron"/> Members to the Service Collection
/// </summary>
public static IServiceCollection AddElectron(this IServiceCollection services)
=> services
// adding in this manner to ensure late binding.
.AddSingleton(provider => IpcMain.Instance)
.AddSingleton(provider => App.Instance)
.AddSingleton(provider => AutoUpdater.Instance)
.AddSingleton(provider => WindowManager.Instance)
.AddSingleton(provider => Menu.Instance)
.AddSingleton(provider => Dialog.Instance)
.AddSingleton(provider => Notification.Instance)
.AddSingleton(provider => Tray.Instance)
.AddSingleton(provider => GlobalShortcut.Instance)
.AddSingleton(provider => Shell.Instance)
.AddSingleton(provider => Screen.Instance)
.AddSingleton(provider => Clipboard.Instance)
.AddSingleton(provider => HostHook.Instance)
.AddSingleton(provider => PowerMonitor.Instance)
.AddSingleton(provider => NativeTheme.Instance)
.AddSingleton(provider => Dock.Instance);
}
}
30 changes: 30 additions & 0 deletions ElectronNET.API/Tray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,5 +351,35 @@ public Task<bool> IsDestroyedAsync()
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
};

private const string ModuleName = "tray";
/// <summary>
/// Subscribe to an unmapped event on the <see cref="Tray"/> module.
/// </summary>
/// <param name="eventName">The event name</param>
/// <param name="fn">The handler</param>
public void On(string eventName, Action fn)
=> Events.Instance.On(ModuleName, eventName, fn);
/// <summary>
/// Subscribe to an unmapped event on the <see cref="Tray"/> module.
/// </summary>
/// <param name="eventName">The event name</param>
/// <param name="fn">The handler</param>
public void On(string eventName, Action<object> fn)
=> Events.Instance.On(ModuleName, eventName, fn);
/// <summary>
/// Subscribe to an unmapped event on the <see cref="Tray"/> module once.
/// </summary>
/// <param name="eventName">The event name</param>
/// <param name="fn">The handler</param>
public void Once(string eventName, Action fn)
=> Events.Instance.Once(ModuleName, eventName, fn);
/// <summary>
/// Subscribe to an unmapped event on the <see cref="Tray"/> module once.
/// </summary>
/// <param name="eventName">The event name</param>
/// <param name="fn">The handler</param>
public void Once(string eventName, Action<object> fn)
=> Events.Instance.Once(ModuleName, eventName, fn);
}
}
20 changes: 20 additions & 0 deletions ElectronNET.Host/api/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading