| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Brick.Widgets.MenuBar
Description
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 ofSimpleMenus created withsimpleMenuDispatchingMenuBar: a menu bar made up ofDispatchingMenus created withmenuWithDispatcherMenuBar: the fully general type for menu bars with menus created withmenu
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 withmakeLenses. - Populate the application's initial state with the menu bar.
- Render the menu bar with
renderMenuBar. - Handle incoming events first with
handleMenuBarEvent, and whenhandleMenuBarEventreturnsFalse, pass unhandled events on to the existing application event handler.
Synopsis
- data MenuBar s n k
- type SimpleMenuBar s n = MenuBar s n (EventM n s ())
- type DispatchingMenuBar s n k = MenuBar s n (EventM n s (EntryTrigger s n k))
- newMenuBar :: Ord n => [Menu s n k] -> MenuBar s n k
- handleMenuBarEvent :: Eq n => Lens' s (MenuBar s n k) -> BrickEvent n e -> EventM n s Bool
- renderMenuBar :: Ord n => s -> MenuBar s n k -> Widget n
- hasOpenMenu :: MenuBar s n k -> Bool
- closeAllMenus :: MenuBar s n k -> MenuBar s n k
- openMenuAtIndex :: Int -> MenuBar s n k -> MenuBar s n k
- toggleMenuAtIndex :: Int -> MenuBar s n k -> MenuBar s n k
- toggleNamedMenu :: Eq n => (MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k
- openNamedMenu :: Eq n => (MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k
- setMenuBarOrientation :: MenuOrientation -> MenuBar s n k -> MenuBar s n k
Types
A menu bar holding a sequence of menus.
A menu bar can have up to one open menu at a time.
type SimpleMenuBar s n = MenuBar s n (EventM n s ()) Source #
A specialization of MenuBar for menus with EventM handlers; use
this with simpleMenu.
type DispatchingMenuBar s n k = MenuBar s n (EventM n s (EntryTrigger s n k)) Source #
A specialization of MenuBar for menus with abstract key event
triggers; this with menuWithDispatcher.
Creating menu bars
newMenuBar :: Ord n => [Menu s n k] -> MenuBar s n k Source #
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.
Handling events
Arguments
| :: 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 |
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.
Escwill 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
Rendering
renderMenuBar :: Ord n => s -> MenuBar s n k -> Widget n Source #
Render this menu bar with the given application state as input.
Working with menu bars
hasOpenMenu :: MenuBar s n k -> Bool Source #
Return whether this menu bar has an open menu.
closeAllMenus :: MenuBar s n k -> MenuBar s n k Source #
Close all open menus in this menu bar.
openMenuAtIndex :: Int -> MenuBar s n k -> MenuBar s n k Source #
Open the menu at the specified index, closing 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 Source #
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.
toggleNamedMenu :: Eq n => (MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k Source #
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.
openNamedMenu :: Eq n => (MenuRegion -> n) -> MenuBar s n k -> MenuBar s n k Source #
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.
setMenuBarOrientation :: MenuOrientation -> MenuBar s n k -> MenuBar s n k Source #
Set the menu orientation of the menu bar and all of its menus. For
details, see setMenuOrientation.