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)))
(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]
(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))
true))
(fn active? [view tag] (= view.imstate.active (make-tag tag)))
(fn button [view tag x y w h]
(when (mouse-inside x y w h) (set view.cursor :hand))
@ -165,6 +171,7 @@
(fn textbox [view tag text x y w]
(var textNew (or text ""))
(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
(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 (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))
(when (= view.imstate.left :pressed)
(when initial-press
(set view.imstate.focus.iAnchor mouse-i))
(set view.imstate.focus.i mouse-i))
@ -225,25 +232,27 @@
(fn dropdown [view tag selection options x y w]
(local row-h (+ (style.font:get_height) style.padding.y))
(local new-selection (and (focused? view tag) view.imstate.focus.selection))
(local focused-h (if (focused? view tag) (* row-h (+ (length options) 1)) row-h))
(when new-selection (set view.imstate.focus nil))
(var new-selection nil)
(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.icon_font "-" (+ x w (- style.padding.x)) (+ y (/ style.padding.y 2)) style.text)
(postpone view
#(when (focus view tag x y w focused-h)
(when (focused? view tag)
(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)
(when (button view [(make-tag tag) i] x row-y w row-h)
(set new-selection option))
(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)
(when (button view [(make-tag tag) i] x row-y w row-h)
(set view.imstate.focus.selection option))
(set row-y (+ row-y row-h)))))
(set row-y (+ row-y row-h))))))
(focus view tag x y w row-h)
(values (or new-selection selection) (+ y row-h)))
{: attach-imstate : cmd-predicate : postpone : mouse-inside : activate : active?