Skip to content

Commit

Permalink
List: refactor rendering routine to only render the items in view (fixes
Browse files Browse the repository at this point in the history
 #7)

- Changes renderList to expect an item height and all rendered items
  must match this height
- Uses vector slicing to efficiently get the sub-list of items to be
  rendered
- Renders 2 * H items at time instead of all N, where H is the height of
  the list's viewport
  • Loading branch information
jtdaugherty committed Aug 18, 2015
1 parent 74aa344 commit 693abbb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion programs/ListDemo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ drawUI l = [ui]
box = B.borderWithLabel label $
hLimit 25 $
vLimit 15 $
L.renderList l listDrawElement
L.renderList l listDrawElement 1
ui = C.vCenter $ vBox [ C.hCenter box
, " "
, C.hCenter "Press +/- to add/remove list elements."
Expand Down
52 changes: 34 additions & 18 deletions src/Brick/Widgets/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,41 @@ list name es =
in List es selIndex name

-- | Turn a list state value into a widget given an item drawing
-- function.
renderList :: List e -> (Bool -> e -> Widget) -> Widget
renderList l drawElem =
-- function. The integer specifies the height, in rows, of the widgets
-- returned by the item drawing function. All item widgets must be thism
-- any rows high.
renderList :: List e -> (Bool -> e -> Widget) -> Int -> Widget
renderList l drawElem itemHeight =
withDefAttr listAttr $
viewport (l^.listNameL) Vertical $
vBox $
drawListElements l drawElem

drawListElements :: List e -> (Bool -> e -> Widget) -> [Widget]
drawListElements l drawElem = V.toList drawnElements
where
es = l^.listElementsL
drawnElements = (flip V.imap) es $ \i e ->
let isSelected = Just i == l^.listSelectedL
elemWidget = drawElem isSelected e
makeVisible = if isSelected
then (visible . withDefAttr listSelectedAttr)
else id
in makeVisible elemWidget
drawListElements l drawElem itemHeight

drawListElements :: List e -> (Bool -> e -> Widget) -> Int -> Widget
drawListElements l drawElem itemHeight =
Widget Fixed Fixed $ do
c <- getContext

let es = V.slice start num (l^.listElementsL)
idx = case l^.listSelectedL of
Nothing -> 0
Just i -> i

start = max 0 $ idx - numPerHeight + 1
num = min (numPerHeight * 2) (V.length (l^.listElementsL) - start)
numPerHeight = (c^.availHeightL) `div` itemHeight

off = start * itemHeight

drawnElements = (flip V.imap) es $ \i e ->
let isSelected = Just (i + start) == l^.listSelectedL
elemWidget = drawElem isSelected e
makeVisible = if isSelected
then (visible . withDefAttr listSelectedAttr)
else id
in makeVisible elemWidget

render $ viewport (l^.listNameL) Vertical $
translateBy (Location (0, off)) $
vBox $ V.toList drawnElements

-- | Insert an item into a list at the specified position.
listInsert :: Int
Expand Down

0 comments on commit 693abbb

Please sign in to comment.