diff --git a/docs/api-migrations.md b/docs/api-migrations.md index 76dcc0eb..44fd2a8f 100644 --- a/docs/api-migrations.md +++ b/docs/api-migrations.md @@ -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 diff --git a/platform/native/lui_window/lui_window.ml b/platform/native/lui_window/lui_window.ml index f8658778..3005c8c5 100644 --- a/platform/native/lui_window/lui_window.ml +++ b/platform/native/lui_window/lui_window.ml @@ -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 diff --git a/platform/native/lui_window/lui_window.mli b/platform/native/lui_window/lui_window.mli index daf218fe..6195d025 100644 --- a/platform/native/lui_window/lui_window.mli +++ b/platform/native/lui_window/lui_window.mli @@ -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 diff --git a/platform/native/lui_window/test/lui_window_test.ml b/platform/native/lui_window/test/lui_window_test.ml index 268a1a27..32a5673c 100644 --- a/platform/native/lui_window/test/lui_window_test.ml +++ b/platform/native/lui_window/test/lui_window_test.ml @@ -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 @@ -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;