Skip to content

Commit

Permalink
Factor out globals
Browse files Browse the repository at this point in the history
  • Loading branch information
Negabinary committed May 22, 2024
1 parent 60df359 commit 7d3ff12
Show file tree
Hide file tree
Showing 32 changed files with 590 additions and 655 deletions.
3 changes: 3 additions & 0 deletions src/haz3lweb/ColorSteps.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
open Sexplib.Std;

[@deriving (show({with_path: false}), sexp, yojson)]
type colorMap = Haz3lcore.Id.Map.t(string);

/*[@deriving sexp]*/
Expand Down
2 changes: 1 addition & 1 deletion src/haz3lweb/DebugConsole.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open Haz3lcore;

let print =
(
{settings, editors, ui_state: {active_editor, _}, results, _}: Model.t,
{globals: {settings, _}, editors, active_editor, results, _}: Model.t,
key: string,
)
: unit => {
Expand Down
15 changes: 12 additions & 3 deletions src/haz3lweb/Grading.re
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ module TestValidationReport = {
report.test_results
|> Option.map(test_results =>
TestView.test_bar(
~inject,
~inject_jump=
TestView.jump_to_test(
~inject,
YourTestsValidation,
_,
(),
),
~test_results,
YourTestsValidation,
)
),
),
Expand Down Expand Up @@ -444,7 +449,11 @@ module ImplGradingReport = {
@ Option.to_list(
report.test_results
|> Option.map(test_results =>
TestView.test_bar(~inject, ~test_results, HiddenTests)
TestView.test_bar(
~inject_jump=
TestView.jump_to_test(~inject, HiddenTests, _, ()),
~test_results,
)
),
),
),
Expand Down
4 changes: 2 additions & 2 deletions src/haz3lweb/Keyboard.re
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let handle_key_event = (k: Key.t): option(Update.t) => {
keydown, making an update here may trigger an entire
extra redraw, contingent on model.cutoff */
switch (key) {
| "Alt" => Some(SetMeta(ShowBackpackTargets(false)))
| "Alt" => Some(Globals(SetShowBackpackTargets(false)))
| _ => None
}
| {key: D(key), sys: _, shift: Down, meta: Up, ctrl: Up, alt: Up}
Expand Down Expand Up @@ -111,7 +111,7 @@ let handle_key_event = (k: Key.t): option(Update.t) => {
switch (sys, key) {
| (_, "ArrowLeft") => now(MoveToBackpackTarget(Left(ByToken)))
| (_, "ArrowRight") => now(MoveToBackpackTarget(Right(ByToken)))
| (_, "Alt") => Some(SetMeta(ShowBackpackTargets(true)))
| (_, "Alt") => Some(Globals(SetShowBackpackTargets(true)))
| (_, "ArrowUp") => now(MoveToBackpackTarget(Up))
| (_, "ArrowDown") => now(MoveToBackpackTarget(Down))
| _ => None
Expand Down
7 changes: 5 additions & 2 deletions src/haz3lweb/Log.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ open Sexplib.Std;

let is_action_logged: UpdateAction.t => bool =
fun
| SetMeta(_)
| Globals(
SetMousedown(_) | SetShowBackpackTargets(_) | SetFontMetrics(_) |
JumpToTile(_),
)
| Save
| InitImportAll(_)
| InitImportScratchpad(_)
Expand All @@ -18,7 +21,7 @@ let is_action_logged: UpdateAction.t => bool =
| Reset
| TAB
| Assistant(_)
| Set(_)
| Globals(Set(_))
| SwitchScratchSlide(_)
| SwitchDocumentationSlide(_)
| MakeActive(_)
Expand Down
4 changes: 2 additions & 2 deletions src/haz3lweb/Main.re
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module App = {
let on_startup = (~schedule_action, m: Model.t) => {
let _ =
observe_font_specimen("font-specimen", fm =>
schedule_action(Haz3lweb.Update.SetMeta(FontMetrics(fm)))
schedule_action(Haz3lweb.Update.Globals(SetFontMetrics(fm)))
);

JsUtil.focus_clipboard_shim();
Expand Down Expand Up @@ -138,6 +138,6 @@ switch (JsUtil.Fragment.get_current()) {
(module App),
~debug=false,
~bind_to_element_with_id="container",
~initial_model=Model.load(Model.blank),
~initial_model=Model.load(),
)
};
60 changes: 18 additions & 42 deletions src/haz3lweb/Model.re
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,17 @@ open Haz3lcore;
[@deriving (show({with_path: false}), yojson, sexp)]
type timestamp = float;

/* Non-persistent application state */
[@deriving (show({with_path: false}), yojson, sexp)]
type ui_state = {
font_metrics: FontMetrics.t,
show_backpack_targets: bool,
mousedown: bool,
active_editor: option(Editors.Selection.t),
};

let ui_state_init = {
font_metrics: FontMetrics.init,
show_backpack_targets: false,
mousedown: false,
active_editor: None,
};

type t = {
editors: Editors.t,
settings: Settings.t,
results: ModelResults.t,
statics: CachedStatics.t,
explainThisModel: ExplainThisModel.t,
ui_state,
globals: Globals.t,
active_editor: option(Editors.Selection.t),
};

let cutoff = (===);

let mk = (editors, results, statics) => {
editors,
settings: Init.startup.settings,
results,
statics,
explainThisModel: ExplainThisModel.init,
ui_state: ui_state_init,
};

let blank =
mk(Editors.Scratch(0, []), ModelResults.empty, CachedStatics.empty);

let load_editors = (~settings: Settings.t): (Editors.t, ModelResults.t) =>
switch (settings.mode) {
| Scratch =>
Expand Down Expand Up @@ -86,19 +58,23 @@ let save_editors =
Store.Exercise.save((n, specs, exercise), ~instructor_mode)
};

let load = (init_model: t): t => {
let settings = Store.Settings.load();
let explainThisModel = Store.ExplainThisModel.load();
let load = (): t => {
let globals = Globals.Model.load();
let settings = globals.settings;
let (editors, results) = load_editors(~settings);
let ui_state = init_model.ui_state;
let statics = Editors.mk_statics(~settings, editors);
{editors, settings, results, statics, explainThisModel, ui_state};
let explainThisModel = Store.ExplainThisModel.load();
{editors, results, statics, globals, explainThisModel, active_editor: None};
};

let save = ({editors, settings, explainThisModel, results, _}: t) => {
save_editors(editors, results, ~instructor_mode=settings.instructor_mode);
let save = ({editors, globals, results, explainThisModel, _}: t) => {
save_editors(
editors,
results,
~instructor_mode=globals.settings.instructor_mode,
);
Globals.Model.save(globals);
Store.ExplainThisModel.save(explainThisModel);
Store.Settings.save(settings);
};

let save_and_return = (model: t) => {
Expand All @@ -114,12 +90,12 @@ let reset = (model: t): t => {
ignore(Store.Scratch.init(~settings));
ignore(Store.Documentation.init(~settings));
ignore(Store.Exercise.init(~instructor_mode=true));
let new_model = load(blank);
let new_model = load();
{
...new_model,
ui_state: {
...model.ui_state,
font_metrics: model.ui_state.font_metrics,
globals: {
...model.globals,
font_metrics: model.globals.font_metrics,
},
};
};
35 changes: 0 additions & 35 deletions src/haz3lweb/Settings.re

This file was deleted.

2 changes: 1 addition & 1 deletion src/haz3lweb/Store.re
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Settings = {
data
|> Sexplib.Sexp.of_string
|> Settings.t_of_sexp
|> Settings.fix_instructor_mode
|> Settings.Model.fix_instructor_mode
) {
| _ =>
print_endline("Could not deserialize settings.");
Expand Down
Loading

0 comments on commit 7d3ff12

Please sign in to comment.