Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions docs/api-migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4060,3 +4060,65 @@ unknown values; they no longer raise `Invalid_argument`.
Mounting `split ~sizing_signal` no longer emits a spurious default
`ProgressValue 0.5` before the signal's first value. Mode transitions remove
only properties belonging to the previous sizing mode.

## Native store: structural patch validation

`Lui_store.apply_batch` validates a batch against a structural shadow before
mutating the live tree. Cyclic `InsertChild`/`MoveChild`, `RemoveChild` that
names the wrong parent, and structural ops on missing nodes raise
`Invalid_argument` with op index and a `describe_op` prefix (`patch op N (...):
...`). The store is unchanged so `Lui_host.apply_batch` can return `false` and
the runtime can resync, matching Web and Android. Property ops on missing
nodes are still ignored. `InsertChild`/`MoveChild` whose parent is not a node
(including the gallery sentinel `parent=-1`) stay a no-op so the child remains
a root, matching `apply_op`. `apply_op` stays lenient except that inserting a
node under itself or an ancestor raises to keep `iter_preorder` acyclic.

```ocaml
(* Before: InsertChild (2, 1) after InsertChild (1, 2) closed a cycle and
hung iter_preorder; RemoveChild (2, 3) while parent(3)=1 duplicated 3 in
the root set. *)
Lui_store.apply_batch store { generation; ops = [ InsertChild (2, 1, 0) ] }

(* After: the batch raises and the tree is unchanged. Hosts treat the
failure as a rejected batch:
Lui_host.apply_batch host batch (* false *)
*)
```

## Native hosts tick the application clock

SDL/native window loops (macOS, Linux, Windows) call
`Lui_app.tick_clock ~now` every iteration and fold `Lui_clock.next_delay`
into the event-wait timeout via `Lui_window.fold_wait_ms`. `Lui_ui.after`
and `Lui_ui.every` therefore fire on native windows, matching Apple,
Android and Web.

Toast `duration` is applied by the native UI layer: `Ui.store_changed`
schedules a `Dismiss` at `now + duration` and `Ui.next_input_deadline`
wakes the host for it. Demo drivers no longer keep a private
`toast_since` clock.

Native window loops also call `Lui_host.tick ~now` every iteration
(seconds, matching the host animation clock). That starts
`resize-duration` split transitions and advances retained popup motion;
`Lui_host.wants_repaint` already requests frames while those animations
run.

## Native resource executor and Vulkan frame resize

`Lui_resource.create` now defaults to a bounded worker pool instead of
one `Domain.spawn` per load. `request`/`reload` mark the entry `Failed`
when the executor raises (for example past the domain cap) instead of
leaving it stuck `Loading`. Hosts call `drain_all` every frame and may
install `set_completion_wake` (SDL hosts use
`Lui_sdl3.run_on_main_thread`) so completions wake an idle wait.

`Lui_vulkan.resize` recreates only the offscreen frame image and
framebuffer. Window swapchain presentation remains a follow-up: Linux
still reads the frame back and uploads damaged rows through
`Lui_blit`.

Terminal `pump` retries short PTY writes; `flush_pending` is the
testable helper. Incoming chunks enqueue in O(1). `Terminal_pty.close`
is idempotent.
22 changes: 12 additions & 10 deletions platform/native/e2e/test/e2e_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -400,17 +400,19 @@ let test_real_layout_positions () =
let test_invalid_ops_ignored () =
let h = E2e_harness.create () in
let store = E2e_harness.store h in
ignore (E2e_harness.apply_batch h
{ generation = 1;
ops =
[ SetProp (99, Gap, IntValue 4);
RemoveProp (99, Gap);
InsertChild (1, 99, 0);
RemoveChild (1, 99);
DropNode 99 ] });
let accepted =
E2e_harness.apply_batch h
{ generation = 1;
ops =
[ SetProp (99, Gap, IntValue 4);
RemoveProp (99, Gap);
InsertChild (1, 99, 0);
RemoveChild (1, 99);
DropNode 99 ] }
in
check bool "structural ops on missing ids reject the batch" false accepted;
check int "empty store" 0 (Lui_store.node_count store);
(* DropNode on a nonexistent id touches nothing — not even the
dirty set. *)
(* Rejection rolls the batch back — nothing is dirtied. *)
check int "nothing dirtied" 0
(List.length (Lui_store.drain_dirty store))

