Skip to content
Closed
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
18 changes: 18 additions & 0 deletions docs/api-migrations.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# LUI API migration guide

### Native hosts can set the editable selection programmatically

`Lui_window.Ui.set_selection ui store id ~anchor ~caret` focuses the node when
needed and places the editable selection at byte offsets into the node's text,
the native counterpart of the DOM's `setSelectionRange`. Offsets clamp to the
text length and round back to UTF-8 sequence starts, mirroring how the browser
clamps to the field length. Hosts that previously emulated select-all through
the primary-modifier+A accelerator should call this API instead; keyboard and
pointer selection behavior is unchanged, and no application migration is
required.

```ocaml
(* select-all, replacing the primary-modifier+A accelerator hack: *)
let text = Lui_store.string_prop store id "text" |> Option.value ~default:"" in
ignore (Ui.set_selection ui store id ~anchor:0 ~caret:(String.length text))
```


### Linux native text rasterizes with subpixel antialiasing by default

The Linux Pango text backend now rasterizes monochrome glyphs as horizontal
Expand Down
27 changes: 27 additions & 0 deletions platform/native/lui_window/lui_window.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,33 @@ module Ui = struct

let set_focused ?(kbd = false) t store id = focus_move ~kbd t store id

(* Programmatic selection, the native counterpart of the DOM's
setSelectionRange: focus the node when needed (which attaches the
session), then place anchor and caret on UTF-8 boundaries of the
session text. Offsets are byte positions clamped like the browser
clamps to the field length. *)
let set_selection t store id ~anchor ~caret =
if id = 0 || Lui_store.kind store id = "" then []
else begin
let moved = Lui_focus.focused t.focus <> id in
if moved then focus_move t store id;
if Lui_text_input.node t.session <> id then []
else begin
let text = Lui_text_input.text t.session in
let clamp = Lui_query.clamp_boundary text in
let caret = clamp caret and anchor = clamp anchor in
let session =
Lui_text_input.set_caret t.session ~off:anchor ~extend:false
in
let session = Lui_text_input.set_caret session ~off:caret ~extend:true in
if session != t.session then begin
t.session <- session;
t.dirty <- true
end;
if moved then [ Focus_changed id ] else []
end
end

(* Perform one machine intent: side effects land on the composer
state here and the driver-facing [action] list comes back. *)
let rec perform t store = function
Expand Down
15 changes: 15 additions & 0 deletions platform/native/lui_window/lui_window.mli
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,21 @@ module Ui : sig
{!focus_visible}); the default is pointer/programmatic focus. *)
val set_focused : ?kbd:bool -> t -> Lui_store.t -> int -> unit

(** Programmatic selection, the native counterpart of the DOM's
[setSelectionRange]: focuses [id] when it is not focused, then
sets the editable selection to [min anchor caret .. max anchor
caret). Offsets are byte positions into the node's text — the
live session text, so hosts should run {!store_changed} first
when model updates may still be unsynced — clamped to the text
length and rounded back to UTF-8 sequence starts, the browser's
clamping semantics applied to UTF-8 storage. The returned actions
carry [Focus_changed] when the focus moved, and must be applied
like {!handle} results so drivers start text input and update
accessibility focus. No-op (with no actions) for node 0 or
unknown nodes. *)
val set_selection :
t -> Lui_store.t -> int -> anchor:int -> caret:int -> action list

(** Effects that react to store changes: dead-focus cleanup, modal
overlay focus save/restore, Appear lifecycle events,
scroll-target requests and track-visible-range reports. The
Expand Down
35 changes: 35 additions & 0 deletions platform/native/lui_window/test/lui_window_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,39 @@ let test_modal_pointer_scope () =
click 230. 80.)
[ 1.; 2. ]

let test_set_selection_programmatic () =
let s, rects = ui_store_fresh () in
let ui = Ui.create () in
ignore
(Ui.handle ui s rects
(Input.Button_down (5., 40., Input.Left, 1, Input.mods_none)));
ignore (Ui.handle ui s rects (Input.Text_input "abcdef"));
(* mid-range selection: typing replaces the middle instead of
appending at the caret *)
ignore (Ui.set_selection ui s 4 ~anchor:2 ~caret:4);
Alcotest.(check int) "caret lands on the range end" 4 (Ui.caret ui);
Alcotest.(check int) "anchor lands on the range start" 2 (Ui.sel_anchor ui);
(match press_actions (Ui.handle ui s rects (Input.Text_input "X")) with
| [ TextChanged (4, "abXef") ] -> ()
| _ -> Alcotest.fail "expected typing to replace the selected middle");
(* selecting an unfocused field focuses it and reports the move *)
Ui.set_focused ui s 0;
(match Ui.set_selection ui s 4 ~anchor:0 ~caret:1 with
| [ Focus_changed 4 ] -> ()
| _ -> Alcotest.fail "expected Focus_changed when selection focuses");
(* backwards and out-of-range offsets clamp like the browser's *)
ignore (Ui.set_selection ui s 4 ~anchor:1000 ~caret:1);
Alcotest.(check int) "caret clamps into the text" 1 (Ui.caret ui);
Alcotest.(check int) "anchor clamps to the text length" 5 (Ui.sel_anchor ui);
(* offsets inside a multi-byte sequence round back to its start *)
Lui_store.apply_op s (SetProp (4, TextValue, StringValue "a字z"));
ignore (Ui.store_changed ui s);
(match Ui.set_selection ui s 4 ~anchor:2 ~caret:3 with
| [] -> ()
| _ -> Alcotest.fail "re-selecting the focused node emits no actions");
Alcotest.(check int) "anchor rounds back to a UTF-8 boundary" 1 (Ui.sel_anchor ui);
Alcotest.(check int) "caret rounds back to a UTF-8 boundary" 1 (Ui.caret ui)

let test_click_focus () =
let s, rects = ui_store_fresh () in
let ui = Ui.create () in
Expand Down Expand Up @@ -5088,6 +5121,8 @@ let () =
test_click_release_elsewhere;
Alcotest.test_case "focus" `Quick test_click_focus;
Alcotest.test_case "text+keys" `Quick test_text_input_and_keys;
Alcotest.test_case "programmatic set_selection" `Quick
test_set_selection_programmatic;
Alcotest.test_case "caret ops" `Quick test_caret_ops_wiring;
Alcotest.test_case "text revisions" `Quick
test_text_revisions;
Expand Down
Loading