Skip to content

Commit 218b998

Browse files
committed
Add dynamic tray menu update functionality
Introduced a `SetMenuItems` method in `Tray.cs` to enable updating the tray's context menu dynamically. This method clears existing menu items, adds new ones, and registers click handlers. Added XML documentation for the method.
1 parent 7f507a6 commit 218b998

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

src/ElectronNET.API/API/Tray.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,33 @@ public async Task Show(string image, MenuItem[] menuItems)
220220
_items.Clear();
221221
_items.AddRange(menuItems);
222222

223+
RegisterMenuItemClickedHandler();
224+
}
225+
226+
/// <summary>
227+
/// Sets the tray menu items.
228+
/// </summary>
229+
/// <remarks>Calling this method updates the context menu with the specified items. Any previously
230+
/// set menu items will be replaced.</remarks>
231+
/// <param name="menuItems">An array of <see cref="MenuItem"/> objects representing the menu items to display in the tray menu.
232+
/// Cannot be null.</param>
233+
public async Task SetMenuItems(MenuItem[] menuItems)
234+
{
235+
menuItems.AddMenuItemsId();
236+
await BridgeConnector.Socket.Emit("set-contextMenu", [menuItems]).ConfigureAwait(false);
237+
_items.Clear();
238+
_items.AddRange(menuItems);
239+
240+
RegisterMenuItemClickedHandler();
241+
}
242+
243+
private void RegisterMenuItemClickedHandler()
244+
{
223245
BridgeConnector.Socket.Off("trayMenuItemClicked");
224246
BridgeConnector.Socket.On<string>("trayMenuItemClicked", (id) =>
225247
{
226248
MenuItem menuItem = _items.GetMenuItem(id);
227-
menuItem?.Click();
249+
menuItem?.Click?.Invoke();
228250
});
229251
}
230252

src/ElectronNET.Host/api/tray.js

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

src/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.

src/ElectronNET.Host/api/tray.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,7 @@ export = (socket: Socket) => {
5959
tray.value = new Tray(trayIcon);
6060

6161
if (menuItems) {
62-
const menu = Menu.buildFromTemplate(menuItems);
63-
64-
addMenuItemClickConnector(menu.items, (id) => {
65-
electronSocket.emit('trayMenuItemClicked', id);
66-
});
67-
tray.value.setContextMenu(menu);
62+
applyContextMenu(menuItems);
6863
}
6964
});
7065

@@ -74,6 +69,12 @@ export = (socket: Socket) => {
7469
}
7570
});
7671

72+
socket.on('set-contextMenu', (menuItems) => {
73+
if (menuItems && tray.value) {
74+
applyContextMenu(menuItems);
75+
}
76+
});
77+
7778
socket.on('tray-setImage', (image) => {
7879
if (tray.value) {
7980
tray.value.setImage(image);
@@ -136,6 +137,14 @@ export = (socket: Socket) => {
136137
}
137138
});
138139

140+
function applyContextMenu(menuItems) {
141+
const menu = Menu.buildFromTemplate(menuItems);
142+
addMenuItemClickConnector(menu.items, (id) => {
143+
electronSocket.emit('trayMenuItemClicked', id);
144+
});
145+
tray.value.setContextMenu(menu);
146+
}
147+
139148
function addMenuItemClickConnector(menuItems, callback) {
140149
menuItems.forEach((item) => {
141150
if (item.submenu && item.submenu.items.length > 0) {

0 commit comments

Comments
 (0)