From 4a767a1cda68020a8e861dcdec26749c5afdf5c9 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 14 Apr 2020 22:29:06 -0700 Subject: [PATCH] buffered refresh --- words/app.rkt | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/words/app.rkt b/words/app.rkt index 231b178..0ba4c6e 100644 --- a/words/app.rkt +++ b/words/app.rkt @@ -11,6 +11,11 @@ (define current-omit (make-parameter #f)) (define current-mandatory (make-parameter #f)) (define current-combo (make-parameter #f)) +(define refresh-thread (thread void)) +(define (buffered-refresh) + (set! refresh-thread (let () + (kill-thread refresh-thread) + (thread (λ () (sleep 0.2) (refresh-wordbox)))))) (for ([param (list current-optional current-omit current-mandatory current-combo)] [str '("optional letters" "omitted letters" "mandatory letters" "mandatory combo")]) (new text-field% @@ -22,7 +27,9 @@ (param (match (send tf get-value) [(? non-empty-string? str) str] [_ #false])) - (refresh-wordbox))])) + ;; put delay on refresh so that rapid typing + ;; doesn't trigger too many refreshes + (buffered-refresh))])) (define current-min-size (make-parameter 3)) (define current-max-size (make-parameter 20)) @@ -77,15 +84,15 @@ (send ed insert (string-join (match (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)) + #: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)) [(list words ..1) words] [_ (list "[no matching words]")]) " ")))