Skip to content

Commit 469add8

Browse files
committed
add 'snip-special's to text:ports<%> in the framework
related to PR 15049
1 parent f9870b0 commit 469add8

File tree

3 files changed

+104
-50
lines changed

3 files changed

+104
-50
lines changed

gui-lib/framework/main.rkt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,32 @@
157157
@{Returns the editor instance whose port-name matches the given symbol.
158158
If no editor can be found, then returns @racket[false].})
159159

160+
(proc-doc/names
161+
text:make-snip-special
162+
(-> (is-a?/c snip%) text:snip-special?)
163+
(snip)
164+
@{Returns a @racket[snip-special] to be used as a
165+
@tech[#:doc '(lib "scribblings/reference/reference.scrbl")]{special}
166+
with the ports in @racket[text:ports<%>].
167+
168+
When a snip is sent as a special, if it has a @racket[snip-class%]
169+
from a different @tech[#:doc '(lib "scribblings/gui/gui.scrbl")]{eventspace},
170+
it may not work properly
171+
in the @racket[text%] object connected to the ports in a @racket[text:port<%>]
172+
object. This function, when it is called, constructs the bytes
173+
corresponding to the result of using the @racket[snip]'s
174+
@method[snip% write] method and saves them in it's result. Then,
175+
when the result is used as a special, the snip will rebuild from
176+
the bytes, but now using the @racket[snip-class%] from the
177+
@tech[#:doc '(lib "scribblings/gui/gui.scrbl")]{eventspace}
178+
where the @racket[text:ports<%>] operates.})
179+
180+
(proc-doc/names
181+
text:snip-special?
182+
(-> any/c boolean?)
183+
(v)
184+
@{Recognizes the result of @racket[text:make-snip-special].})
185+
160186
(proc-doc/names
161187
number-snip:make-repeating-decimal-snip
162188
(real? boolean? . -> . (is-a?/c snip%))

gui-lib/framework/private/sig.rkt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,10 @@
251251
range-end
252252
range-caret-space?
253253
range-style
254-
range-color))
254+
range-color
255+
256+
make-snip-special
257+
snip-special?))
255258

