@@ -4,6 +4,7 @@ module Reflex.Vty.Widget.Scroll where
4
4
5
5
import Control.Monad.Fix
6
6
import Data.Default
7
+ import Data.List (foldl' )
7
8
import qualified Graphics.Vty as V
8
9
import Reflex
9
10
import Reflex.Vty.Widget
@@ -55,8 +56,7 @@ scrollable
55
56
-> m (Scrollable t , a )
56
57
scrollable (ScrollableConfig scrollBy scrollTo startingPos onAppend) mkImg = do
57
58
((update, a), imgs) <- captureImages mkImg
58
- let img = V. vertCat <$> imgs
59
- let sz = V. imageHeight <$> img
59
+ let sz = foldl' max 0 . fmap V. imageHeight <$> imgs
60
60
kup <- key V. KUp
61
61
kdown <- key V. KDown
62
62
m <- mouseScroll
@@ -84,15 +84,19 @@ scrollable (ScrollableConfig scrollBy scrollTo startingPos onAppend) mkImg = do
84
84
_ -> Nothing ) <$> tag onAppend update
85
85
]
86
86
let imgsToTell height scrollPos totalLines images = case scrollPos of
87
- ScrollPos_Bottom -> V. translateY ((- 1 ) * max 0 (totalLines - height)) images
87
+ ScrollPos_Bottom -> cropFromTop ((1 ) * max 0 (totalLines - height)) <$> images
88
88
ScrollPos_Top -> images -- take height images
89
- ScrollPos_Line n -> V. translateY ((- 1 ) * n) images
90
- tellImages $ fmap ( : [] ) $ imgsToTell <$> current dh <*> current lineIndex <*> sz <*> img
89
+ ScrollPos_Line n -> cropFromTop ((1 ) * max 0 n) <$> images
90
+ tellImages $ imgsToTell <$> current dh <*> current lineIndex <*> sz <*> imgs
91
91
return $ (,a) $ Scrollable
92
92
{ _scrollable_scrollPosition = current lineIndex
93
93
, _scrollable_totalLines = sz
94
94
, _scrollable_scrollHeight = current dh
95
95
}
96
+ where
97
+ cropFromTop :: Int -> V. Image -> V. Image
98
+ cropFromTop rows i =
99
+ V. cropTop (max 0 $ V. imageHeight i - rows) i
96
100
97
101
-- | Modify the scroll position by the given number of lines
98
102
scrollByLines :: ScrollPos -> Int -> Int -> Int -> ScrollPos
0 commit comments