Skip to content

Commit 03669eb

Browse files
committed
feat(ui): better string input dialog
- optionally allow cancelling (for partition view) - focus the input field on display - validate on "enter" keypress
1 parent 1584a08 commit 03669eb

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/app/learnocaml_common.ml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,25 @@ let confirm ~title ?(ok_label=[%i"OK"]) ?(cancel_label=[%i"Cancel"]) contents f
143143
close_button cancel_label;
144144
]
145145

146-
let ask_string ~title ?(ok_label=[%i"OK"]) contents =
146+
let ask_string ~title ?(ok_label=[%i"OK"]) ?(may_cancel=true) contents =
147147
let input_field =
148148
H.input ~a:[
149149
H.a_input_type `Text;
150150
] ()
151151
in
152152
let result_t, up = Lwt.wait () in
153-
ext_alert ~title (contents @ [input_field]) ~buttons:[
154-
box_button ok_label (fun () -> Lwt.wakeup up @@ Manip.value input_field)
155-
];
153+
let validate _ =
154+
Lwt.wakeup up @@ Manip.value input_field
155+
in
156+
Manip.Ev.onreturn input_field validate;
157+
let buttons =
158+
box_button ok_label validate
159+
:: (if may_cancel
160+
then [close_button [%i"Cancel"]]
161+
else [])
162+
in
163+
ext_alert ~title (contents @ [input_field]) ~buttons;
164+
Manip.focus input_field;
156165
result_t
157166

158167
let default_exn_printer = function
@@ -1157,7 +1166,7 @@ let get_token ?(has_server = true) () =
11571166
Lwt.return
11581167
with
11591168
Not_found ->
1160-
ask_string ~title:"Token"
1169+
ask_string ~title:"Token" ~may_cancel:false
11611170
[H.txt [%i"Enter your token"]]
11621171
>>= fun input_tok ->
11631172
let token = Token.parse (input_tok) in

src/app/learnocaml_common.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ val confirm :
4545
val ask_string :
4646
title: string ->
4747
?ok_label: string ->
48+
?may_cancel: bool ->
4849
[< Html_types.div_content > `Input] Tyxml_js.Html.elt list ->
4950
string Lwt.t
5051

src/app/learnocaml_teacher_tab.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ let rec teacher_tab token _select _params () =
211211
in
212212
let open_partition_ () =
213213
Lwt.async (fun () ->
214-
ask_string ~title:"Choose a function name"
214+
ask_string ~title:"Partitioning of student solutions"
215215
[H.txt @@ "Choose a function name to partition codes from "^ id ^": "]
216216
>|= fun funname ->
217217
let _win =

0 commit comments

Comments
 (0)