From c11d19a8930911d16c9bf5a9e0bdbabc19ebe4f4 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 20 Nov 2021 15:33:28 -1000 Subject: [PATCH] fix: ui: scroll selection to middle on first entry, also The viewport doesn't exist until after first render, and scrollSelectionToMiddle didn't need it; viewportScroll queues up events for it. https://github.com/jtdaugherty/brick/issues/349 --- hledger-ui/Hledger/UI/AccountsScreen.hs | 1 - hledger-ui/Hledger/UI/UIUtils.hs | 18 ++++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/hledger-ui/Hledger/UI/AccountsScreen.hs b/hledger-ui/Hledger/UI/AccountsScreen.hs index 2a8d1b214..aa0ca7a8e 100644 --- a/hledger-ui/Hledger/UI/AccountsScreen.hs +++ b/hledger-ui/Hledger/UI/AccountsScreen.hs @@ -372,7 +372,6 @@ asHandle ui0@UIState{ asHandle _ _ = error "event handler called with wrong screen type, should not happen" -- PARTIAL: asEnterRegister d selacct ui = do - -- TODO center selection after entering register screen; neither of these works till second time entering; easy strictifications didn't help rsCenterAndContinue $ -- flip rsHandle (VtyEvent (EvKey (KChar 'l') [MCtrl])) $ screenEnter d regscr ui diff --git a/hledger-ui/Hledger/UI/UIUtils.hs b/hledger-ui/Hledger/UI/UIUtils.hs index 383e53d3c..1844660af 100644 --- a/hledger-ui/Hledger/UI/UIUtils.hs +++ b/hledger-ui/Hledger/UI/UIUtils.hs @@ -42,7 +42,7 @@ import Data.List import qualified Data.Text as T import Data.Time (Day, addDays) import Graphics.Vty - (Event(..),Key(..),Modifier(..),Vty(..),Color,Attr,currentAttr,refresh + (Event(..),Key(..),Modifier(..),Vty(..),Color,Attr,currentAttr,refresh, displayBounds -- ,Output(displayBounds,mkDisplayContext),DisplayContext(..) ) import Lens.Micro.Platform @@ -321,18 +321,16 @@ withBorderAttr attr = updateAttrMap (applyAttrMappings [("border", attr)]) -- middle of the display area. scrollSelectionToMiddle :: List Name e -> EventM Name () scrollSelectionToMiddle list = do - let mselectedrow = list^.listSelectedL - vpname = list^.listNameL - mvp <- lookupViewport vpname - case (mselectedrow, mvp) of - (Just selectedrow, Just vp) -> do + case list^.listSelectedL of + Nothing -> return () + Just selectedrow -> do + Vty{outputIface} <- getVtyHandle + pageheight <- dbg4 "pageheight" . snd <$> liftIO (displayBounds outputIface) let itemheight = dbg4 "itemheight" $ list^.listItemHeightL - vpheight = dbg4 "vpheight" $ vp^.vpSize._2 - itemsperpage = dbg4 "itemsperpage" $ vpheight `div` itemheight + itemsperpage = dbg4 "itemsperpage" $ pageheight `div` itemheight toprow = dbg4 "toprow" $ max 0 (selectedrow - (itemsperpage `div` 2)) -- assuming ViewportScroll's row offset is measured in list items not screen rows - setTop (viewportScroll vpname) toprow - _ -> return () + setTop (viewportScroll $ list^.listNameL) toprow -- arrow keys vi keys emacs keys moveUpEvents = [EvKey KUp [] , EvKey (KChar 'k') [], EvKey (KChar 'p') [MCtrl]]