Prevent unfolding of nodes without children

This commit is contained in:
Joscha 2020-02-09 11:44:25 +00:00
parent 909d587c53
commit 573a835022
4 changed files with 20 additions and 8 deletions

View file

@ -51,7 +51,11 @@ newClientState node = ClientState
} }
clientDraw :: ClientState -> [Widget ResourceName] clientDraw :: ClientState -> [Widget ResourceName]
clientDraw cs = [renderTree boxDrawingBranching (csEditor cs) (csTree cs)] clientDraw cs =
[ renderTree boxDrawingBranching (csEditor cs) (csTree cs) <=>
txt "--------------------------------------------------------------------------------" <=>
txtWrap (T.pack $ show $ csTree cs)
]
isQuitEvent :: BrickEvent a b -> Bool isQuitEvent :: BrickEvent a b -> Bool
isQuitEvent (VtyEvent (Vty.EvKey (Vty.KChar 'q') [])) = True isQuitEvent (VtyEvent (Vty.EvKey (Vty.KChar 'q') [])) = True

View file

@ -34,7 +34,7 @@ narrowDrawState nodeId ds = ds
nodeToWidget :: Bool -> Node -> Widget ResourceName nodeToWidget :: Bool -> Node -> Widget ResourceName
nodeToWidget focused node = nodeToWidget focused node =
let nodeWidget = txt $ nodeText node let nodeWidget = txt $ nodeText node
expandStyle = if null (nodeChildren node) then "noexpand" else "expand" expandStyle = if hasChildren node then "expand" else "noexpand"
focusStyle = if focused then "focus" else "nofocus" focusStyle = if focused then "focus" else "nofocus"
in withDefAttr focusStyle $ withDefAttr expandStyle nodeWidget in withDefAttr focusStyle $ withDefAttr expandStyle nodeWidget

View file

@ -57,7 +57,12 @@ newTree node focused unfolded = Tree
, treeUnfolded = safeUnfolded , treeUnfolded = safeUnfolded
} }
where where
safeUnfolded = Set.filter (isValidPath node) unfolded isValidFold :: Node -> Path -> Bool
isValidFold n p = case applyPath p n of
Nothing -> False
Just childNode -> hasChildren childNode
safeUnfolded = Set.filter (isValidFold node) unfolded
safeFocused = findNearestFocus (applyFolds safeUnfolded node) focused safeFocused = findNearestFocus (applyFolds safeUnfolded node) focused
-- | Switch out a tree's node, keeping as much of the focus and folding -- | Switch out a tree's node, keeping as much of the focus and folding
@ -137,14 +142,13 @@ isCurrentFolded tree = not $ treeFocused tree `Set.member` treeUnfolded tree
-- | Fold the currently focused node. Does nothing if it is already folded. -- | Fold the currently focused node. Does nothing if it is already folded.
foldCurrent :: Tree -> Tree foldCurrent :: Tree -> Tree
foldCurrent tree@Tree {treeFocused = f, treeUnfolded = u} = foldCurrent Tree{treeNode=n, treeFocused=f, treeUnfolded=u} =
let foldedTree = withFolds tree {treeUnfolded = Set.delete f u} newTree n f $ Set.delete f u
in foldedTree {treeNode = treeNode tree}
-- | Unfold the currently focused node. Does nothing if it is already unfolded. -- | Unfold the currently focused node. Does nothing if it is already unfolded.
unfoldCurrent :: Tree -> Tree unfoldCurrent :: Tree -> Tree
unfoldCurrent tree@Tree {treeFocused = f, treeUnfolded = u} = unfoldCurrent Tree{treeNode=n, treeFocused=f, treeUnfolded=u} =
tree {treeUnfolded = Set.insert f u} newTree n f $ Set.insert f u
-- | Toggle whether the currently focused node is folded. -- | Toggle whether the currently focused node is folded.
toggleFold :: Tree -> Tree toggleFold :: Tree -> Tree

View file

@ -7,6 +7,7 @@ module Forest.Node
-- * Node -- * Node
NodeId NodeId
, Node(..) , Node(..)
, hasChildren
, emptyNode , emptyNode
, initialNode , initialNode
, applyId , applyId
@ -55,6 +56,9 @@ instance ToJSON Node where
instance FromJSON Node where instance FromJSON Node where
parseJSON = genericParseJSON nodeOptions parseJSON = genericParseJSON nodeOptions
hasChildren :: Node -> Bool
hasChildren = not . Map.null . nodeChildren
emptyNode :: T.Text -> Bool -> Bool -> Bool -> Bool -> Node emptyNode :: T.Text -> Bool -> Bool -> Bool -> Bool -> Node
emptyNode text edit delete reply act = Node text edit delete reply act Map.empty emptyNode text edit delete reply act = Node text edit delete reply act Map.empty