{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-unused-top-binds #-}
-- | This module provides a menu bar for grouping menus together.
--
-- Menu bars carry menus of a particular type using the menu types
-- provided in the @Brick.Widgets.Menu@ module. The type aliases
-- provided here correspond to the aliases for menu use cases:
--
-- * 'SimpleMenuBar': a menu bar made up of 'SimpleMenu's created with
--   'simpleMenu'
-- * 'DispatchingMenuBar': a menu bar made up of 'DispatchingMenu's
--    created with 'menuWithDispatcher'
-- * 'MenuBar': the fully general type for menu bars with menus created
--   with 'menu'
--
-- In all cases, use 'newMenuBar' to construct a menu bar, and create
-- its menus using the corresponding menu constructor for the type of
-- menu bar you want to use.
--
-- Render the menu bar with 'renderMenuBar' and handle menu bar events
-- with 'handleMenuBarEvent', deferring to the application's event
-- handling for events that the menu bar doesn't handle.
--
-- Similar to individual menus, menu bars have an orientation that can
-- be changed with 'setMenuBarOrientation'.
--
-- This API requires the use of lenses for application state fields that
-- store menu bar state.
--
-- See the @MenuBarDemo@ demonstration program for a complete working
-- example of using this API.
--
-- = Adding a Menu Bar to An Application
--
-- To use this module in an application:
--
-- * Choose a menu bar type that you want to work with such as
--   'SimpleMenuBar'.
-- * Add an application state field and lens for a value of the menu
--   bar's type, and add a constructor to the application's resource
--   name type, with an argument of type 'MenuRegion', for each menu
--   in the menu bar. Add lenses to the application state type with
--   'Lens.Micro.TH.makeLenses'.
-- * Populate the application's initial state with the menu bar.
-- * Render the menu bar with 'renderMenuBar'.
-- * Handle incoming events first with 'handleMenuBarEvent', and when
--   'handleMenuBarEvent' returns @False@, pass unhandled events on to
--   the existing application event handler.
module Brick.Widgets.MenuBar
  (
  -- * Types
    MenuBar
  , SimpleMenuBar
  , DispatchingMenuBar

  -- * Creating menu bars
  , newMenuBar

  -- * Handling events
  , handleMenuBarEvent

  -- * Rendering
  , renderMenuBar

  -- * Working with menu bars
  , hasOpenMenu
  , closeAllMenus
  , openMenuAtIndex
  , toggleMenuAtIndex
  , toggleNamedMenu
  , openNamedMenu
  , setMenuBarOrientation
  )
where

import Control.Monad (when)
import qualified Data.Set as S
import Data.Maybe (isJust, listToMaybe, fromMaybe)
import Lens.Micro.Platform ((^.), (&), (%~), (.~), Lens', ix, each)
import Lens.Micro.Mtl

import qualified Data.Foldable as F
import qualified Data.Vector as V

import qualified Graphics.Vty as Vty

import Brick.Types
import Brick.Widgets.Core
import Brick.Widgets.Menu

-- | A menu bar holding a sequence of menus.
--
-- A menu bar can have up to one open menu at a time.
data MenuBar s n k =
    MenuBar { forall s n k. MenuBar s n k -> MenuOrientation
menuBarOrientation :: !MenuOrientation
            , forall s n k. MenuBar s n k -> Vector (Menu s n k)
menuBarMenus :: !(V.Vector (Menu s n k))
            }

suffixLenses ''MenuBar

-- | A specialization of 'MenuBar' for menus with 'EventM' handlers; use
-- this with 'simpleMenu'.
type SimpleMenuBar s n = MenuBar s n (EventM n s ())

-- | A specialization of 'MenuBar' for menus with abstract key event
-- triggers; this with 'menuWithDispatcher'.
type DispatchingMenuBar s n k = MenuBar s n (EventM n s (EntryTrigger s n k))

-- | Create a new menu bar from the specified menu list. If the list is
-- empty, this calls 'error'. If the list contains menus with the same
-- resource name constructor, this calls 'error'.
newMenuBar :: (Ord n) => [Menu s n k] -> MenuBar s n k
newMenuBar :: forall n s k. Ord n => [Menu s n k] -> MenuBar s n k
newMenuBar [] = [Char] -> MenuBar s n k
forall a. HasCallStack => [Char] -> a
error [Char]
"BUG: newMenuBar requires a non-empty list"
newMenuBar [Menu s n k]
ms = MenuOrientation -> Vector (Menu s n k) -> MenuBar s n k
forall s n k.
MenuOrientation -> Vector (Menu s n k) -> MenuBar s n k
MenuBar MenuOrientation
LeftToRight (Vector (Menu s n k) -> MenuBar s n k)
-> Vector (Menu s n k) -> MenuBar s n k
forall a b. (a -> b) -> a -> b
$ [Menu s n k] -> Vector (Menu s n k)
forall a. [a] -> Vector a
V.fromList [Menu s n k]
uniqueMenus
    where
        uniqueMenus :: [Menu s n k]
uniqueMenus = if Set n -> Int
forall a. Set a -> Int
S.size Set n
menuNames Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [Menu s n k] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Menu s n k]
ms
                      then [Menu s n k]
ms
                      else [Char] -> [Menu s n k]
forall a. HasCallStack => [Char] -> a
error [Char]
"newMenuBar: all menus must use unique resource name constructors"
        menuNames :: Set n
menuNames = [n] -> Set n
forall a. Ord a => [a] -> Set a
S.fromList ([n] -> Set n) -> [n] -> Set n
forall a b. (a -> b) -> a -> b
$
                    Menu s n k -> n
forall s n k. Menu s n k -> n
menuTitleName (Menu s n k -> n) -> [Menu s n k] -> [n]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Menu s n k]
ms

