Recursive AI

Getting started with OnePlatform

All oneplatform docs

Open a window and run its event loop, then drive a window with no display at all, the way a test does. This guide was run on the 1.0.0 package from NuGet, on Linux: the window on X11 (a private Xvfb display), the test on the headless backend.

Install

You need the .NET 10 SDK. Make a console project and add the package:

Terminal
dotnet new console -n Hello
cd Hello
dotnet add package RecursiveAI.OnePlatform --version 1.0.0

The package depends only on the .NET base class library. It talks to each operating system through that system’s own API: Win32 on Windows, Cocoa on macOS, Wayland or X11 on Linux.

Open a window

Replace Program.cs with:

C#
using OnePlatform;
 
// Picks Wayland, X11, Win32, Cocoa... for this system.
using var app = PlatformApp.Create();
var window = app.CreateWindow(
    new WindowOptions { Title = "Hello", Width = 800, Height = 600 });
 
Console.WriteLine($"Backend: {app.Backend}");
Console.WriteLine(
    $"Window {window.Id}: {window.Width} x {window.Height} px, " +
    $"scale {window.Scale}, {window.NativeHandle.Kind}");
 
// Stop after two seconds. Wake() is safe from any thread and ends the wait.
var timeUp = false;
using var timer = new Timer(_ =>
{
    Volatile.Write(ref timeUp, true);
    app.Wake();
}, null, 2000, Timeout.Infinite);
 
var running = true;
while (running)
{
    // Sleeps until an event arrives, a wake, or a second passes.
    app.PumpEvents(1000);
    while (app.TryGetEvent(out var e))
    {
        Console.WriteLine($"  {e.Kind}");
        if (e.Kind == PlatformEventKind.QuitRequested ||
            e.Kind == PlatformEventKind.WindowCloseRequested)
        {
            running = false;
        }
    }
    if (Volatile.Read(ref timeUp))
    {
        running = false;
    }
}
Console.WriteLine("Closed");

Run it with dotnet run. On X11 it printed:

Text
Backend: X11
Window 1: 800 x 600 px, scale 1, X11
  PointerInside
  WindowFocusChanged
Closed

PlatformApp.Create() picks the backend for the system it runs on: Wayland when WAYLAND_DISPLAY is set, otherwise X11 when DISPLAY is set, Win32 on Windows and Cocoa on macOS. Without the timer, the loop runs until the user closes the window (WindowCloseRequested) or the session asks the app to quit (QuitRequested).

window.NativeHandle is the native window for a renderer: its Kind, Display and Window. window.Width and Height are physical pixels, and window.Scale converts them to logical units. Work that arrives on another thread calls app.Wake() to end the wait, as the timer does here. See Waking the event loop.

Drive a window in a test

The headless backend needs no display: its windows are in-memory objects, and app.Injector sends them the input a user would. Replace Program.cs with:

C#
using OnePlatform;
 
// No display needed: the headless backend's windows are in-memory objects.
using var app = PlatformApp.Create(
    new PlatformOptions { Backend = PlatformBackendKind.Headless });
var window = app.CreateWindow(
    new WindowOptions { Title = "Test", Width = 640, Height = 480 });
 
// Drive it the way a user would.
var input = app.Injector;
input.PointerMove(window.Id, 120, 80);
input.PointerButton(window.Id, MouseButton.Left, true);
input.PointerButton(window.Id, MouseButton.Left, false);
input.Text(window.Id, "héllo");
input.KeyPress(window.Id, Key.Enter);
input.CloseRequest(window.Id);
 
app.PumpEvents();
while (app.TryGetEvent(out var e))
{
    var detail = e.Kind switch
    {
        PlatformEventKind.PointerMoved => $"{e.X}, {e.Y}",
        PlatformEventKind.PointerButtonDown or
            PlatformEventKind.PointerButtonUp => $"{e.Button}",
        PlatformEventKind.TextInput => $"\"{e.Text}\"",
        PlatformEventKind.KeyDown or PlatformEventKind.KeyUp => $"{e.Key}",
        _ => "",
    };
    Console.WriteLine($"{e.Kind,-20} {detail}");
}
 
app.Clipboard.SetText("copied");
Console.WriteLine($"Clipboard: {app.Clipboard.GetText()}");

It printed:

Text
PointerMoved         120, 80
PointerButtonDown    Left
PointerButtonUp      Left
TextInput            "héllo"
KeyDown              Enter
KeyUp                Enter
WindowCloseRequested 
Clipboard: copied

Injected input goes through the same queue and the same input state (app.Input) as real input, so the code under test reads it as it would on a desktop. InputInjector also has wheel, touch, IME composition, gamepad, lifecycle and back-button events, and the README lists it on every system.

Next