Skip to content

Commit 22f2f19

Browse files
committed
Don't use vertCat to combine images
1 parent 5266610 commit 22f2f19

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src-bin/example.hs

+14-8
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ main = mainWidget $ withCtrlC $ do
6565
initManager_ $ do
6666
tabNavigation
6767
let gf = grout . fixed
68-
t = tile flex
68+
t = tile (fixed 3)
6969
buttons = col $ do
7070
gf 3 $ col $ do
7171
gf 1 $ text "Select an example."
@@ -113,13 +113,19 @@ scrollingWithLayout
113113
, PerformEvent t m
114114
) => m ()
115115
scrollingWithLayout = col $ do
116-
scrollable def $ do
117-
result <- boxTitle (constant def) (constant "Tracks") $ do
118-
col $ forM [0..10] $ \n -> do
119-
grout (fixed 1) $ do
120-
textButtonStatic def $ T.pack (show n)
121-
pure n
122-
pure $ (never, result)
116+
(s, x) <- tile flex $ boxTitle (constant def) (constant "Tracks") $ scrollable def $ do
117+
result <- do
118+
forM_ [(0::Int)..10] $ \n -> do
119+
tile (fixed 5) $ do
120+
tile (fixed 4) $ textButtonStatic def $ T.pack (show n)
121+
askRegion
122+
pure (never, result)
123+
grout (fixed 1) $
124+
text $ ("Total Lines: "<>) . T.pack . show <$> _scrollable_totalLines s
125+
grout (fixed 1) $
126+
text $ ("Scroll Pos: "<>) . T.pack . show <$> _scrollable_scrollPosition s
127+
grout (fixed 1) $
128+
text $ ("Scroll Height: "<>) . T.pack . show <$> _scrollable_scrollHeight s
123129
pure ()
124130

125131

src/Reflex/Vty/Widget/Scroll.hs

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Reflex.Vty.Widget.Scroll where
44

55
import Control.Monad.Fix
66
import Data.Default
7+
import Data.List (foldl')
78
import qualified Graphics.Vty as V
89
import Reflex
910
import Reflex.Vty.Widget
@@ -55,8 +56,7 @@ scrollable
5556
-> m (Scrollable t, a)
5657
scrollable (ScrollableConfig scrollBy scrollTo startingPos onAppend) mkImg = do
5758
((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
6060
kup <- key V.KUp
6161
kdown <- key V.KDown
6262
m <- mouseScroll
@@ -84,15 +84,19 @@ scrollable (ScrollableConfig scrollBy scrollTo startingPos onAppend) mkImg = do
8484
_ -> Nothing) <$> tag onAppend update
8585
]
8686
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
8888
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
9191
return $ (,a) $ Scrollable
9292
{ _scrollable_scrollPosition = current lineIndex
9393
, _scrollable_totalLines = sz
9494
, _scrollable_scrollHeight = current dh
9595
}
96+
where
97+
cropFromTop :: Int -> V.Image -> V.Image
98+
cropFromTop rows i =
99+
V.cropTop (max 0 $ V.imageHeight i - rows) i
96100

97101
-- | Modify the scroll position by the given number of lines
98102
scrollByLines :: ScrollPos -> Int -> Int -> Int -> ScrollPos

0 commit comments

Comments
 (0)