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

@ -11,6 +11,9 @@
;; presets.png, then deletes the preset.
;; - Saves a search result and the kept result as layouts, saves
;; 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.
(import (std prelude))
@ -20,6 +23,7 @@
(import (keyscore layouts))
(import (keyscore params))
(import (keyscore saved))
(import (keyscore score))
(import/class
(Object (: System.Object))
@ -63,12 +67,29 @@
(: (-> Layout string))
(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)
(def folder (if (> (vec-length args) 0) (vec-ref args 0) "."))
(start-headless!)
;; 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))
(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.
(: (-> (-> Model bool) bool))
(defun (wait-for done?)
@ -80,6 +101,8 @@
(: scored? (-> Model bool))
(def scored? (fun (m) (= (vec-length (.scores m)) (vec-length (.layouts m)))))
(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)))
(app-dispatch! app (SetParam 3 60.0))
(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)))))
(panic! (str "the search did not finish: " (.status (app-model app)))))
(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"))
(app-run-pending! app)
(save-shot (cast Control frame) 1500 900 (path-combine folder "searched.png"))
;; --- presets
(def had-presets (directory-exists? presets-folder))
(def had-layouts (file-exists? layouts-file))
(: weight (-> Model double))
(def weight (fun (m) (record-ref (.params m) key-weight)))
(app-dispatch! app (SetPresetName "gui-test"))
@ -140,7 +162,7 @@
(None (panic! (str "no layout is named " n)))))
(app-dispatch! app (SetLayoutName "gui test"))
(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 "."))))))
(panic! (str "a result was not saved: " (.status (app-model app)))))
(when (not (string-contains? (file-read-text layouts-file) "gui test\t"))
@ -151,7 +173,7 @@
(app-dispatch! app SaveLayout)
(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)))))
(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"))
(panic! "the kept layout was not renamed"))
(when (not (wait-for (fun (m) (string-contains? (.status m) "Saved the layout gui kept"))))
@ -160,16 +182,63 @@
(app-run-pending! app)
(save-shot (cast Control frame) 1500 900 (path-combine folder "layout-saved.png"))
(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"))
(choose-named "gui test")
(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")))))
(panic! (str "the layouts were not removed: " (.status (app-model app)))))
(when (string-contains? (file-read-text layouts-file) "gui ")
(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))
(directory-delete presets-folder))
(println "gui shots ok")