packages feed

mp-1.0.0: src/Mp/UI/MainMenuBar.hs

{-
 *  Programmer:	Piotr Borek
 *  E-mail:     piotrborek@op.pl
 *  Copyright 2017 Piotr Borek
 *
 *  Distributed under the terms of the GPL (GNU Public License)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}
{-# LANGUAGE QuasiQuotes #-}

module Mp.UI.MainMenuBar (
    mainMenuBarNew
) where

import           Control.Monad
import qualified Graphics.Vty              as Vty
import           Simple.Locale.TranslateTH
import           Simple.UI.All

import           Mp.Locale.TranslateFile   ()
import           Mp.UI.MpData

mainMenuBarNew :: SingleLayout -> Int -> UIApp MpData SimpleMenuBar
mainMenuBarNew _layout activeMenu = do
    mpData <- view appUserData
    menuBar <- simpleMenuBarNew

    set menuBar colorForeground (mpData ^. mpColors . tabForeground)
    set menuBar colorBackground (mpData ^. mpColors . tabBackground)
    set menuBar colorForegroundSelected (mpData ^. mpColors . tabActiveForeground)
    set menuBar colorBackgroundSelected (mpData ^. mpColors . tabActiveBackground)
    set menuBar colorStyleSelected DrawStyleBold

    item1 <- simpleMenuItemNew '1' [tr|Help    |]
    item2 <- simpleMenuItemNew '2' [tr|Queue   |]
    item3 <- simpleMenuItemNew '3' [tr|Playlist|]
    item4 <- simpleMenuItemNew '4' [tr|Browser |]

    let items = [item1, item2, item3, item4]
    forM_ items $ menuBarItemAdd menuBar

    on_ item1 activated $
        set _layout layoutIndex 0

    on_ item2 activated $
        set _layout layoutIndex 1

    on_ item3 activated $
        set _layout layoutIndex 2

    on_ item4 activated $
        set _layout layoutIndex 3

    on_ menuBar keyPressed $ \key _ ->
        case key of
            Vty.KChar '\t' -> do
                i <- get _layout layoutIndex
                menuItemActivate (items !! (succ i `mod` 4))
            Vty.KBackTab -> do
                i <- get _layout layoutIndex
                menuItemActivate (items !! (pred i `mod` 4))
            _ ->
                return ()

    menuItemActivate (items !! activeMenu)

    return menuBar