module Yi.Tab
(
Tab,
TabRef,
tabWindowsA,
tabLayoutManagerA,
tabDividerPositionA,
tkey,
tabMiniWindows,
tabFocus,
forceTab,
mapWindows,
tabLayout,
tabFoldl,
makeTab,
makeTab1,
) where
import qualified Prelude
import Yi.Prelude
import qualified Data.Binary as Binary
import Data.Accessor.Basic
import qualified Data.List.PointedList as PL
import Yi.Buffer.Basic(WindowRef)
import Yi.Layout
import Yi.Window
type TabRef = Int
data Tab = Tab {
tkey :: !TabRef,
tabWindows :: !(PL.PointedList Window),
tabLayout :: !(Layout WindowRef),
tabLayoutManager :: !AnyLayoutManager
}
deriving Typeable
tabFocus :: Tab -> Window
tabFocus = PL._focus . tabWindows
tabMiniWindows :: Tab -> [Window]
tabMiniWindows = Prelude.filter isMini . toList . tabWindows
tabWindowsA :: Accessor Tab (PL.PointedList Window)
tabWindowsA = fromSetGet setter getter
where
setter ws t = relayoutIf (toList ws /= toList (tabWindows t)) (t { tabWindows = ws})
getter = tabWindows
tabLayoutManagerA :: Accessor Tab AnyLayoutManager
tabLayoutManagerA = fromSetGet setter getter
where
setter lm t = relayoutIf (lm /= tabLayoutManager t) (t { tabLayoutManager = lm })
getter = tabLayoutManager
tabDividerPositionA :: DividerRef -> Accessor Tab DividerPosition
tabDividerPositionA ref = dividerPositionA ref . fromSetGet (\l t -> t { tabLayout = l}) tabLayout
relayoutIf :: Bool -> Tab -> Tab
relayoutIf False t = t
relayoutIf True t = relayout t
relayout :: Tab -> Tab
relayout t = t { tabLayout = buildLayout (tabWindows t) (tabLayoutManager t) (tabLayout t) }
instance Binary.Binary Tab where
put (Tab tk ws _ _) = Binary.put tk >> Binary.put ws
get = makeTab <$> Binary.get <*> Binary.get
instance Eq Tab where
(==) t1 t2 = tkey t1 == tkey t2
instance Show Tab where
show t = "Tab " ++ show (tkey t)
mapWindows :: (Window -> Window) -> Tab -> Tab
mapWindows f = modify tabWindowsA (fmap f)
forceTab :: Tab -> Tab
forceTab t = foldr seq t (t ^. tabWindowsA)
tabFoldl :: (a -> Window -> a) -> a -> Tab -> a
tabFoldl f z t = foldl f z (t ^. tabWindowsA)
buildLayout :: PL.PointedList Window -> AnyLayoutManager -> Layout WindowRef -> Layout WindowRef
buildLayout ws m l = pureLayout m l . fmap wkey . Prelude.filter (not . isMini) . toList $ ws
makeTab :: TabRef -> PL.PointedList Window -> Tab
makeTab key ws = Tab key ws (buildLayout ws initial initial) initial
makeTab1 :: TabRef -> Window -> Tab
makeTab1 key win = makeTab key (PL.singleton win)