Skip to content

Commit

Permalink
Consolidate more UI code into UI.hs (#901)
Browse files Browse the repository at this point in the history
Also fixes module pluralization according to [this comment](#880 (comment)).

This is a no-op refactoring, only involving renaming/code relocations.

Towards #707.
  • Loading branch information
kostmo authored Dec 14, 2022
1 parent 158d861 commit 066dd43
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/Swarm/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Swarm.Game.Robot (LogSource (ErrorTrace, Said))
import Swarm.TUI.Attr
import Swarm.TUI.Controller
import Swarm.TUI.Model
import Swarm.TUI.Model.StateUpdates
import Swarm.TUI.Model.StateUpdate
import Swarm.TUI.View
import Swarm.Version (getNewerReleaseVersion)
import Swarm.Web
Expand Down
4 changes: 2 additions & 2 deletions src/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ import Swarm.Language.Requirement qualified as R
import Swarm.Language.Syntax
import Swarm.Language.Typed (Typed (..))
import Swarm.Language.Types
import Swarm.TUI.Controller.Utils
import Swarm.TUI.Controller.Util
import Swarm.TUI.Inventory.Sorting (cycleSortDirection, cycleSortOrder)
import Swarm.TUI.List
import Swarm.TUI.Model
import Swarm.TUI.Model.Repl
import Swarm.TUI.Model.StateUpdates
import Swarm.TUI.Model.StateUpdate
import Swarm.TUI.View (generateModal)
import Swarm.Util hiding ((<<.=))
import Swarm.Version (NewReleaseFailure (..))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE PatternSynonyms #-}

module Swarm.TUI.Controller.Utils where
module Swarm.TUI.Controller.Util where

import Brick hiding (Direction)
import Graphics.Vty qualified as V
Expand Down
68 changes: 2 additions & 66 deletions src/Swarm/TUI/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ module Swarm.TUI.Model (
replHistory,
newREPLEditor,

-- ** Initialization
initFocusRing,
initLgTicksPerSecond,
initUIState,

-- ** Updating
populateInventoryList,
infoScroll,
Expand Down Expand Up @@ -147,7 +142,6 @@ module Swarm.TUI.Model (
) where

import Brick
import Brick.Focus
import Brick.Widgets.List qualified as BL
import Control.Lens hiding (from, (<.>))
import Control.Monad.Except
Expand All @@ -156,7 +150,6 @@ import Data.List (findIndex)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Vector qualified as V
import GitHash (GitInfo)
import Graphics.Vty (ColorMode (..))
Expand All @@ -170,13 +163,11 @@ import Swarm.Game.ScenarioInfo (
)
import Swarm.Game.State
import Swarm.TUI.Inventory.Sorting
import Swarm.TUI.Model.Menus
import Swarm.TUI.Model.Names
import Swarm.TUI.Model.Menu
import Swarm.TUI.Model.Name
import Swarm.TUI.Model.Repl
import Swarm.TUI.Model.UI
import Swarm.Util
import Swarm.Version (NewReleaseFailure (NoMainUpstreamRelease))
import System.Clock

------------------------------------------------------------
-- Custom UI label types
Expand Down Expand Up @@ -292,61 +283,6 @@ focusedEntity =
InventoryEntry _ e -> Just e
InstalledEntry e -> Just e

--------------------------------------------------
-- UIState initialization

-- | The initial state of the focus ring.
initFocusRing :: FocusRing Name
initFocusRing = focusRing $ map FocusablePanel listEnums

-- | The initial tick speed.
initLgTicksPerSecond :: Int
initLgTicksPerSecond = 4 -- 2^4 = 16 ticks / second

-- | Initialize the UI state. This needs to be in the IO monad since
-- it involves reading a REPL history file, getting the current
-- time, and loading text files from the data directory. The @Bool@
-- parameter indicates whether we should start off by showing the
-- main menu.
initUIState :: Bool -> Bool -> ExceptT Text IO UIState
initUIState showMainMenu cheatMode = liftIO $ do
historyT <- readFileMayT =<< getSwarmHistoryPath False
appDataMap <- readAppData
let history = maybe [] (map REPLEntry . T.lines) historyT
startTime <- getTime Monotonic
return $
UIState
{ _uiMenu = if showMainMenu then MainMenu (mainMenu NewGame) else NoMenu
, _uiPlaying = not showMainMenu
, _uiCheatMode = cheatMode
, _uiFocusRing = initFocusRing
, _uiWorldCursor = Nothing
, _uiREPL = initREPLState $ newREPLHistory history
, _uiInventory = Nothing
, _uiInventorySort = defaultSortOptions
, _uiMoreInfoTop = False
, _uiMoreInfoBot = False
, _uiScrollToEnd = False
, _uiError = Nothing
, _uiModal = Nothing
, _uiGoal = Nothing
, _uiShowFPS = False
, _uiShowZero = True
, _uiHideRobotsUntil = startTime - 1
, _uiInventoryShouldUpdate = False
, _uiTPF = 0
, _uiFPS = 0
, _lgTicksPerSecond = initLgTicksPerSecond
, _lastFrameTime = startTime
, _accumulatedTime = 0
, _lastInfoTime = 0
, _tickCount = 0
, _frameCount = 0
, _frameTickCount = 0
, _appData = appDataMap
, _scenarioRef = Nothing
}

------------------------------------------------------------
-- Functions for updating the UI state
------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/Swarm/TUI/Model/Menus.hs → src/Swarm/TUI/Model/Menu.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ViewPatterns #-}

module Swarm.TUI.Model.Menus where
module Swarm.TUI.Model.Menu where

import Brick.Widgets.Dialog (Dialog)
import Brick.Widgets.List qualified as BL
Expand All @@ -24,7 +24,7 @@ import Swarm.Game.ScenarioInfo (
scenarioCollectionToList,
)
import Swarm.Game.State
import Swarm.TUI.Model.Names
import Swarm.TUI.Model.Name
import Swarm.Util
import System.FilePath (dropTrailingPathSeparator, splitPath, takeFileName)
import Witch (into)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Swarm.TUI.Model.Names where
module Swarm.TUI.Model.Name where

data FocusablePanel
= -- | The panel containing the REPL.
Expand Down
2 changes: 1 addition & 1 deletion src/Swarm/TUI/Model/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Zipper qualified as TZ
import Swarm.Language.Types
import Swarm.TUI.Model.Names
import Swarm.TUI.Model.Name

------------------------------------------------------------
-- REPL History
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Swarm.TUI.Model.StateUpdates (
module Swarm.TUI.Model.StateUpdate (
initAppState,
startGame,
restartGame,
Expand Down Expand Up @@ -31,6 +31,7 @@ import Swarm.Game.State
import Swarm.TUI.Inventory.Sorting
import Swarm.TUI.Model
import Swarm.TUI.Model.Repl
import Swarm.TUI.Model.UI
import System.Clock

-- | Initialize the 'AppState'.
Expand Down
67 changes: 65 additions & 2 deletions src/Swarm/TUI/Model/UI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,30 @@ module Swarm.TUI.Model.UI (
uiFPS,
scenarioRef,
appData,

-- ** Initialization
initFocusRing,
initLgTicksPerSecond,
initUIState,
) where

import Brick.Focus
import Brick.Widgets.List qualified as BL
import Control.Lens hiding (from, (<.>))
import Control.Monad.Except
import Data.Bits (FiniteBits (finiteBitSize))
import Data.Map (Map)
import Data.Text (Text)
import Data.Text qualified as T
import Swarm.Game.ScenarioInfo (
ScenarioInfoPair,
)
import Swarm.Game.World qualified as W
import Swarm.TUI.Inventory.Sorting
import Swarm.TUI.Model.Menus
import Swarm.TUI.Model.Names
import Swarm.TUI.Model.Menu
import Swarm.TUI.Model.Name
import Swarm.TUI.Model.Repl
import Swarm.Util
import System.Clock

------------------------------------------------------------
Expand Down Expand Up @@ -223,3 +231,58 @@ accumulatedTime :: Lens' UIState TimeSpec
-- | Free-form data loaded from the @data@ directory, for things like
-- the logo, about page, tutorial story, etc.
appData :: Lens' UIState (Map Text Text)

--------------------------------------------------
-- UIState initialization

-- | The initial state of the focus ring.
initFocusRing :: FocusRing Name
initFocusRing = focusRing $ map FocusablePanel listEnums

-- | The initial tick speed.
initLgTicksPerSecond :: Int
initLgTicksPerSecond = 4 -- 2^4 = 16 ticks / second

-- | Initialize the UI state. This needs to be in the IO monad since
-- it involves reading a REPL history file, getting the current
-- time, and loading text files from the data directory. The @Bool@
-- parameter indicates whether we should start off by showing the
-- main menu.
initUIState :: Bool -> Bool -> ExceptT Text IO UIState
initUIState showMainMenu cheatMode = liftIO $ do
historyT <- readFileMayT =<< getSwarmHistoryPath False
appDataMap <- readAppData
let history = maybe [] (map REPLEntry . T.lines) historyT
startTime <- getTime Monotonic
return $
UIState
{ _uiMenu = if showMainMenu then MainMenu (mainMenu NewGame) else NoMenu
, _uiPlaying = not showMainMenu
, _uiCheatMode = cheatMode
, _uiFocusRing = initFocusRing
, _uiWorldCursor = Nothing
, _uiREPL = initREPLState $ newREPLHistory history
, _uiInventory = Nothing
, _uiInventorySort = defaultSortOptions
, _uiMoreInfoTop = False
, _uiMoreInfoBot = False
, _uiScrollToEnd = False
, _uiError = Nothing
, _uiModal = Nothing
, _uiGoal = Nothing
, _uiShowFPS = False
, _uiShowZero = True
, _uiHideRobotsUntil = startTime - 1
, _uiInventoryShouldUpdate = False
, _uiTPF = 0
, _uiFPS = 0
, _lgTicksPerSecond = initLgTicksPerSecond
, _lastFrameTime = startTime
, _accumulatedTime = 0
, _lastInfoTime = 0
, _tickCount = 0
, _frameCount = 0
, _frameTickCount = 0
, _appData = appDataMap
, _scenarioRef = Nothing
}
2 changes: 1 addition & 1 deletion src/Swarm/TUI/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ import Swarm.TUI.Inventory.Sorting (renderSortMethod)
import Swarm.TUI.Model
import Swarm.TUI.Model.Repl
import Swarm.TUI.Panel
import Swarm.TUI.View.Utils
import Swarm.TUI.View.Util
import Swarm.Util
import Swarm.Version (NewReleaseFailure (..))
import System.Clock (TimeSpec (..))
Expand Down
2 changes: 1 addition & 1 deletion src/Swarm/TUI/View/Utils.hs → src/Swarm/TUI/View/Util.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE OverloadedStrings #-}

module Swarm.TUI.View.Utils where
module Swarm.TUI.View.Util where

import Brick hiding (Direction)
import Brick.Widgets.Dialog
Expand Down
10 changes: 5 additions & 5 deletions swarm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ library
Swarm.TUI.List
Swarm.TUI.Panel
Swarm.TUI.Model
Swarm.TUI.Model.Menus
Swarm.TUI.Model.Names
Swarm.TUI.Model.Menu
Swarm.TUI.Model.Name
Swarm.TUI.Model.Repl
Swarm.TUI.Model.StateUpdates
Swarm.TUI.Model.StateUpdate
Swarm.TUI.Model.UI
Swarm.TUI.View
Swarm.TUI.View.Utils
Swarm.TUI.View.Util
Swarm.TUI.Controller
Swarm.TUI.Controller.Utils
Swarm.TUI.Controller.Util
Swarm.TUI.Inventory.Sorting
Swarm.App
Swarm.Version
Expand Down

0 comments on commit 066dd43

Please sign in to comment.