Expand Down
7 changes: 7 additions & 0 deletions platform/native/lui_ax_linux/lui_ax_linux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ external c_dbus_open : string -> int = "lui_ax_dbus_open"
external c_dbus_close : int -> unit = "lui_ax_dbus_close"
external c_dbus_name : int -> string = "lui_ax_dbus_name"
external c_dbus_a11y_address : int -> string = "lui_ax_dbus_a11y_address"
external c_dbus_unix_fd : int -> int = "lui_ax_dbus_unix_fd"
external c_dbus_embed : int -> string -> string * string
= "lui_ax_dbus_embed"
external c_dbus_poll : int -> int ->
Expand Down Expand Up @@ -1885,6 +1886,12 @@ let set_desktop_ref t r = t.desktop <- Some r
let app_id t = t.app_id
let transport t = t.trans

let unix_fd t =
if !(t.conn) = 0 then None
else
let fd = c_dbus_unix_fd !(t.conn) in
if fd < 0 then None else Some fd

let session_bus_address () =
match Sys.getenv_opt "DBUS_SESSION_BUS_ADDRESS" with
| Some a when a <> "" -> a
Expand Down
5 changes: 5 additions & 0 deletions platform/native/lui_ax_linux/lui_ax_linux.mli
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ val connect : t -> transport
emission APIs keep queueing. Requires Linux. *)

val transport : t -> transport

val unix_fd : t -> int option
(** Session or a11y-bus connection fd for the host wait loop, or
[None] when offline. *)

val disconnect : t -> unit
val flush : t -> int
(** [flush t] sends every queued event over the bus; returns how many
Expand Down
26 changes: 26 additions & 0 deletions platform/native/lui_ax_linux/lui_ax_linux_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <caml/callback.h>
#include <caml/fail.h>
#include <caml/custom.h>
#include <caml/threads.h>

/* ---- Minimal libdbus declarations (no headers required) ---- */

Expand Down Expand Up @@ -119,6 +120,8 @@ static DBusMessage *(*p_dbus_connection_pop_message)(
DBusConnection *) = 0;
static DBusMessage *(*p_dbus_connection_send_with_reply_and_block)(
DBusConnection *, DBusMessage *, int, DBusError *) = 0;
static dbus_bool_t (*p_dbus_connection_get_unix_fd)(
DBusConnection *, int *) = 0;
static DBusMessage *(*p_dbus_message_new_method_call)(const char *,
const char *,
const char *,
Expand Down Expand Up @@ -196,6 +199,8 @@ CAMLprim value lui_ax_dbus_probe(value unit)
(void **) &p_dbus_connection_pop_message);
ok &= sym(h, "dbus_connection_send_with_reply_and_block",
(void **) &p_dbus_connection_send_with_reply_and_block);
ok &= sym(h, "dbus_connection_get_unix_fd",
(void **) &p_dbus_connection_get_unix_fd);
ok &= sym(h, "dbus_message_new_method_call",
(void **) &p_dbus_message_new_method_call);
ok &= sym(h, "dbus_message_new_signal",
Expand Down Expand Up @@ -386,8 +391,10 @@ CAMLprim value lui_ax_dbus_a11y_address(value vc)
CAMLreturn(vs);
}
p_dbus_error_init(&err);
caml_release_runtime_system();
rep = p_dbus_connection_send_with_reply_and_block(c->conn, call, 5000,
&err);
caml_acquire_runtime_system();
p_dbus_message_unref(call);
if (rep && p_dbus_message_iter_init(rep, &it)
&& p_dbus_message_iter_get_arg_type(&it) == DBUS_TYPE_STRING)
Expand Down Expand Up @@ -428,8 +435,10 @@ CAMLprim value lui_ax_dbus_embed(value vc, value vpath)
}
p_dbus_message_iter_close_container(&it, &sub);
p_dbus_error_init(&err);
caml_release_runtime_system();
rep = p_dbus_connection_send_with_reply_and_block(c->conn, call, 5000,
&err);
caml_acquire_runtime_system();
if (rep && p_dbus_message_iter_init(rep, &it)
&& p_dbus_message_iter_get_arg_type(&it) == DBUS_TYPE_STRUCT) {
p_dbus_message_iter_recurse(&it, &sub);
Expand All @@ -453,6 +462,17 @@ CAMLprim value lui_ax_dbus_embed(value vc, value vpath)
CAMLreturn(vt);
}

