Skip to content

Commit 9b7311e

Browse files
change inputInFocusedRegion such that it filters mouse scroll wheel input based on focus (#73)
* maybe fix scroll wheel issue with inputInFocusedRegion * aoeu * more debugging * aoeu * aoeu * oauoeu * aoeu * update changelog
1 parent f67bf03 commit 9b7311e

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* `scrollableText` can be configured to remain scrolled to the bottom on new output, either always or whenever the user is scrolled to the bottom and new output appears.
1010
* Added a new `scrollable` widget in `Reflex.Vty.Widget.Scroll` that allows vertical scrolling when an `Image` is taller than the widget's height.
1111
* Add `ctrlc`, a convenience function that returns an event that fires when a Ctrl+c keypress is detected
12+
* Change `inputInFocusedRegion` to filter mouse scroll wheel input based on if the region is in focus rather than mouse drag tracking
1213
* Fix several issues with wide chars, cursor position and word wrapping in Zipper.hs
1314
* Add `centerText` function to Reflex.Vty.Widget.Box
1415

src/Reflex/Vty/Widget.hs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import Reflex.Class ()
2323
import Reflex.Host.Class (MonadReflexCreateTrigger)
2424
import Reflex.Vty.Host
2525

26+
import Debug.Trace
27+
2628
-- * Running a vty application
2729

2830
-- | Sets up the top-level context for a vty widget and runs it with that context
@@ -208,16 +210,18 @@ mouseInRegion (Region l t w h) e = case e of
208210
-- * 'WaitingForInput' means state will be set on next 'EvMouseDown' event
209211
data MouseTrackingState = Tracking V.Button | NotTracking | WaitingForInput deriving (Show, Eq)
210212

211-
-- | Filter mouse input outside the current display region and
212-
-- all input if the region is not focused
213-
-- mouse drag sequences that start OFF the region are NOT reported
214-
-- mouse drag sequences that start ON the region and drag off ARE reported
213+
-- | Filter mouse input outside the current display region
214+
-- keyboard input is reported only if the region is focused
215+
-- scroll wheel input is reported only if the region is focused
216+
-- mouse input is reported if the mouse is in the region
217+
-- EXCEPT mouse drag sequences that start OFF the region are NOT reported
218+
-- AND mouse drag sequences that start ON the region and drag off ARE reported
215219
inputInFocusedRegion
216220
:: forall t m. (MonadFix m, MonadHold t m, HasDisplayRegion t m, HasFocusReader t m, HasInput t m)
217221
=> m (Event t VtyEvent)
218222
inputInFocusedRegion = do
219223
inp <- input
220-
reg <- current <$> askRegion
224+
regBeh <- current <$> askRegion
221225
foc <- current <$> focus
222226
let
223227
trackMouse ::
@@ -228,11 +232,20 @@ inputInFocusedRegion = do
228232
-- sampling (as oppose to using attachPromptlyDyn) is necessary here as the focus may change from the event produced here
229233
focused <- sample foc
230234
-- strictly speaking the same could also happen here too
231-
reg'@(Region l t _ _) <- sample reg
235+
reg@(Region l t _ _) <- sample regBeh
232236
return $ case e of
237+
238+
-- filter keyboard input if region is not focused
233239
V.EvKey _ _ | not focused -> Nothing
240+
241+
-- filter scroll wheel input based on mouse position
242+
x@(V.EvMouseDown _ _ btn _) | btn == V.BScrollUp || btn == V.BScrollDown -> case tracking of
243+
trck@(Tracking _) -> Just (trck, Nothing)
244+
_ -> Just (WaitingForInput, if focused then Just x else Nothing)
245+
246+
-- only do tracking for l/m/r mouse buttons
234247
V.EvMouseDown x y btn m ->
235-
if tracking == Tracking btn || (tracking == WaitingForInput && withinRegion reg' x y)
248+
if tracking == Tracking btn || (tracking == WaitingForInput && withinRegion reg x y)
236249
then Just (Tracking btn, Just $ V.EvMouseDown (x - l) (y - t) btn m)
237250
else Just (NotTracking, Nothing)
238251
V.EvMouseUp x y mbtn -> case mbtn of

0 commit comments

Comments
 (0)