Skip to content

Commit 92e028f

Browse files
committed
Simplify widgets
1 parent 09d8cbe commit 92e028f

File tree

1 file changed

+54
-62
lines changed

1 file changed

+54
-62
lines changed

ff-brick/Main.hs

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ import Brick.Widgets.List (
4444
handleListEvent,
4545
list,
4646
listSelectedAttr,
47+
listSelectedElement,
4748
listSelectedElementL,
4849
listSelectedFocusedAttr,
4950
renderList,
5051
)
51-
import Control.Monad (void, when)
52-
import Data.Foldable (toList)
52+
import Control.Monad (void)
5353
import Data.Function ((&))
5454
import Data.Generics.Labels ()
5555
import Data.Maybe (isJust)
@@ -100,8 +100,7 @@ data WN
100100

101101
data Model = Model
102102
{ visibleNotes :: List WN (EntityDoc Note)
103-
, openNoteM :: Maybe (EntityDoc Note)
104-
-- ^ currently opened note, if opened
103+
, isNoteOpen :: Bool
105104
}
106105
deriving (Generic)
107106

@@ -125,7 +124,7 @@ main = do
125124
Model
126125
{ visibleNotes =
127126
list NoteList (Vector.fromList filteredNotes) listItemHeight
128-
, openNoteM = Nothing
127+
, isNoteOpen = False
129128
}
130129
void $ defaultMain app initialModel
131130

@@ -162,50 +161,50 @@ withBorderIf cond =
162161
withBorderStyle if cond then unicode else borderStyleFromChar ' '
163162

164163
appDraw :: Model -> [Widget]
165-
appDraw Model{visibleNotes, openNoteM} = [mainWidget <=> keysHelpLine]
164+
appDraw Model{visibleNotes, isNoteOpen} = [mainWidget <=> keysHelpLine]
166165
where
167-
focusedWidget =
168-
case openNoteM of
169-
Nothing -> NoteList
170-
Just _ -> OpenNoteViewport
171-
172-
mainWidget = hBox $ noteList : toList openNoteWidget
166+
mainWidget = noteList <+> openNoteWidget
173167

174168
noteList =
175169
border
176170
( renderList renderListItem True visibleNotes
177171
& withVScrollBars OnRight
178172
)
179-
& withBorderIf (focusedWidget == NoteList)
173+
& withBorderIf (not isNoteOpen)
180174

181-
openNoteWidget = do
182-
Entity{entityVal = Note{note_text}} <- openNoteM
183-
let noteText = Text.pack $ filter (/= '\r') $ fromRgaM note_text
184-
Just $
185-
border
186-
( viewport
187-
OpenNoteViewport
188-
Both
189-
(txt {- TODO txtWrap? -} noteText)
190-
& withHScrollBars OnBottom
191-
& withVScrollBars OnRight
192-
)
193-
& withBorderIf (focusedWidget == OpenNoteViewport)
175+
openNoteWidget =
176+
border
177+
( viewport
178+
OpenNoteViewport
179+
Both
180+
(txt {- TODO txtWrap? -} content)
181+
& withHScrollBars OnBottom
182+
& withVScrollBars OnRight
183+
)
184+
& withBorderIf isNoteOpen
185+
where
186+
content =
187+
case listSelectedElement visibleNotes of
188+
Nothing -> ""
189+
Just (_, Entity{entityVal = Note{note_text}}) ->
190+
Text.pack $ filter (/= '\r') $ fromRgaM note_text
194191

195192
keysHelpLine =
196-
case focusedWidget of
197-
NoteList ->
198-
withAttr highlightAttr (txt "^q")
199-
<+> txt " "
200-
<+> withAttr highlightAttr (txt "Esc")
201-
<+> txt " exit "
202-
<+> withAttr highlightAttr (txt "Enter")
203-
<+> txt " open"
204-
OpenNoteViewport ->
205-
withAttr highlightAttr (txt "^q")
206-
<+> txt " exit "
207-
<+> withAttr highlightAttr (txt "Esc")
208-
<+> txt " close"
193+
hBox
194+
if isNoteOpen then
195+
[ withAttr highlightAttr (txt "^q")
196+
, txt " exit "
197+
, withAttr highlightAttr (txt "Esc")
198+
, txt " close"
199+
]
200+
else
201+
[ withAttr highlightAttr (txt "^q")
202+
, txt " "
203+
, withAttr highlightAttr (txt "Esc")
204+
, txt " exit "
205+
, withAttr highlightAttr (txt "Enter")
206+
, txt " open"
207+
]
209208

210209
highlightAttr :: AttrName
211210
highlightAttr = attrName "highlight"
@@ -217,29 +216,22 @@ appHandleEvent = \case
217216

218217
appHandleVtyEvent :: Event -> EventM ()
219218
appHandleVtyEvent event = do
220-
openNoteM <- use #openNoteM
221-
let focusedWidget =
222-
case openNoteM of
223-
Nothing -> NoteList
224-
Just _ -> OpenNoteViewport
225-
case focusedWidget of
226-
NoteList ->
227-
case event of
228-
EvKey (KChar 'q') [MCtrl] -> halt
229-
EvKey KEsc [] -> halt
230-
EvKey KEnter [] -> do
231-
-- open selected note
232-
selectedNoteM <-
233-
preuse $ #visibleNotes . listSelectedElementL
234-
#openNoteM .= selectedNoteM
235-
e -> zoom #visibleNotes $ handleListEvent e
236-
OpenNoteViewport ->
237-
case event of
238-
EvKey (KChar 'q') [MCtrl] -> halt
239-
EvKey KEsc [] ->
240-
-- close note view
241-
#openNoteM .= Nothing
242-
e -> when (isJust openNoteM) $ handleViewportEvent e
219+
isNoteOpen <- use #isNoteOpen
220+
if isNoteOpen then case event of
221+
EvKey (KChar 'q') [MCtrl] -> halt
222+
EvKey KEsc [] ->
223+
-- close note view
224+
#isNoteOpen .= False
225+
e -> handleViewportEvent e
226+
else case event of
227+
EvKey (KChar 'q') [MCtrl] -> halt
228+
EvKey KEsc [] -> halt
229+
EvKey KEnter [] -> do
230+
-- open selected note
231+
selectedNoteM <-
232+
preuse $ #visibleNotes . listSelectedElementL
233+
#isNoteOpen .= isJust selectedNoteM
234+
e -> zoom #visibleNotes $ handleListEvent e
243235

244236
handleViewportEvent :: Event -> EventM ()
245237
handleViewportEvent = \case

0 commit comments

Comments
 (0)