mirror of
https://github.com/jtdaugherty/brick.git
synced 2024-09-11 15:55:54 +03:00
List: pass the item height to the constructor instead of the draw function to make it available to event handlers
This commit is contained in:
parent
ee64f2faf4
commit
1671c3732c
@ -37,7 +37,7 @@ drawUI l = [ui]
|
||||
box = B.borderWithLabel label $
|
||||
hLimit 25 $
|
||||
vLimit 15 $
|
||||
L.renderList l listDrawElement 1
|
||||
L.renderList l listDrawElement
|
||||
ui = C.vCenter $ vBox [ C.hCenter box
|
||||
, str " "
|
||||
, C.hCenter $ str "Press +/- to add/remove list elements."
|
||||
@ -68,7 +68,7 @@ listDrawElement sel i =
|
||||
in C.hCenter $ str "Item " <+> (selStr $ show i)
|
||||
|
||||
initialState :: L.List Int
|
||||
initialState = L.list (T.Name "list") (V.fromList [0, 1, 2])
|
||||
initialState = L.list (T.Name "list") (V.fromList [0, 1, 2]) 1
|
||||
|
||||
customAttr :: A.AttrName
|
||||
customAttr = L.listSelectedAttr <> "custom"
|
||||
|
@ -4,7 +4,7 @@
|
||||
-- | This module provides a scrollable list type and functions for
|
||||
-- manipulating and rendering it.
|
||||
module Brick.Widgets.List
|
||||
( List(listElements, listSelected, listName)
|
||||
( List(listElements, listSelected, listName, listItemHeight)
|
||||
|
||||
-- * Consructing a list
|
||||
, list
|
||||
@ -16,6 +16,7 @@ module Brick.Widgets.List
|
||||
, listElementsL
|
||||
, listSelectedL
|
||||
, listNameL
|
||||
, listItemHeightL
|
||||
|
||||
-- * Manipulating a list
|
||||
, listMoveBy
|
||||
@ -80,22 +81,25 @@ list :: Name
|
||||
-- ^ The list name (must be unique)
|
||||
-> V.Vector e
|
||||
-- ^ The initial list contents
|
||||
-> Int
|
||||
-- ^ The list item height in rows (all list item widgets must be
|
||||
-- this high)
|
||||
-> List e
|
||||
list name es =
|
||||
list name es h =
|
||||
let selIndex = if V.null es then Nothing else Just 0
|
||||
in List es selIndex name
|
||||
in List es selIndex name h
|
||||
|
||||
-- | Turn a list state value into a widget given an item drawing
|
||||
-- function. The integer specifies the height, in rows, of the widgets
|
||||
-- returned by the item drawing function. All item widgets must be this
|
||||
-- many rows high.
|
||||
renderList :: List e -> (Bool -> e -> Widget) -> Int -> Widget
|
||||
renderList l drawElem itemHeight =
|
||||
renderList :: List e -> (Bool -> e -> Widget) -> Widget
|
||||
renderList l drawElem =
|
||||
withDefAttr listAttr $
|
||||
drawListElements l drawElem itemHeight
|
||||
drawListElements l drawElem
|
||||
|
||||
drawListElements :: List e -> (Bool -> e -> Widget) -> Int -> Widget
|
||||
drawListElements l drawElem itemHeight =
|
||||
drawListElements :: List e -> (Bool -> e -> Widget) -> Widget
|
||||
drawListElements l drawElem =
|
||||
Widget Fixed Fixed $ do
|
||||
c <- getContext
|
||||
|
||||
@ -106,9 +110,9 @@ drawListElements l drawElem itemHeight =
|
||||
|
||||
start = max 0 $ idx - numPerHeight + 1
|
||||
num = min (numPerHeight * 2) (V.length (l^.listElementsL) - start)
|
||||
numPerHeight = (c^.availHeightL) `div` itemHeight
|
||||
numPerHeight = (c^.availHeightL) `div` (l^.listItemHeightL)
|
||||
|
||||
off = start * itemHeight
|
||||
off = start * (l^.listItemHeightL)
|
||||
|
||||
drawnElements = (flip V.imap) es $ \i e ->
|
||||
let isSelected = Just (i + start) == l^.listSelectedL
|
||||
|
Loading…
Reference in New Issue
Block a user