Skip to content

Commit

Permalink
Use current keybindings for recipes and commands in popups
Browse files Browse the repository at this point in the history
  • Loading branch information
byorgey committed Jul 14, 2024
1 parent 0b98f12 commit 4d43dca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
25 changes: 5 additions & 20 deletions src/swarm-tui/Swarm/TUI/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ module Swarm.TUI.View (
import Brick hiding (Direction, Location)
import Brick.Focus
import Brick.Forms
import Brick.Keybindings (Binding (..), firstActiveBinding, ppBinding)
import Brick.Widgets.Border (
hBorder,
hBorderWithLabel,
Expand Down Expand Up @@ -70,7 +69,6 @@ import Data.Set qualified as Set (toList)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Time (NominalDiffTime, defaultTimeLocale, formatTime)
import Graphics.Vty qualified as V
import Linear
import Network.Wai.Handler.Warp (Port)
import Numeric (showFFloat)
Expand Down Expand Up @@ -135,7 +133,6 @@ import Swarm.TUI.Inventory.Sorting (renderSortMethod)
import Swarm.TUI.Launch.Model
import Swarm.TUI.Launch.View
import Swarm.TUI.Model
import Swarm.TUI.Model.Event (SwarmEvent)
import Swarm.TUI.Model.Event qualified as SE
import Swarm.TUI.Model.Goal (goalsContent, hasAnythingToShow)
import Swarm.TUI.Model.KeyBindings (handlerNameKeysDescription)
Expand Down Expand Up @@ -979,7 +976,7 @@ drawModalMenu s = vLimit 1 . hBox $ map (padLeftRight 1 . drawKeyCmd) globalKeyC
, notificationKey messageNotifications SE.ViewMessagesEvent "Messages"
, structuresKey
]
keyM = bindingText s . SE.Main
keyM = VU.bindingText s . SE.Main

-- | Draw a menu explaining what key commands are available for the
-- current panel. This menu is displayed as one or two lines in
Expand Down Expand Up @@ -1100,22 +1097,10 @@ drawKeyMenu s =
]
keyCmdsFor (Just (FocusablePanel InfoPanel)) = []
keyCmdsFor _ = []
keyM = bindingText s . SE.Main
keyR = bindingText s . SE.REPL
keyE = bindingText s . SE.Robot
keyW = bindingText s . SE.World

bindingText :: AppState -> SwarmEvent -> Text
bindingText s e = maybe "" ppBindingShort b
where
conf = s ^. keyEventHandling . keyConfig
b = firstActiveBinding conf e
ppBindingShort = \case
Binding V.KUp m | null m -> ""
Binding V.KDown m | null m -> ""
Binding V.KLeft m | null m -> ""
Binding V.KRight m | null m -> ""
bi -> ppBinding bi
keyM = VU.bindingText s . SE.Main
keyR = VU.bindingText s . SE.REPL
keyE = VU.bindingText s . SE.Robot
keyW = VU.bindingText s . SE.World

data KeyHighlight = NoHighlight | Alert | PanelSpecific

Expand Down
12 changes: 7 additions & 5 deletions src/swarm-tui/Swarm/TUI/View/Popup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import Swarm.Game.Achievement.Definitions (title)
import Swarm.Game.Achievement.Description (describe)
import Swarm.Language.Syntax (constInfo, syntax)
import Swarm.TUI.Model (AppState, Name, uiState)
import Swarm.TUI.Model.Event qualified as SE
import Swarm.TUI.Model.Popup (Popup (..), currentPopup, popupFrames)
import Swarm.TUI.Model.UI (uiPopups)
import Swarm.TUI.View.Attribute.Attr (notifAttr)
import Swarm.TUI.View.Util (bindingText)
import Swarm.Util (commaList, squote)

-- | The number of frames taken by each step of the notification popup
Expand All @@ -30,11 +32,11 @@ drawPopups :: AppState -> Widget Name
drawPopups s = hCenterLayer $
case s ^. uiState . uiPopups . currentPopup of
Just (notif, f) ->
cropTopTo (popupRows f) . border . padLeftRight 2 $ drawPopup notif
cropTopTo (popupRows f) . border . padLeftRight 2 $ drawPopup s notif
_ -> emptyWidget

drawPopup :: Popup -> Widget Name
drawPopup = \case
drawPopup :: AppState -> Popup -> Widget Name
drawPopup s = \case
AchievementPopup ach ->
hBox
[ withAttr notifAttr (txt "Achievement unlocked: ")
Expand All @@ -43,15 +45,15 @@ drawPopup = \case
RecipesPopup _ ->
hBox
[ withAttr notifAttr (txt "New recipes unlocked! ")
, txt "[F3] to view."
, txt $ bindingText s (SE.Main SE.ViewRecipesEvent) <> " to view."
]
CommandsPopup cmds ->
vBox
[ hBox
[ withAttr notifAttr (txt "New commands unlocked: ")
, txt . commaList $ map (squote . syntax . constInfo) cmds
]
, txt "Hit [F4] to view all available commands."
, txt $ "Hit " <> bindingText s (SE.Main SE.ViewCommandsEvent) <> " to view all available commands."
]

-- | Compute the number of rows of the notification popup we should be
Expand Down
15 changes: 15 additions & 0 deletions src/swarm-tui/Swarm/TUI/View/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Swarm.TUI.View.Util where

import Brick hiding (Direction, Location)
import Brick.Keybindings (Binding (..), firstActiveBinding, ppBinding)
import Brick.Widgets.Dialog
import Brick.Widgets.List qualified as BL
import Control.Lens hiding (Const, from)
Expand All @@ -29,6 +30,7 @@ import Swarm.Language.Syntax (Syntax)
import Swarm.Language.Text.Markdown qualified as Markdown
import Swarm.Language.Types (Polytype)
import Swarm.TUI.Model
import Swarm.TUI.Model.Event (SwarmEvent)
import Swarm.TUI.Model.UI
import Swarm.TUI.View.Attribute.Attr
import Swarm.TUI.View.CellDisplay
Expand Down Expand Up @@ -240,3 +242,16 @@ drawLabelledEntityName e =
[ padRight (Pad 2) (renderDisplay (e ^. entityDisplay))
, txt (e ^. entityName)
]

-- | Render the keybinding bound to a specific event.
bindingText :: AppState -> SwarmEvent -> Text
bindingText s e = maybe "" ppBindingShort b
where
conf = s ^. keyEventHandling . keyConfig
b = firstActiveBinding conf e
ppBindingShort = \case
Binding V.KUp m | null m -> ""
Binding V.KDown m | null m -> ""
Binding V.KLeft m | null m -> ""
Binding V.KRight m | null m -> ""
bi -> ppBinding bi

0 comments on commit 4d43dca

Please sign in to comment.