Skip to content

Commit

Permalink
Don't use vertCat to combine images
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-abrar committed Nov 10, 2024
1 parent 5266610 commit 22f2f19
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
22 changes: 14 additions & 8 deletions src-bin/example.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ main = mainWidget $ withCtrlC $ do
initManager_ $ do
tabNavigation
let gf = grout . fixed
t = tile flex
t = tile (fixed 3)
buttons = col $ do
gf 3 $ col $ do
gf 1 $ text "Select an example."
Expand Down Expand Up @@ -113,13 +113,19 @@ scrollingWithLayout
, PerformEvent t m
) => m ()
scrollingWithLayout = col $ do
scrollable def $ do
result <- boxTitle (constant def) (constant "Tracks") $ do
col $ forM [0..10] $ \n -> do
grout (fixed 1) $ do
textButtonStatic def $ T.pack (show n)
pure n
pure $ (never, result)
(s, x) <- tile flex $ boxTitle (constant def) (constant "Tracks") $ scrollable def $ do

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 8.8.4 on ubuntu-latest

Defined but not used: ‘x’

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10.7 on ubuntu-latest

Defined but not used: ‘x’

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 8.6.5 on ubuntu-latest

Defined but not used: ‘x’

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 8.4.4 on ubuntu-latest

Defined but not used: ‘x’

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4.5 on ubuntu-latest

Defined but not used: ‘x’

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 9.6.1 on ubuntu-latest

Defined but not used: ‘x’

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 8.4.4 on macos-latest

Defined but not used: ‘x’

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10.7 on macos-latest

Defined but not used: ‘x’

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 8.6.5 on macos-latest

Defined but not used: ‘x’

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 8.8.4 on macos-latest

Defined but not used: ‘x’

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4.5 on macos-latest

Defined but not used: ‘x’

Check warning on line 116 in src-bin/example.hs

View workflow job for this annotation

GitHub Actions / GHC 9.6.1 on macos-latest

Defined but not used: ‘x’
result <- do
forM_ [(0::Int)..10] $ \n -> do
tile (fixed 5) $ do
tile (fixed 4) $ textButtonStatic def $ T.pack (show n)
askRegion
pure (never, result)
grout (fixed 1) $
text $ ("Total Lines: "<>) . T.pack . show <$> _scrollable_totalLines s
grout (fixed 1) $
text $ ("Scroll Pos: "<>) . T.pack . show <$> _scrollable_scrollPosition s
grout (fixed 1) $
text $ ("Scroll Height: "<>) . T.pack . show <$> _scrollable_scrollHeight s
pure ()


Expand Down
14 changes: 9 additions & 5 deletions src/Reflex/Vty/Widget/Scroll.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Reflex.Vty.Widget.Scroll where

import Control.Monad.Fix
import Data.Default
import Data.List (foldl')
import qualified Graphics.Vty as V
import Reflex
import Reflex.Vty.Widget
Expand Down Expand Up @@ -55,8 +56,7 @@ scrollable
-> m (Scrollable t, a)
scrollable (ScrollableConfig scrollBy scrollTo startingPos onAppend) mkImg = do
((update, a), imgs) <- captureImages mkImg
let img = V.vertCat <$> imgs
let sz = V.imageHeight <$> img
let sz = foldl' max 0 . fmap V.imageHeight <$> imgs
kup <- key V.KUp
kdown <- key V.KDown
m <- mouseScroll
Expand Down Expand Up @@ -84,15 +84,19 @@ scrollable (ScrollableConfig scrollBy scrollTo startingPos onAppend) mkImg = do
_ -> Nothing) <$> tag onAppend update
]
let imgsToTell height scrollPos totalLines images = case scrollPos of
ScrollPos_Bottom -> V.translateY ((-1) * max 0 (totalLines - height)) images
ScrollPos_Bottom -> cropFromTop ((1) * max 0 (totalLines - height)) <$> images
ScrollPos_Top -> images -- take height images
ScrollPos_Line n -> V.translateY ((-1) * n) images
tellImages $ fmap (:[]) $ imgsToTell <$> current dh <*> current lineIndex <*> sz <*> img
ScrollPos_Line n -> cropFromTop ((1) * max 0 n) <$> images
tellImages $ imgsToTell <$> current dh <*> current lineIndex <*> sz <*> imgs
return $ (,a) $ Scrollable
{ _scrollable_scrollPosition = current lineIndex
, _scrollable_totalLines = sz
, _scrollable_scrollHeight = current dh
}
where
cropFromTop :: Int -> V.Image -> V.Image
cropFromTop rows i =
V.cropTop (max 0 $ V.imageHeight i - rows) i

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

0 comments on commit 22f2f19

Please sign in to comment.