NAME
RmlUi - Native Perl bindings for RmlUi
SYNOPSIS
use RmlUi;
my $app = RmlUi::Application->new(title => 'Hello');
my $document = $app->load_document_from_string(
'<rml><body>Hello from Perl!</body></rml>');
$document->show;
$app->run;
For lower-level control or a custom backend:
use RmlUi;
RmlUi::initialize() or die "RmlUi initialization failed";
my $context = RmlUi::create_context('main', 800, 600)
or die "Context creation failed";
my $doc = $context->load_document_from_string(
'<rml><body><div id="message">Hello</div></body></rml>'
) or die "Document loading failed";
my $element = $doc->get_element_by_id('message');
$element->set_inner_rml('Hello from Perl');
$element->set_property('color', '#ffffff');
$doc->show;
$context->update;
$context->render; # headless without a renderer
$doc->close;
$context->update;
$context->close;
RmlUi::shutdown();
DESCRIPTION
Low-level, renderer-neutral and event-loop-neutral bindings for RmlUi. By default the binding installs a no-op renderer and uses RmlUi's default system and file interfaces. Supply a renderer object at initialization to receive native geometry, texture and scissor calls. open_window installs the bundled SDL3/OpenGL 3.3 backend and opens a native desktop window. See RmlUi::Application for the convenient native application lifecycle, and RmlUi::Renderer for custom backends.
FUNCTIONS
defer($callback) queues a CODE reference from an event listener to run once the outermost native call has finished successfully, before that call returns to Perl. This allows structural DOM changes without reentering native event dispatch. Deferred callbacks run in registration order and may invoke normal RmlUi methods. Nested event dispatches get their own batch. If native dispatch or a deferred callback throws, remaining queued callbacks are discarded and the exception propagates. defer is only available inside event listeners, not renderer or system callbacks.
open_window(title => 'Example', width => 800, height => 600) initializes the bundled backend and returns its context. It must be called on the main application thread before any other initialization. Only one window and context are supported by the bundled backend in version 0.01. By default, the bundled LatoLatin regular, bold, italic and bold-italic faces are loaded and the context root supplies LatoLatin as its inherited font family. Documents can override the font-family in RCSS, and additional fonts can be loaded normally. Pass default_font => 0 to skip the bundled fonts. The headless and application-supplied backends are unchanged. The fonts are distributed under the SIL Open Font License 1.1. Call the context's process_events until it returns false, then shutdown. The context's render presents the frame automatically. The bundled backend loads PNG, JPEG, BMP, TGA and GIF (first frame) textures from RmlUi's file interface. Include a head element in RML documents using relative asset paths: RmlUi 6.3 records the document source URL when parsing the head. Use trusted application assets. Image files are limited to 64 MiB, 8192 pixels per axis and 32 Mi pixels.
version() returns the linked native library version. initialize() and shutdown() are idempotent when called without options. initialize(renderer => $backend) installs an application-provided renderer for the entire runtime. initialize(system => $backend) installs an optional system interface; both options can be combined. Neither interface can be replaced until shutdown. See RmlUi::System. is_initialized() reports whether the calling interpreter owns the initialized runtime.
create_context($name, $width, $height) returns a context or undef if creation fails. Dimensions must be positive; names must not be empty.
load_font_face($path, $fallback = 0, $face_index = 0) loads a font file through RmlUi's file interface. Returns true on success. Load fonts after initialization and before laying out documents with text.
elapsed_time() returns seconds from the installed system clock. set_clipboard_text($text) and get_clipboard_text() use the installed system clipboard interface (the native default is an in-process clipboard).
OBJECTS
All object handles are borrowed references. Releasing a Perl handle does not destroy a native context, document or element. Use close explicitly for contexts and documents. is_valid tests handle validity. Other methods throw on stale or fabricated handles. Missing elements, attributes and unknown properties return undef.
RmlUi::Context
name, dimensions (two-element list), set_dimensions($w, $h), load_document($path), load_document_from_string($rml, $source_url = ''), update, render, close.
process_events($max_wait_ms = 0) processes up to 256 native window events. It returns false when the window requests closure. A positive integer timeout allows SDL to sleep until an event arrives or the timeout expires; RmlUi's next update deadline can shorten that wait. Only the first event may block. Zero preserves nonblocking polling. Call update and render before waiting for the next frame. The timeout is a maximum, not a guarantee of elapsed time. This method requires a native window.
density_independent_pixel_ratio, set_density_independent_pixel_ratio($ratio), next_update_delay (seconds, possibly infinity), focus_element, hover_element, is_mouse_interacting.
Input methods: process_mouse_move($x, $y, $modifiers = 0), process_mouse_button_down($button, $modifiers = 0), process_mouse_button_up($button, $modifiers = 0), process_mouse_wheel($x, $y, $modifiers = 0), process_mouse_leave, process_key_down($key, $modifiers = 0), process_key_up($key, $modifiers = 0), process_text_input($text).
Mouse buttons are 0 (left), 1 (right), 2 (middle). Key and modifier constants are available from RmlUi::Input. Native input results are returned unchanged: for mouse movement/buttons/leave, true means the mouse is not interacting with RmlUi; for key/text/wheel, the result is the native propagation result. Send input explicitly from your own backend. Input methods must not be called recursively from an event callback.
RmlUi::Document
Inherits element methods. Also provides show, hide, close. Closing invalidates the document and its elements immediately from Perl's perspective, even if native destruction is deferred until update.
RmlUi::Element
get_element_by_id($id), tag_name, id, is_visible, get_attribute($name), set_attribute($name, $value), remove_attribute($name), get_property($name), set_property($name, $value), remove_property($name), get_inner_rml, set_inner_rml($rml).
Strings containing markup or DOM values are Perl character strings; the binding converts them to UTF-8 for RmlUi. File paths are passed as Perl's underlying byte representation.
focus($visible = 0), blur, bounding_box (x, y, width, height of the untransformed main border box in context coordinates), client_size, scroll_size, scroll_offset (two-element lists), set_scroll_offset($x, $y). Update the context before inspecting layout. Focus, blur and scroll changes are not allowed during callbacks.
DOM construction
my $row = $element->append_element('div');
$row->set_attribute('class', 'row');
$row->append_text('Plain text, not markup');
$element->insert_element_before('div', $row);
parent_element, child_count, child($index) inspect the DOM. Child indices are zero-based; absent children return undef. The parent of a document is not exposed.
append_element($tag), insert_element_before($tag, $child), append_text($text) create and attach native elements in one operation. They return borrowed handles (or undef on creation failure).
move_child($child, $before = undef) moves an existing element into this parent in the same document, optionally before a direct child. Existing handles remain valid. remove_child($child) destroys a direct DOM child and its descendants, invalidating their handles; it returns false when the element is not a direct DOM child. Moves across documents and cycles are rejected. Structural operations are forbidden inside event callbacks.
EVENT LISTENERS
my $token = $element->add_event_listener('click', sub {
my ($event) = @_;
print $event->{type}, "\n";
$event->{current_target}->set_property('color', 'red');
});
$element->dispatch_event('click', { button => 0 });
$element->remove_event_listener($token);
add_event_listener($type, $callback, $capture = 0) returns a numeric listener token. The element retains the callback until removal, native element destruction, or runtime shutdown. Removing a listener returns true if it was attached to that element. A listener can remove itself.
Callbacks receive a blessed RmlUi::Event hash reference with type, phase (capture, target or bubble), target, current_target (checked element handles, or undef if no live document owns the element), and parameters (a copied hash of supported scalar values; unsupported native types are undef). The snapshot may outlive the event. During its callback only, call is_interruptible, stop_propagation or stop_immediate_propagation on the event object. is_active reports whether these methods are currently available; calling them on an expired snapshot throws. Synthetic event parameters are strings in this checkpoint. dispatch_event returns the native propagation result.
Callback exceptions are rethrown after the outermost native operation finishes, including operations such as document close. Additional Perl listeners are skipped after the first exception. The native operation may already have completed and is not rolled back.
Callbacks may read DOM state, change CSS properties, and remove listeners. Structural DOM changes, attribute changes, document/context lifecycle operations, recursive event dispatch, and listener addition are rejected during callbacks in this checkpoint. Perform these after the enclosing native call returns. Runtime shutdown suppresses callbacks.
LIMITATIONS
One interpreter and thread may own the native runtime at a time. Handles are not transferable to Perl ithreads. A competing interpreter cannot initialize the runtime until the owner shuts it down. Forking an initialized runtime is unsupported. Real rendering is deferred to later checkpoints. Explicit shutdown is recommended; an END block also shuts down the runtime when its owning interpreter exits.
LICENSE
MIT. See LICENSE.