menu
| (require menu) |
Menu data structures used by the webview library.
This module provides constructors, predicates, traversal helpers, mutation operations, and JSON conversion for menu trees.
1 Overview
A menu is represented as a tree. A menu consists of menu items, and a menu item may optionally contain a submenu.
Menu identifiers are symbols. Menu item titles are strings. Icons are stored as strings and may be supplied either as #f, strings, or URL values.
The module does not display menus itself. It provides the menu data structure used by higher layers.
2 Internal Representation
Internally, menus are represented by transparent structure values. These structure constructors and predicates are not exported directly. The public API uses constructor procedures and helper functions operating on those internal values.
3 Predicates
procedure
(is-wv-menu? mnu) → boolean?
mnu : any/c
A value is recognized as a menu if it is an internal menu structure whose item list is a list of internal menu items, and every submenu recursively also satisfies is-wv-menu?.
4 Constructors
If the first argument is a symbol, it is used as the menu identifier and removed from the remaining arguments. Otherwise the menu identifier is #f.
If the first remaining argument is itself a list, that list is used as the item list. Otherwise all remaining arguments are treated as menu items.
This means the following forms are accepted:
(wv-menu item ...) (wv-menu 'some-id item ...) (wv-menu (list item ...)) (wv-menu 'some-id (list item ...))
The result is a value satisfying is-wv-menu?.
procedure
(wv-menu-item id title [ #:icon-url icon-url #:callback callback #:submenu submenu #:separator separator]) → any/c id : symbol? title : string? icon-url : (or/c boolean? string? url?) = #f callback : procedure? = (λ args #t) submenu : (or/c boolean? any/c) = #f separator : boolean? = #f
id must be a symbol and title must be a string.
icon-url must be #f, a string, or a URL value. If it is a URL value, it is converted to a string using url->string before being stored.
submenu must be #f or a value satisfying is-wv-menu?.
separator must be a boolean.
If any argument does not satisfy these conditions, an exception is raised.
5 Traversal and Lookup
procedure
(wv-menu-for-each menu cb) → boolean?
menu : any/c cb : procedure?
If a menu item contains a submenu, that submenu is traversed recursively.
The callback is invoked only for menu items that are reached by the traversal. The function returns #t.
procedure
menu : any/c id : symbol? cb : procedure?
If menu does not satisfy is-wv-menu?, an exception is raised. If id is not a symbol, an exception is raised. If no item with the given id can be found, an exception is raised.
After the callback has been applied, the original menu value is returned.
6 Mutation
title must be a string. The function returns the original menu value.
icon-url must be #f, a string, or a URL value. If it is a URL value, it is converted to a string using url->string before being stored.
The function returns the original menu value.
procedure
menu : any/c id : symbol? cb : procedure?
cb must be a procedure. The function returns the original menu value.
7 Conversion
procedure
(wv-menu->json menu) → string?
menu : any/c
The conversion first builds a hash-based representation of the menu and then writes that representation with write-json.
In the JSON representation:
the top-level object contains the keys 'menu and 'id
menu item identifiers are converted to strings
menu item titles are written under the key 'name
an icon is written only if it is not #f
a submenu is written recursively only if it is not #f
a separator flag is written only if it is not #f
The 'id field of the top-level menu is also converted to a string in the JSON output.
8 Accessors
procedure
(wv-menu-item-id mi) → symbol?
mi : any/c
procedure
(wv-menu-item-callback mi) → procedure?
mi : any/c