From 3f4339886652d6a97b1b23dc4dc53ec474c4ea10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Fri, 12 May 2023 14:39:02 +0200 Subject: [PATCH] Cleanup, reduce Lwd.set calls --- prompt.ml | 9 +++++++-- window.ml | 11 +++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/prompt.ml b/prompt.ml index ec232f3..402c064 100644 --- a/prompt.ml +++ b/prompt.ml @@ -64,7 +64,9 @@ module User_prompt = struct let text, position = Utils.render_cursor ~width:(max 0 (w - 3)) state.cursor in - Lwd.set cursor (position + 1, y); + let new_cursor = (position + 1, y) in + if new_cursor <> (Lwd.peek cursor) then + Lwd.set cursor new_cursor; I.hcat [ I.char A.empty ' ' 1 1 ; text ] end @@ -116,7 +118,10 @@ let make ~quit ~message cursor = let ( and+ ) = Lwd.map2 ~f:(fun x y -> (x, y)) in let state = Lwd.var (make quit message) in let position = Lwd.var (0, 0) in - let hook = Lwd.set state in + let hook state' = + if (Lwd.peek state).cursor != state'.cursor then + Lwd.set state state' + in let update_prompt state (y, w) = let user = User_prompt.render ~cursor ~y ~w state in Ui.keyboard_area (handler ~hook state) (Ui.atom user) diff --git a/window.ml b/window.ml index 29a7c1e..b7444cd 100644 --- a/window.ml +++ b/window.ml @@ -51,27 +51,22 @@ let render { w; h } msgs = done; Ui.atom (I.vsnap ~align:`Bottom h !image) -let handler ~hook:_ _state _buffer _key = `Unhandled - let make w = let ( let* ) x f = Lwd.bind ~f x in let ( let+ ) x f = Lwd.map ~f x in let ( and+ ) = Lwd.map2 ~f:(fun x y -> (x, y)) in - let state = Lwd.var { w = 0; h = 0; p = 0 } in - let hook = Lwd.set state in + let state = Lwd.var { w = 0; h = 0 } in let* document = let+ state = Lwd.get state and+ buffer = Lwd.get w in - Ui.keyboard_area - (handler ~hook state buffer) - (render state buffer) + render state buffer in let update_size ~w ~h = let state' = Lwd.peek state in - if state'.w <> w || state'.h <> h then Lwd.set state { state' with w; h } + if state'.w <> w || state'.h <> h then Lwd.set state { w; h } in let measure_size document =