Skip to content

Commit fd5801f

Browse files
Update Context Menu Item for tray app on-the-fly. #270
1 parent b6417d0 commit fd5801f

File tree

8 files changed

+39
-28
lines changed

8 files changed

+39
-28
lines changed

ElectronNET.API/Entities/MenuItem.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,11 @@ public class MenuItem
6969
/// <summary>
7070
/// If false, the menu item will be greyed out and unclickable.
7171
/// </summary>
72-
[DefaultValue(true)]
7372
public bool Enabled { get; set; } = true;
7473

7574
/// <summary>
7675
/// If false, the menu item will be entirely hidden.
7776
/// </summary>
78-
[DefaultValue(true)]
7977
public bool Visible { get; set; } = true;
8078

8179
/// <summary>

ElectronNET.API/Menu.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,25 @@ public void SetApplicationMenu(MenuItem[] menuItems)
8282
/// <param name="menuItems">The menu items.</param>
8383
public void SetContextMenu(BrowserWindow browserWindow, MenuItem[] menuItems)
8484
{
85+
menuItems.AddMenuItemsId();
86+
BridgeConnector.Socket.Emit("menu-setContextMenu", browserWindow.Id, JArray.FromObject(menuItems, _jsonSerializer));
87+
8588
if (!_contextMenuItems.ContainsKey(browserWindow.Id))
8689
{
87-
menuItems.AddMenuItemsId();
88-
BridgeConnector.Socket.Emit("menu-setContextMenu", browserWindow.Id, JArray.FromObject(menuItems, _jsonSerializer));
8990
_contextMenuItems.Add(browserWindow.Id, menuItems.ToList());
90-
9191
var x = _contextMenuItems.ToDictionary(kv => kv.Key, kv => kv.Value.AsReadOnly());
9292
ContextMenuItems = new ReadOnlyDictionary<int, ReadOnlyCollection<MenuItem>>(x);
93+
}
9394

94-
BridgeConnector.Socket.Off("contextMenuItemClicked");
95-
BridgeConnector.Socket.On("contextMenuItemClicked", (results) =>
96-
{
97-
var id = ((JArray)results).First.ToString();
98-
var browserWindowId = (int)((JArray)results).Last;
95+
BridgeConnector.Socket.Off("contextMenuItemClicked");
96+
BridgeConnector.Socket.On("contextMenuItemClicked", (results) =>
97+
{
98+
var id = ((JArray)results).First.ToString();
99+
var browserWindowId = (int)((JArray)results).Last;
99100

100-
MenuItem menuItem = _contextMenuItems[browserWindowId].GetMenuItem(id);
101-
menuItem.Click?.Invoke();
102-
});
103-
}
101+
MenuItem menuItem = _contextMenuItems[browserWindowId].GetMenuItem(id);
102+
menuItem.Click?.Invoke();
103+
});
104104
}
105105

106106
/// <summary>
@@ -115,8 +115,7 @@ public void ContextMenuPopup(BrowserWindow browserWindow)
115115
private JsonSerializer _jsonSerializer = new JsonSerializer()
116116
{
117117
ContractResolver = new CamelCasePropertyNamesContractResolver(),
118-
NullValueHandling = NullValueHandling.Ignore,
119-
DefaultValueHandling = DefaultValueHandling.Ignore
118+
NullValueHandling = NullValueHandling.Ignore
120119
};
121120
}
122121
}

ElectronNET.API/Tray.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ public void Show(string image, MenuItem[] menuItems)
252252
{
253253
menuItems.AddMenuItemsId();
254254
BridgeConnector.Socket.Emit("create-tray", image, JArray.FromObject(menuItems, _jsonSerializer));
255+
_items.Clear();
255256
_items.AddRange(menuItems);
256257

257258
BridgeConnector.Socket.Off("trayMenuItemClicked");
@@ -339,8 +340,7 @@ public Task<bool> IsDestroyedAsync()
339340
private JsonSerializer _jsonSerializer = new JsonSerializer()
340341
{
341342
ContractResolver = new CamelCasePropertyNamesContractResolver(),
342-
NullValueHandling = NullValueHandling.Ignore,
343-
DefaultValueHandling = DefaultValueHandling.Ignore
343+
NullValueHandling = NullValueHandling.Ignore
344344
};
345345
}
346346
}

ElectronNET.Host/api/clipboard.js

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

ElectronNET.Host/api/clipboard.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/menu.js

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

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ export = (socket: SocketIO.Socket) => {
1111
electronSocket.emit('contextMenuItemClicked', [id, browserWindowId]);
1212
});
1313

14-
contextMenuItems.push({
14+
const index = contextMenuItems.findIndex(contextMenu => contextMenu.browserWindowId === browserWindowId);
15+
16+
const contextMenuItem = {
1517
menu: menu,
1618
browserWindowId: browserWindowId
17-
});
19+
};
20+
21+
if (index === -1) {
22+
contextMenuItems.push(contextMenuItem);
23+
} else {
24+
contextMenuItems[index] = contextMenuItem;
25+
}
1826
});
1927

2028
function addContextMenuItemClickConnector(menuItems, browserWindowId, callback) {

0 commit comments

Comments
 (0)