256259
(define-signature canvas-class^
257260
(basic<%>

gui-lib/framework/private/text.rkt

Lines changed: 74 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,6 +2230,26 @@
22302230
;; to do the work
22312231
(define-struct data/chan (data to-insert-chan))
22322232

2233+
(struct snip-special (snip name bytes))
2234+
(define (make-snip-special snip)
2235+
(define base (new editor-stream-out-bytes-base%))
2236+
(define stream (make-object editor-stream-out% base))
2237+
(send snip write stream)
2238+
(snip-special snip
2239+
(send (send snip get-snipclass) get-classname)
2240+
(send base get-bytes)))
2241+
(define (snip-special->snip snip-special)
2242+
(define snipclass (send (get-the-snip-class-list) find (snip-special-name snip-special)))
2243+
(cond
2244+
[snipclass
2245+
(define base (make-object editor-stream-in-bytes-base%
2246+
(snip-special-bytes snip-special)))
2247+
(define es (make-object editor-stream-in% base))
2248+
(or (send snipclass read es)
2249+
(snip-special-snip snip-special))]
2250+
[else
2251+
(snip-special-snip snip-special)]))
2252+
22332253
(define ports-mixin
22342254
(mixin (wide-snip<%>) (ports<%>)
22352255
(inherit begin-edit-sequence
@@ -2578,55 +2598,59 @@
25782598
;; do-insertion : (listof (cons (union string snip) style-delta)) boolean -> void
25792599
;; thread: eventspace main thread
25802600
(define/private (do-insertion txts showing-input?)
2581-
(let ([locked? (is-locked?)]
2582-
[sf? (get-styles-fixed)])
2583-
(begin-edit-sequence)
2584-
(lock #f)
2585-
(set-styles-fixed #f)
2586-
(set! allow-edits? #t)
2587-
(let loop ([txts txts])
2588-
(cond
2589-
[(null? txts) (void)]
2590-
[else
2591-
(let* ([fst (car txts)]
2592-
[str/snp (car fst)]
2593-
[style (cdr fst)])
2594-
2595-
(let ([inserted-count
2596-
(if (is-a? str/snp snip%)
2597-
(send str/snp get-count)
2598-
(string-length str/snp))]
2599-
[old-insertion-point insertion-point])
2600-
(set! insertion-point (+ insertion-point inserted-count))
2601-
(set! unread-start-point (+ unread-start-point inserted-count))
2602-
2603-
(insert (if (is-a? str/snp snip%)
2604-
(let ([s (send str/snp copy)])
2605-
(if (is-a? s snip%)
2606-
s
2607-
(new snip%)))
2608-
str/snp)
2609-
old-insertion-point
2610-
old-insertion-point
2611-
#t)
2612-
2613-
;; the idea here is that if you made a string snip, you
2614-
;; could have made a string and gotten the style, so you
2615-
;; must intend to have your own style.
2616-
(unless (is-a? str/snp string-snip%)
2617-
(change-style style old-insertion-point insertion-point))))
2618-
(loop (cdr txts))]))
2619-
(set-styles-fixed sf?)
2620-
(set! allow-edits? #f)
2621-
(lock locked?)
2622-
(unless showing-input?
2623-
(when box-input
2624-
(adjust-box-input-width)
2625-
(when (eq? box-input (get-focus-snip))
2626-
(scroll-to-position (last-position)))))
2627-
(end-edit-sequence)
2628-
(unless (null? txts)
2629-
(after-io-insertion))))
2601+
(define locked? (is-locked?))
2602+
(define sf? (get-styles-fixed))
2603+
(begin-edit-sequence)
2604+
(lock #f)
2605+
(set-styles-fixed #f)
2606+
(set! allow-edits? #t)
2607+
(let loop ([txts txts])
2608+
(cond
2609+
[(null? txts) (void)]
2610+
[else
2611+
(define fst (car txts))
2612+
(define str/snp
2613+
(cond
2614+
[(snip-special? (car fst))
2615+
(snip-special->snip (car fst))]
2616+
[else (car fst)]))
2617+
(define style (cdr fst))
2618+
2619+
(define inserted-count
2620+
(if (is-a? str/snp snip%)
2621+
(send str/snp get-count)
2622+
(string-length str/snp)))
2623+
(define old-insertion-point insertion-point)
2624+
(set! insertion-point (+ insertion-point inserted-count))
2625+
(set! unread-start-point (+ unread-start-point inserted-count))
2626+
2627+
(insert (if (is-a? str/snp snip%)
2628+
(let ([s (send str/snp copy)])
2629+
(if (is-a? s snip%)
2630+
s
2631+
(new snip%)))
2632+
str/snp)
2633+
old-insertion-point
2634+
old-insertion-point
2635+
#t)
2636+
2637+
;; the idea here is that if you made a string snip, you
2638+
;; could have made a string and gotten the style, so you
2639+
;; must intend to have your own style.
2640+
(unless (is-a? str/snp string-snip%)
2641+
(change-style style old-insertion-point insertion-point))
2642+
(loop (cdr txts))]))
2643+
(set-styles-fixed sf?)
2644+
(set! allow-edits? #f)
2645+
(lock locked?)
2646+
(unless showing-input?
2647+
(when box-input
2648+
(adjust-box-input-width)
2649+
(when (eq? box-input (get-focus-snip))
2650+
(scroll-to-position (last-position)))))
2651+
(end-edit-sequence)
2652+
(unless (null? txts)
2653+
(after-io-insertion)))
26302654

26312655
(define/public (after-io-insertion) (void))
26322656

@@ -2762,6 +2786,7 @@
27622786
(λ (special can-buffer? enable-breaks?)
27632787
(define str/snp (cond
27642788
[(string? special) special]
2789+
[(snip-special? special) special]
27652790
[(is-a? special snip%) special]
27662791
[else (format "~s" special)]))
27672792
(define to-send (cons str/snp style))

0 commit comments

Comments
 (0)