Skip to content

Commit

Permalink
Make on_startup an action
Browse files Browse the repository at this point in the history
  • Loading branch information
Negabinary committed Sep 27, 2024
1 parent 2e2aa4c commit 6440099
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/haz3lweb/Keyboard.re
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/haz3lweb/Log.re
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ let is_action_logged: UpdateAction.t => bool =
| FinishImportAll(_)
| FinishImportScratchpad(_)
| Benchmark(_)
| DebugConsole(_) => false
| DebugConsole(_)
| Startup => false
| Reset
| TAB
| Set(_)
Expand Down
41 changes: 2 additions & 39 deletions src/haz3lweb/Main.re
Original file line number Diff line number Diff line change
Expand Up @@ -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
// <https://css-tricks.com/restart-css-animation/>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
};
};
Expand Down
38 changes: 38 additions & 0 deletions src/haz3lweb/Update.re
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) =>
Expand Down
10 changes: 7 additions & 3 deletions src/haz3lweb/UpdateAction.re
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -144,7 +145,8 @@ let is_edit: t => bool =
| DebugConsole(_)
| InitImportAll(_)
| InitImportScratchpad(_)
| Benchmark(_) => false;
| Benchmark(_)
| Startup => false;

let reevaluate_post_update: t => bool =
fun
Expand Down Expand Up @@ -197,7 +199,8 @@ let reevaluate_post_update: t => bool =
| SwitchDocumentationSlide(_)
| Reset
| Undo
| Redo => true;
| Redo
| Startup => true;

let should_scroll_to_caret =
fun
Expand Down Expand Up @@ -235,7 +238,8 @@ let should_scroll_to_caret =
| Reset
| Undo
| Redo
| TAB => true
| TAB
| Startup => true
| PerformAction(a) =>
switch (a) {
| Move(_)
Expand Down

0 comments on commit 6440099

Please sign in to comment.