Skip to content

Commit

Permalink
Reset scroll position when switching modals (#1971)
Browse files Browse the repository at this point in the history
Closes #1901 

When switching description modals or info panel in the bottom left corner, the viewport used to retail the scrolled state.
This PR attempts to reset the scroll position when we switch modals.
  • Loading branch information
nitinprakash96 authored Jun 23, 2024
1 parent 9fe1184 commit 92a6524
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
17 changes: 10 additions & 7 deletions src/swarm-tui/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,12 @@ handleMainEvent ev = do
_ev -> do
fring <- use $ uiState . uiGameplay . uiFocusRing
case focusGetCurrent fring of
Just (FocusablePanel x) -> ($ ev) $ case x of
REPLPanel -> handleREPLEvent
WorldPanel -> handleWorldEvent
WorldEditorPanel -> EC.handleWorldEditorPanelEvent
RobotPanel -> handleRobotPanelEvent
InfoPanel -> handleInfoPanelEvent infoScroll
Just (FocusablePanel x) -> case x of
REPLPanel -> handleREPLEvent ev
WorldPanel -> handleWorldEvent ev
WorldEditorPanel -> EC.handleWorldEditorPanelEvent ev
RobotPanel -> handleRobotPanelEvent ev
InfoPanel -> handleInfoPanelEvent infoScroll ev
_ -> continueWithoutRedraw

-- | Set the game to Running if it was (auto) paused otherwise to paused.
Expand Down Expand Up @@ -1461,6 +1461,7 @@ handleInventoryListEvent ev = do
case mList of
Nothing -> continueWithoutRedraw
Just l -> do
when (isValidListMovement ev) $ resetViewport infoScroll
l' <- nestEventM' l (handleListEventWithSeparators ev (is _Separator))
uiState . uiGameplay . uiInventory . uiInventoryList . _Just . _2 .= l'

Expand All @@ -1480,7 +1481,8 @@ handleInventorySearchEvent = \case
uiInventorySearch .= Nothing
gets focusedEntity >>= maybe continueWithoutRedraw descriptionModal
-- Any old character: append to the current search string
CharKey c ->
CharKey c -> do
resetViewport infoScroll
Brick.zoom (uiState . uiGameplay . uiInventory) $ do
uiInventoryShouldUpdate .= True
uiInventorySearch %= fmap (`snoc` c)
Expand Down Expand Up @@ -1510,6 +1512,7 @@ makeEntity e = do
descriptionModal :: Entity -> EventM Name AppState ()
descriptionModal e = do
s <- get
resetViewport modalScroll
uiState . uiGameplay . uiModal ?= generateModal s (DescriptionModal e)

------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions src/swarm-tui/Swarm/TUI/Controller/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pattern FKey c = VtyEvent (V.EvKey (V.KFun c) [])

openModal :: ModalType -> EventM Name AppState ()
openModal mt = do
resetViewport modalScroll
newModal <- gets $ flip generateModal mt
ensurePause
uiState . uiGameplay . uiModal ?= newModal
Expand Down Expand Up @@ -110,3 +111,9 @@ hasDebugCapability :: Bool -> AppState -> Bool
hasDebugCapability isCreative s =
maybe isCreative (S.member CDebug . getCapabilitySet) $
s ^? gameState . to focusedRobot . _Just . robotCapabilities

-- | Resets the viewport scroll position
resetViewport :: ViewportScroll Name -> EventM Name AppState ()
resetViewport n = do
vScrollToBeginning n
hScrollToBeginning n
35 changes: 22 additions & 13 deletions src/swarm-tui/Swarm/TUI/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
--
-- A special modified version of 'Brick.Widgets.List.handleListEvent'
-- to deal with skipping over separators.
module Swarm.TUI.List (handleListEventWithSeparators) where
module Swarm.TUI.List (
handleListEventWithSeparators,
isValidListMovement,
) where

import Brick (EventM)
import Brick.Widgets.List qualified as BL
Expand All @@ -20,15 +23,21 @@ handleListEventWithSeparators ::
(e -> Bool) ->
EventM n (BL.GenericList n t e) ()
handleListEventWithSeparators e isSep =
listSkip isSep movement
where
movement = case e of
V.EvKey V.KUp [] -> Move One Bwd
V.EvKey (V.KChar 'k') [] -> Move One Bwd
V.EvKey V.KDown [] -> Move One Fwd
V.EvKey (V.KChar 'j') [] -> Move One Fwd
V.EvKey V.KHome [] -> Move Most Bwd
V.EvKey V.KEnd [] -> Move Most Fwd
V.EvKey V.KPageDown [] -> Move Page Fwd
V.EvKey V.KPageUp [] -> Move Page Bwd
_ -> NoMove
listSkip isSep (movement e)

-- | A movement is considered legitimate when a key event
-- results in @Move@ action.
isValidListMovement :: V.Event -> Bool
isValidListMovement e = movement e /= NoMove

movement :: V.Event -> Move
movement = \case
V.EvKey V.KUp [] -> Move One Bwd
V.EvKey (V.KChar 'k') [] -> Move One Bwd
V.EvKey V.KDown [] -> Move One Fwd
V.EvKey (V.KChar 'j') [] -> Move One Fwd
V.EvKey V.KHome [] -> Move Most Bwd
V.EvKey V.KEnd [] -> Move Most Fwd
V.EvKey V.KPageDown [] -> Move Page Fwd
V.EvKey V.KPageUp [] -> Move Page Bwd
_ -> NoMove

0 comments on commit 92a6524

Please sign in to comment.