WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit c8a3578

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 e854451 commit c8a3578

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
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.

0 commit comments

Comments
 (0)