text editor issue is a problem with focus resolution

might require Focus.release, but may be incorrect use of Focus.request.
This commit is contained in:
cqc
2022-12-14 19:39:08 -06:00
parent a12db025e0
commit b1ac36ce3e
2 changed files with 4590 additions and 4582 deletions

File diff suppressed because one or more lines are too long

View File

@ -1333,6 +1333,10 @@ module Nottui = struct
module Ui = struct module Ui = struct
type may_handle = [ `Unhandled | `Handled ] type may_handle = [ `Unhandled | `Handled ]
let may_handle (type a) (v : a option) (f : a -> may_handle) :
may_handle =
match v with Some v' -> f v' | None -> `Unhandled
let pp_may_handle ppf = function let pp_may_handle ppf = function
| `Unhandled -> F.pf ppf "`Unhandled" | `Unhandled -> F.pf ppf "`Unhandled"
| `Handled -> F.pf ppf "`Handled" | `Handled -> F.pf ppf "`Handled"
@ -2722,13 +2726,21 @@ module Nottui_widgets = struct
open Lwd.Infix open Lwd.Infix
let eq_uc_c uc c = Uchar.(equal uc (of_char c))
type line = { type line = {
focus : Focus.handle; focus : Focus.handle;
state : (string * int) Lwd.var; state : (string * int) Lwd.var;
ui : Ui.t Lwd.t; ui : Ui.t Lwd.t;
} }
let eq_uc_c uc c = Uchar.(equal uc (of_char c)) let line_make ?(focus = Focus.make ()) str =
let state = Lwd.var (str, 0) in
{ focus; state; ui = edit_field ~focus state }
let line_append ?(table = Lwd_table.make ()) ?focus str =
let row = Lwd_table.append table in
Lwd_table.set row (line_make ?focus str)
let copy_line_cursor (x : line) (y : line) = let copy_line_cursor (x : line) (y : line) =
let _, xi = Lwd.peek x.state in let _, xi = Lwd.peek x.state in
@ -2736,43 +2748,33 @@ module Nottui_widgets = struct
let yi = Int.max 0 (Int.min xi (String.length ys)) in let yi = Int.max 0 (Int.min xi (String.length ys)) in
Lwd.set y.state (ys, yi) Lwd.set y.state (ys, yi)
let row_of_cursor cursor f =
Ui.may_handle (Lwd.peek cursor) (fun row -> f row)
let line_of_cursor cursor let line_of_cursor cursor
(f : line Lwd_table.row -> line -> Ui.may_handle) : (f : line Lwd_table.row -> line -> Ui.may_handle) :
Ui.may_handle = Ui.may_handle =
match Lwd.peek cursor with Ui.may_handle (Lwd.peek cursor) (fun row ->
| Some row -> ( Ui.may_handle (Lwd_table.get row) (fun line -> f row line))
match Lwd_table.get row with
| Some line -> f row line
| None -> `Unhandled)
| None -> `Unhandled
let cursor_move cursor let cursor_move cursor
(f : line Lwd_table.row -> line Lwd_table.row option) = (f : line Lwd_table.row -> line Lwd_table.row option) =
match Lwd.peek cursor with match Lwd.peek cursor with
| Some cursor_line -> ( | Some cursor_row -> (
match f cursor_line with match f cursor_row with
| Some new_row ->
(match Lwd_table.get new_row with
| Some new_line -> | Some new_line ->
(match Lwd_table.get new_line with cursor_row |> Lwd_table.get
| Some line' -> |> Option.iter (fun cursor_line ->
cursor_line |> Lwd_table.get copy_line_cursor cursor_line new_line);
|> Option.iter (fun line -> Focus.request new_line.focus
copy_line_cursor line line');
Focus.request line'.focus
| None -> ()); | None -> ());
Lwd.set cursor (Some new_line); Lwd.set cursor (Some new_row);
`Handled `Handled
| None -> `Unhandled) | None -> `Unhandled)
| None -> `Unhandled | None -> `Unhandled
let line_make ?(focus = Focus.make ()) str =
let state = Lwd.var (str, 0) in
{ focus; state; ui = edit_field ~focus state }
let line_append ?(table = Lwd_table.make ())
?(focus = Focus.make ()) str =
let row = Lwd_table.append table in
Lwd_table.set row (line_make ~focus str)
let line_table_of_string ?(table = Lwd_table.make ()) let line_table_of_string ?(table = Lwd_table.make ())
?(focus = Focus.make ()) (s : string) : Ui.t Lwd.t = ?(focus = Focus.make ()) (s : string) : Ui.t Lwd.t =
(* Append lines from s to table *) (* Append lines from s to table *)
@ -2832,12 +2834,12 @@ module Nottui_widgets = struct
let str_prev, _ = let str_prev, _ =
Lwd.peek line_prev.state Lwd.peek line_prev.state
in in
Focus.request line_prev.focus;
Lwd.set line_prev.state Lwd.set line_prev.state
( str_prev ^ str, ( str_prev ^ str,
String.length str_prev ); String.length str_prev );
Focus.request line_prev.focus;
Lwd_table.remove row;
Lwd.set cursor (Some row_prev); Lwd.set cursor (Some row_prev);
Lwd_table.remove row;
`Handled `Handled
| None -> `Unhandled) | None -> `Unhandled)
| _ -> `Unhandled) | _ -> `Unhandled)