You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
8.5 KiB
Racket

4 years ago
#lang debug racket/gui
4 years ago
(require words)
4 years ago
(define app-mono-fam
4 years ago
(for*/first ([preferred
'("Triplicate T4" "Menlo" "Consolas" "Andale Mono" "Courier")]
[mono-fam (in-list (get-face-list 'mono))]
#:when (equal? preferred mono-fam))
preferred))
4 years ago
4 years ago
(define app-font-size 16)
(define app-font (make-font #:face (send normal-control-font get-face) #:size app-font-size))
4 years ago
4 years ago
(define window (new frame% [label "Words"]
4 years ago
[width 700]
4 years ago
[height 700]
[x 40]
[y 40]
4 years ago
[alignment '(left top)]
4 years ago
[spacing 6]
4 years ago
[border 6]))
4 years ago
4 years ago
(define current-optional (make-parameter "etaoinshrdluw"))
(define current-omit (make-parameter #f))
(define current-mandatory (make-parameter #f))
(define current-combo (make-parameter #f))
4 years ago
(define refresh-thread (thread void))
(define (buffered-refresh)
(set! refresh-thread (let ()
(kill-thread refresh-thread)
(thread (λ () (sleep 0.2) (refresh-wordbox))))))
4 years ago
(define (update-text-field! tf param)
(param (match (send tf get-value)
[(? non-empty-string? str) str]
[_ #false]))
;; put delay on refresh so that rapid typing
;; doesn't trigger too many refreshes
(buffered-refresh))
(define (make-text-field param str [parent-panel #f])
4 years ago
(new text-field%
4 years ago
[parent (or parent-panel window)]
4 years ago
[label str]
4 years ago
[font app-font]
4 years ago
[horiz-margin (if parent-panel 0 12)]
4 years ago
[init-value (or (param) "")]
4 years ago
[min-width 330]
4 years ago
[stretchable-width #f]
4 years ago
[callback (λ (tf evt) (update-text-field! tf param))]))
(define optional-letter-panel (new horizontal-panel%
[parent window]
[horiz-margin 6]
[alignment '(left top)]
[stretchable-height #false]))
(define tf-optional
(make-text-field current-optional "optional letters" optional-letter-panel))
(define ((tf-optional-button-callback str) button evt)
(send tf-optional set-value str)
(update-text-field! tf-optional current-optional))
(match-define (list button-clear button-az button-common)
(for/list ([label-str '("clear" "a-z" "etaoinshrdluw")]
[str '("" "abcdefghijklmnopqrstuvwxyz" "etaoinshrdluw")])
(new button%
[label label-str]
[parent optional-letter-panel]
[font app-font]
[callback (tf-optional-button-callback str)])))
4 years ago
(for ([param (list current-omit current-mandatory current-combo)]
[str '("omitted letters" "mandatory letters" "mandatory combo")])
(make-text-field param str))
4 years ago
(define current-min-size (make-parameter 3))
4 years ago
(define current-max-size (make-parameter 24))
(define length-panel (new horizontal-panel%
[parent window]
[horiz-margin 6]
[alignment '(left top)]
[stretchable-width #true]
[stretchable-height #false]))
4 years ago
(for ([param (list current-min-size current-max-size)]
4 years ago
[label-str '("word length from" "to")])
(new slider%
[parent length-panel]
[label label-str]
[font app-font]
[min-value (current-min-size)]
[max-value (current-max-size)]
[init-value (param)]
[stretchable-width #t]
[callback (λ (cb evt)
(param (send cb get-value))
(buffered-refresh))]))
4 years ago
(define current-proper-names-choice (make-parameter #f))
(define current-hide-plurals (make-parameter #f))
(let ([checkbox-panel (new horizontal-panel%
[parent window]
4 years ago
[alignment '(left top)]
4 years ago
[stretchable-height #false])])
(for ([param (list current-proper-names-choice current-hide-plurals)]
[msg '("show proper names" "hide plurals")])
(new check-box%
[parent checkbox-panel]
[label msg]
[font app-font]
[horiz-margin 6]
[callback (λ (cb evt)
(param (send cb get-value))
(refresh-wordbox))])))
4 years ago
(define current-case-choice (make-parameter #f))
4 years ago
(define rb-casing
(new radio-box%
[parent window]
4 years ago
[label "casing "]
4 years ago
[font app-font]
4 years ago
[horiz-margin 12]
[style '(horizontal)]
[choices '("default" "Title Case" "lowercase" "UPPERCASE")]
[callback (λ (rb evt)
(current-case-choice (match (send rb get-selection)
[1 'title]
[2 'lower]
[3 'upper]
[_ #false]))
(refresh-wordbox))]))
4 years ago
(define (refresh-wordbox)
4 years ago
(define ed (send wordbox get-editor))
(send ed erase)
(define wordlist (make-words #:count (current-word-count)
#:letters (current-optional)
#:omit (current-omit)
#:mandatory (current-mandatory)
#:combo (current-combo)
#:case (current-case-choice)
#:min (current-min-size)
#:max (current-max-size)
#:proper-names (current-proper-names-choice)
#:hide-plurals (current-hide-plurals)))
4 years ago
(send ed insert
(string-join
(match wordlist
4 years ago
[(list words ..1) words]
[_ (list "[no matching words]")]) " "))
(update-copy-button-label (length wordlist)))
4 years ago
4 years ago
(define current-word-count (make-parameter 50))
4 years ago
4 years ago
(let ([button-panel (new horizontal-panel% [parent window]
4 years ago
[alignment '(left top)]
4 years ago
[stretchable-height #false])])
4 years ago
(for ([count (in-list '(50 100 200 400 800 1600))])
(define count-str (format "~a" count))
(new button% [parent button-panel]
[label count-str]
[font app-font]
[callback (λ (button evt)
(current-word-count (string->number count-str))
(refresh-wordbox))])))
4 years ago
(define wordbox (new text-field%
[label #f]
[style '(multiple)]
4 years ago
[parent window]
4 years ago
[font (make-font #:face app-mono-fam #:size app-font-size)]))
(let ([ed (send wordbox get-editor)])
(send ed set-line-spacing (* app-font-size 0.4))
(send ed set-padding 6 3 6 3))
4 years ago
(define (words-to-clipboard item evt)
(send the-clipboard set-clipboard-string (send wordbox get-value) (send evt get-time-stamp)))
(define button-copy
(new button% [parent window]
[label "copy words"]
4 years ago
[font app-font]
4 years ago
[stretchable-width #true]
[callback words-to-clipboard]))
(define menubar (new menu-bar%
[parent window]))
(define menu-edit (new menu%
[parent menubar]
[label "Actions"]))
(define (make-menu-item str char proc)
(new menu-item%
[parent menu-edit]
[label str]
[shortcut char]
[callback proc]))
(define menu-item-copy (make-menu-item "Copy" #\C words-to-clipboard))
(define menu-item-refresh
(make-menu-item "Regenerate" #\R (λ (thing evt) (refresh-wordbox))))
(define (update-copy-button-label count)
4 years ago
(send button-copy set-label (format "copy ~a word~a" count (if (= 1 count) "" "s"))))
(define (change-word-count amt)
(define new-count (max (+ (current-word-count) amt) 0))
(current-word-count new-count))
4 years ago
(define menu-item-more-words
(make-menu-item "More words" #\= (λ (thing evt)
(change-word-count 25)
4 years ago
(refresh-wordbox))))
(define menu-item-fewer-words
(make-menu-item "Fewer words" #\- (λ (thing evt)
(change-word-count -25)
4 years ago
(refresh-wordbox))))
4 years ago
; (list button-clear button-az button-common)
(define menu-item-clear
(make-menu-item "Clear letters" #\1 (λ (thing evt) (send button-clear command evt))))
(define menu-item-az
(make-menu-item "Use a-z" #\2 (λ (thing evt) (send button-az command evt))))
(define menu-item-common
(make-menu-item "Use etaoin" #\3 (λ (thing evt) (send button-common command evt))))
4 years ago
(refresh-wordbox)
(send window show #t)
4 years ago