Skip to content

Commit

Permalink
Selection in steppers
Browse files Browse the repository at this point in the history
  • Loading branch information
Negabinary committed Sep 6, 2024
1 parent ec8305f commit 55401fe
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 120 deletions.
25 changes: 13 additions & 12 deletions src/haz3lweb/app/editors/decoration/Deco.re
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,12 @@ module Deco =
Some((l, r));
};
};
PieceDec.next_step_indicated(
PieceDec.indicated(
~base_clss="tile-next-step",
~attr=[Virtual_dom.Vdom.Attr.on_mousedown(_ => {inject(i)})],
~line_clss=["next-step-line"],
~font_metrics,
~caret=(Id.invalid, 0),
~inject=() => inject(i),
~rows=measured.rows,
~tiles=[(id, mold, shards)],
)
Expand All @@ -516,14 +518,10 @@ module Deco =
|> List.flatten;
};

let taken_step = taken_step => {
let tiles =
List.filter_map(
TileMap.find_opt(_, tiles),
taken_step |> Option.to_list,
);
List.filter_map(
(t: Tile.t) => {
let taken_steps = taken_steps => {
let tiles = List.filter_map(TileMap.find_opt(_, tiles), taken_steps);
List.mapi(
(_, t: Tile.t) => {
let id = Tile.id(t);
let mold = t.mold;
let shards = Measured.find_shards(t, map);
Expand All @@ -538,16 +536,19 @@ module Deco =
Some((l, r));
};
};
PieceDec.taken_step_indicated(
PieceDec.indicated(
~base_clss="tile-taken-step",
~line_clss=["next-step-line"],
~font_metrics,
~caret=(Id.invalid, 0),
~tiles=[(id, mold, shards)],
~rows=measured.rows,
~tiles=[(id, mold, shards)],
)
|> Option.map(_, range);
},
tiles,
)
|> List.filter_map(x => x)
|> List.flatten;
};

Expand Down
109 changes: 34 additions & 75 deletions src/haz3lweb/app/editors/decoration/PieceDec.re
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,44 @@ let tips_of_shapes = ((l, r): (Nib.Shape.t, Nib.Shape.t)): (tip, tip) => (
Some(r),
);

let simple_shard_indicated = (shard_dims, ~sort: Sort.t, ~at_caret: bool): t =>
let simple_shard_indicated =
(
~attr=?,
~base_cls="indicated",
shard_dims,
~sort: Sort.t,
~at_caret: bool,
)
: t =>
simple_shard(
~attr?,
shard_dims,
["indicated", Sort.to_string(sort)] @ (at_caret ? ["caret"] : []),
[base_cls, Sort.to_string(sort)] @ (at_caret ? ["caret"] : []),
);

let simple_shards_indicated =
(~font_metrics: FontMetrics.t, ~caret: (Id.t, int), (id, mold, shards))
(
~attr: option(list(Attr.t))=?,
~base_cls=?,
~font_metrics: FontMetrics.t,
~caret: (Id.t, int),
(id, mold, shards),
)
: list(t) =>
List.map(
((index, measurement)) =>
((index, measurement)) => {
simple_shard_indicated(
~attr?,
~base_cls?,
{
font_metrics,
measurement,
tips: tips_of_shapes(Mold.nib_shapes(~index, mold)),
},
~sort=mold.out,
~at_caret=caret == (id, index),
),
)
},
shards,
);

Expand Down Expand Up @@ -99,62 +117,6 @@ let simple_shards_errors = (~font_metrics: FontMetrics.t, mold, shards) =>
shards,
);

let next_step_indicated =
(~inject, ~font_metrics, ~tips, ~measurement: Measured.measurement): t => {
let base_cls = ["tile-next-step"];
simple_shard(
~attr=[Attr.on_mousedown(_ => {inject()})],
{font_metrics, tips, measurement},
base_cls,
);
};

let next_step_shards_indicated =
(
~inject,
~font_metrics: FontMetrics.t,
~caret as _: (Id.t, int),
(_, mold, shards),
)
: list(t) =>
List.map(
((index, measurement)) =>
next_step_indicated(
~inject,
~font_metrics,
~tips=
Mold.nib_shapes(~index, mold)
|> (((x, y)) => (Some(x), Some(y))),
~measurement,
),
shards,
);

let taken_step_indicated =
(~font_metrics, ~tips, ~measurement: Measured.measurement): t => {
let base_cls = ["tile-taken-step"];
simple_shard({font_metrics, tips, measurement}, base_cls);
};

let taken_step_shards_indicated =
(
~font_metrics: FontMetrics.t,
~caret as _: (Id.t, int),
(_, mold, shards),
)
: list(t) =>
List.map(
((index, measurement)) =>
taken_step_indicated(
~font_metrics,
~tips=
Mold.nib_shapes(~index, mold)
|> (((x, y)) => (Some(x), Some(y))),
~measurement,
),
shards,
);

let shadowfudge = Path.cmdfudge(~y=DecUtil.shadow_adj);

let shards_of_tiles = tiles =>
Expand Down Expand Up @@ -385,28 +347,25 @@ let uni_lines =

let indicated =
(
~simple_shards,
~attr=?,
~font_metrics: FontMetrics.t,
~rows: Measured.Rows.t,
~caret,
~tiles,
~line_clss: list(string),
~base_clss=?,
range,
)
: list(Node.t) => {
List.concat_map(simple_shards(~font_metrics, ~caret), tiles)
List.concat_map(
simple_shards_indicated(
~attr?,
~font_metrics,
~caret,
~base_cls=?base_clss,
),
tiles,
)
@ uni_lines(~line_clss, ~font_metrics, ~rows, range, tiles)
@ bi_lines(~line_clss, ~font_metrics, ~rows, tiles);
};