-- | Return whether this menu bar has an open menu.
hasOpenMenu :: MenuBar s n k -> Bool
hasOpenMenu :: forall s n k. MenuBar s n k -> Bool
hasOpenMenu = Maybe (Int, Menu s n k) -> Bool
forall a. Maybe a -> Bool
isJust (Maybe (Int, Menu s n k) -> Bool)
-> (MenuBar s n k -> Maybe (Int, Menu s n k))
-> MenuBar s n k
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MenuBar s n k -> Maybe (Int, Menu s n k)
forall s n k. MenuBar s n k -> Maybe (Int, Menu s n k)
getOpenMenu

-- | Get this menu bar's current open menu and its index, if any.
getOpenMenu :: MenuBar s n k -> Maybe (Int, Menu s n k)
getOpenMenu :: forall s n k. MenuBar s n k -> Maybe (Int, Menu s n k)
getOpenMenu MenuBar s n k
mb = do
    let ms :: Vector (Menu s n k)
ms = MenuBar s n k -> Vector (Menu s n k)
forall s n k. MenuBar s n k -> Vector (Menu s n k)
menuBarMenus MenuBar s n k
mb
    idx <- (Menu s n k -> Bool) -> Vector (Menu s n k) -> Maybe Int
forall a. (a -> Bool) -> Vector a -> Maybe Int
V.findIndex Menu s n k -> Bool
forall s n k. Menu s n k -> Bool
menuIsOpen Vector (Menu s n k)
ms
    return (idx, ms V.! idx)

-- | Render this menu bar with the given application state as input.
renderMenuBar :: (Ord n) => s -> MenuBar s n k -> Widget n
renderMenuBar :: forall n s k. Ord n => s -> MenuBar s n k -> Widget n
renderMenuBar s
s MenuBar s n k
mb =
    AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withDefAttr AttrName
