Skip to content

Commit

Permalink
GUI: remove refresh_file reactive event
Browse files Browse the repository at this point in the history
  • Loading branch information
pirbo committed Feb 26, 2024
1 parent 8e3233f commit ff5d137
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
6 changes: 4 additions & 2 deletions gui/state_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ let model, set_directory_state = React.S.create blank_state

type refresh = { filename: string; content: string; line: int option }

let refresh_file, set_refresh_file = React.E.create ()
let refresh_file_hook = ref []

let register_refresh_file_hook x = refresh_file_hook := x :: !refresh_file_hook

let current_filename =
React.S.map
Expand Down Expand Up @@ -85,7 +87,7 @@ let send_refresh (line : int option) : unit Api.result Lwt.t =
get_file ()
>>= Api_common.result_bind_lwt ~ok:(fun (content, filename) ->
let () = Common.debug content in
let () = set_refresh_file { filename; content; line } in
let () = List.iter (fun f -> f { filename; content; line }) !refresh_file_hook in
Lwt.return (Result_util.ok ()))

let update_directory ~reset current catalog =
Expand Down
2 changes: 1 addition & 1 deletion gui/state_file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ val remove_file : unit -> unit Api.result Lwt.t
*)
type refresh = { filename: string; content: string; line: int option }

val refresh_file : refresh React.event
val register_refresh_file_hook : (refresh -> unit) -> unit
(* Meta data of current file *)

val cursor_activity : line:int -> ch:int -> unit
Expand Down
42 changes: 20 additions & 22 deletions gui/subpanel_editor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ module Html = Tyxml_js.Html5

let editor_full, set_editor_full = React.S.create (false : bool)
let filename, set_filename = React.S.create (None : string option)
let move_cursor, set_move_cursor = React.E.create ()

let move_cursor_hook = ref None

let set_move_cursor loc = Option.iter (fun f -> f loc) !move_cursor_hook

let file_label =
Tyxml_js.R.Html.txt
Expand Down Expand Up @@ -128,7 +131,6 @@ let jump_to_line (codemirror : codemirror Js.t) (line : int) : unit =
let () = codemirror##scrollTo Js.null (Js.some scrollLine) in
()

let dont_gc_me_events = ref []
let dont_gc_me_signals = ref []

let onload () : unit =
Expand Down Expand Up @@ -158,7 +160,7 @@ let onload () : unit =
Codemirror.fromTextArea textarea configuration
in
let () = codemirror##setValue (Js.string "") in
let _ =
let () =
Subpanel_editor_controller.with_file
(Result_util.fold
~ok:(fun (content, id) ->
Expand Down Expand Up @@ -239,23 +241,7 @@ let onload () : unit =
]
in
let () =
dont_gc_me_events :=
[
React.E.map
(fun pos ->
if Some pos.Loc.file = React.S.value filename then (
let beg = pos.Loc.from_position in
let first =
new%js Codemirror.position (beg.Loc.line - 1) beg.Loc.chr
in
let en = pos.Loc.from_position in
let last =
new%js Codemirror.position (en.Loc.line - 1) en.Loc.chr
in
codemirror##setSelection first last
))
move_cursor;
React.E.map
State_file.register_refresh_file_hook
(fun refresh ->
let () = set_filename (Some refresh.State_file.filename) in
let cand = Js.string refresh.State_file.content in
Expand All @@ -267,9 +253,21 @@ let onload () : unit =
| Some line -> jump_to_line codemirror line
in
()
)) in
let () =
move_cursor_hook := Some
(fun pos ->
if Some pos.Loc.file = React.S.value filename then (
let beg = pos.Loc.from_position in
let first =
new%js Codemirror.position (beg.Loc.line - 1) beg.Loc.chr
in
let en = pos.Loc.from_position in
let last =
new%js Codemirror.position (en.Loc.line - 1) en.Loc.chr
in
codemirror##setSelection first last
))
State_file.refresh_file;
]
in
()

Expand Down
2 changes: 1 addition & 1 deletion gui/subpanel_editor.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
(******************************************************************************)

val editor_full : bool React.signal
val set_move_cursor : ?step:React.step -> Loc.t -> unit
val set_move_cursor : Loc.t -> unit

include Ui_common.Panel

0 comments on commit ff5d137

Please sign in to comment.