GUI: edit layouts, copy a layout, and start with saved files present

- The Layout tab edits the chosen layout: its three rows as text, or
  two keys swapped by clicking them on the board. Copy adds a copy to
  edit; built-in layouts cannot be edited.
- An edit that puts a character on two keys, or leaves a character of
  the texts on no key, is rejected and reported.
- The initial model is a function called from main. As a module-level
  value it read saved-layouts.txt, params.txt and presets/ where no
  scope is open, and the GUI did not start when one of them existed.
- The test covers the editor and runs with saved layouts present. It
  writes saved-layouts.txt back as it was.
- New screenshots, and a README section on editing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-11-17 12:03:16 +01:00
parent 6e6afad071
commit 1da78595bd
8 changed files with 297 additions and 45 deletions

View file

@ -89,6 +89,20 @@ The chosen layout and each part of its score, for each text and as the mean.
You can save a layout under a name, or remove a layout that is not built in. You can save a layout under a name, or remove a layout that is not built in.
Saved layouts are kept in `saved-layouts.txt` and are listed at start. Saved layouts are kept in `saved-layouts.txt` and are listed at start.
To make a layout of your own, choose one and click **Copy**. Then change the
copy:
- Type in the three row fields. They use the same format as
`src/layouts.bjo`.
- Click two keys on the board to swap them.
The scores change at once. A built-in layout cannot be changed, and the
original of a copy does not change. An edit is rejected if a character is on
two keys, or if a character of the texts is on no key. A changed layout is
saved to the file only when you click **Save layout**.
![A copy of ZYK,Å with Y and Z swapped](docs/editing.png)
### Search ### Search
Runs the optimizer from the chosen layout with the current numbers. For each Runs the optimizer from the chosen layout with the current numbers. For each

BIN
docs/editing.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 358 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 182 KiB

After

Width:  |  Height:  |  Size: 196 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 334 KiB

After

Width:  |  Height:  |  Size: 342 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 254 KiB

After

Width:  |  Height:  |  Size: 254 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 273 KiB

After

Width:  |  Height:  |  Size: 286 KiB

Before After
Before After

View file

