# RmlUi

[![CI](https://github.com/haxmeister/perl-RmlUi/actions/workflows/ci.yml/badge.svg)](https://github.com/haxmeister/perl-RmlUi/actions/workflows/ci.yml)

RmlUi lets you build graphical applications in Perl using HTML-like markup
(RML) and CSS-like styles (RCSS). You can write your application logic in Perl
and keep the appearance of the interface separate.

The built-in SDL3/OpenGL backend opens a real desktop window, handles input
and draws the interface. You can also use RmlUi without a window (headless)
or supply your own renderer and system services.

## Install

Install from CPAN:

```sh
cpanm RmlUi
```

To build from a Git checkout, you need a C++17 compiler, Alien::RmlUi 0.02
or newer, and Alien::SDL3::Native 0.01 or newer.

```sh
cpanm --installdeps .
perl Makefile.PL
make
make test
make install
```

Native windows need an SDL3 video driver and OpenGL 3.3 support. The
headless API does not need a display. Linux desktop rendering has been
tested; Windows CI covers compilation and headless tests, not a real
desktop. macOS graphical operation has not been verified.

## Your first application

Save this as hello.pl and run it with perl hello.pl:

```perl
use strict;
use warnings;
use RmlUi::Application;

my $app = RmlUi::Application->new(
    title  => 'Hello',
    width  => 400,
    height => 200,
);

my $doc = $app->load_document_from_string(<<'RML');
<rml>
<head>
<style>
body { background-color: #20242b; color: white; padding: 20px; }
button { padding: 8px; background-color: #386ba1; }
</style>
</head>
<body>
  <p>Hello from Perl!</p>
  <button id="quit">Close</button>
</body>
</rml>
RML

$doc->get_element_by_id('quit')->add_event_listener(click => sub {
    $app->quit;
});

$doc->show;
$app->run;
```

The application creates a window, loads the document, and runs the event
loop. Clicking Close or closing the desktop window ends the application.

Use load_document($path) to load an RML file instead of writing the markup
inside your Perl program. Use get_element_by_id() to find an element and
add_event_listener() to respond to user input.

To change the document structure from inside an event listener, use
RmlUi::defer(sub { ... }). It runs your code after native event dispatch
has finished.

## Windows, contexts and documents

A window is the actual desktop window. A context holds the interface inside
it. A document contains the RML markup, and elements are individual parts
of that document, such as buttons and text.

Use RmlUi::Application for ordinary desktop programs. Use RmlUi::open_window
when you want to manage the event loop yourself.

### Window control (new in 0.04)

This example creates a hidden, borderless window, gets its native window
information and then shows it.

```perl
use strict;
use warnings;
use RmlUi::Application;

my $app = RmlUi::Application->new(
    title      => 'Panel',
    width      => 640,
    height     => 36,
    x          => 0,
    y          => 0,
    resizable  => 0,
    borderless => 1,
    hidden     => 1,
);

my $window = $app->window;
my $native = $window->native_handle;

if ($native->{platform} eq 'x11' && $native->{window}) {
    print "X11 window ID: $native->{window}\n";
    # An X11 library can configure desktop properties here.
}

my $doc = $app->load_document_from_string(
    '<rml><body style="background-color: #20242b; color: white;">Panel</body></rml>'
);
$doc->show;
$window->show;
$app->run;
```

| Method | What it does |
| --- | --- |
| show / hide | Show or hide the desktop window |
| is_visible | Check whether SDL marks the window as visible |
| is_borderless | Check whether the window is borderless |
| position | Return x and y coordinates |
| size | Return width and height |
| set_position($x, $y) | Request a new window position |
| set_size($w, $h) | Request a new window size |
| native_handle | Return driver name and X11 ID when available |
| is_valid | Check that the native window still exists |

Window sizes use SDL logical units; the context dimensions may use more
pixels on high-DPI displays. The operating system or window manager can
adjust or reject positioning and sizing requests. Window objects become
invalid when the runtime shuts down. Do not destroy an SDL-owned window
through another library.

## Using your own event loop

RmlUi does not require a specific Perl event loop. For native windows,
call open_window(), then process_events(0), update() and render() on the
returned context as needed. process_events() returns false when the
window requests closure. Call RmlUi::shutdown() when finished. The native
SDL event pump must run on the main application thread.

There is currently no general pollable SDL event source exposed by this
backend. See the POD for details about timed event processing.

## Fonts and images

The desktop backend loads bundled LatoLatin fonts by default. Use
default_font => 0 to opt out, or load your own fonts with load_font_face().
The bundled fonts are licensed under the SIL Open Font License 1.1.

PNG, JPEG, BMP, TGA and the first frame of GIF images are supported.
Include a head element in RML documents that use relative asset paths.
Use trusted assets; images are limited to 64 MiB per file, 8192 pixels
per axis and 32 Mi pixels in total.

### In-memory images (new in 0.05)

With the native window open, register tightly packed straight-alpha RGBA
bytes and use the named source in RML:

```perl
my $pixels = pack('C4', 255, 80, 20, 255) x (16 * 16);
RmlUi::set_image_rgba('sample', 16, 16, $pixels)
    or die 'Invalid image';
# RML: <img src="?rmlui-image:sample"/>
```

Reuse image names when updating dynamic content. Remove the referencing
elements before calling RmlUi::remove_image('sample'). See perldoc RmlUi
for limits and lifetime rules.

## Documentation and examples

Run perldoc RmlUi for the API reference, or perldoc RmlUi::Application
for the desktop application wrapper. RmlUi::Input describes input
constants; RmlUi::Renderer and RmlUi::System describe custom backends.

After building, try:

```sh
perl -Mblib examples/application.pl
perl -Mblib examples/window.pl
RMLUI_TEST_WINDOW=1 make test
```

The bundled backend currently supports one native window and context.
Headless and custom rendering backends remain available through initialize().
The binding and bundled backend code use the MIT license (see LICENSE and
LICENSE.RmlUi-backends); see third_party/README.md for the image decoder.

## Release verification (Unix)

From a Git checkout (developer tools are not shipped to CPAN), run `RMLUI_TEST_WINDOW=1 sh tools/test-dist.sh` on a graphical desktop. Omit `RMLUI_TEST_WINDOW` for a headless test. The script generates the CPAN tarball, builds and tests it in a temporary directory, installs it into a temporary prefix, and loads the installed modules and fonts outside the source tree. It does not install RmlUi globally or upload anything. The tarball remains in the repository root for release.

## Continuous integration

GitHub Actions tests threaded Perl 5.16, 5.18, 5.20, 5.22, 5.32, 5.40 and 5.44, plus non-threaded Perl 5.44, on Ubuntu 24.04. Each job installs CPAN prerequisites, builds and tests without a display, then tests the complete graphical suite and staged distribution installation under Xvfb and Mesa software rendering. A Windows job uses Strawberry Perl 5.42 and its MinGW compiler to build and link the XS extension and run the display-independent test suite with gmake. The workflow runs on pushes, pull requests and manual dispatches; failures are not ignored.
