| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Brick.Widgets.Menu
Description
This module provides a menu widget that is similar to the ones commonly found in most graphical interface toolkits. Menus carry entries that can be activated with the mouse and keyboard and invoke event handlers that you specify when creating the menus and entries.
General Information
Menus carry a sequence of items, expressed by the MenuItem type.
Items can be:
- entries - named menu items that can be activated with the keyboard or mouse
- submenus - entries that contain nested menus
- separators - horizontal lines dividing up groups of other entries
- gaps - vertical space between items
Menu entries can be either enabled or disabled; their status in
this regard is determined by invoking a function of type s -> Bool
at rendering and event-handling time.
Menus and submenus support both mouse and keyboard interaction. See
handleMenuEvent for details.
Menus have an orientation that can be changed with
setMenuOrientation to suit different writing systems. This affects
how entries and submenus are rendered and how left/right arrow keys
navigate submenus.
Use Cases
This module provides a fully general Menu type and a few
specialized menu types for common menu use cases:
SimpleMenu: a menu with entries that haveEventMhandlers. Create one of these withsimpleMenu. This is a good starting point.DispatchingMenu: a menu whose entries correspond to abstract key events bound to keys by aKeyDispatcher. Create one of these withmenuWithDispatcher. This is a good choice when you already have aKeyDispatcherset up and would like menu entries to be triggered by the rebindable keys that trigger the dispatcher's handlers.Menu: the fully general type for menus. Create one of these withmenu.
Depending on the type of menu you're creating, different item
constructors may apply. See the MenuItem type aliases since their
naming convention follows that of the menu types.
See the MenuDemo and MenuKeybindingsDemo demonstration programs
for complete working examples of using this API.
Adding Menus to An Application
To use this module in an application:
- Choose a menu type that you want to work with such as
SimpleMenu. - For each menu that you want to host, add an application state field
and lens for a value of the menu's type, and add a constructor
to the application's resource name type, with an argument of
type
MenuRegion. Add lenses to the application state type withmakeLenses. - Populate the application's initial state with the menus.
- Render menus with
renderMenu. - Handle incoming events first with
handleMenuEvent, and whenhandleMenuEventreturnsFalse, pass unhandled events on to the existing application event handler. - As desired, add entries to the application's
AttrMapfor the attributes used in this module.
Use MenuBar if you want to host more than one
menu in a group.
Handling Events
Menu events are handled with handleMenuEvent, and any unhandled
events should be deferred to the application's event handler.
To support mouse events, each menu must be identified by a unique
resource name; this is done by providing a resource name constructor
when creating each menu. The application's name type must provide a
constructor of type MenuRegion -> n to uniquely identify the menu
and its constituent parts. For example, if the application's resource
name type is as follows,
data Name = Editor1 | Editor2
It would need to be modified so that a new data constructor (e.g.
FileMenu) could be given to the menu constuctors:
data Name = Editor1 | Editor2 | FileMenu MenuRegion
Synopsis
- data Menu s n k
- menuIsOpen :: Menu s n k -> Bool
- menuContentWidth :: Menu s n k -> Int
- menuTitleName :: Menu s n k -> n
- data MenuRegion
- = MenuTitle
- | MenuBody
- | MenuItemAt Int
- data MenuOrientation
- openMenu :: Menu s n k -> Menu s n k
- closeMenu :: Menu s n k -> Menu s n k
- toggleMenu :: Menu s n k -> Menu s n k
- menu :: Text -> (MenuRegion -> n) -> [MenuItem s n k] -> (k -> EventM n s ()) -> Menu s n k
- data MenuItem s n k
- menuEntry :: Text -> k -> MenuItem s n k
- menuSeparator :: MenuItem s n k
- menuGap :: MenuItem s n k
- submenu :: Menu s n k -> MenuItem s n k
- setDefaultEntryRenderer :: (MenuOrientation -> k -> Text -> Widget n) -> Menu s n k -> Menu s n k
- setTitleRenderer :: (s -> Text -> Widget n) -> Menu s n k -> Menu s n k
- titleHightlightKey :: Char -> s -> Text -> Widget n
- setMenuOrientation :: MenuOrientation -> Menu s n k -> Menu s n k
- setEnabledWith :: (s -> Bool) -> MenuItem s n k -> MenuItem s n k
- setEntryRenderer :: (MenuOrientation -> k -> Text -> Widget n) -> MenuItem s n k -> MenuItem s n k
- type SimpleMenu s n = Menu s n (EventM n s ())
- type SimpleMenuItem s n = MenuItem s n (EventM n s ())
- simpleMenu :: Text -> (MenuRegion -> n) -> [SimpleMenuItem s n] -> SimpleMenu s n
- type DispatchingMenu s n k = Menu s n (EntryTrigger s n k)
- type DispatchingMenuItem s n k = MenuItem s n (EntryTrigger s n k)
- data EntryTrigger s n k
- = TriggerEvent !(EventTrigger k)
- | TriggerAction !(EventM n s ())
- menuWithDispatcher :: Eq k => KeyDispatcher k (EventM n s) -> Text -> (MenuRegion -> n) -> [DispatchingMenuItem s n k] -> DispatchingMenu s n k
- menuEntryForKey :: Text -> Binding -> DispatchingMenuItem s n k
- menuEntryForEvent :: Text -> k -> DispatchingMenuItem s n k
- menuEntryForAction :: Text -> EventM n s () -> DispatchingMenuItem s n k
- entryWithKeybinding :: Eq k => KeyDispatcher k (EventM n s) -> MenuOrientation -> EntryTrigger s n k -> Text -> Widget n
- handleMenuEvent :: Eq n => Traversal' s (Menu s n k) -> BrickEvent n e -> EventM n s Bool
- renderMenu :: Ord n => s -> Menu s n k -> Widget n
- menuAttr :: AttrName
- menuTitleAttr :: AttrName
- menuTitleSelectedAttr :: AttrName
- menuTitleKeyHighlightAttr :: AttrName
- menuBodyAttr :: AttrName
- menuEntryDisabledAttr :: AttrName
- menuEntrySelectedAttr :: AttrName
- menuEntrySelectedDisabledAttr :: AttrName
- menuEntryKeybindingAttr :: AttrName
Documentation
The general menu type.
Menus and their items are parameterized on three types:
s: the application state type used in theApptype,n: the application resource name type used in the application'sWidgettype, andk: the type of data carried and handled by the menu's event handler when a menu entry has been activated.
Menus contain a sequence of items of type MenuItem. See the
documentation above and the constructors for both menus and menu
items to create menus.
A menu is either open, in which case its contents are being shown
in a floating layer above the application's UI and it is responding
to events that manipulate the menu's selected entry, or it is
closed, in which case its contents are not shown and it is not
responding to events other than mouse clicks on its title. The menu's
open/closed state is affected by calls to openMenu, closeMenu,
and mouse click events on the menu's title.
At any given time, a menu may or may not have a currently-selected
entry. See handleMenuEvent for details on how keyboard and mouse
events influence the choice and behavior of the selected entry. When
an entry is selected, it can be activated by an Enter keypress or
a mouse click. When activated, its event data is used to invoke the
menu's event handler.
To support mouse events, each menu must be identified by a unique
resource name; this is done by providing a resource name constructor
when creating each menu. The application's name type must provide a
constructor of type MenuRegion -> n to uniquely identify the menu
and its constituent parts.
A menu carries an event handler that will be invoked by
handleMenuEvent whenever a menu entry is selected.
menuIsOpen :: Menu s n k -> Bool Source #
Whether the menu is open.
menuContentWidth :: Menu s n k -> Int Source #
The width of the menu's items within the enclosing border. This is a record accessor so it can also be used to change the menu's width.
menuTitleName :: Menu s n k -> n Source #
The resource name for this menu's title for generating and detecting mouse click events
data MenuRegion Source #
The type of menu regions for embedding in the application's resource name and reporting mouse click events.
Constructors
| MenuTitle | The region of a menu's title |
| MenuBody | The region of a menu's body |
| MenuItemAt Int | The region of the menu item at the specified index |
Instances
| Eq MenuRegion Source # | |
Defined in Brick.Widgets.Menu | |
| Ord MenuRegion Source # | |
Defined in Brick.Widgets.Menu Methods compare :: MenuRegion -> MenuRegion -> Ordering (<) :: MenuRegion -> MenuRegion -> Bool (<=) :: MenuRegion -> MenuRegion -> Bool (>) :: MenuRegion -> MenuRegion -> Bool (>=) :: MenuRegion -> MenuRegion -> Bool max :: MenuRegion -> MenuRegion -> MenuRegion min :: MenuRegion -> MenuRegion -> MenuRegion | |
| Show MenuRegion Source # | |
Defined in Brick.Widgets.Menu Methods showsPrec :: Int -> MenuRegion -> ShowS show :: MenuRegion -> String showList :: [MenuRegion] -> ShowS | |
data MenuOrientation Source #
Orientation for menu contents.
Constructors
| LeftToRight | Menu entries are laid out with labels on the left and submenus opening to the right |
| RightToLeft | Menu entries are laid out with labels on the right and submenus opening to the left |
Instances
| Eq MenuOrientation Source # | |
Defined in Brick.Widgets.Menu Methods (==) :: MenuOrientation -> MenuOrientation -> Bool (/=) :: MenuOrientation -> MenuOrientation -> Bool | |
| Ord MenuOrientation Source # | |
Defined in Brick.Widgets.Menu Methods compare :: MenuOrientation -> MenuOrientation -> Ordering (<) :: MenuOrientation -> MenuOrientation -> Bool (<=) :: MenuOrientation -> MenuOrientation -> Bool (>) :: MenuOrientation -> MenuOrientation -> Bool (>=) :: MenuOrientation -> MenuOrientation -> Bool max :: MenuOrientation -> MenuOrientation -> MenuOrientation min :: MenuOrientation -> MenuOrientation -> MenuOrientation | |
| Show MenuOrientation Source # | |
Defined in Brick.Widgets.Menu Methods showsPrec :: Int -> MenuOrientation -> ShowS show :: MenuOrientation -> String showList :: [MenuOrientation] -> ShowS | |
closeMenu :: Menu s n k -> Menu s n k Source #
Close a menu and unselect any selected entry. Also closes any open submenus in the menu, recursively.
toggleMenu :: Menu s n k -> Menu s n k Source #
Toggle the menu's open state.
Constructing menus and items
Arguments
| :: Text | The menu's title |
| -> (MenuRegion -> n) | The menu's resource name constructor |
| -> [MenuItem s n k] | The items in this menu |
| -> (k -> EventM n s ()) | The event handler to invoke when entries are activated |
| -> Menu s n k |
Create a Menu.
By default, menus use the LeftToRight content orientation. This can
be changed with setMenuOrientation.
By default, entries are rendered using txt. Change this with
setDefaultEntryRenderer or setEntryRenderer.
By default, the menu title is rendered using txt. Change this with
setTitleRenderer.
Arguments
| :: Text | The menu entry's label |
| -> k | The event data carried by the menu entry that will be passed to the enclosing menu's event handler when this entry is activated |
| -> MenuItem s n k |
Create a menu entry with the specified label and event data. When the entry is activated, its event data will be passed to the event handler of the enclosing menu.
By default, this entry has no custom renderer so its appearance is
determined by the default entry renderer of the enclosing menu.
To change either of these behaviors, use setEntryRenderer or
setDefaultEntryRenderer.
By default, this entry is always enabled regardless of the
application state. To change this, use setEnabledWith.
This is the fully general entry constructor. For more specific use
cases, see the other MenuItem constructors in this module.
menuSeparator :: MenuItem s n k Source #
A separator between menu items.
submenu :: Menu s n k -> MenuItem s n k Source #
A submenu. The menu's title will be used as the submenu's label in its parent menu.
Configuring menus
setDefaultEntryRenderer :: (MenuOrientation -> k -> Text -> Widget n) -> Menu s n k -> Menu s n k Source #
Set this menu's entry rendering function.
setTitleRenderer :: (s -> Text -> Widget n) -> Menu s n k -> Menu s n k Source #
Set this menu's title renderer.
titleHightlightKey :: Char -> s -> Text -> Widget n Source #
A title rendering function that highlights the specified character
with menuTitleKeyHighlightAttr if it appears in the title,
case-insensitively. Use with setTitleRenderer.
setMenuOrientation :: MenuOrientation -> Menu s n k -> Menu s n k Source #
Set the menu's content orientation, including the orientation of all of its submenus.
Configuring menu items
setEnabledWith :: (s -> Bool) -> MenuItem s n k -> MenuItem s n k Source #
Set this menu entry's function used to check for its enabled state.
This is equivalent to id for non-entry items.
setEntryRenderer :: (MenuOrientation -> k -> Text -> Widget n) -> MenuItem s n k -> MenuItem s n k Source #
Set this menu entry's rendering function, overriding the menu's
default rendering behavior for this entry. This is equivalent to id
for non-entry items.
Menus with EventM handlers
type SimpleMenu s n = Menu s n (EventM n s ()) Source #
A specialization of Menu that has EventM handlers in each menu
entry that are evaluated whenever the entries are activated. Create
one of these with simpleMenu.
type SimpleMenuItem s n = MenuItem s n (EventM n s ()) Source #
A specialization of MenuItem for SimpleMenu. Create these with
menuGap, menuSeparator, submenu, and menuEntry.
Arguments
| :: Text | The menu's title |
| -> (MenuRegion -> n) | The menu's resource name constructor |
| -> [SimpleMenuItem s n] | The items in this menu |
| -> SimpleMenu s n |
Create a SimpleMenu whose entries carry ordinary EventM
handlers that are evaluated whenever the menu's entries are
activated.
Menus with custom keybindings
type DispatchingMenu s n k = Menu s n (EntryTrigger s n k) Source #
A specialization of Menu whose entries are associated with
specific keys or abstract key events handled by a KeyDispatcher.
Create one of these with menuWithDispatcher.
type DispatchingMenuItem s n k = MenuItem s n (EntryTrigger s n k) Source #
A specialization of MenuItem for DispatchingMenu. Create these
with menuGap, menuSeparator, submenu, menuEntryForKey,
menuEntryForAction, and menuEntryForEvent.
data EntryTrigger s n k Source #
A trigger to be executed when an entry with this trigger
is activated. This is exposed for completeness only; use
menuEntryForKey, menuEntryForAction, and menuEntryForEvent to
work with this data type indirectly.
Constructors
| TriggerEvent !(EventTrigger k) | The entry produces an |
| TriggerAction !(EventM n s ()) | The entry runs a specific |
Arguments
| :: Eq k | |
| => KeyDispatcher k (EventM n s) | The key dispatcher to use to build the menu, and whose handlers should be invoked by the menu's entries when activated |
| -> Text | The menu's title |
| -> (MenuRegion -> n) | The menu's resource name constructor |
| -> [DispatchingMenuItem s n k] | The items in this menu |
| -> DispatchingMenu s n k |
Create a Menu whose entries are activated by specific triggers,
including specified key bindings or abstract key events associated
with a KeyDispatcher. This uses entryWithKeybinding as its
default entry renderer to show available keybindings for entries
associated with key events.
To create entries in this menu, use menuEntryForKey,
menuEntryForEvent, and menuEntryForAction.
Arguments
| :: Text | The menu entry's label |
| -> Binding | The specific key binding to trigger this menu entry |
| -> DispatchingMenuItem s n k |
Create a menu entry that is activated by the specified key binding,
irrespective of the enclosing menu's KeyDispatcher configuration.
This entry will show the specified keybinding in its text.
Arguments
| :: Text | The menu entry's label |
| -> k | The abstract key event to generate when this entry is activated |
| -> DispatchingMenuItem s n k |
Create a menu entry that generates the specified abstract key event
when activated, thus triggering the enclosing menu's KeyDispatcher
handler for that event. This entry will show the first known
keybinding for the specified abstract key event, if any.
Arguments
| :: Text | The menu entry's label |
| -> EventM n s () | The action to evaluate when this entry is activated |
| -> DispatchingMenuItem s n k |
Create a menu entry that invokes the specified EventM action when
activated. Use this for entries that are not invoked by specific keys
or associated with abstract key events.
Arguments
| :: Eq k | |
| => KeyDispatcher k (EventM n s) | The key dispatcher to check for bindings |
| -> MenuOrientation | The menu's orientation |
| -> EntryTrigger s n k | The entry's trigger |
| -> Text | The entry's label |
| -> Widget n |
An entry rendering function usable with setDefaultEntryRenderer
and setEntryRenderer that renders a menu entry with the first known
available keybinding for its abstract event, as configured in the
specified KeyDispatcher.
Handling events
Arguments
| :: Eq n | |
| => Traversal' s (Menu s n k) | The traversal 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 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 the menu was not open to receive the
event).
Events handled include:
- Mouse clicks on the menu title will toggle whether the menu is open.
- If a submenu entry is selected, arrow keys will open and close it depending on the menu orientation.
- Mouse clicks on submenu entries will open their submenus.
Escwill close the menu if no submenus are open; otherwise it will close the last open submenu.- If no entry is selected, the Down arrow key will select the first entry and the Up arrow key will select the last entry.
- If an entry is selected, the Down arrow key will select the next entry and the Up arrow key will select the previous entry.
- If the selected entry is a submenu and the submenu is open, events will be delegated to the submenu until it closes.
In all other cases, this will attempt to defer to the menu's selected
entry or submenu 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 menu's selected entry, 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; a
return value of False indicates that the event should be handled by
the application because it did not affect the menu or its entries 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 <- handleMenuEvent myMenuLens e
when (not handled) $ do
-- Go on to handle the event in the rest of the application
Rendering menus
renderMenu :: Ord n => s -> Menu s n k -> Widget n Source #
Render a menu.
If the menu is closed, only its title is rendered. If the menu is open, its title is rendered with its contents shown as a floating layer vertically positioned below the title.
When menu contents are shown, they are rendered in a border, and
separators are rendered with hBorder. Use withBorderStyle to
change how such borders are drawn, e.g.,
drawUi :: s -> Widget n
drawUi s =
withBorderStyle unicodeRounded $
renderMenu s (s^.myMenu)
Attributes
menuTitleAttr :: AttrName Source #
Menu titles.
menuTitleSelectedAttr :: AttrName Source #
Selected menu titles, for open menus.
menuTitleKeyHighlightAttr :: AttrName Source #
A highlighted key in a menu title as rendered with
titleHightlightKey, based on menuTitleAttr.
menuBodyAttr :: AttrName Source #
The base attribute for menu bodies.
menuEntryDisabledAttr :: AttrName Source #
Disabled menu entries.
menuEntrySelectedAttr :: AttrName Source #
Selected and enabled menu entries.
menuEntrySelectedDisabledAttr :: AttrName Source #
Selected and disnabled menu entries.
menuEntryKeybindingAttr :: AttrName Source #
Menu entry keybindings for entries in menus created with
menuWithDispatcher.