Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 71 additions & 61 deletions gui-easy-lib/gui/easy/private/view/input.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
(when background-color
(send the-field set-field-background background-color))
(begin0 the-field
(send the-field set-context 'last-val content)))
(send the-field set-context 'last-val content)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meta note: I think you've formatted a bunch of code that didn't need to be touched

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, hate it when that happens. :/

Is there a particular linting set of rules I could use to avoid these problems?
I mostly use drracket but otherwise a neovim user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure: I might have configured begin0 to indent correctly recently, but I'm not sure about case/dep.

Depending on the (Neo)Vim plugins you use, it might be as simple as adding those forms to lispwords.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In DrRacket, you can add begin0 and case/dep to "Preferences" -> "Editing" -> "Define-like Keywords".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked, and if you're using my vim-racket, both forms should already be configured correctly for indentation. They should also be in lispwords if you have a recent-ish Vim (since I contribute some parts of that plugin back to upstream runtime files occasionally). Vim users (but not Neovim, sorry) also benefit from the indentexpr I wrote that correctly handles for and for/fold forms, FWIW.


(define (call-preserving-position ed thunk)
(define old-text (send ed get-text))
Expand All @@ -69,57 +69,57 @@
(and (= start 0)
(= end (string-length old-text))))
(begin0 (thunk)
;; When the contents of the editor are empty, avoid changing
;; the position since doing so would place the cursor before
;; any newly-inserted text.
(unless (string=? "" old-text)
(if full-selection?
(send ed set-position 0 (string-length (send ed get-text)))
(send ed set-position start end)))))
;; When the contents of the editor are empty, avoid changing
;; the position since doing so would place the cursor before
;; any newly-inserted text.
(unless (string=? "" old-text)
(if full-selection?
(send ed set-position 0 (string-length (send ed get-text)))
(send ed set-position start end)))))

(define/public (update v what val)
(case/dep what
[@label
(send v set-label val)]
[@content
(define last-val (send v get-context 'last-val))
(define text
(value->text val))
(cond
[(not (value=? val last-val))
(send v set-context 'last-val val)
(call-preserving-position
(send v get-editor)
(lambda ()
(send v set-value text)
(send v refresh)))]
[@label
(send v set-label val)]
[@content
(define last-val (send v get-context 'last-val))
(define text
(value->text val))
(cond
[(not (value=? val last-val))
(send v set-context 'last-val val)
(call-preserving-position
(send v get-editor)
(lambda ()
(send v set-value text)
(send v refresh)))]

[(string=? text "")
(send v set-value "")
(send v refresh)]

[else
(void)])]
[@enabled?
(send v enable val)]
[@background-color
(send v set-field-background val)]
[@margin
(match-define (list h-m v-m) val)
(send* v
(horiz-margin h-m)
(vert-margin v-m))]
[@min-size
(match-define (list w h) val)
(send* v
(min-width (or w 0))
(min-height (or h 0)))]
[@stretch
(match-define (list w-s? h-s?) val)
(send* v
(stretchable-width w-s?)
(stretchable-height h-s?))]))
[(string=? text "")
(send v set-value "")
(send v refresh)]

[else
(void)])]
[@enabled?
(send v enable val)]
[@background-color
(send v set-field-background val)]
[@margin
(match-define (list h-m v-m) val)
(send* v
(horiz-margin h-m)
(vert-margin v-m))]
[@min-size
(match-define (list w h) val)
(send* v
(min-width (or w 0))
(min-height (or h 0)))]
[@stretch
(match-define (list w-s? h-s?) val)
(send* v
(stretchable-width w-s?)
(stretchable-height h-s?))]))

(define/public (destroy v)
(send v clear-context))))

Expand All @@ -136,17 +136,27 @@
#:mixin [mix values]
#:value=? [value=? equal?]
#:value->text [value->text values])
(new (input% (mix gui:text-field%))
[@label @label]
[@content @content]
[@enabled? @enabled?]
[@background-color @background-color]
[@margin @margin]
[@min-size @min-size]
[@stretch @stretch]
[action action]
[style style]
[font font]
[keymap keymap]
[value=? value=?]
[value->text value->text]))
; The modified text-field makes the action responsive to the control receiving or losing the focus.
(define modified-text-field% (class gui:text-field%
(super-new)
(define/override (on-focus focused?)
(action
(case focused?
[(#t) 'has-focus]
[(#f) 'lost-focus])
(send this get-value)))))
(new (input% (mix modified-text-field%))
[@label @label]
[@content @content]
[@enabled? @enabled?]
[@background-color @background-color]
[@margin @margin]
[@min-size @min-size]
[@stretch @stretch]
[action action]
[style style]
[font font]
[keymap keymap]
[value=? value=?]
[value->text value->text]))

2 changes: 1 addition & 1 deletion gui-easy-lib/gui/easy/view.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
#:mode (maybe-obs/c (or/c 'fit 'fill))]
view/c)]
[input (->* [(maybe-obs/c any/c)]
[(-> (or/c 'input 'return) string? any)
[(-> (or/c 'input 'return 'has-focus 'lost-focus) string? any)
#:label (maybe-obs/c maybe-label/c)
#:enabled? (maybe-obs/c boolean?)
#:background-color (maybe-obs/c (or/c #f (is-a?/c gui:color%)))
Expand Down
9 changes: 7 additions & 2 deletions gui-easy/gui/easy/scribblings/reference.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ packages in the Racket ecosystem, into their projects.
}

@defproc[(input [value (maybe-obs/c any/c)]
[action (-> (or/c 'input 'return) string? any) void]
[action (-> (or/c 'input 'return 'has-focus 'lost-focus) string? any) void]
[#:label label (maybe-obs/c maybe-label/c) #f]
[#:enabled? enabled? (maybe-obs/c boolean?) #t]
[#:background-color background-color (maybe-obs/c (or/c #f (is-a?/c gui:color%))) #f]
Expand All @@ -559,7 +559,7 @@ packages in the Racket ecosystem, into their projects.
[#:value->text value->text (-> any/c string?) values]) (is-a?/c view<%>)]{
Returns a representation of a text field that calls @racket[action]
on change. The first argument to the @racket[action] is the type of
event that caused the input to change and the second is the contents
event that the control is responding to and the second is the contents
of the text field.

The @racket[#:value=?] argument controls when changes to the input
Expand All @@ -574,6 +574,11 @@ packages in the Racket ecosystem, into their projects.
The @racket[#:value->text] argument controls how the input values
are rendered to strings. If not provided, @racket[value] must be
either a @racket[string?] or an observable of strings.

@history[
#:changed "0.21" @elem{@racket[input] also responds to the @racket['has-focus] and
@racket['lost-focus] events.}
]
}

@defproc[(progress [value (maybe-obs/c gui:position-integer?)]
Expand Down