From 547ac9c7d8721fa4dd373861fdb8110e9551dbad Mon Sep 17 00:00:00 2001 From: Neil Toronto Date: Tue, 22 Nov 2011 15:21:43 -0700 Subject: [PATCH] Focus tweaks to capture off-snip movement; account for split editor --- collects/plot/common/snip.rkt | 46 ++++++++++++++++++++++++----------- collects/plot/plot2d/snip.rkt | 8 +++--- collects/plot/plot3d/snip.rkt | 14 +++++------ 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/collects/plot/common/snip.rkt b/collects/plot/common/snip.rkt index e0f8d63849..8aa845af56 100644 --- a/collects/plot/common/snip.rkt +++ b/collects/plot/common/snip.rkt @@ -13,7 +13,7 @@ (class image-snip% (init bm) (init-field saved-plot-parameters) - (inherit set-bitmap get-bitmap) + (inherit set-bitmap get-bitmap get-admin) (super-make-object bm) @@ -82,22 +82,40 @@ (parameterize/group ([plot-parameters saved-plot-parameters]) (draw-message dc x y)))) + (define left-down-admin #f) + (define/public (get-left-down-here?) + (eq? (get-admin) left-down-admin)) + (send this set-flags (list* 'handles-events 'handles-all-mouse-events (send this get-flags))) (define/override (on-event dc x y editorx editory evt) - (define editor (send (send this get-admin) get-editor)) - (when (member (send evt get-event-type) '(left-down middle-down right-down)) - ;; The snip has been given caret ownership by now. But we don't want the snip to own the - ;; caret because it'll hog all the mouse move events, keeping the other plot snips from - ;; showing messages when the mouse hovers over them. Besides, a plot snip has no selectable - ;; text or any other reason to own the caret. - ;; This gives ownership to the editor: - (send editor set-caret-owner #f)) - (when (eq? (send evt get-event-type) 'right-down) - ;; The 'handles-events flag keeps the editor from handling the right-click event, meaning the - ;; pop-up menu won't pop up. So we call the "local" event handler, which would have been - ;; called had this snip not trapped events: - (send editor on-local-event evt))) + (define admin (get-admin)) + (define editor (send admin get-editor)) + (case (send evt get-event-type) + ;; The editor gives ownership to the snip as soon as the mouse left-clicks on it, before the + ;; snip handles any events. On one hand, this is good: it means that mouse movement events are + ;; sent to the snip even after the mouse leaves. On the other hand, it's bad: we don't want + ;; the snip to *retain* ownership after the button is up, because it'll continue to hog all + ;; the mouse movement events, keeping other plot snips from displaying hover messages. Also, + ;; a plot snip has no selectable text, focusable controls, or any other reason to own the + ;; caret. + ;; So on left button up, if the left button went down on this snip, we give ownership to the + ;; snip's editor. + [(left-down) (set! left-down-admin admin)] + [(left-up) (when (get-left-down-here?) + (set! left-down-admin #f) + (send editor set-caret-owner #f))] + ;; The 'handles-events flag keeps the editor from handling right-click events, so the pop-up + ;; menu won't pop up. So we call the editor's "local" event handler, which would have been + ;; called had this snip not trapped events. + ;; We also don't allow right/middle mouse clicks to transfer caret ownership, ever. We would + ;; have similar rules to left-up, but the pop-up menu apparently gets right-up events, not + ;; the snip. + [(right-down middle-down) (send editor set-caret-owner #f) + (send editor on-local-event evt)] + ;; Just in case (we don't want these events anyway): + [(right-up middle-up) (send editor on-local-event evt)] + )) (define cross-cursor (make-object cursor% 'cross)) (define/override (adjust-cursor dc x y editorx editory evt) cross-cursor) diff --git a/collects/plot/plot2d/snip.rkt b/collects/plot/plot2d/snip.rkt index cd904ab196..683eef663f 100644 --- a/collects/plot/plot2d/snip.rkt +++ b/collects/plot/plot2d/snip.rkt @@ -20,7 +20,8 @@ (inherit set-bitmap get-bitmap get-saved-plot-parameters - refresh set-message reset-message-timeout get-admin) + refresh set-message reset-message-timeout + get-left-down-here?) (super-make-object bm saved-plot-parameters) @@ -42,7 +43,6 @@ (vector (ivl left-click-x left-drag-x) (ivl left-click-y left-drag-y))))) (define dragging? #f) - (define left-down? #f) ; only #t if left-down happened on this snip (define zoom-timer #f) (define (set-zoom-timer) @@ -75,12 +75,10 @@ (set! left-drag-x mouse-x) (set! left-drag-y mouse-y) (set! dragging? #f) - (set! left-down? #t) (set-message #f) (set-zoom-timer)] [(left-up) (set! left-drag-x mouse-x) (set! left-drag-y mouse-y) - (set! left-down? #f) (cond [dragging? (set! dragging? #f) (define new-rect (area-bounds->plot-bounds (get-area-bounds-rect))) @@ -95,7 +93,7 @@ (set! plot-bounds-rects (rest plot-bounds-rects)) (update-plot new-rect) (set! show-zoom-message? #f)])] - [(motion) (cond [left-down? ; not event's left-down: only #t if clicked on snip + [(motion) (cond [(get-left-down-here?) ; not event's left-down: only #t if clicked on snip (when (not (and (= left-drag-x mouse-x) (= left-drag-y mouse-y))) (set! left-drag-x mouse-x) diff --git a/collects/plot/plot3d/snip.rkt b/collects/plot/plot3d/snip.rkt index ab3112207b..d3c8ee4653 100644 --- a/collects/plot/plot3d/snip.rkt +++ b/collects/plot/plot3d/snip.rkt @@ -26,7 +26,9 @@ (init bm saved-plot-parameters) (init-field make-bm angle altitude) - (inherit set-bitmap get-bitmap get-saved-plot-parameters set-message reset-message-timeout) + (inherit set-bitmap get-bitmap + get-saved-plot-parameters set-message reset-message-timeout + get-left-down-here?) (super-make-object bm saved-plot-parameters) @@ -50,10 +52,8 @@ (define dy (- left-drag-y left-click-y)) (clamp (+ altitude (* dy degrees-per-pixel)) 0 90)) - (define draw? #t) - (define left-down? #f) ; only #t if left-down happened on this snip - (define rth (make-render-thread make-bm saved-plot-parameters)) + (define draw? #t) (define update-timer #f) (define (stop-update-timer) @@ -104,13 +104,11 @@ (set! angle (new-angle)) (set! altitude (new-altitude)) (set-angles-message angle altitude) - (set! left-down? #t) (set! draw? #t) (start-update-timer)] - [(left-up) (when left-down? + [(left-up) (when (get-left-down-here?) (stop-update-timer) (set! draw? #f) - (set! left-down? #f) (worker-thread-wait rth) (set! left-drag-x mouse-x) (set! left-drag-y mouse-y) @@ -120,7 +118,7 @@ (define new-bm (worker-thread-send rth (draw-command #f angle altitude))) (when (is-a? new-bm bitmap%) (set-bitmap new-bm)))] - [(motion) (cond [left-down? + [(motion) (cond [(get-left-down-here?) (when (not (and (= left-drag-x mouse-x) (= left-drag-y mouse-y))) (set! left-drag-x mouse-x)