Recursive AI

Open a native popup anchored to a window in C#

OnePlatform is a free, open-source library for windows, input, clipboard, displays and gamepads in .NET from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

Use CreatePopup with the parent window and a rectangle, such as the bounds of a button.

Terminal
dotnet add package RecursiveAI.OnePlatform

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using System;
using OnePlatform;
 
using var app = PlatformApp.Create();
var parent = app.CreateWindow(new()
{
    Width = 480, Height = 320,
    SoftwareSurface = true
});
var popup = app.CreatePopup(parent, new()
{
    AnchorX = 24, AnchorY = 20,
    AnchorWidth = 96, AnchorHeight = 32,
    Grab = false, // No outside-click dismissal.
    SoftwareSurface = true
});
parent.FillSolid(0xff26354a);
popup.FillSolid(0xffe7a348);
Console.WriteLine(popup.Role);
while (true) app.PumpEvents(1000);

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

The snippet opens a popup below a 96 × 32 rectangle starting at (24, 20) in the parent window. On Wayland the popup is gold against the dark parent; on X11 it’s blank because X11 ignores FillSolid. Stop the snippet with Ctrl+C. It prints:

Text
Popup

The full program

CreatePopup opens native popups on Windows, macOS and Linux (Wayland and X11); on Windows and macOS this snippet also shows a gold, borderless popup against the dark parent. The parent’s Close button exits the full example. The anchor is an invisible rectangle for this demo; in an app, use the bounds of the control that opened the popup.

C#
using System;
using OnePlatform;
using static OnePlatform.PlatformEventKind;
 
using var app = PlatformApp.Create();
var parent = app.CreateWindow(new()
{
    Title = "Popup parent",
    Width = 480, Height = 320,
    SoftwareSurface = true
});
var popup = app.CreatePopup(parent, new()
{
    AnchorX = 24, AnchorY = 20,
    AnchorWidth = 96, AnchorHeight = 32,
    Grab = false, // No outside-click dismissal.
    SoftwareSurface = true
});
parent.FillSolid(0xff26354a);
popup.FillSolid(0xffe7a348);
Console.WriteLine(popup.Role);
 
while (true)
{
    app.PumpEvents(1000);
    while (app.TryGetEvent(out var evt))
        if (evt.Kind is WindowCloseRequested
            or QuitRequested)
            return;
}
Text
Popup

Grab = false leaves input alone instead of capturing it for menu-style dismissal, so this example can open at startup. On Wayland, a popup with a grab needs a recent click or key press in the app; create a grabbed menu from that input instead of at startup.

Variations

Add this property to the CreatePopup options to request an eight-pixel gap below the anchor:

C#
OffsetY = 8,

That changes the requested position from (24, 52) to (24, 60) before screen-edge adjustments.

The short answer

Call PlatformApp.CreatePopup(parent, options). Put the position and size of the opening control into AnchorX, AnchorY, AnchorWidth, and AnchorHeight in parent-window pixels. The default anchor and gravity put the popup’s top-left corner at the rectangle’s bottom-left corner, space permitting. The popup’s Width and Height are separate from the anchor’s dimensions; the example leaves the popup at its default 200 × 120 pixels.

How do I position a popup below a button in a parent window?

Measure the button from the top left of the parent’s content, not from the edge of the monitor. With the default Anchor = PopupEdge.BottomLeft and Gravity = PopupEdge.BottomRight, the anchor picks the bottom-left corner of the rectangle and gravity makes the popup extend down and right from that point. With a button at (24, 20) that is 32 pixels high, the requested popup position is (24, 52). The layout is the same if the parent starts somewhere else on the screen, because those coordinates are relative to its content.

Near a screen edge, the requested location may not fit. The default Adjustments allow the popup to slide horizontally, flip vertically, or slide vertically. Check Window.PositionInParent after placement if you need to know where it actually landed, for example to draw an arrow pointing to the button. An offset requests a gap; it doesn’t disable those fit adjustments.

How do I open a popup instead of another ordinary window?

Use a popup when content belongs next to a control in the current window, like a drop-down or tooltip. A dialog created as another ordinary window has a different role; CreatePopup supplies the rectangle-based positioning and returns a window with Role set to Popup. You still supply the popup’s content. The colored surface in the example is just a background, not a menu or a hit target.

For a menu, the default Grab = true enables outside-click dismissal where supported: dismissal posts PopupDismissed with the popup’s window ID and closes it. Clicks inside the app are yours to handle. On X11, keyboard input stays in the parent even though there’s a pointer grab, so don’t assume the popup itself receives arrow keys. For a tooltip without a grab, close it yourself when it’s no longer needed.

All oneplatform recipes