let next_step_indicated = (~inject) =>
indicated(
~simple_shards=next_step_shards_indicated(~inject),
~line_clss=["next-step-line"],
);
let taken_step_indicated =
indicated(
~simple_shards=taken_step_shards_indicated,
~line_clss=["taken-step-line"],
);
let indicated = indicated(~simple_shards=simple_shards_indicated);
13 changes: 10 additions & 3 deletions src/haz3lweb/app/editors/result/EvalResult.re
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ module Update = {
| (ToggleStepper, {kind: Evaluation, _}) =>
{...model, kind: Stepper} |> Updated.return
| (StepperAction(a), {result: Stepper(s), _}) =>
let* stepper = Stepper.Update.update(a, s);
let* stepper = Stepper.Update.update(~settings, a, s);
{...model, result: Stepper(stepper)};
| (StepperAction(_), _) => model |> Updated.return_quiet
| (
Expand Down Expand Up @@ -230,7 +230,8 @@ module Selection = {
open Cursor;
[@deriving (show({with_path: false}), sexp, yojson)]
type t =
| Evaluation(CodeSelectable.Selection.t);
| Evaluation(CodeSelectable.Selection.t)
| Stepper(Stepper.Selection.t);
// TODO: Selection in stepper

let get_cursor_info = (~selection: t, mr: Model.t): cursor(Update.t) =>
Expand Down Expand Up @@ -374,10 +375,16 @@ module View = {
| Stepper(s) =>
Stepper.View.view(
~globals,
~selection=
switch (selected) {
| Some(Stepper(s)) => Some(s)
| _ => None
},
~signal=
fun
| HideStepper => inject(ToggleStepper)
| JumpTo(id) => signal(JumpTo(id)),
| JumpTo(id) => signal(JumpTo(id))
| MakeActive(s) => signal(MakeActive(Stepper(s))),
~inject=x => inject(StepperAction(x)),
~read_only=locked,
s,
Expand Down
64 changes: 57 additions & 7 deletions src/haz3lweb/app/editors/result/Stepper.re
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ module Model = {
module Update = {
[@deriving (show({with_path: false}), sexp, yojson)]
type t =
| StepperEditor(int, StepperEditor.Update.t)
| StepForward(int)
| StepBackward;

let update = (action: t, model: Model.t): Updated.t(Model.t) => {
let update = (~settings, action: t, model: Model.t): Updated.t(Model.t) => {
switch (action) {
| StepForward(idx) =>
{
Expand Down Expand Up @@ -117,6 +118,29 @@ module Update = {
},
}
|> Updated.return
| StepperEditor(idx, x) =>
{
...model,
history:
model.history
|> Aba.get_as
|> List.mapi((i, a: Model.a) =>
if (i == idx) {
switch (a) {
| A(a) =>
let editor =
CodeSelectable.Update.update(~settings, x, a.editor)
|> ((u: Updated.t('a)) => u.model);
Model.A({...a, editor});
| PendingStep => PendingStep
};
} else {
a;
}
)
|> Aba.mk(_, model.history |> Aba.get_bs),
}
|> Updated.return(~is_edit=false)
};
};

Expand Down Expand Up @@ -272,19 +296,27 @@ module Update = {
};
};

module Selection = {
[@deriving (show({with_path: false}), sexp, yojson)]
type t =
| A(int, StepperEditor.Selection.t);
};

module View = {
open Virtual_dom.Vdom;
open Node;

type event =
| HideStepper
| JumpTo(Haz3lcore.Id.t);
| JumpTo(Haz3lcore.Id.t)
| MakeActive(Selection.t);

let view =
(
~globals as {settings, inject_global, _} as globals: Globals.t,
~signal: event => Ui_effect.t(unit),
~inject: Update.t => Ui_effect.t(unit),
~selection: option(Selection.t),
~read_only: bool,
stepper: Model.t,
) => {
Expand Down Expand Up @@ -319,7 +351,7 @@ module View = {
settings.core.evaluation.show_hidden_steps
? x => x : List.filter(((_, b: Model.b, _)) => !b.hidden)
)
|> List.map(((_, b: Model.b, a: Model.a)) =>
|> List.mapi((i, (_, b: Model.b, a: Model.a)) =>
switch (a) {
| A(a) => [
div(
Expand All @@ -331,12 +363,21 @@ module View = {
],
[
div(~attrs=[Attr.class_("equiv")], [Node.text("≡")]),
StepperEditor.Stepped.view(
StepperEditor.View.view(
~globals,
~overlays=[],
~selected=selection == Some(A(i, ())),
~inject=
(x: StepperEditor.Update.t) =>
inject(StepperEditor(i, x)),
~signal=
fun
| TakeStep(_) => Ui_effect.Ignore
| MakeActive => signal(MakeActive(A(i, ()))),
{
editor: a.editor,
step_id: Some(b.step.d_loc |> Exp.rep_id),
next_steps: [],
taken_steps: [b.step.d_loc |> Exp.rep_id],
},
)
|> (x => [x])
Expand All @@ -362,15 +403,23 @@ module View = {
};
let current_step = {
let model = stepper.history |> Aba.hd;
let current_n = stepper.history |> Aba.get_bs |> List.length;
div(
~attrs=[Attr.classes(["cell-item", "cell-result"])],
(
switch (model) {
| A(model) => [
div(~attrs=[Attr.class_("equiv")], [Node.text("≡")]),
StepperEditor.Steppable.view(
StepperEditor.View.view(
~globals,
~signal=(TakeStep(x)) => inject(Update.StepForward(x)),
~selected=selection == Some(A(current_n, ())),
~inject=
(x: StepperEditor.Update.t) =>
inject(StepperEditor(current_n, x)),
~signal=
fun
| TakeStep(x) => inject(Update.StepForward(x))
| MakeActive => signal(MakeActive(A(current_n, ()))),
~overlays=[],
{
editor: model.editor,
Expand All @@ -379,6 +428,7 @@ module View = {
(option: Model.b) => option.step.d_loc |> Exp.rep_id,
model.next_steps,
),
taken_steps: [],
},
)
|> (x => [x])
Expand Down
Loading

0 comments on commit 55401fe

Please sign in to comment.