Fix dropdown event priority issues

This commit is contained in:
Jeremy Penner 2021-04-24 15:36:19 -04:00
parent 34766e3709
commit a8d77b232c

View file

@ -76,10 +76,16 @@
(local (mx my) (values (love.mouse.getX) (love.mouse.getY))) (local (mx my) (values (love.mouse.getX) (love.mouse.getY)))
(and (>= mx x) (<= mx (+ x w)) (>= my y) (<= my (+ y h)))) (and (>= mx x) (<= mx (+ x w)) (>= my y) (<= my (+ y h))))
(fn consume-pressed [view button]
(when (= (. view.imstate button) :pressed)
(tset view.imstate button :down)
true))
(fn activate [view tag x y w h] (fn activate [view tag x y w h]
(when (and (= view.imstate.left :pressed) (mouse-inside x y w h)) (when (and (mouse-inside x y w h) (consume-pressed view :left))
(set view.imstate.active (make-tag tag)) (set view.imstate.active (make-tag tag))
true)) true))
(fn active? [view tag] (= view.imstate.active (make-tag tag))) (fn active? [view tag] (= view.imstate.active (make-tag tag)))
(fn button [view tag x y w h] (fn button [view tag x y w h]
(when (mouse-inside x y w h) (set view.cursor :hand)) (when (mouse-inside x y w h) (set view.cursor :hand))
@ -165,6 +171,7 @@
(fn textbox [view tag text x y w] (fn textbox [view tag text x y w]
(var textNew (or text "")) (var textNew (or text ""))
(local (h hText xText yText) (values (+ (style.font:get_height) 4) (style.font:get_height) (+ x 2) (+ y 2))) (local (h hText xText yText) (values (+ (style.font:get_height) 4) (style.font:get_height) (+ x 2) (+ y 2)))
(local initial-press (= view.imstate.left :pressed))
; handle key events ; handle key events
(when (focus view tag x y w h {:i 1 :iAnchor 1 :blink (love.timer.getTime)}) (when (focus view tag x y w h {:i 1 :iAnchor 1 :blink (love.timer.getTime)})
@ -193,7 +200,7 @@
(when (mouse-inside x y w h) (set view.cursor :ibeam)) (when (mouse-inside x y w h) (set view.cursor :ibeam))
(when (and (focused? view tag) (active? view tag) (mouse-inside x y w h)) (when (and (focused? view tag) (active? view tag) (mouse-inside x y w h))
(local mouse-i (i-from-x textNew (love.mouse.getX) x style.font)) (local mouse-i (i-from-x textNew (love.mouse.getX) x style.font))
(when (= view.imstate.left :pressed) (when initial-press
(set view.imstate.focus.iAnchor mouse-i)) (set view.imstate.focus.iAnchor mouse-i))
(set view.imstate.focus.i mouse-i)) (set view.imstate.focus.i mouse-i))
@ -225,25 +232,27 @@
(fn dropdown [view tag selection options x y w] (fn dropdown [view tag selection options x y w]
(local row-h (+ (style.font:get_height) style.padding.y)) (local row-h (+ (style.font:get_height) style.padding.y))
(local new-selection (and (focused? view tag) view.imstate.focus.selection)) (var new-selection nil)
(local focused-h (if (focused? view tag) (* row-h (+ (length options) 1)) row-h))
(when new-selection (set view.imstate.focus nil))
(renderer.draw_rect x y w row-h style.selection) (renderer.draw_rect x y w row-h style.selection)
(renderer.draw_text style.font selection (+ x style.padding.x) (+ y (/ style.padding.y 2)) style.text) (renderer.draw_text style.font selection (+ x style.padding.x) (+ y (/ style.padding.y 2)) style.text)
(renderer.draw_text style.icon_font "-" (+ x w (- style.padding.x)) (+ y (/ style.padding.y 2)) style.text) (renderer.draw_text style.icon_font "-" (+ x w (- style.padding.x)) (+ y (/ style.padding.y 2)) style.text)
(postpone view (when (focused? view tag)
#(when (focus view tag x y w focused-h)
(var row-y (+ y row-h)) (var row-y (+ y row-h))
(each [i option (ipairs options)] (each [i option (ipairs options)]
(renderer.draw_rect x row-y w row-h style.selection) (renderer.draw_rect x row-y w row-h style.selection)
(renderer.draw_text style.font option (+ x style.padding.x) (+ row-y (/ style.padding.y 2)) style.text) (renderer.draw_text style.font option (+ x style.padding.x) (+ row-y (/ style.padding.y 2)) style.text)
(when (button view [(make-tag tag) i] x row-y w row-h) (when (button view [(make-tag tag) i] x row-y w row-h)
(set view.imstate.focus.selection option)) (set new-selection option))
(set row-y (+ row-y row-h))))) (set row-y (+ row-y row-h)))
(postpone view (fn []
(var row-y (+ y row-h))
(each [i option (ipairs options)]
(renderer.draw_rect x row-y w row-h style.selection)
(renderer.draw_text style.font option (+ x style.padding.x) (+ row-y (/ style.padding.y 2)) style.text)
(set row-y (+ row-y row-h))))))
(focus view tag x y w row-h)
(values (or new-selection selection) (+ y row-h))) (values (or new-selection selection) (+ y row-h)))
{: attach-imstate : cmd-predicate : postpone : mouse-inside : activate : active? {: attach-imstate : cmd-predicate : postpone : mouse-inside : activate : active?