menu
1 Overview
2 Internal Representation
3 Predicates
is-wv-menu?
4 Constructors
wv-menu
wv-menu-item
5 Traversal and Lookup
wv-menu-for-each
with-wv-menu-item
6 Mutation
wv-menu-set-title!
wv-menu-set-icon!
wv-menu-set-callback!
7 Conversion
wv-menu->json
8 Accessors
wv-menu-id
wv-menu-item-id
wv-menu-item-callback
9.1

menu🔗ℹ

Hans Dijkema <hans@dijkewijk.nl>

 (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
Returns #t if mnu is a valid menu tree and #f otherwise.

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🔗ℹ

procedure

(wv-menu item-or-id ...)  any/c

  item-or-id : any/c
Creates a menu.

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
Creates a menu item.

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?
Traverses menu depth-first and calls cb for each menu item.

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

(with-wv-menu-item menu id cb)  any/c

  menu : any/c
  id : symbol?
  cb : procedure?
Finds the menu item identified by id and applies cb to it.

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🔗ℹ

procedure

(wv-menu-set-title! menu id title)  any/c

  menu : any/c
  id : symbol?
  title : string?
Sets the title of the menu item identified by id.

title must be a string. The function returns the original menu value.

procedure

(wv-menu-set-icon! menu id icon-url)  any/c

  menu : any/c
  id : symbol?
  icon-url : (or/c boolean? string? url?)
Sets the icon URL of the menu item identified by id.

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

(wv-menu-set-callback! menu id cb)  any/c

  menu : any/c
  id : symbol?
  cb : procedure?
Sets the callback of the menu item identified by id.

cb must be a procedure. The function returns the original menu value.

7 Conversion🔗ℹ

procedure

(wv-menu->json menu)  string?

  menu : any/c
Converts menu to a JSON string.

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-id m)  any/c

  m : any/c
Returns the identifier of m.

procedure

(wv-menu-item-id mi)  symbol?

  mi : any/c
Returns the identifier of the menu item mi.

procedure

(wv-menu-item-callback mi)  procedure?

  mi : any/c
Returns the callback associated with the menu item mi.