@ -18,6 +18,10 @@
;; (keyscore saved). ;; (keyscore saved).
;; - Layouts can be saved by name, from the Layout tab or from a search ;; - Layouts can be saved by name, from the Layout tab or from a search
;; result, to saved-layouts.txt. The saved layouts are listed at start. ;; result, to saved-layouts.txt. The saved layouts are listed at start.
;; - The Layout tab edits the chosen layout: its rows as text, or two keys
;; swapped by clicking them on the board. A built-in layout cannot be
;; edited; Copy adds a copy to edit. An edit that leaves a counted
;; character with no key, or puts one on two keys, is rejected.
;; - An "i" mark shows what a number or an option does. ;; - An "i" mark shows what a number or an option does.
(import (std prelude)) (import (std prelude))
@ -33,7 +37,7 @@
(import (keyscore search)) (import (keyscore search))
(import (keyscore texts)) (import (keyscore texts))
(export Model Msg initial gui-update view subscriptions ranking) (export Model Msg initial gui-update view subscriptions ranking mean-of)
;; --- the model ------------------------------------------------------------------ ;; --- the model ------------------------------------------------------------------
@ -48,6 +52,9 @@
;; - saved: the names of the layouts in `layouts-file`. ;; - saved: the names of the layouts in `layouts-file`.
;; - layout-name: the name a layout is saved as. ;; - layout-name: the name a layout is saved as.
;; - presets, preset-name: the presets, and the name one is saved as. ;; - presets, preset-name: the presets, and the name one is saved as.
;; - rows-text: the edit fields of the chosen layout's rows. They can hold an
;; edit that was rejected; the layout keeps its last valid rows.
;; - edited: the names of saved layouts changed since they were saved.
(type (: Model (Record (: params Params) (type (: Model (Record (: params Params)
(: counts (Vec Counts)) (: counts (Vec Counts))
(: layouts (Vec Layout)) (: layouts (Vec Layout))
@ -63,6 +70,8 @@
(: layout-name string) (: layout-name string)
(: presets (Vec string)) (: presets (Vec string))
(: preset-name string) (: preset-name string)
(: rows-text (Vec string))
(: edited (Vec string))
(: status string)))) (: status string))))
(type (: Msg (Union (: Counted (Vec Counts)) (type (: Msg (Union (: Counted (Vec Counts))
@ -96,18 +105,21 @@
(: SaveResult int) (: SaveResult int)
RemoveLayout RemoveLayout
(: Wrote string) (: Wrote string)
CopyLayout
(: SetRow int string)
(: SwapKeys int int)
Noop))) Noop)))
;; The layouts of `layouts-file` when the program starts. ;; The name of `l`.
(: (Vec Layout))
(def saved-at-start (read-saved-layouts))
(: (-> Layout string)) (: (-> Layout string))
(defun (lname l) (record-ref l name)) (defun (lname l) (record-ref l name))
;; The model at start: the numbers of `params-file`, the built-in and saved
(: Model) ;; layouts, and the presets. It reads files, so call it from `main` or from a
(def initial ;; function that main calls, not from a module-level `def`.
(: (-> Model))
(defun (initial)
(def saved-at-start (read-saved-layouts))
(Model (params (current-params)) (Model (params (current-params))
(counts []) (counts [])
(layouts (vec-append layouts saved-at-start)) (layouts (vec-append layouts saved-at-start))
@ -123,6 +135,8 @@
(layout-name "") (layout-name "")
(presets (preset-names)) (presets (preset-names))
(preset-name "") (preset-name "")
(rows-text (record-ref (vec-ref layouts (- (vec-length layouts) 1)) rows))
(edited [])
(status "Reading the texts…"))) (status "Reading the texts…")))
;; --- scoring -------------------------------------------------------------------- ;; --- scoring --------------------------------------------------------------------
@ -193,6 +207,14 @@
(: string) (: string)
(def bad-name "A name cannot be empty, start or end with a space, start with a dot, or hold / \\ or a tab.") (def bad-name "A name cannot be empty, start or end with a space, start with a dot, or hold / \\ or a tab.")
;; `m` with the layout `i` chosen: its rows in the edit fields, and, unless
;; it is built in, its name in the name field.
(: (-> Model int Model))
(defun (choose m i)
(def l (vec-ref (.layouts m) i))
(record-set m (chosen i) (rows-text (record-ref l rows))
(layout-name (if (< i (vec-length layouts)) (.layout-name m) (lname l)))))
;; `m` with `l` saved as `name`, and the commands that score the layouts and ;; `m` with `l` saved as `name`, and the commands that score the layouts and
;; write the file. ;; write the file.
;; ;;
@ -225,11 +247,65 @@
(def before (lname (vec-ref ls (.chosen m)))) (def before (lname (vec-ref ls (.chosen m))))
(def chosen (if choose? last (option-ref-or (index-named new-ls before) last))) (def chosen (if choose? last (option-ref-or (index-named new-ls before) last)))
(def saved (vec-append (vec-filter (fun (n) (not (or (= n name) (= (Some n) old)))) (.saved m)) [name])) (def saved (vec-append (vec-filter (fun (n) (not (or (= n name) (= (Some n) old)))) (.saved m)) [name]))
(and-then (rescore (record-set m (layouts new-ls) (saved saved) (chosen chosen) (scores []) (def edited (vec-filter (fun (n) (not (or (= n name) (= (Some n) old)))) (.edited m)))
(layout-name name)) (def picked (choose (record-set m (layouts new-ls) (saved saved) (edited edited) (scores [])) chosen))
(.params m)) (and-then (rescore (record-set picked (layout-name name)) (.params m))
(write-layouts new-ls saved (str "Saved the layout " name " to " layouts-file ".")))))) (write-layouts new-ls saved (str "Saved the layout " name " to " layouts-file "."))))))
;; The characters every layout must type: those the texts are counted for.
(: (List char))
(def counted-chars (common-chars layouts))
;; The layout named `name` with `rows`, or why it cannot be scored: a row
;; longer than the board, a character on two keys, or a counted character on
;; no key.
(: (-> string (Vec string) (Result string Layout)))
(defun (layout-from-rows name rows)
(match (try (make-layout name (vec-ref rows 0) (vec-ref rows 1) (vec-ref rows 2)) #:catch (System.Exception))
((Err _) (Err "A row has more characters than the board has keys."))
((Ok l)
(def base (layout-base-chars l))
(: twice? (-> char bool))
(def twice? (fun (c) (> (list-length (->list (filter (fun (d) (char=? d c)) base))) 1)))
(def twice (->list (map-keys (->map (map (fun (c) (Tuple c #t)) (filter twice? base))))))
;; A shifted character is missing only when its key's character is.
(def missing (->list (filter (fun (c) (and (none? (layout-key l c))
(not (string-contains? ";:_*>" (char->string c)))))
counted-chars)))
(cond ((not (= (list-length twice) 0)) (Err (str "On two keys: " (string-join (->vec (map char->string twice)) " "))))
((not (= (list-length missing) 0))
(Err (str "On no key: " (string-join (->vec (map char->string missing)) " ")
". Every character of the texts needs a key.")))
(else (Ok l))))))
;; The character written on the key at `at` in `board`, if `l` puts one there.
(: (-> Layout int (Option char)))
(defun (char-on-key l at)
(find (fun (c) (match (layout-key l c) ((Some k) (= (record-ref k index) at)) (None #f)))
(layout-base-chars l)))
;; A name for a copy of the layout `base` that no layout in `m` has.
(: (-> Model string string))
(defun (copy-name m base)
(let go ((n 1))
(def candidate (if (= n 1) (str base " copy") (str base " copy " (displayed n))))
(if (some? (index-named (.layouts m) candidate)) (go (+ n 1)) candidate)))
;; `m` with `l` in place of the chosen layout, and the scoring started. A
;; saved layout is marked as edited; the file changes on Save layout.
(: (-> Model Layout (Tuple Model (List (Event Msg)))))
(defun (apply-edit m l)
(def name (lname l))
(def edited (if (and (vec-contains? (.saved m) name) (not (vec-contains? (.edited m) name)))
(vec-add (.edited m) name)
(.edited m)))
(rescore (record-set m (layouts (vec-set (.layouts m) (.chosen m) l)) (rows-text (record-ref l rows))
(edited edited) (status ""))
(.params m)))
(: string)
(def built-in-edit "A built-in layout cannot be changed. Click Copy and change the copy.")
;; Whether `l` types a vowel on the right hand. ;; Whether `l` types a vowel on the right hand.
(: (-> Layout bool)) (: (-> Layout bool))
(defun (vowel-right? l) (defun (vowel-right? l)
@ -250,10 +326,7 @@
((Choose pos) ((Choose pos)
(def r (ranking m)) (def r (ranking m))
(if (and (>= pos 0) (< pos (vec-length r))) (if (and (>= pos 0) (< pos (vec-length r)))
(let ((i (vec-ref r pos))) (next (choose m (vec-ref r pos)))
(next (record-set m (chosen i)
(layout-name (if (< i (vec-length layouts)) (.layout-name m)
(lname (vec-ref (.layouts m) i)))))))
(next m))) (next m)))
((Tab t) (next (record-set m (tab t)))) ((Tab t) (next (record-set m (tab t))))
((SetRuns x) (next (record-set m (options (record-set o (runs (cast int x))))))) ((SetRuns x) (next (record-set m (options (record-set o (runs (cast int x)))))))
@ -338,13 +411,42 @@
(:when (not (= j i))) (:when (not (= j i)))
(:acc out (vecing x)))) (:acc out (vecing x))))
(def saved (vec-filter (fun (n) (not (= n name))) (.saved m))) (def saved (vec-filter (fun (n) (not (= n name))) (.saved m)))
(def step (rescore (record-set m (layouts new-ls) (saved saved) (chosen (- built 1)) (scores []) (def edited (vec-filter (fun (n) (not (= n name))) (.edited m)))
(status (str "Removed " name "."))) (def step (rescore (choose (record-set m (layouts new-ls) (saved saved) (edited edited) (scores [])
(status (str "Removed " name ".")))
(- built 1))
(.params m))) (.params m)))
(if (vec-contains? (.saved m) name) (if (vec-contains? (.saved m) name)
(and-then step (write-layouts new-ls saved (str "Removed " name " from " layouts-file "."))) (and-then step (write-layouts new-ls saved (str "Removed " name " from " layouts-file ".")))
step)))) step))))
((Wrote what) (next (record-set m (status what)))) ((Wrote what) (next (record-set m (status what))))
(CopyLayout
(def l (vec-ref (.layouts m) (.chosen m)))
(def name (copy-name m (lname l)))
(def ls (vec-append (.layouts m) [(record-set l (name name))]))
(rescore (choose (record-set m (layouts ls) (scores [])
(status (str "Copied " (lname l) " to " name ". The copy is not saved.")))
(- (vec-length ls) 1))
(.params m)))
((SetRow r t)
(def i (.chosen m))
(def m2 (record-set m (rows-text (vec-set (.rows-text m) r t))))
(if (< i (vec-length layouts))
(next (record-set m (status built-in-edit)))
(match (layout-from-rows (lname (vec-ref (.layouts m) i)) (.rows-text m2))
((Ok l) (apply-edit m2 l))
((Err why) (next (record-set m2 (status why)))))))
((SwapKeys a b)
(def i (.chosen m))
(def l (vec-ref (.layouts m) i))
(if (< i (vec-length layouts))
(next (record-set m (status built-in-edit)))
(match (Tuple (char-on-key l a) (char-on-key l b))
((Tuple (Some ca) (Some cb))
(def swapped (swap-keys l (lname l) ca cb))
(def rows (vec-map string-upcase (record-ref swapped rows)))
(apply-edit m (make-layout (lname l) (vec-ref rows 0) (vec-ref rows 1) (vec-ref rows 2))))
(_ (next (record-set m (status "A key that types nothing cannot be swapped.")))))))
(Noop (next m)))) (Noop (next m))))
(: subscriptions (-> Model (List (Sub Msg)))) (: subscriptions (-> Model (List (Sub Msg))))
@ -383,20 +485,61 @@
(: double) (: double)
(def board-height (* key-size 3.0)) (def board-height (* key-size 3.0))
;; The board, with a label on each key of `board`, in its order. A stretch ;; The drawn size of a key, in pixels.
;; key has a darker edge. (: double)
(def key-face (- key-size 4.0))
;; Draws the board, with a label on each key of `board`, in its order. A
;; stretch key has a darker edge.
(: (-> Pen (Vec string) void))
(defun (draw-board pen labels)
(loop (:for k board) (:for label labels)
(:let (Tuple x y) (key-place k))
(:do (fill-rect pen x y key-face key-face (finger-colour k) #:radius 5.0)
(when (record-ref k stretch?)
(stroke-rect pen x y key-face key-face "#888888" #:width 1.5 #:radius 5.0))
(draw-text pen label (+ x (/ key-face 2.0)) (+ y 7.0) "#202124"
#:size 17.0 #:bold #t #:align "centre"))))
(: keyboard (Component (Vec string) Unit Unit)) (: keyboard (Component (Vec string) Unit Unit))
(def keyboard (def keyboard
(component unit (component unit (fun (pen w h s labels) (draw-board pen labels))))
(fun (pen w h s labels)
(loop (:for k board) (:for label labels) ;; The index in `board` of the key drawn at `x`, `y`, if there is one.
(:let (Tuple x y) (key-place k)) (: (-> double double (Option int)))
(:let size (- key-size 4.0)) (defun (key-at x y)
(:do (fill-rect pen x y size size (finger-colour k) #:radius 5.0) (: on-key? (-> int bool))
(when (record-ref k stretch?) (def on-key? (fun (i)
(stroke-rect pen x y size size "#888888" #:width 1.5 #:radius 5.0)) (def (Tuple kx ky) (key-place (vec-ref board i)))
(draw-text pen label (+ x (/ size 2.0)) (+ y 7.0) "#202124" (and (>= x kx) (< x (+ kx key-face)) (>= y ky) (< y (+ ky key-face)))))
#:size 17.0 #:bold #t #:align "centre")))))) (find on-key? (range 0 (vec-length board))))
;; Draws a thick blue edge around the key at `i` in `board`.
(: (-> Pen int void))
(defun (draw-picked pen i)
(def (Tuple x y) (key-place (vec-ref board i)))
(stroke-rect pen x y key-face key-face "#1a73e8" #:width 3.0 #:radius 5.0))
;; The board for editing. Its state is the picked key, by index in `board`.
;;
;; - A click on a key picks it, and draws it with a blue edge.
;; - A click on a second key sends the two keys' indices, and drops the pick.
;; - A click on the picked key, or off the keys, drops the pick.
(: editable-keyboard (Component (Vec string) (Option int) (Tuple int int)))
(def editable-keyboard
(component None
(fun (pen w h picked labels)
(draw-board pen labels)
(match picked ((Some i) (draw-picked pen i)) (None unit)))
#:input (fun (in w h picked labels)
(match in
((PointerDown p)
(match (Tuple (key-at (record-ref p x) (record-ref p y)) picked)
((Tuple (Some i) (Some j)) (if (= i j) (Tuple None None) (Tuple None (Some (Tuple j i)))))
((Tuple (Some i) None) (Tuple (Some i) None))
(_ (Tuple None None))))
(_ (Tuple picked None))))
#:cursor (fun (s v) "Hand")))
;; The character `l` puts on each key of `board`, upper case, or "". ;; The character `l` puts on each key of `board`, upper case, or "".
(: (-> Layout (Vec string))) (: (-> Layout (Vec string)))
@ -476,7 +619,7 @@
(defun (scores-view m) (defun (scores-view m)
(def cs (record-ref (.params m) corpora)) (def cs (record-ref (.params m) corpora))
(def r (ranking m)) (def r (ranking m))
(def columns (string-join (vec-append ["90" "60"] (vec-map (fun (_) "60") cs)) ",")) (def columns (string-join (vec-append ["130" "60"] (vec-map (fun (_) "60") cs)) ","))
(: line (-> (Vec string) (View Msg))) (: line (-> (Vec string) (View Msg)))
(def line (fun (cells) (def line (fun (cells)
(apply grid #:columns columns (apply grid #:columns columns
@ -495,7 +638,7 @@
(column #:spacing 6.0 #:margin 8.0 (column #:spacing 6.0 #:margin 8.0
(text "Score per key press, lower is better" #:size 15.0) (text "Score per key press, lower is better" #:size 15.0)
(line (vec-append ["layout" "mean"] (vec-map (fun (c) (file-label (corpus-path c))) cs))) (line (vec-append ["layout" "mean"] (vec-map (fun (c) (file-label (corpus-path c))) cs)))
(list-box items selected Choose #:width 340.0))) (list-box items selected Choose #:width 380.0)))
;; The numbers of one group, each with its input. ;; The numbers of one group, each with its input.
(: (-> Model string (View Msg))) (: (-> Model string (View Msg)))
@ -594,13 +737,16 @@
"Point at a key to see its default cost. A key of the right pinky past P and Ö, and the centre columns, are stretch keys: the Stretch key number is added to pairs with them.") "Point at a key to see its default cost. A key of the right pinky past P and Ö, and the centre columns, are stretch keys: the Stretch key number is added to pairs with them.")
(board-row 0) (board-row 1) (board-row 2))) (board-row 0) (board-row 1) (board-row 2)))
;; What the chosen layout is: built in, saved, or kept and not saved. ;; What the chosen layout is: built in, saved, edited since it was saved, or
;; not saved.
(: (-> Model string)) (: (-> Model string))
(defun (kind-of-chosen m) (defun (kind-of-chosen m)
(def i (.chosen m)) (def i (.chosen m))
(def name (lname (vec-ref (.layouts m) i)))
(cond ((< i (vec-length layouts)) "built in") (cond ((< i (vec-length layouts)) "built in")
((vec-contains? (.saved m) (lname (vec-ref (.layouts m) i))) (str "saved in " layouts-file)) ((vec-contains? (.edited m) name) "changed, not saved")
(else "kept, not saved"))) ((vec-contains? (.saved m) name) (str "saved in " layouts-file))
(else "not saved")))
;; The name input and the Save layout and Remove buttons. ;; The name input and the Save layout and Remove buttons.
(: (-> Model (View Msg))) (: (-> Model (View Msg)))
@ -612,10 +758,33 @@
(tooltip (button "Save layout" SaveLayout) (tooltip (button "Save layout" SaveLayout)
(str "Saves the chosen layout under this name in " layouts-file ". A built-in layout is copied. " (str "Saves the chosen layout under this name in " layouts-file ". A built-in layout is copied. "
"A kept or saved layout gets the new name. A saved layout of the same name is replaced.")) "A kept or saved layout gets the new name. A saved layout of the same name is replaced."))
(tooltip (button "Copy" CopyLayout)
"Adds a copy of the chosen layout to the list and chooses it. Change the copy, and the original stays as it is. The copy is not saved until you click Save layout.")
(tooltip (button "Remove" RemoveLayout #:enabled (not built?)) (tooltip (button "Remove" RemoveLayout #:enabled (not built?))
(str "Removes the chosen layout from the list, and from " layouts-file " if it is saved there.")) (str "Removes the chosen layout from the list, and from " layouts-file " if it is saved there."))
(info (str "Saved layouts are kept in " layouts-file ", one a line, and are listed at start.")))) (info (str "Saved layouts are kept in " layouts-file ", one a line, and are listed at start."))))
;; The chosen layout's rows as edit fields, and the board to swap keys on.
(: (-> Model Layout (View Msg)))
(defun (edit-view m l)
(def built? (< (.chosen m) (vec-length layouts)))
(: field (-> int string string (View Msg)))
(def field (fun (r label first)
(row #:spacing 6.0
(text label #:width 90.0)
(text-input (vec-ref (.rows-text m) r) (fun (t) (SetRow r t)) #:enabled (not built?) #:width 260.0)
(info (str "The characters of the " (string-downcase label) ", from the " first " key. "
"Spaces are skipped, so you can split the row at the hands. "
"A row can be shorter than the board: its last keys type nothing.")))))
(column #:spacing 6.0
(text (if built? built-in-edit
"Change the rows, or click two keys on the board to swap them. The scores change at once."))
(field 0 "Top row" "Q")
(field 1 "Home row" "A")
(field 2 "Bottom row" "<")
(drawn editable-keyboard (labels-of l) (fun (pair) (SwapKeys (fst pair) (snd pair)))
#:width board-width #:height board-height)))
;; The chosen layout on the board, and the parts of its score. ;; The chosen layout on the board, and the parts of its score.
(: (-> Model (View Msg))) (: (-> Model (View Msg)))
(defun (layout-view m) (defun (layout-view m)
@ -657,9 +826,9 @@
(column #:spacing 10.0 #:margin 8.0 (column #:spacing 10.0 #:margin 8.0
heading heading
(save-layout-view m) (save-layout-view m)
(board-view (labels-of l)) (edit-view m l)
(table (string-join (vec-append ["Auto"] (vec-map (fun (_) "70") (vec-ref rows 0))) ",") rows)))) (table (string-join (vec-append ["Auto"] (vec-map (fun (_) "70") (vec-ref rows 0))) ",") rows))))
(column #:spacing 10.0 #:margin 8.0 heading (save-layout-view m) (board-view (labels-of l))))) (column #:spacing 10.0 #:margin 8.0 heading (save-layout-view m) (edit-view m l))))
(: (-> Model (View Msg))) (: (-> Model (View Msg)))
(defun (search-view m) (defun (search-view m)
@ -724,7 +893,7 @@
(tab "Search" (search-view m))))) (tab "Search" (search-view m)))))
(defun (main) (defun (main)
(run-app initial gui-update view (run-app (initial) gui-update view
#:subscriptions subscriptions #:subscriptions subscriptions
#:title "keyscore" #:width 1500.0 #:height 900.0) #:title "keyscore" #:width 1500.0 #:height 900.0)
0) 0)

View file

@ -11,6 +11,9 @@
;; presets.png, then deletes the preset. ;; presets.png, then deletes the preset.
;; - Saves a search result and the kept result as layouts, saves ;; - Saves a search result and the kept result as layouts, saves
;; layout-saved.png, and removes both again. ;; layout-saved.png, and removes both again.
;; - Copies ZYK,Å, edits a row of the copy, swaps two keys on its board,
;; and checks that bad rows and edits of a built-in layout are rejected.
;; Saves editing.png, then removes the copy.
;; - Leaves no presets folder or saved-layouts.txt that was not there before. ;; - Leaves no presets folder or saved-layouts.txt that was not there before.
(import (std prelude)) (import (std prelude))
@ -20,6 +23,7 @@
(import (keyscore layouts)) (import (keyscore layouts))
(import (keyscore params)) (import (keyscore params))
(import (keyscore saved)) (import (keyscore saved))
(import (keyscore score))
(import/class (import/class
(Object (: System.Object)) (Object (: System.Object))
@ -63,12 +67,29 @@
(: (-> Layout string)) (: (-> Layout string))
(defun (layout-name-of l) (record-ref l name)) (defun (layout-name-of l) (record-ref l name))
;; The chosen layout's mean score.
(: (-> Model double))
(defun (mean-total m) (mean-of m (.chosen m) (fun (s) (record-ref s total))))
;; The rows of the layout named `n`.
(: (-> Model string (Vec string)))
(defun (rows-named m n)
(match (find (fun (l) (= (layout-name-of l) n)) (.layouts m))
((Some l) (record-ref l rows))
(None (panic! (str "no layout is named " n)))))
;; The rows of the chosen layout.
(: (-> Model (Vec string)))
(defun (chosen-rows m) (record-ref (vec-ref (.layouts m) (.chosen m)) rows))
(defun (main args) (defun (main args)
(def folder (if (> (vec-length args) 0) (vec-ref args 0) ".")) (def folder (if (> (vec-length args) 0) (vec-ref args 0) "."))
(start-headless!) (start-headless!)
;; With no window there is no theme, and a control with a template draws nothing. ;; With no window there is no theme, and a control with a template draws nothing.
(styles-add! (app-styles (cast Application current-app)) (fluent-theme)) (styles-add! (app-styles (cast Application current-app)) (fluent-theme))
(def app (start-app initial gui-update view (fun (c) unit) #:subscriptions subscriptions)) ;; What the saved layouts file holds, written back at the end.
(def layouts-before (if (file-exists? layouts-file) (Some (file-read-text layouts-file)) None))
(def app (start-app (initial) gui-update view (fun (c) unit) #:subscriptions subscriptions))
;; Runs what the fibers posted until `done?` holds, for two minutes at most. ;; Runs what the fibers posted until `done?` holds, for two minutes at most.
(: (-> (-> Model bool) bool)) (: (-> (-> Model bool) bool))
(defun (wait-for done?) (defun (wait-for done?)
@ -80,6 +101,8 @@
(: scored? (-> Model bool)) (: scored? (-> Model bool))
(def scored? (fun (m) (= (vec-length (.scores m)) (vec-length (.layouts m))))) (def scored? (fun (m) (= (vec-length (.scores m)) (vec-length (.layouts m)))))
(when (not (wait-for scored?)) (panic! "the layouts were not scored")) (when (not (wait-for scored?)) (panic! "the layouts were not scored"))
;; The built-in layouts and those saved before the test.
(def base (vec-length (.layouts (app-model app))))
(def before (.generation (app-model app))) (def before (.generation (app-model app)))
(app-dispatch! app (SetParam 3 60.0)) (app-dispatch! app (SetParam 3 60.0))
(when (not (wait-for (fun (m) (and (> (.generation m) before) (scored? m))))) (when (not (wait-for (fun (m) (and (> (.generation m) before) (scored? m)))))
@ -102,14 +125,13 @@
(when (not (wait-for (fun (m) (and (not (.searching m)) (= (vec-length (.results m)) 2))))) (when (not (wait-for (fun (m) (and (not (.searching m)) (= (vec-length (.results m)) 2)))))
(panic! (str "the search did not finish: " (.status (app-model app))))) (panic! (str "the search did not finish: " (.status (app-model app)))))
(app-dispatch! app (Keep 0)) (app-dispatch! app (Keep 0))
(when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) 17) (scored? m))))) (when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) (+ base 1)) (scored? m)))))
(panic! "a kept layout was not scored")) (panic! "a kept layout was not scored"))
(app-run-pending! app) (app-run-pending! app)
(save-shot (cast Control frame) 1500 900 (path-combine folder "searched.png")) (save-shot (cast Control frame) 1500 900 (path-combine folder "searched.png"))
;; --- presets ;; --- presets
(def had-presets (directory-exists? presets-folder)) (def had-presets (directory-exists? presets-folder))
(def had-layouts (file-exists? layouts-file))
(: weight (-> Model double)) (: weight (-> Model double))
(def weight (fun (m) (record-ref (.params m) key-weight))) (def weight (fun (m) (record-ref (.params m) key-weight)))
(app-dispatch! app (SetPresetName "gui-test")) (app-dispatch! app (SetPresetName "gui-test"))
@ -140,7 +162,7 @@
(None (panic! (str "no layout is named " n))))) (None (panic! (str "no layout is named " n)))))
(app-dispatch! app (SetLayoutName "gui test")) (app-dispatch! app (SetLayoutName "gui test"))
(app-dispatch! app (SaveResult 1)) (app-dispatch! app (SaveResult 1))
(when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) 18) (scored? m) (when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) (+ base 2)) (scored? m)
(= (.status m) (str "Saved the layout gui test to " layouts-file ".")))))) (= (.status m) (str "Saved the layout gui test to " layouts-file "."))))))
(panic! (str "a result was not saved: " (.status (app-model app))))) (panic! (str "a result was not saved: " (.status (app-model app)))))
(when (not (string-contains? (file-read-text layouts-file) "gui test\t")) (when (not (string-contains? (file-read-text layouts-file) "gui test\t"))
@ -151,7 +173,7 @@
(app-dispatch! app SaveLayout) (app-dispatch! app SaveLayout)
(when (not (wait-for (fun (m) (and (scored? m) (vec-contains? (.saved m) "gui kept"))))) (when (not (wait-for (fun (m) (and (scored? m) (vec-contains? (.saved m) "gui kept")))))
(panic! (str "the kept layout was not saved: " (.status (app-model app))))) (panic! (str "the kept layout was not saved: " (.status (app-model app)))))
(when (or (not (= (vec-length (.layouts (app-model app))) 18)) (when (or (not (= (vec-length (.layouts (app-model app))) (+ base 2)))
(vec-contains? (names (app-model app)) "found 1")) (vec-contains? (names (app-model app)) "found 1"))
(panic! "the kept layout was not renamed")) (panic! "the kept layout was not renamed"))
(when (not (wait-for (fun (m) (string-contains? (.status m) "Saved the layout gui kept")))) (when (not (wait-for (fun (m) (string-contains? (.status m) "Saved the layout gui kept"))))
@ -160,16 +182,63 @@
(app-run-pending! app) (app-run-pending! app)
(save-shot (cast Control frame) 1500 900 (path-combine folder "layout-saved.png")) (save-shot (cast Control frame) 1500 900 (path-combine folder "layout-saved.png"))
(app-dispatch! app RemoveLayout) (app-dispatch! app RemoveLayout)
(when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) 17) (scored? m))))) (when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) (+ base 1)) (scored? m)))))
(panic! "the kept layout was not removed")) (panic! "the kept layout was not removed"))
(choose-named "gui test") (choose-named "gui test")
(app-dispatch! app RemoveLayout) (app-dispatch! app RemoveLayout)
(when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) 16) (scored? m) (when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) base) (scored? m)
(string-contains? (.status m) "Removed gui test from"))))) (string-contains? (.status m) "Removed gui test from")))))
(panic! (str "the layouts were not removed: " (.status (app-model app))))) (panic! (str "the layouts were not removed: " (.status (app-model app)))))
(when (string-contains? (file-read-text layouts-file) "gui ") (when (string-contains? (file-read-text layouts-file) "gui ")
(panic! "a removed layout is still in the file")) (panic! "a removed layout is still in the file"))
(when (not had-layouts) (file-delete layouts-file))
;; --- editing
(choose-named "ZYK,Å")
(app-dispatch! app (SetRow 0 "YZK,Å XCHPW"))
(when (not (wait-for (fun (m) (string-starts-with? (.status m) "A built-in layout cannot be changed"))))
(panic! "an edit of a built-in layout was not rejected"))
(when (not (= (vec-ref (chosen-rows (app-model app)) 0) "ZYK,Å XCHPW"))
(panic! "a built-in layout was changed"))
(app-dispatch! app CopyLayout)
(when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) (+ base 1)) (scored? m)
(= (layout-name-of (vec-ref (.layouts m) (.chosen m))) "ZYK,Å copy")))))
(panic! (str "the layout was not copied: " (.status (app-model app)))))
(def before-edit (mean-total (app-model app)))
(app-dispatch! app (SetRow 0 "YZK,Å XCHPW"))
(when (not (wait-for (fun (m) (and (scored? m) (= (vec-ref (chosen-rows m) 0) "YZK,Å XCHPW")
(not (= (mean-total m) before-edit))))))
(panic! (str "a row edit was not applied and scored: " (.status (app-model app)))))
(when (not (= (vec-ref (rows-named (app-model app) "ZYK,Å") 0) "ZYK,Å XCHPW"))
(panic! "the edit changed the original layout"))
;; A character on two keys, and one on no key.
(app-dispatch! app (SetRow 1 "IEDAA VTRNS"))
(when (not (wait-for (fun (m) (string-starts-with? (.status m) "On two keys: a"))))
(panic! (str "a character on two keys was not rejected: " (.status (app-model app)))))
(when (not (and (= (vec-ref (chosen-rows (app-model app)) 1) "IEDAÄ VTRNS")
(= (vec-ref (.rows-text (app-model app)) 1) "IEDAA VTRNS")))
(panic! "a rejected row changed the layout, or was not kept in its field"))
(app-dispatch! app (SetRow 1 "IEDAÄ VTRNS"))
(app-dispatch! app (SetRow 2 "ÖUFO."))
(when (not (wait-for (fun (m) (string-starts-with? (.status m) "On no key: "))))
(panic! (str "a missing character was not rejected: " (.status (app-model app)))))
(app-dispatch! app (SetRow 2 "ÖUFO. Q GMLBJ"))
;; Q and W are keys 0 and 1 of `board`: Y and Z change places again.
(app-dispatch! app (SwapKeys 0 1))
(when (not (wait-for (fun (m) (and (scored? m) (= (vec-ref (chosen-rows m) 0) "ZYK,Å XCHPW")
(= (vec-ref (.rows-text m) 0) "ZYK,Å XCHPW")))))
(panic! (str "a key swap was not applied: " (vec-ref (chosen-rows (app-model app)) 0))))
(app-dispatch! app (SetRow 0 "YZK,Å XCHPW"))
(ignore (wait-for scored?))
(app-dispatch! app (Tab 2))
(app-run-pending! app)
(save-shot (cast Control frame) 1500 900 (path-combine folder "editing.png"))
(app-dispatch! app RemoveLayout)
(when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) base) (scored? m)))))
(panic! "the copy was not removed"))
(match layouts-before
((Some text) (file-write-text layouts-file text))
(None (file-delete layouts-file)))
(when (and (not had-presets) (= (vec-length (directory-files presets-folder)) 0)) (when (and (not had-presets) (= (vec-length (directory-files presets-folder)) 0))
(directory-delete presets-folder)) (directory-delete presets-folder))
(println "gui shots ok") (println "gui shots ok")