From 3a5781440222f8f65d9b187857bd5cebdfc9121a Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 18 Nov 2021 07:06:01 -1000 Subject: [PATCH] ui: debounced clicking; click top/left margin to exit --- hledger-ui/Hledger/UI/AccountsScreen.hs | 12 +++++++---- hledger-ui/Hledger/UI/RegisterScreen.hs | 25 +++++++++++++--------- hledger-ui/Hledger/UI/TransactionScreen.hs | 7 +++++- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/hledger-ui/Hledger/UI/AccountsScreen.hs b/hledger-ui/Hledger/UI/AccountsScreen.hs index b05ac8bc6..bf7a9d75c 100644 --- a/hledger-ui/Hledger/UI/AccountsScreen.hs +++ b/hledger-ui/Hledger/UI/AccountsScreen.hs @@ -323,11 +323,15 @@ asHandle ui0@UIState{ VtyEvent e | e `elem` moveRightEvents , not $ isBlankElement $ listSelectedElement _asList -> asEnterRegister d selacct ui + -- MouseDown is sometimes duplicated, https://github.com/jtdaugherty/brick/issues/347 + -- just use it to move the selection MouseDown _n BLeft _mods Location{loc=(_x,y)} | not $ (=="") clickedacct -> do - let list' = listMoveTo y _asList - asEnterRegister d clickedacct ui{aScreen=scr{_asList=list'}} - where - clickedacct = maybe "" asItemAccountName $ listElements _asList !? y + continue ui{aScreen=scr{_asList=listMoveTo y _asList}} + where clickedacct = maybe "" asItemAccountName $ listElements _asList !? y + -- and on MouseUp, enter the subscreen + MouseUp _n (Just BLeft) Location{loc=(_x,y)} | not $ (=="") clickedacct -> do + asEnterRegister d clickedacct ui + where clickedacct = maybe "" asItemAccountName $ listElements _asList !? y -- prevent moving down over blank padding items; -- instead scroll down by one, until maximally scrolled - shows the end has been reached diff --git a/hledger-ui/Hledger/UI/RegisterScreen.hs b/hledger-ui/Hledger/UI/RegisterScreen.hs index bf11f3dcb..167dbaaf0 100644 --- a/hledger-ui/Hledger/UI/RegisterScreen.hs +++ b/hledger-ui/Hledger/UI/RegisterScreen.hs @@ -348,24 +348,29 @@ rsHandle ui@UIState{ VtyEvent (EvKey (KRight) [MShift]) -> continue $ regenerateScreens j d $ nextReportPeriod journalspan ui VtyEvent (EvKey (KLeft) [MShift]) -> continue $ regenerateScreens j d $ previousReportPeriod journalspan ui VtyEvent (EvKey k []) | k `elem` [KBS, KDel] -> (continue $ regenerateScreens j d $ resetFilter ui) - VtyEvent e | e `elem` moveLeftEvents -> continue $ popScreen ui VtyEvent (EvKey (KChar 'l') [MCtrl]) -> scrollSelectionToMiddle rsList >> redraw ui VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui - -- enter transaction screen for selected transaction + -- exit screen on LEFT + VtyEvent e | e `elem` moveLeftEvents -> continue $ popScreen ui + -- or on a click in the app's left or top margin. This is a VtyEvent since not in a clickable widget. + VtyEvent (EvMouseUp x y (Just BLeft)) | x==0 || y==0 -> continue $ popScreen ui + + -- enter transaction screen on RIGHT VtyEvent e | e `elem` moveRightEvents -> case listSelectedElement rsList of Just _ -> continue $ screenEnter d transactionScreen{tsAccount=rsAccount} ui Nothing -> continue ui - - -- or clicked transaction + -- or on transaction click + -- MouseDown is sometimes duplicated, https://github.com/jtdaugherty/brick/issues/347 + -- just use it to move the selection MouseDown _n BLeft _mods Location{loc=(_x,y)} | not $ (=="") clickeddate -> do - let - list' = listMoveTo y rsList - ui' = ui{aScreen=s{rsList=list'}} - continue $ screenEnter d transactionScreen{tsAccount=rsAccount} ui' - where - clickeddate = maybe "" rsItemDate $ listElements rsList !? y + continue $ ui{aScreen=s{rsList=listMoveTo y rsList}} + where clickeddate = maybe "" rsItemDate $ listElements rsList !? y + -- and on MouseUp, enter the subscreen + MouseUp _n (Just BLeft) Location{loc=(_x,y)} | not $ (=="") clickeddate -> do + continue $ screenEnter d transactionScreen{tsAccount=rsAccount} ui + where clickeddate = maybe "" rsItemDate $ listElements rsList !? y -- when at the last item, instead of moving down, scroll down by one, until maximally scrolled VtyEvent e | e `elem` moveDownEvents, isBlankElement mnextelement -> do diff --git a/hledger-ui/Hledger/UI/TransactionScreen.hs b/hledger-ui/Hledger/UI/TransactionScreen.hs index aa4f28908..b6f1373f3 100644 --- a/hledger-ui/Hledger/UI/TransactionScreen.hs +++ b/hledger-ui/Hledger/UI/TransactionScreen.hs @@ -15,7 +15,7 @@ import Data.Maybe import qualified Data.Text as T import Data.Time.Calendar (Day) import qualified Data.Vector as V -import Graphics.Vty (Event(..),Key(..),Modifier(..)) +import Graphics.Vty (Event(..),Key(..),Modifier(..), Button (BLeft)) import Lens.Micro ((^.)) import Brick import Brick.Widgets.List (listElementsL, listMoveTo, listSelectedElement) @@ -181,7 +181,12 @@ tsHandle ui@UIState{aScreen=TransactionScreen{tsTransaction=(i,t), tsTransaction VtyEvent e | e `elem` moveUpEvents -> continue $ tsSelect iprev tprev ui VtyEvent e | e `elem` moveDownEvents -> continue $ tsSelect inext tnext ui + + -- exit screen on LEFT VtyEvent e | e `elem` moveLeftEvents -> continue . popScreen $ tsSelect i t ui -- Probably not necessary to tsSelect here, but it's safe. + -- or on a click in the app's left or top margin. This is a VtyEvent since not in a clickable widget. + VtyEvent (EvMouseUp x y (Just BLeft)) | x==0 || y==0 -> continue . popScreen $ tsSelect i t ui + VtyEvent (EvKey (KChar 'l') [MCtrl]) -> redraw ui VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui _ -> continue ui