From 644009954b82e5b1be5d8a19e3fff6112a755d8f Mon Sep 17 00:00:00 2001 From: Matt Keenan Date: Fri, 27 Sep 2024 11:58:15 -0400 Subject: [PATCH] Make on_startup an action --- src/haz3lweb/Keyboard.re | 2 +- src/haz3lweb/Log.re | 3 ++- src/haz3lweb/Main.re | 41 ++---------------------------------- src/haz3lweb/Update.re | 38 +++++++++++++++++++++++++++++++++ src/haz3lweb/UpdateAction.re | 10 ++++++--- 5 files changed, 50 insertions(+), 44 deletions(-) diff --git a/src/haz3lweb/Keyboard.re b/src/haz3lweb/Keyboard.re index f0501a66a4..7e4e655f26 100644 --- a/src/haz3lweb/Keyboard.re +++ b/src/haz3lweb/Keyboard.re @@ -227,7 +227,7 @@ let shortcuts = (sys: Key.sys): list(shortcut) => ] @ (if (ExerciseSettings.show_instructor) {instructor_shortcuts} else {[]}); -let handle_key_event = (k: Key.t): option(Update.t) => { +let handle_key_event = (k: Key.t): option(UpdateAction.t) => { let now = (a: Action.t): option(UpdateAction.t) => Some(PerformAction(a)); switch (k) { diff --git a/src/haz3lweb/Log.re b/src/haz3lweb/Log.re index 4d98345dab..7c87a640f6 100644 --- a/src/haz3lweb/Log.re +++ b/src/haz3lweb/Log.re @@ -12,7 +12,8 @@ let is_action_logged: UpdateAction.t => bool = | FinishImportAll(_) | FinishImportScratchpad(_) | Benchmark(_) - | DebugConsole(_) => false + | DebugConsole(_) + | Startup => false | Reset | TAB | Set(_) diff --git a/src/haz3lweb/Main.re b/src/haz3lweb/Main.re index 0bfbd505ae..653a709923 100644 --- a/src/haz3lweb/Main.re +++ b/src/haz3lweb/Main.re @@ -6,23 +6,6 @@ let scroll_to_caret = ref(true); let edit_action_applied = ref(true); let last_edit_action = ref(JsUtil.timestamp()); -let observe_font_specimen = (id, update) => - ResizeObserver.observe( - ~node=JsUtil.get_elem_by_id(id), - ~f= - (entries, _) => { - let specimen = Js.to_array(entries)[0]; - let rect = specimen##.contentRect; - update( - Haz3lweb.FontMetrics.{ - row_height: rect##.bottom -. rect##.top, - col_width: rect##.right -. rect##.left, - }, - ); - }, - (), - ); - let restart_caret_animation = () => // necessary to trigger reflow // @@ -69,26 +52,6 @@ let apply = (model, action, ~schedule_action): Model.t => { }; }; -let on_startup = - (~inject: UpdateAction.t => Ui_effect.t(unit), m: Model.t) - : Ui_effect.t(unit) => { - let _ = - observe_font_specimen("font-specimen", fm => - schedule_action(Haz3lweb.Update.SetMeta(FontMetrics(fm))) - ); - NinjaKeys.initialize(NinjaKeys.options(schedule_action)); - JsUtil.focus_clipboard_shim(); - /* initialize state. */ - /* Initial evaluation on a worker */ - Update.schedule_evaluation(~schedule_action, m); - Os.is_mac := - Dom_html.window##.navigator##.platform##toUpperCase##indexOf( - Js.string("MAC"), - ) - >= 0; - Ui_effect.Ignore; -}; - module App = { module Model = Model; module Action = Update; @@ -146,9 +109,9 @@ let view = { let%sub after_display = { switch%sub (startup_completed) { | {state: false, set_state, _} => - let%arr (model, inject) = app + let%arr (_model, inject) = app and set_state = set_state; - Bonsai.Effect.Many([on_startup(~inject, model), set_state(true)]); + Bonsai.Effect.Many([set_state(true), inject(Update.Startup)]); | {state: true, _} => Bonsai.Computation.return(Ui_effect.Ignore) }; }; diff --git a/src/haz3lweb/Update.re b/src/haz3lweb/Update.re index de814c4561..26324a9c2e 100644 --- a/src/haz3lweb/Update.re +++ b/src/haz3lweb/Update.re @@ -1,8 +1,26 @@ open Util; +open Js_of_ocaml; open Haz3lcore; include UpdateAction; // to prevent circularity +let observe_font_specimen = (id, update) => + ResizeObserver.observe( + ~node=JsUtil.get_elem_by_id(id), + ~f= + (entries, _) => { + let specimen = Js.to_array(entries)[0]; + let rect = specimen##.contentRect; + update( + FontMetrics.{ + row_height: rect##.bottom -. rect##.top, + col_width: rect##.right -. rect##.left, + }, + ); + }, + (), + ); + let update_settings = (a: settings_action, {settings, _} as model: Model.t): Model.t => switch (a) { @@ -213,6 +231,25 @@ let schedule_evaluation = (~schedule_action, model: Model.t): unit => }; }; +let on_startup = + (~schedule_action: UpdateAction.t => unit, m: Model.t): Model.t => { + let _ = + observe_font_specimen("font-specimen", fm => + schedule_action(UpdateAction.SetMeta(FontMetrics(fm))) + ); + NinjaKeys.initialize(NinjaKeys.options(schedule_action)); + JsUtil.focus_clipboard_shim(); + /* initialize state. */ + /* Initial evaluation on a worker */ + schedule_evaluation(~schedule_action, m); + Os.is_mac := + Dom_html.window##.navigator##.platform##toUpperCase##indexOf( + Js.string("MAC"), + ) + >= 0; + m; +}; + let update_cached_data = (~schedule_action, update, m: Model.t): Model.t => { let update_dynamics = reevaluate_post_update(update); /* If we switch editors, or change settings which require statics @@ -373,6 +410,7 @@ let apply = }; let m: Result.t(Model.t) = switch (update) { + | Startup => Ok(on_startup(~schedule_action, model)) | Reset => Ok(Model.reset(model)) | Set(Evaluation(_) as s_action) => Ok(update_settings(s_action, model)) | Set(s_action) => diff --git a/src/haz3lweb/UpdateAction.re b/src/haz3lweb/UpdateAction.re index b67861090f..cd2f145f3e 100644 --- a/src/haz3lweb/UpdateAction.re +++ b/src/haz3lweb/UpdateAction.re @@ -57,6 +57,7 @@ type export_action = [@deriving (show({with_path: false}), sexp, yojson)] type t = /* meta */ + | Startup | Reset | Set(settings_action) | SetMeta(set_meta) @@ -144,7 +145,8 @@ let is_edit: t => bool = | DebugConsole(_) | InitImportAll(_) | InitImportScratchpad(_) - | Benchmark(_) => false; + | Benchmark(_) + | Startup => false; let reevaluate_post_update: t => bool = fun @@ -197,7 +199,8 @@ let reevaluate_post_update: t => bool = | SwitchDocumentationSlide(_) | Reset | Undo - | Redo => true; + | Redo + | Startup => true; let should_scroll_to_caret = fun @@ -235,7 +238,8 @@ let should_scroll_to_caret = | Reset | Undo | Redo - | TAB => true + | TAB + | Startup => true | PerformAction(a) => switch (a) { | Move(_)