The example has one note button and one action, Pin note. Call host.ShowMenu on a right-click to show the action at the pointer. It’s a separate OS popup window drawn by OneApp, not the system’s own menu. On desktop, OneApp runs on Linux (Wayland and X11), macOS (Apple silicon), and Windows.
dotnet add package RecursiveAI.OneAppRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
#nullable enable using OneApp; using OneRender.Interaction; const string html = "<button id='note'>" + "Right-click this note</button>"; // BindMenu runs before Create returns. // Its input callback reads app later. AppHost? app = null; using var host = app = AppHost.Create( new AppOptions(html) { Title = "Note actions", Width = 440, Height = 260, Setup = BindMenu }); host.Run(); // Setup binds the handler; input comes later. void BindMenu(InteractivePage page) { var note = page.FindElement("note"); var target = EventTarget.Of(note); page.Events.AddEventListener( target, "pointerdown", ShowActions); } void ShowActions(DomEvent e) { if (e is not MouseEvent pointer || pointer.Button != 2) return; var window = app!.MainWindow; var pin = new MenuItem("Pin note", () => Console.WriteLine( "Pin note selected")); var actions = new Menu(pin); float scale = window.Window.Scale; app.ShowMenu(window, actions, (int)(pointer.ClientX * scale), (int)(pointer.ClientY * scale)); }
Call ShowMenu from the input handler. A window system that requires a recent click or key press can refuse an out-of-context popup with PlatformException.
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Right-click the note, then select Pin note. Checked on Linux (X11): the popup closes and the terminal prints:
Pin note selectedVariations
Add a disabled action by replacing the actions declaration in ShowActions in Program.cs. pin is the existing Pin note menu item:
var actions = new Menu( pin, MenuItem.Separator(), new MenuItem("Remove note") { Enabled = false });
Remove note stays visible, but arrow-key navigation skips it. Pin note still runs normally.
Related recipes
The short answer
Listen for button 2 (the secondary button) in a pointerdown handler and pass a Menu to host.ShowMenu. Supply the window that received the click. ClientX and ClientY are page viewport coordinates in CSS pixels; multiply by the window scale to give ShowMenu the parent-window pixels it expects. Don’t pass desktop coordinates or add the document’s scroll offset.
What is a right-click context menu?
It’s a list of actions for what the user clicked. A note might offer Pin note; right-clicking an empty part of the notebook needn’t show that action. Bind the listener to the element that has those actions, and capture the item you’re acting on in the callback. In the example there’s only one note, and the action prints a line rather than modifying any data.
This popup is separate from the page, so you don’t need to add absolutely positioned menu markup or move it around when the page scrolls. It isn’t a system-drawn menu: the desktop shell API creates the popup and renders the items with the page renderer.
How do I bring up and dismiss a context menu?
Right-click the note to open it. Once it’s open, the arrow keys, Home, and End move between enabled items; Enter or Space picks one. Escape and a click outside dismiss it without running an action. The selected callback runs on the UI thread after the popup closes, so it can open a dialog of its own.
For keyboard users who can’t right-click, also expose the same actions through a visible button. Open the menu from the button’s input handler and anchor it below the button instead of at the pointer. A Shortcut on a MenuItem displays a hint; it doesn’t register a key binding for this popup.