menuTitleAttr (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$ Widget n -> Widget n
forall {n}. Widget n -> Widget n
padForOrientation Widget n
body
    where
        padForOrientation :: Widget n -> Widget n
padForOrientation = case MenuBar s n k
mbMenuBar s n k
-> Getting MenuOrientation (MenuBar s n k) MenuOrientation
-> MenuOrientation
forall s a. s -> Getting a s a -> a
^.Getting MenuOrientation (MenuBar s n k) MenuOrientation
forall s n k (f :: * -> *).
Functor f =>
(MenuOrientation -> f MenuOrientation)
-> MenuBar s n k -> f (MenuBar s n k)
menuBarOrientationL of
            MenuOrientation
LeftToRight -> Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padRight Padding
Max
            MenuOrientation
RightToLeft -> Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padLeft Padding
Max (Widget n -> Widget n)
-> (Widget n -> Widget n) -> Widget n -> Widget n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padRight (Int -> Padding
Pad Int
1)

        body :: Widget n
body = [Widget n] -> Widget n
forall n. [Widget n] -> Widget n
hBox ([Widget n] -> Widget n) -> [Widget n] -> Widget n
forall a b. (a -> b) -> a -> b
$
               Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padLeft (Int -> Padding
Pad Int
1) (Widget n -> Widget n) -> [Widget n] -> [Widget n]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
               Vector (Widget n) -> [Widget n]
forall a. Vector a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList (s -> Menu s n k -> Widget n
forall n s k. Ord n => s -> Menu s n k -> Widget n
renderMenu s
s (Menu s n k -> Widget n)
-> Vector (Menu s n k) -> Vector (Widget n)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MenuBar s n k -> Vector (Menu s n k)
forall s n k. MenuBar s n k -> Vector (Menu s n k)
menuBarMenus MenuBar s n k
mb)

-- | Given a resource name, find the menu whose title bar portion
-- matches the resource name, if any.
getMenuTitleMatch :: (Eq n) => MenuBar s n k -> n -> Maybe (Int, Menu s n k)
getMenuTitleMatch :: forall n s k. Eq n => MenuBar s n k -> n -> Maybe (Int, Menu s n k)
getMenuTitleMatch MenuBar s n k
mb n
n =
    [(Int, Menu s n k)] -> Maybe (Int, Menu s n k)
forall a. [a] -> Maybe a
listToMaybe ([(Int, Menu s n k)] -> Maybe (Int, Menu s n k))
-> [(Int, Menu s n k)] -> Maybe (Int, Menu s n k)
forall a b. (a -> b) -> a -> b
$ ((Int, Menu s n k) -> Bool)
-> [(Int, Menu s n k)] -> [(Int, Menu s n k)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Int, Menu s n k) -> Bool
forall {a} {s} {k}. (a, Menu s n k) -> Bool
matchesTitle ([(Int, Menu s n k)] -> [(Int, Menu s n k)])
-> [(Int, Menu s n k)] -> [(Int, Menu s n k)]
forall a b. (a -> b) -> a -> b
$ [Int] -> [Menu s n k] -> [(Int, Menu s n k)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] (Vector (Menu s n k) -> [Menu s n k]
forall a. Vector a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList (Vector (Menu s n k) -> [Menu s n k])
-> Vector (Menu s n k) -> [Menu s n k]
forall a b. (a -> b) -> a -> b
$ MenuBar s n k
mbMenuBar s n k
-> Getting
     (Vector (Menu s n k)) (MenuBar s n k) (Vector (Menu s n k))
-> Vector (Menu s n k)
forall s a. s -> Getting a s a -> a
^.Getting (Vector (Menu s n k)) (MenuBar s n k) (Vector (Menu s n k))
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL)
    where
        matchesTitle :: (a, Menu s n k) -> Bool
matchesTitle (a
_, Menu s n k
m) = n
n n -> n -> Bool
forall a. Eq a => a -> a -> Bool
== Menu s n k -> n
forall s n k. Menu s n k -> n
menuTitleName Menu s n k
m

-- | Handle an event for this menu bar and return @True@, or return
-- @False@ if the event was not handled (e.g. because the event was not
-- a menu title mouse click or because no menu was open to receive the
-- event).
--
-- Events handled include:
--
-- * Mouse clicks on menu titles will open the clicked menu, closing
--   other open menus.
-- * Left and Right arrow keys will cycle between menus if there is an
--   open menu.
-- * If a submenu entry is selected, the arrow keys will open it or
--   close it if it is open, depending on the configured menu bar
--   orientation.
-- * @Esc@ will close the currently-open menu.
--
-- In all other cases, this will attempt to defer to the opened menu to
-- handle the event. This returns @True@ if the event was one of the
-- above and was handled, @True@ if the event was not one of the above
-- but was handled by the open menu, or @False@ otherwise.
--
-- A return value of @True@ indicates that the event should not be
-- handled by the application because it was destined for the menu bar
-- or one of its menus; a return value of @False@ indicates that the
-- event should be handled by the application because it did not affect
-- the menu bar or its menus in their current state for any reason.
-- Consequently, a common pattern when using this function will look
-- something like this:
--
-- @
-- myApplicationEventHandler :: BrickEvent n e -> EventM n s ()
-- myApplicationEventHandler e = do
--     handled <- handleMenuBarEvent myMenuBarLens e
--     when (not handled) $ do
--         -- Go on to handle the event in the rest of the application
-- @
handleMenuBarEvent :: (Eq n)
                   => Lens' s (MenuBar s n k)
                   -- ^ The lens into the application state where the
                   -- menu state can be found
                   -> BrickEvent n e
                   -- ^ The event to handle
                   -> EventM n s Bool
handleMenuBarEvent :: forall n s k e.
Eq n =>
Lens' s (MenuBar s n k) -> BrickEvent n e -> EventM n s Bool
handleMenuBarEvent Lens' s (MenuBar s n k)
which e :: BrickEvent n e
e@(VtyEvent (Vty.EvKey Key
Vty.KLeft [])) = do
    -- Since this key might be handled by the open menu, try that first
    -- and only switch menus if it wasn't handled by the menu.
    handled <- Lens' s (MenuBar s n k)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
forall s n k.
Lens' s (MenuBar s n k)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
withOpenMenu (MenuBar s n k -> f (MenuBar s n k)) -> s -> f s
Lens' s (MenuBar s n k)
which (((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
forall a b. (a -> b) -> a -> b
$ \(Int
idx, Menu s n k
_) ->
        Traversal' s (Menu s n k) -> BrickEvent n e -> EventM n s Bool
forall n s k e.
Eq n =>
Traversal' s (Menu s n k) -> BrickEvent n e -> EventM n s Bool
handleMenuEvent ((MenuBar s n k -> f (MenuBar s n k)) -> s -> f s
Lens' s (MenuBar s n k)
which((MenuBar s n k -> f (MenuBar s n k)) -> s -> f s)
-> ((Menu s n k -> f (Menu s n k))
    -> MenuBar s n k -> f (MenuBar s n k))
-> (Menu s n k -> f (Menu s n k))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL((Vector (Menu s n k) -> f (Vector (Menu s n k)))
 -> MenuBar s n k -> f (MenuBar s n k))
-> ((Menu s n k -> f (Menu s n k))
    -> Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> (Menu s n k -> f (Menu s n k))
-> MenuBar s n k
-> f (MenuBar s n k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Index (Vector (Menu s n k))
-> Traversal' (Vector (Menu s n k)) (IxValue (Vector (Menu s n k)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Int
Index (Vector (Menu s n k))
idx) BrickEvent n e
e

    when (not handled) $
        which %= openPreviousMenu

    return True
handleMenuBarEvent Lens' s (MenuBar s n k)
which e :: BrickEvent n e
e@(VtyEvent (Vty.EvKey Key
Vty.KRight [])) = do
    -- Since this key might be handled by the open menu, try that first
    -- and only switch menus if it wasn't handled by the menu.
    handled <- Lens' s (MenuBar s n k)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
forall s n k.
Lens' s (MenuBar s n k)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
withOpenMenu (MenuBar s n k -> f (MenuBar s n k)) -> s -> f s
Lens' s (MenuBar s n k)
which (((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
forall a b. (a -> b) -> a -> b
$ \(Int
idx, Menu s n k
_) ->
        Traversal' s (Menu s n k) -> BrickEvent n e -> EventM n s Bool
forall n s k e.
Eq n =>
Traversal' s (Menu s n k) -> BrickEvent n e -> EventM n s Bool
handleMenuEvent ((MenuBar s n k -> f (MenuBar s n k)) -> s -> f s
Lens' s (MenuBar s n k)
which((MenuBar s n k -> f (MenuBar s n k)) -> s -> f s)
-> ((Menu s n k -> f (Menu s n k))
    -> MenuBar s n k -> f (MenuBar s n k))
-> (Menu s n k -> f (Menu s n k))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL((Vector (Menu s n k) -> f (Vector (Menu s n k)))
 -> MenuBar s n k -> f (MenuBar s n k))
-> ((Menu s n k -> f (Menu s n k))
    -> Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> (Menu s n k -> f (Menu s n k))
-> MenuBar s n k
-> f (MenuBar s n k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Index (Vector (Menu s n k))
-> Traversal' (Vector (Menu s n k)) (IxValue (Vector (Menu s n k)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Int
Index (Vector (Menu s n k))
idx) BrickEvent n e
e

    when (not handled) $
        which %= openNextMenu

    return True
handleMenuBarEvent Lens' s (MenuBar s n k)
which e :: BrickEvent n e
e@(MouseDown n
n Button
_ [Modifier]
_ Location
_) = do
    mb <- Getting (MenuBar s n k) s (MenuBar s n k)
-> EventM n s (MenuBar s n k)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting (MenuBar s n k) s (MenuBar s n k)
Lens' s (MenuBar s n k)
which
    case getMenuTitleMatch mb n of
        Maybe (Int, Menu s n k)
Nothing -> Lens' s (MenuBar s n k)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
forall s n k.
Lens' s (MenuBar s n k)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
withOpenMenu (MenuBar s n k -> f (MenuBar s n k)) -> s -> f s
Lens' s (MenuBar s n k)
which (((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
forall a b. (a -> b) -> a -> b
$ \(Int
idx, Menu s n k
_) ->
            Traversal' s (Menu s n k) -> BrickEvent n e -> EventM n s Bool
forall n s k e.
Eq n =>
Traversal' s (Menu s n k) -> BrickEvent n e -> EventM n s Bool
handleMenuEvent ((MenuBar s n k -> f (MenuBar s n k)) -> s -> f s
Lens' s (MenuBar s n k)
which((MenuBar s n k -> f (MenuBar s n k)) -> s -> f s)
-> ((Menu s n k -> f (Menu s n k))
    -> MenuBar s n k -> f (MenuBar s n k))
-> (Menu s n k -> f (Menu s n k))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL((Vector (Menu s n k) -> f (Vector (Menu s n k)))
 -> MenuBar s n k -> f (MenuBar s n k))
-> ((Menu s n k -> f (Menu s n k))
    -> Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> (Menu s n k -> f (Menu s n k))
-> MenuBar s n k
-> f (MenuBar s n k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Index (Vector (Menu s n k))
-> Traversal' (Vector (Menu s n k)) (IxValue (Vector (Menu s n k)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Int
Index (Vector (Menu s n k))
idx) BrickEvent n e
e
        Just (Int
i, Menu s n k
_) -> do
            mMatchingMenu <- Getting (First (Menu s n k)) s (Menu s n k)
-> EventM n s (Maybe (Menu s n k))
forall s (m :: * -> *) a.
MonadState s m =>
Getting (First a) s a -> m (Maybe a)
preuse ((MenuBar s n k -> Const (First (Menu s n k)) (MenuBar s n k))
-> s -> Const (First (Menu s n k)) s
Lens' s (MenuBar s n k)
which((MenuBar s n k -> Const (First (Menu s n k)) (MenuBar s n k))
 -> s -> Const (First (Menu s n k)) s)
-> ((Menu s n k -> Const (First (Menu s n k)) (Menu s n k))
    -> MenuBar s n k -> Const (First (Menu s n k)) (MenuBar s n k))
-> Getting (First (Menu s n k)) s (Menu s n k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Vector (Menu s n k)
 -> Const (First (Menu s n k)) (Vector (Menu s n k)))
-> MenuBar s n k -> Const (First (Menu s n k)) (MenuBar s n k)
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL((Vector (Menu s n k)
  -> Const (First (Menu s n k)) (Vector (Menu s n k)))
 -> MenuBar s n k -> Const (First (Menu s n k)) (MenuBar s n k))
-> ((Menu s n k -> Const (First (Menu s n k)) (Menu s n k))
    -> Vector (Menu s n k)
    -> Const (First (Menu s n k)) (Vector (Menu s n k)))
-> (Menu s n k -> Const (First (Menu s n k)) (Menu s n k))
-> MenuBar s n k
-> Const (First (Menu s n k)) (MenuBar s n k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Index (Vector (Menu s n k))
-> Traversal' (Vector (Menu s n k)) (IxValue (Vector (Menu s n k)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Int
Index (Vector (Menu s n k))
i)
            case mMatchingMenu of
                Maybe (Menu s n k)
Nothing -> () -> EventM n s ()
forall a. a -> EventM n s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                Just Menu s n k
matchingMenu ->
                    Bool -> EventM n s () -> EventM n s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Menu s n k -> Bool
forall s n k. Menu s n k -> Bool
menuIsOpen Menu s n k
matchingMenu) (EventM n s () -> EventM n s ()) -> EventM n s () -> EventM n s ()
forall a b. (a -> b) -> a -> b
$ do
                        (MenuBar s n k -> Identity (MenuBar s n k)) -> s -> Identity s
Lens' s (MenuBar s n k)
which ((MenuBar s n k -> Identity (MenuBar s n k)) -> s -> Identity s)
-> (MenuBar s n k -> MenuBar s n k) -> EventM n s ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= MenuBar s n k -> MenuBar s n k
forall s n k. MenuBar s n k -> MenuBar s n k
closeAllMenus
                        (MenuBar s n k -> Identity (MenuBar s n k)) -> s -> Identity s
Lens' s (MenuBar s n k)
which ((MenuBar s n k -> Identity (MenuBar s n k)) -> s -> Identity s)
-> (MenuBar s n k -> MenuBar s n k) -> EventM n s ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= Int -> MenuBar s n k -> MenuBar s n k
forall s n k. Int -> MenuBar s n k -> MenuBar s n k
openMenuAtIndex Int
i
            return True
handleMenuBarEvent Lens' s (MenuBar s n k)
which BrickEvent n e
e =
    Lens' s (MenuBar s n k)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
forall s n k.
Lens' s (MenuBar s n k)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
withOpenMenu (MenuBar s n k -> f (MenuBar s n k)) -> s -> f s
Lens' s (MenuBar s n k)
which (((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
forall a b. (a -> b) -> a -> b
$ \(Int
idx, Menu s n k
_) ->
        Traversal' s (Menu s n k) -> BrickEvent n e -> EventM n s Bool
forall n s k e.
Eq n =>
Traversal' s (Menu s n k) -> BrickEvent n e -> EventM n s Bool
handleMenuEvent ((MenuBar s n k -> f (MenuBar s n k)) -> s -> f s
Lens' s (MenuBar s n k)
which((MenuBar s n k -> f (MenuBar s n k)) -> s -> f s)
-> ((Menu s n k -> f (Menu s n k))
    -> MenuBar s n k -> f (MenuBar s n k))
-> (Menu s n k -> f (Menu s n k))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL((Vector (Menu s n k) -> f (Vector (Menu s n k)))
 -> MenuBar s n k -> f (MenuBar s n k))
-> ((Menu s n k -> f (Menu s n k))
    -> Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> (Menu s n k -> f (Menu s n k))
-> MenuBar s n k
-> f (MenuBar s n k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Index (Vector (Menu s n k))
-> Traversal' (Vector (Menu s n k)) (IxValue (Vector (Menu s n k)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Int
Index (Vector (Menu s n k))
idx) BrickEvent n e
e

-- | Given a menu bar with an open menu, switch the open menu to the one
-- preceding the currently open one, or do nothing if no menu is open.
openPreviousMenu :: MenuBar s n k -> MenuBar s n k
openPreviousMenu :: forall s n k. MenuBar s n k -> MenuBar s n k
openPreviousMenu MenuBar s n k
mb = MenuBar s n k -> Maybe (MenuBar s n k) -> MenuBar s n k
forall a. a -> Maybe a -> a
fromMaybe MenuBar s n k
mb (Maybe (MenuBar s n k) -> MenuBar s n k)
-> Maybe (MenuBar s n k) -> MenuBar s n k
forall a b. (a -> b) -> a -> b
$ do
    (i, _) <- MenuBar s n k -> Maybe (Int, Menu s n k)
forall s n k. MenuBar s n k -> Maybe (Int, Menu s n k)
getOpenMenu MenuBar s n k
mb
    let newIndex = if Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
                   then Vector (Menu s n k) -> Int
forall a. Vector a -> Int
V.length (MenuBar s n k
mbMenuBar s n k
-> Getting
     (Vector (Menu s n k)) (MenuBar s n k) (Vector (Menu s n k))
-> Vector (Menu s n k)
forall s a. s -> Getting a s a -> a
^.Getting (Vector (Menu s n k)) (MenuBar s n k) (Vector (Menu s n k))
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
                   else Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
    return $ openMenuAtIndex newIndex $ closeAllMenus mb

-- | Given a menu bar with an open menu, switch the open menu to the one
-- following the currently open one, or do nothing if no menu is open.
openNextMenu :: MenuBar s n k -> MenuBar s n k
openNextMenu :: forall s n k. MenuBar s n k -> MenuBar s n k
openNextMenu MenuBar s n k
mb = MenuBar s n k -> Maybe (MenuBar s n k) -> MenuBar s n k
forall a. a -> Maybe a -> a
fromMaybe MenuBar s n k
mb (Maybe (MenuBar s n k) -> MenuBar s n k)
-> Maybe (MenuBar s n k) -> MenuBar s n k
forall a b. (a -> b) -> a -> b
$ do
    (i, _) <- MenuBar s n k -> Maybe (Int, Menu s n k)
forall s n k. MenuBar s n k -> Maybe (Int, Menu s n k)
getOpenMenu MenuBar s n k
mb
    let newIndex = if Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Vector (Menu s n k) -> Int
forall a. Vector a -> Int
V.length (MenuBar s n k
mbMenuBar s n k
-> Getting
     (Vector (Menu s n k)) (MenuBar s n k) (Vector (Menu s n k))
-> Vector (Menu s n k)
forall s a. s -> Getting a s a -> a
^.Getting (Vector (Menu s n k)) (MenuBar s n k) (Vector (Menu s n k))
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
                   then Int
0
                   else Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
    return $ openMenuAtIndex newIndex mb

-- | Close all open menus in this menu bar.
closeAllMenus :: MenuBar s n k -> MenuBar s n k
closeAllMenus :: forall s n k. MenuBar s n k -> MenuBar s n k
closeAllMenus MenuBar s n k
mb = MenuBar s n k
mb MenuBar s n k -> (MenuBar s n k -> MenuBar s n k) -> MenuBar s n k
forall a b. a -> (a -> b) -> b
& (Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
-> MenuBar s n k -> Identity (MenuBar s n k)
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL((Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
 -> MenuBar s n k -> Identity (MenuBar s n k))
-> ((Menu s n k -> Identity (Menu s n k))
    -> Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
-> (Menu s n k -> Identity (Menu s n k))
-> MenuBar s n k
-> Identity (MenuBar s n k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Menu s n k -> Identity (Menu s n k))
-> Vector (Menu s n k) -> Identity (Vector (Menu s n k))
forall s t a b. Each s t a b => Traversal s t a b
Traversal
  (Vector (Menu s n k))
  (Vector (Menu s n k))
  (Menu s n k)
  (Menu s n k)
each ((Menu s n k -> Identity (Menu s n k))
 -> MenuBar s n k -> Identity (MenuBar s n k))
-> (Menu s n k -> Menu s n k) -> MenuBar s n k -> MenuBar s n k
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Menu s n k -> Menu s n k
forall s n k. Menu s n k -> Menu s n k
closeMenu

-- | Open the menu at the specified index, closing any other open menus
-- in the menu bar. If the index is invalid, this does nothing.
openMenuAtIndex :: Int -> MenuBar s n k -> MenuBar s n k
openMenuAtIndex :: forall s n k. Int -> MenuBar s n k -> MenuBar s n k
openMenuAtIndex Int
i MenuBar s n k
mb = (MenuBar s n k -> MenuBar s n k
forall s n k. MenuBar s n k -> MenuBar s n k
closeAllMenus MenuBar s n k
mb) MenuBar s n k -> (MenuBar s n k -> MenuBar s n k) -> MenuBar s n k
forall a b. a -> (a -> b) -> b
& (Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
-> MenuBar s n k -> Identity (MenuBar s n k)
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL((Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
 -> MenuBar s n k -> Identity (MenuBar s n k))
-> ((Menu s n k -> Identity (Menu s n k))
    -> Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
-> (Menu s n k -> Identity (Menu s n k))
-> MenuBar s n k
-> Identity (MenuBar s n k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Index (Vector (Menu s n k))
-> Traversal' (Vector (Menu s n k)) (IxValue (Vector (Menu s n k)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Int
Index (Vector (Menu s n k))
i ((Menu s n k -> Identity (Menu s n k))
 -> MenuBar s n k -> Identity (MenuBar s n k))
-> (Menu s n k -> Menu s n k) -> MenuBar s n k -> MenuBar s n k
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Menu s n k -> Menu s n k
forall s n k. Menu s n k -> Menu s n k
openMenu

-- | Open the menu with the specified resource name constructor, closing
-- any other open menus in the menu bar. If the index is invalid, this
-- does nothing.
openNamedMenu :: (Eq n) => (MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k
openNamedMenu :: forall n s k.
Eq n =>
(MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k
openNamedMenu MenuRegion -> n
f MenuBar s n k
mb =
    case (MenuRegion -> n) -> MenuBar s n k -> Maybe (Int, Menu s n k)
forall n s k.
Eq n =>
(MenuRegion -> n) -> MenuBar s n k -> Maybe (Int, Menu s n k)
findNamedMenu MenuRegion -> n
f MenuBar s n k
mb of
        Just (Int
i, Menu s n k
_) -> (MenuBar s n k -> MenuBar s n k
forall s n k. MenuBar s n k -> MenuBar s n k
closeAllMenus MenuBar s n k
mb) MenuBar s n k -> (MenuBar s n k -> MenuBar s n k) -> MenuBar s n k
forall a b. a -> (a -> b) -> b
& (Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
-> MenuBar s n k -> Identity (MenuBar s n k)
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL((Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
 -> MenuBar s n k -> Identity (MenuBar s n k))
-> ((Menu s n k -> Identity (Menu s n k))
    -> Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
-> (Menu s n k -> Identity (Menu s n k))
-> MenuBar s n k
-> Identity (MenuBar s n k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Index (Vector (Menu s n k))
-> Traversal' (Vector (Menu s n k)) (IxValue (Vector (Menu s n k)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Int
Index (Vector (Menu s n k))
i ((Menu s n k -> Identity (Menu s n k))
 -> MenuBar s n k -> Identity (MenuBar s n k))
-> (Menu s n k -> Menu s n k) -> MenuBar s n k -> MenuBar s n k
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Menu s n k -> Menu s n k
forall s n k. Menu s n k -> Menu s n k
openMenu
        Maybe (Int, Menu s n k)
Nothing -> MenuBar s n k
mb

findNamedMenu :: (Eq n) => (MenuRegion -> n) -> MenuBar s n k -> Maybe (Int, Menu s n k)
findNamedMenu :: forall n s k.
Eq n =>
(MenuRegion -> n) -> MenuBar s n k -> Maybe (Int, Menu s n k)
findNamedMenu MenuRegion -> n
f MenuBar s n k
mb =
    ((Int, Menu s n k) -> Bool)
-> Vector (Int, Menu s n k) -> Maybe (Int, Menu s n k)
forall a. (a -> Bool) -> Vector a -> Maybe a
V.find ((n -> n -> Bool
forall a. Eq a => a -> a -> Bool
== (MenuRegion -> n
f MenuRegion
MenuTitle)) (n -> Bool)
-> ((Int, Menu s n k) -> n) -> (Int, Menu s n k) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Menu s n k -> n
forall s n k. Menu s n k -> n
menuTitleName (Menu s n k -> n)
-> ((Int, Menu s n k) -> Menu s n k) -> (Int, Menu s n k) -> n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, Menu s n k) -> Menu s n k
forall a b. (a, b) -> b
snd) (Vector (Int, Menu s n k) -> Maybe (Int, Menu s n k))
-> Vector (Int, Menu s n k) -> Maybe (Int, Menu s n k)
forall a b. (a -> b) -> a -> b
$
    Vector Int -> Vector (Menu s n k) -> Vector (Int, Menu s n k)
forall a b. Vector a -> Vector b -> Vector (a, b)
V.zip ([Int] -> Vector Int
forall a. [a] -> Vector a
V.fromList [Int
0..]) (Vector (Menu s n k) -> Vector (Int, Menu s n k))
-> Vector (Menu s n k) -> Vector (Int, Menu s n k)
forall a b. (a -> b) -> a -> b
$ MenuBar s n k -> Vector (Menu s n k)
forall s n k. MenuBar s n k -> Vector (Menu s n k)
menuBarMenus MenuBar s n k
mb

-- | Set the menu orientation of the menu bar and all of its menus. For
-- details, see 'setMenuOrientation'.
setMenuBarOrientation :: MenuOrientation -> MenuBar s n k -> MenuBar s n k
setMenuBarOrientation :: forall s n k. MenuOrientation -> MenuBar s n k -> MenuBar s n k
setMenuBarOrientation MenuOrientation
o MenuBar s n k
mb = MenuBar s n k
mb MenuBar s n k -> (MenuBar s n k -> MenuBar s n k) -> MenuBar s n k
forall a b. a -> (a -> b) -> b
& (MenuOrientation -> Identity MenuOrientation)
-> MenuBar s n k -> Identity (MenuBar s n k)
forall s n k (f :: * -> *).
Functor f =>
(MenuOrientation -> f MenuOrientation)
-> MenuBar s n k -> f (MenuBar s n k)
menuBarOrientationL ((MenuOrientation -> Identity MenuOrientation)
 -> MenuBar s n k -> Identity (MenuBar s n k))
-> MenuOrientation -> MenuBar s n k -> MenuBar s n k
forall s t a b. ASetter s t a b -> b -> s -> t
.~ MenuOrientation
o
                                MenuBar s n k -> (MenuBar s n k -> MenuBar s n k) -> MenuBar s n k
forall a b. a -> (a -> b) -> b
& (Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
-> MenuBar s n k -> Identity (MenuBar s n k)
forall s n k s n k (f :: * -> *).
Functor f =>
(Vector (Menu s n k) -> f (Vector (Menu s n k)))
-> MenuBar s n k -> f (MenuBar s n k)
menuBarMenusL((Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
 -> MenuBar s n k -> Identity (MenuBar s n k))
-> ((Menu s n k -> Identity (Menu s n k))
    -> Vector (Menu s n k) -> Identity (Vector (Menu s n k)))
-> (Menu s n k -> Identity (Menu s n k))
-> MenuBar s n k
-> Identity (MenuBar s n k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Menu s n k -> Identity (Menu s n k))
-> Vector (Menu s n k) -> Identity (Vector (Menu s n k))
forall s t a b. Each s t a b => Traversal s t a b
Traversal
  (Vector (Menu s n k))
  (Vector (Menu s n k))
  (Menu s n k)
  (Menu s n k)
each ((Menu s n k -> Identity (Menu s n k))
 -> MenuBar s n k -> Identity (MenuBar s n k))
-> (Menu s n k -> Menu s n k) -> MenuBar s n k -> MenuBar s n k
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ MenuOrientation -> Menu s n k -> Menu s n k
forall s n k. MenuOrientation -> Menu s n k -> Menu s n k
setMenuOrientation MenuOrientation
o

-- | Toggle the open state of the menu at the specified index. If
-- toggling to open, this will close any other open menus in the menu
-- bar. If the index is invalid, this does nothing.
toggleMenuAtIndex :: Int -> MenuBar s n k -> MenuBar s n k
toggleMenuAtIndex :: forall s n k. Int -> MenuBar s n k -> MenuBar s n k
toggleMenuAtIndex Int
i MenuBar s n k
mb =
    case MenuBar s n k -> Maybe (Int, Menu s n k)
forall s n k. MenuBar s n k -> Maybe (Int, Menu s n k)
getOpenMenu MenuBar s n k
mb of
        Maybe (Int, Menu s n k)
Nothing -> Int -> MenuBar s n k -> MenuBar s n k
forall s n k. Int -> MenuBar s n k -> MenuBar s n k
openMenuAtIndex Int
i MenuBar s n k
mb
        Just (Int
idx, Menu s n k
_) -> if Int
idx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i
                         then MenuBar s n k -> MenuBar s n k
forall s n k. MenuBar s n k -> MenuBar s n k
closeAllMenus MenuBar s n k
mb
                         else Int -> MenuBar s n k -> MenuBar s n k
forall s n k. Int -> MenuBar s n k -> MenuBar s n k
openMenuAtIndex Int
i MenuBar s n k
mb

-- | Toggle the open state of the menu with the specified resource name
-- constructor. If toggling to open, this will close any other open
-- menus in the menu bar. If the index is invalid, this does nothing.
toggleNamedMenu :: (Eq n) => (MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k
toggleNamedMenu :: forall n s k.
Eq n =>
(MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k
toggleNamedMenu MenuRegion -> n
f MenuBar s n k
mb =
    case MenuBar s n k -> Maybe (Int, Menu s n k)
forall s n k. MenuBar s n k -> Maybe (Int, Menu s n k)
getOpenMenu MenuBar s n k
mb of
        Maybe (Int, Menu s n k)
Nothing -> (MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k
forall n s k.
Eq n =>
(MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k
openNamedMenu MenuRegion -> n
f MenuBar s n k
mb
        Just (Int
_, Menu s n k
m) -> if Menu s n k -> n
forall s n k. Menu s n k -> n
menuTitleName Menu s n k
m n -> n -> Bool
forall a. Eq a => a -> a -> Bool
== MenuRegion -> n
f MenuRegion
MenuTitle
                       then MenuBar s n k -> MenuBar s n k
forall s n k. MenuBar s n k -> MenuBar s n k
closeAllMenus MenuBar s n k
mb
                       else (MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k
forall n s k.
Eq n =>
(MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k
openNamedMenu MenuRegion -> n
f MenuBar s n k
mb

-- | Given a lens to access a menu bar and a handler to invoke on its
-- currently open menu, invoke the handler if there is an open menu and
-- return its result, or do nothing and return False otherwise.
withOpenMenu :: Lens' s (MenuBar s n k) -> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
withOpenMenu :: forall s n k.
Lens' s (MenuBar s n k)
-> ((Int, Menu s n k) -> EventM n s Bool) -> EventM n s Bool
withOpenMenu Lens' s (MenuBar s n k)
which (Int, Menu s n k) -> EventM n s Bool
f = do
    mb <- Getting (MenuBar s n k) s (MenuBar s n k)
-> EventM n s (MenuBar s n k)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting (MenuBar s n k) s (MenuBar s n k)
Lens' s (MenuBar s n k)
which
    case getOpenMenu mb of
        Maybe (Int, Menu s n k)
Nothing -> Bool -> EventM n s Bool
forall a. a -> EventM n s a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
        Just (Int, Menu s n k)
pair -> (Int, Menu s n k) -> EventM n s Bool
f (Int, Menu s n k)
pair