CAMLprim value lui_ax_dbus_unix_fd(value vc)
{
CAMLparam1(vc);
conn_t *c = conn_of(vc);
int fd = -1;
if (p_dbus_connection_get_unix_fd == 0 || c->conn == 0
|| !p_dbus_connection_get_unix_fd(c->conn, &fd))
CAMLreturn(Val_int(-1));
CAMLreturn(Val_int(fd));
}

/* ---- Argument decoding ----

Flattens a message's basic arguments into three arrays: ints (all
Expand Down Expand Up @@ -1042,6 +1062,12 @@ CAMLprim value lui_ax_dbus_embed(value a, value b)
return ax_fail();
}

CAMLprim value lui_ax_dbus_unix_fd(value a)
{
(void) a;
return Val_int(-1);
}

CAMLprim value lui_ax_dbus_poll(value a, value b)
{
(void) a;
Expand Down
59 changes: 33 additions & 26 deletions platform/native/lui_host/lui_host.ml
Original file line number Diff line number Diff line change
Expand Up @@ -323,44 +323,51 @@ let set_reduced_motion h enabled =
end

let apply_batch ?(animate_resize=true) h (batch : Lui_protocol.patch_batch) =
let previous = Hashtbl.create 4 and created = Hashtbl.create 4 in
let previous = Hashtbl.create 4 and created = Hashtbl.create 4
and drop_motion = Hashtbl.create 4 in
List.iter (function
| Lui_protocol.CreateNode (id,_) | CreateExtension (id,_,_)
| DropNode id | DetachSubtree id ->
Hashtbl.replace created id ();
Hashtbl.remove previous id;
Hashtbl.remove h.transitions id
Hashtbl.replace drop_motion id ();
Hashtbl.remove previous id
| RemoveProp (id, (ProgressValue | FirstPaneWidth)) ->
Hashtbl.remove previous id;
Hashtbl.remove h.transitions id
Hashtbl.replace drop_motion id ();
Hashtbl.remove previous id
| SetProp (id, ((ProgressValue | FirstPaneWidth) as property), _) when not (Hashtbl.mem created id)
&& Lui_store.kind h.store id = "split" ->
if not animate_resize then Hashtbl.remove h.transitions id;
if not animate_resize then Hashtbl.replace drop_motion id ();
if not (Hashtbl.mem previous id) then
(match Lui_store.float_prop h.store id (Lui_wire_schema.property_name property) with
| Some value -> Hashtbl.add previous id (property, value)
| None -> ())
| _ -> ()) batch.ops;
Lui_store.apply_batch h.store batch;
Hashtbl.filter_map_inplace (fun id motion ->
if Lui_store.mem h.store id then Some motion else None) h.transitions;
Hashtbl.iter (fun id (property, from_value) ->
match h.animation_time, Lui_store.prop h.store id (Lui_wire_schema.property_name property) with
| Some started, Some (Lui_protocol.FloatValue target)
when animate_resize && not h.reduced_motion && from_value <> target ->
let duration = Option.value ~default:0 (Lui_store.int_prop h.store id "resize-duration") in
if duration > 0 then begin
let easing = Option.value ~default:"standard"
(Lui_store.string_prop h.store id "resize-easing") in
(match Hashtbl.find_opt h.transitions id with
| Some motion when motion.property = property && motion.target = target -> ()
| _ -> Hashtbl.replace h.transitions id
{property;from_value;target;started;duration=float duration /. 1000.;easing});
set_transition_value h id property from_value
end else Hashtbl.remove h.transitions id
| _ -> Hashtbl.remove h.transitions id) previous;
h.dirty <- true;
true
match
try Lui_store.apply_batch h.store batch; Ok ()
with Invalid_argument _ -> Error ()
with
| Error () -> false
| Ok () ->
Hashtbl.iter (fun id () -> Hashtbl.remove h.transitions id) drop_motion;
Hashtbl.filter_map_inplace (fun id motion ->
if Lui_store.mem h.store id then Some motion else None) h.transitions;
Hashtbl.iter (fun id (property, from_value) ->
match h.animation_time, Lui_store.prop h.store id (Lui_wire_schema.property_name property) with
| Some started, Some (Lui_protocol.FloatValue target)
when animate_resize && not h.reduced_motion && from_value <> target ->
let duration = Option.value ~default:0 (Lui_store.int_prop h.store id "resize-duration") in
if duration > 0 then begin
let easing = Option.value ~default:"standard"
(Lui_store.string_prop h.store id "resize-easing") in
(match Hashtbl.find_opt h.transitions id with
| Some motion when motion.property = property && motion.target = target -> ()
| _ -> Hashtbl.replace h.transitions id
{property;from_value;target;started;duration=float duration /. 1000.;easing});
set_transition_value h id property from_value
end else Hashtbl.remove h.transitions id
| _ -> Hashtbl.remove h.transitions id) previous;
h.dirty <- true;
true

let resize h ~width ~height ~scale =
h.width <- width;
Expand Down
5 changes: 4 additions & 1 deletion platform/native/lui_host/lui_host.mli
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ val invalidate_paint : t -> unit
a stable store). *)

val apply_batch : ?animate_resize:bool -> t -> Lui_protocol.patch_batch -> bool
(** [animate_resize=false] applies direct manipulation feedback, including
(** Returns [false] when the store rejects the batch (cycles, ops on
missing nodes, wrong parent) so the runtime can resync. The store
is left unchanged on rejection.
[animate_resize=false] applies direct manipulation feedback, including
dependent split corrections, without scheduling eased catch-up. *)

val resize : t -> width:int -> height:int -> scale:float -> unit
Expand Down
16 changes: 16 additions & 0 deletions platform/native/lui_host/test/lui_host_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ let test_apply_marks_dirty () =
check bool "dirty after apply" true (Lui_host.wants_repaint h);
check int "nodes" 2 (Lui_store.node_count (Lui_host.store h))

let test_invalid_batch_rejected () =
let h = make () in
check bool "mount" true
(Lui_host.apply_batch h
(batch [ CreateNode (1, Column); CreateNode (2, Text); InsertChild (1, 2, 0) ]));
ignore (Lui_host.repaint h);
check bool "cycle rejected" false
(Lui_host.apply_batch h (batch [ InsertChild (2, 1, 0) ]));
check (option int) "tree unchanged" None
(Lui_store.parent (Lui_host.store h) 1);
check bool "stale remove rejected" false
(Lui_host.apply_batch h (batch [ RemoveChild (2, 1) ]));
check (list int) "child still under 1" [2]
(Lui_store.child_ids (Lui_host.store h) 1)

let test_repaint_clears_dirty () =
let h = make () in
ignore
Expand Down Expand Up @@ -600,6 +615,7 @@ let () =
( "dirty",
[ test_case "create clean" `Quick test_create_clean;
test_case "apply marks dirty" `Quick test_apply_marks_dirty;
test_case "invalid batch rejected" `Quick test_invalid_batch_rejected;
test_case "repaint clears" `Quick test_repaint_clears_dirty;
test_case "resize marks dirty" `Quick test_resize_marks_dirty;
test_case "set_hidden marks dirty" `Quick test_set_hidden_marks_dirty
Expand Down
8 changes: 4 additions & 4 deletions platform/native/lui_layout/test/lui_layout_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1469,10 +1469,10 @@ let test_incremental_random () =
InsertChild (1, 2, 0); InsertChild (1, 3, 1) ]
@ List.concat_map
(fun i ->
[ CreateNode (10 + i, Text);
InsertChild ((if i mod 2 = 0 then 2 else 3), 10 + i, i / 2);
SetProp (10 + i, WidthValue, IntValue (10 + i));
SetProp (10 + i, HeightValue, IntValue 10) ])
[ CreateNode (i, Text);
InsertChild ((if i mod 2 = 0 then 2 else 3), i, (i - 10) / 2);
SetProp (i, WidthValue, IntValue i);
SetProp (i, HeightValue, IntValue 10) ])
leaves);
let t = Lui_layout.create () in
Lui_layout.sync ~width:200. ~height:100. t store;
Expand Down
Loading
Loading