module Brick.Focus
( FocusRing
, focusRing
, focusNext
, focusPrev
, focusGetCurrent
, focusSetCurrent
, focusRingLength
, focusRingToList
, focusRingCursor
, withFocusRing
, focusRingModify
)
where
import Lens.Micro ((^.))
import Data.Maybe (listToMaybe)
import qualified Data.CircularList as C
import Brick.Types
import Brick.Widgets.Core (Named(..))
newtype FocusRing n = FocusRing (C.CList n)
focusRing :: [n] -> FocusRing n
focusRing :: [n] -> FocusRing n
focusRing = CList n -> FocusRing n
forall n. CList n -> FocusRing n
FocusRing (CList n -> FocusRing n) -> ([n] -> CList n) -> [n] -> FocusRing n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [n] -> CList n
forall a. [a] -> CList a
C.fromList
focusNext :: FocusRing n -> FocusRing n
focusNext :: FocusRing n -> FocusRing n
focusNext r :: FocusRing n
r@(FocusRing CList n
l)
| CList n -> Bool
forall a. CList a -> Bool
C.isEmpty CList n
l = FocusRing n
r
| Bool
otherwise = CList n -> FocusRing n
forall n. CList n -> FocusRing n
FocusRing (CList n -> FocusRing n) -> CList n -> FocusRing n
forall a b. (a -> b) -> a -> b
$ CList n -> CList n
forall a. CList a -> CList a
C.rotR CList n
l
focusPrev :: FocusRing n -> FocusRing n
focusPrev :: FocusRing n -> FocusRing n
focusPrev r :: FocusRing n
r@(FocusRing CList n
l)
| CList n -> Bool
forall a. CList a -> Bool
C.isEmpty CList n
l = FocusRing n
r
| Bool
otherwise = CList n -> FocusRing n
forall n. CList n -> FocusRing n
FocusRing (CList n -> FocusRing n) -> CList n -> FocusRing n
forall a b. (a -> b) -> a -> b
$ CList n -> CList n
forall a. CList a -> CList a
C.rotL CList n
l
withFocusRing :: (Eq n, Named a n)
=> FocusRing n
-> (Bool -> a -> b)
-> a
-> b
withFocusRing :: FocusRing n -> (Bool -> a -> b) -> a -> b
withFocusRing FocusRing n
ring Bool -> a -> b
f a
a = Bool -> a -> b
f (FocusRing n -> Maybe n
forall n. FocusRing n -> Maybe n
focusGetCurrent FocusRing n
ring Maybe n -> Maybe n -> Bool
forall a. Eq a => a -> a -> Bool
== n -> Maybe n
forall a. a -> Maybe a
Just (a -> n
forall a n. Named a n => a -> n
getName a
a)) a
a
focusGetCurrent :: FocusRing n -> Maybe n
focusGetCurrent :: FocusRing n -> Maybe n
focusGetCurrent (FocusRing CList n
l) = CList n -> Maybe n
forall a. CList a -> Maybe a
C.focus CList n
l
focusSetCurrent :: (Eq n) => n -> FocusRing n -> FocusRing n
focusSetCurrent :: n -> FocusRing n -> FocusRing n
focusSetCurrent n
n r :: FocusRing n
r@(FocusRing CList n
l) =
case n -> CList n -> Maybe (CList n)
forall a. Eq a => a -> CList a -> Maybe (CList a)
C.rotateTo n
n CList n
l of
Maybe (CList n)
Nothing -> FocusRing n
r
Just CList n
l' -> CList n -> FocusRing n
forall n. CList n -> FocusRing n
FocusRing CList n
l'
focusRingLength :: FocusRing n -> Int
focusRingLength :: FocusRing n -> Int
focusRingLength (FocusRing CList n
l) = CList n -> Int
forall a. CList a -> Int
C.size CList n
l
focusRingToList :: FocusRing n -> [n]
focusRingToList :: FocusRing n -> [n]
focusRingToList (FocusRing CList n
l) = CList n -> [n]
forall a. CList a -> [a]
C.rightElements CList n
l
focusRingModify :: (C.CList n -> C.CList n) -> FocusRing n -> FocusRing n
focusRingModify :: (CList n -> CList n) -> FocusRing n -> FocusRing n
focusRingModify CList n -> CList n
f (FocusRing CList n
l) = CList n -> FocusRing n
forall n. CList n -> FocusRing n
FocusRing (CList n -> FocusRing n) -> CList n -> FocusRing n
forall a b. (a -> b) -> a -> b
$ CList n -> CList n
f CList n
l
focusRingCursor :: (Eq n)
=> (a -> FocusRing n)
-> a
-> [CursorLocation n]
-> Maybe (CursorLocation n)
focusRingCursor :: (a -> FocusRing n)
-> a -> [CursorLocation n] -> Maybe (CursorLocation n)
focusRingCursor a -> FocusRing n
getRing a
st [CursorLocation n]
ls =
[CursorLocation n] -> Maybe (CursorLocation n)
forall a. [a] -> Maybe a
listToMaybe ([CursorLocation n] -> Maybe (CursorLocation n))
-> [CursorLocation n] -> Maybe (CursorLocation n)
forall a b. (a -> b) -> a -> b
$ (CursorLocation n -> Bool)
-> [CursorLocation n] -> [CursorLocation n]
forall a. (a -> Bool) -> [a] -> [a]
filter CursorLocation n -> Bool
isCurrent [CursorLocation n]
ls
where
isCurrent :: CursorLocation n -> Bool
isCurrent CursorLocation n
cl = CursorLocation n
clCursorLocation n
-> Getting (Maybe n) (CursorLocation n) (Maybe n) -> Maybe n
forall s a. s -> Getting a s a -> a
^.Getting (Maybe n) (CursorLocation n) (Maybe n)
forall n1 n2.
Lens (CursorLocation n1) (CursorLocation n2) (Maybe n1) (Maybe n2)
cursorLocationNameL Maybe n -> Maybe n -> Bool
forall a. Eq a => a -> a -> Bool
==
(FocusRing n -> Maybe n
forall n. FocusRing n -> Maybe n
focusGetCurrent (FocusRing n -> Maybe n) -> FocusRing n -> Maybe n
forall a b. (a -> b) -> a -> b
$ a -> FocusRing n
getRing a
st)