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:
parent
6e6afad071
commit
1da78595bd
8 changed files with 297 additions and 45 deletions
243
src/gui.bjo
243
src/gui.bjo
|
|
@ -18,6 +18,10 @@
|
|||
;; (keyscore saved).
|
||||
;; - 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.
|
||||
;; - 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.
|
||||
|
||||
(import (std prelude))
|
||||
|
|
@ -33,7 +37,7 @@
|
|||
(import (keyscore search))
|
||||
(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 ------------------------------------------------------------------
|
||||
|
||||
|
|
@ -48,6 +52,9 @@
|
|||
;; - saved: the names of the layouts in `layouts-file`.
|
||||
;; - layout-name: the name a layout 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)
|
||||
(: counts (Vec Counts))
|
||||
(: layouts (Vec Layout))
|
||||
|
|
@ -63,6 +70,8 @@
|
|||
(: layout-name string)
|
||||
(: presets (Vec string))
|
||||
(: preset-name string)
|
||||
(: rows-text (Vec string))
|
||||
(: edited (Vec string))
|
||||
(: status string))))
|
||||
|
||||
(type (: Msg (Union (: Counted (Vec Counts))
|
||||
|
|
@ -96,18 +105,21 @@
|
|||
(: SaveResult int)
|
||||
RemoveLayout
|
||||
(: Wrote string)
|
||||
CopyLayout
|
||||
(: SetRow int string)
|
||||
(: SwapKeys int int)
|
||||
Noop)))
|
||||
|
||||
;; The layouts of `layouts-file` when the program starts.
|
||||
(: (Vec Layout))
|
||||
(def saved-at-start (read-saved-layouts))
|
||||
|
||||
;; The name of `l`.
|
||||
(: (-> Layout string))
|
||||
(defun (lname l) (record-ref l name))
|
||||
|
||||
|
||||
(: Model)
|
||||
(def initial
|
||||
;; The model at start: the numbers of `params-file`, the built-in and saved
|
||||
;; layouts, and the presets. It reads files, so call it from `main` or from a
|
||||
;; function that main calls, not from a module-level `def`.
|
||||
(: (-> Model))
|
||||
(defun (initial)
|
||||
(def saved-at-start (read-saved-layouts))
|
||||
(Model (params (current-params))
|
||||
(counts [])
|
||||
(layouts (vec-append layouts saved-at-start))
|
||||
|
|
@ -123,6 +135,8 @@
|
|||
(layout-name "")
|
||||
(presets (preset-names))
|
||||
(preset-name "")
|
||||
(rows-text (record-ref (vec-ref layouts (- (vec-length layouts) 1)) rows))
|
||||
(edited [])
|
||||
(status "Reading the texts…")))
|
||||
|
||||
;; --- scoring --------------------------------------------------------------------
|
||||
|
|
@ -193,6 +207,14 @@
|
|||
(: string)
|
||||
(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
|
||||
;; write the file.
|
||||
;;
|
||||
|
|
@ -225,11 +247,65 @@
|
|||
(def before (lname (vec-ref ls (.chosen m))))
|
||||
(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]))
|
||||
(and-then (rescore (record-set m (layouts new-ls) (saved saved) (chosen chosen) (scores [])
|
||||
(layout-name name))
|
||||
(.params m))
|
||||
(def edited (vec-filter (fun (n) (not (or (= n name) (= (Some n) old)))) (.edited m)))
|
||||
(def picked (choose (record-set m (layouts new-ls) (saved saved) (edited edited) (scores [])) chosen))
|
||||
(and-then (rescore (record-set picked (layout-name name)) (.params m))
|
||||
(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.
|
||||
(: (-> Layout bool))
|
||||
(defun (vowel-right? l)
|
||||
|
|
@ -250,10 +326,7 @@
|
|||
((Choose pos)
|
||||
(def r (ranking m))
|
||||
(if (and (>= pos 0) (< pos (vec-length r)))
|
||||
(let ((i (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 (choose m (vec-ref r pos)))
|
||||
(next m)))
|
||||
((Tab t) (next (record-set m (tab t))))
|
||||
((SetRuns x) (next (record-set m (options (record-set o (runs (cast int x)))))))
|
||||
|
|
@ -338,13 +411,42 @@
|
|||
(:when (not (= j i)))
|
||||
(:acc out (vecing x))))
|
||||
(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 [])
|
||||
(status (str "Removed " name ".")))
|
||||
(def edited (vec-filter (fun (n) (not (= n name))) (.edited m)))
|
||||
(def step (rescore (choose (record-set m (layouts new-ls) (saved saved) (edited edited) (scores [])
|
||||
(status (str "Removed " name ".")))
|
||||
(- built 1))
|
||||
(.params m)))
|
||||
(if (vec-contains? (.saved m) name)
|
||||
(and-then step (write-layouts new-ls saved (str "Removed " name " from " layouts-file ".")))
|
||||
step))))
|
||||
((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))))
|
||||
|
||||
(: subscriptions (-> Model (List (Sub Msg))))
|
||||
|
|
@ -383,20 +485,61 @@
|
|||
(: double)
|
||||
(def board-height (* key-size 3.0))
|
||||
|
||||
;; The board, with a label on each key of `board`, in its order. A stretch
|
||||
;; key has a darker edge.
|
||||
;; The drawn size of a key, in pixels.
|
||||
(: 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))
|
||||
(def keyboard
|
||||
(component unit
|
||||
(fun (pen w h s labels)
|
||||
(loop (:for k board) (:for label labels)
|
||||
(:let (Tuple x y) (key-place k))
|
||||
(:let size (- key-size 4.0))
|
||||
(:do (fill-rect pen x y size size (finger-colour k) #:radius 5.0)
|
||||
(when (record-ref k stretch?)
|
||||
(stroke-rect pen x y size size "#888888" #:width 1.5 #:radius 5.0))
|
||||
(draw-text pen label (+ x (/ size 2.0)) (+ y 7.0) "#202124"
|
||||
#:size 17.0 #:bold #t #:align "centre"))))))
|
||||
(component unit (fun (pen w h s labels) (draw-board pen labels))))
|
||||
|
||||
;; The index in `board` of the key drawn at `x`, `y`, if there is one.
|
||||
(: (-> double double (Option int)))
|
||||
(defun (key-at x y)
|
||||
(: on-key? (-> int bool))
|
||||
(def on-key? (fun (i)
|
||||
(def (Tuple kx ky) (key-place (vec-ref board i)))
|
||||
(and (>= x kx) (< x (+ kx key-face)) (>= y ky) (< y (+ ky key-face)))))
|
||||
(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 "".
|
||||
(: (-> Layout (Vec string)))
|
||||
|
|
@ -476,7 +619,7 @@
|
|||
(defun (scores-view m)
|
||||
(def cs (record-ref (.params m) corpora))
|
||||
(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)))
|
||||
(def line (fun (cells)
|
||||
(apply grid #:columns columns
|
||||
|
|
@ -495,7 +638,7 @@
|
|||
(column #:spacing 6.0 #:margin 8.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)))
|
||||
(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.
|
||||
(: (-> 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.")
|
||||
(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))
|
||||
(defun (kind-of-chosen m)
|
||||
(def i (.chosen m))
|
||||
(def name (lname (vec-ref (.layouts m) i)))
|
||||
(cond ((< i (vec-length layouts)) "built in")
|
||||
((vec-contains? (.saved m) (lname (vec-ref (.layouts m) i))) (str "saved in " layouts-file))
|
||||
(else "kept, not saved")))
|
||||
((vec-contains? (.edited m) name) "changed, 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.
|
||||
(: (-> Model (View Msg)))
|
||||
|
|
@ -612,10 +758,33 @@
|
|||
(tooltip (button "Save layout" SaveLayout)
|
||||
(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."))
|
||||
(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?))
|
||||
(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."))))
|
||||
|
||||
;; 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.
|
||||
(: (-> Model (View Msg)))
|
||||
(defun (layout-view m)
|
||||
|
|
@ -657,9 +826,9 @@
|
|||
(column #:spacing 10.0 #:margin 8.0
|
||||
heading
|
||||
(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))))
|
||||
(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)))
|
||||
(defun (search-view m)
|
||||
|
|
@ -724,7 +893,7 @@
|
|||
(tab "Search" (search-view m)))))
|
||||
|
||||
(defun (main)
|
||||
(run-app initial gui-update view
|
||||
(run-app (initial) gui-update view
|
||||
#:subscriptions subscriptions
|
||||
#:title "keyscore" #:width 1500.0 #:height 900.0)
|
||||
0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue