Skip to content

Commit ce0f3fd

Browse files
DanDan
authored andcommitted
Dock Menu support
fix #527
1 parent 12f5eae commit ce0f3fd

File tree

4 files changed

+73
-10
lines changed

4 files changed

+73
-10
lines changed

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.Host/api/dock.js

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

ElectronNET.Host/api/dock.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/dock.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { app } from 'electron';
1+
import { app, Menu } from 'electron';
22
let electronSocket;
33

44
export = (socket: SocketIO.Socket) => {
@@ -39,8 +39,17 @@ export = (socket: SocketIO.Socket) => {
3939
electronSocket.emit('dock-isVisible-completed', isVisible);
4040
});
4141

42-
// TODO: Menu (macOS) still to be implemented
43-
socket.on('dock-setMenu', (menu) => {
42+
socket.on('dock-setMenu', (menuItems) => {
43+
let menu = null;
44+
45+
if (menuItems) {
46+
menu = Menu.buildFromTemplate(menuItems);
47+
48+
addMenuItemClickConnector(menu.items, (id) => {
49+
electronSocket.emit('dockMenuItemClicked', id);
50+
});
51+
}
52+
4453
app.dock.setMenu(menu);
4554
});
4655

@@ -53,4 +62,16 @@ export = (socket: SocketIO.Socket) => {
5362
socket.on('dock-setIcon', (image) => {
5463
app.dock.setIcon(image);
5564
});
65+
66+
function addMenuItemClickConnector(menuItems, callback) {
67+
menuItems.forEach((item) => {
68+
if (item.submenu && item.submenu.items.length > 0) {
69+
addMenuItemClickConnector(item.submenu.items, callback);
70+
}
71+
72+
if ('id' in item && item.id) {
73+
item.click = () => { callback(item.id); };
74+
}
75+
});
76+
}
5677
};

0 commit comments

Comments
 (0)