High-Level Web Racket GUI API
1 Re-exported backend control and logging
ww-start
ww-stop
ww-set-debug
ww-debug
ww-error
ww-display-log
ww-tail-log
2 Re-exported Web  UI helper command control
ww-set-custom-webui-wire-command!
ww-get-webui-wire-version
3 Re-exported CSS and menu helpers
4 Element wrapper classes
4.1 Generic element
ww-element%
new
get-win-id
get-id
win
callback
connect
disconnect
add-style!
set-style!
style
get-attr
set-attr!
del-attr
get-attrs
add-class!
remove-class!
has-class?
enable
enabled?
disable
disabled?
display
hide
show
4.2 Form input element
ww-input%
get
on-change!
set!
disable
enable
5 Webview windows
5.1 Main windows
ww-webview%
new
clone-settings
handle-click
handle-change
handle-input
handle-navigate
get-win-id
element
get-elements
bind
bind-inputs
bind-buttons
move
resize
get-x
get-y
get-width
get-height
geom
set-title!
get-title
show
hide
maximize
normalize
minimize
fullscreen
show-state
can-close?
close
set-menu!
connect-menu!
set-icon!
set-html-file!
set-html
set-url
html-loaded
file-open
file-save
choose-dir
inherit-checks
5.2 Dialog windows
ww-webview-dialog%
5.3 Message dialogs
ww-webview-message%
6 Settings abstraction
ww-settings%
set
get
clone
set!
9.0

High-Level Web Racket GUI API🔗ℹ

Hans Dijkema <hans@dijkewijk.nl>

 (require web-racket) package: web-racket

The web-racket module provides a higher-level GUI layer on top of the low-level web-wire backend. It is intended as the main entry point for desktop applications that embed a WebUI.

The module exports:

  • OO wrappers for DOM elements and form inputs (ww-element%, ww-input%);

  • classes for main windows and dialogs (ww-webview%, ww-webview-dialog%, ww-webview-message%);

  • an abstract settings interface (ww-settings%);

  • backend control and logging helpers re-exported from web-wire;

  • backend installation/override helpers re-exported from webui-wire-download;

  • CSS and menu construction helpers re-exported from "css.rkt" and "menu.rkt".

The goal of this layer is:

  • You rarely call web-wire directly;

  • you work with windows, elements, and inputs as Racket objects;

  • only when you need fine-grained control do you drop to the underlying wire API.

1 Re-exported backend control and logging🔗ℹ

The following bindings are re-exported directly from web-wire. See that module’s documentation for full details; here is a short summary for convenience.

procedure

(ww-start log-level)  web-rkt?

  log-level : symbol?
Start the Web Wire backend if it is not running already, and return the current backend handle.

Most applications do not call this directly: ww-webview% constructors will automatically call ww-start when the first window is created.

procedure

(ww-stop)  void?

Stop the Web Wire backend.

This is called automatically when the last window created via ww-webview% is closed; you normally do not need to call it directly, but you can use it for explicit shutdown.

procedure

(ww-set-debug on?)  void?

  on? : boolean?
Enable or disable internal debug logging in the web-wire layer. This affects ww-debug output.

syntax

(ww-debug msg-expr)

#:contracts
([msg-expr any/c])
(ww-debug id-expr msg-expr)
#:contracts
([id-expr any/c] [msg-expr any/c])
Debug logging macros that write to the shared log buffer when debug is enabled.

syntax

(ww-error msg-expr)

#:contracts
([msg-expr any/c])
(ww-error id-expr msg-expr)
#:contracts
([id-expr any/c] [msg-expr any/c])
Error logging macros that always log, regardless of the debug setting.

procedure

(ww-display-log [filter])  void?

  filter : (or/c #f string? regexp? (listof string?)) = #f
Display the in-memory log buffer on current-output-port, optionally filtered.

procedure

(ww-tail-log args ...)  void?

  args : any/c
Show the tail of the log buffer and follow new entries (like tail -f). Optional arguments control how many lines to show initially and which lines to filter.

2 Re-exported WebUI helper command control🔗ℹ

The following bindings are re-exported from webui-wire-download; they are documented in more detail in that module. Briefly:

procedure

(ww-set-custom-webui-wire-command! cmd)  string?

  cmd : string?
Override the command used to start the webui-wire helper. Useful in development or when the helper is installed in a non-standard location.

procedure

(ww-get-webui-wire-version)  string?

Return the version string reported by the installed webui-wire helper.

3 Re-exported CSS and menu helpers🔗ℹ

The module also re-exports:

  • (all-from-out "css.rkt") — CSS helper functions and css-style values;

  • (all-from-out "menu.rkt") — menu construction helpers for building menu structures passed to ww-webview%.

See the documentation of those modules for the exact API.

4 Element wrapper classes🔗ℹ

These classes provide OO wrappers around DOM elements managed by webui-wire. They work on top of the lower-level web-wire functions that address elements by id.

4.1 Generic element🔗ℹ

class

ww-element% : class?

  superclass: object%

Base class for element wrappers.

constructor

(new ww-element% 
    [win-id win-id] 
    [id id] 
    ...superclass-args...) 
  (is-a?/c ww-element%)
  win-id : ww-win?
  id : (or/c symbol? string?)

The constructor normally isn’t called directly by user code. Objects are created automatically by ww-webview% when you bind inputs and buttons or when you explicitly request an element.

Fields:

  • win-id — a ww-win handle identifying the WebUI window that owns this element

  • id — the element id (symbol or string), as used in the DOM.

Public methods:

method

(send a-ww-element get-win-id)  ww-win?

Return the window handle used for this element.

method

(send a-ww-element get-id)  (or/c symbol? string?)

Return the element id.

method

(send a-ww-element win)  (or/c ww-webview% #f)

Look up the ww-webview% instance for win-id in the global window table. Returns #f if the window no longer exists.

method

(send a-ww-element callback evt args ...)  any

  evt : symbol?
  args : any/c
Call a previously registered callback for evt with the given args. This is used internally when DOM events arrive from webui-wire.

method

(send a-ww-element connect evt func)  void?

  evt : symbol?
  func : (-> any)
Register func as the handler for event kind evt on this element.

method

(send a-ww-element disconnect evt)  void?

  evt : symbol?
Remove any callback registered for evt.

method

(send a-ww-element add-style! st)  void?

  st : css-style?
Merge the given style into the element’s existing style.

method

(send a-ww-element set-style! st)  void?

  st : css-style?
Replace the element’s style with st.

method

(send a-ww-element style)  css-style?

Return the current style as a css-style value.

method

(send a-ww-element get-attr a)  jsexpr?

  a : (or/c symbol? string?)
Get a single attribute value as JSON.

method

(send a-ww-element set-attr! a val)  void?

  a : (or/c symbol? string?)
  val : any/c
Set an attribute value.

method

(send a-ww-element del-attr a)  void?

  a : (or/c symbol? string?)
Remove an attribute.

method

(send a-ww-element get-attrs)  (hash/c symbol? any/c)

Return all attributes as a hash table (symbol → value).

method

(send a-ww-element add-class! cl)  void?

  cl : (or/c symbol? string?)
Add a CSS class to the element.

method

(send a-ww-element remove-class! cl)  void?

  cl : (or/c symbol? string?)
Remove a CSS class.

method

(send a-ww-element has-class? cl)  boolean?

  cl : (or/c symbol? string?)
Test whether the element has a given CSS class.

method

(send a-ww-element enable)  void?

Remove the disabled state from the element (by manipulating its CSS/attributes).

method

(send a-ww-element enabled?)  boolean?

Return #t when the element is not disabled.

method

(send a-ww-element disable)  void?

Mark the element as disabled (e.g. by adding a disabled attribute and/or class).

method

(send a-ww-element disabled?)  boolean?

Return #t when the element is disabled.

method

(send a-ww-element display [d])  void?

  d : string? = "block"
Set the CSS display style; default is "block".

method

(send a-ww-element hide)  void?

Convenience for (display "none").

method

(send a-ww-element show)  void?

Show the element again (typically restoring display to a non-"none" value).

4.2 Form input element🔗ℹ

class

ww-input% : class?

  superclass: ww-element%

Wrapper for input-type elements (fields that have a “value” and that emit change/input events). Instances are usually created by ww-webview% when you call bind-inputs.
Internally, a ww-input% keeps track of:
  • the current value (mirrored from the DOM);

  • an optional change callback.

Public methods:

method

(send a-ww-input get)  any/c

Return the last known value of the input.

method

(send a-ww-input on-change! callback)  void?

  callback : (-> any/c any)
Register a change callback:

  • The callback is stored;

  • It is immediately called once with the current value;

  • Later, when an input event with a value field arrives from the browser, the stored callback is called with the new value.

method

(send a-ww-input set! v)  void?

  v : any/c
Set the input’s value both locally and in the DOM. This will normally update the form control on screen and may also trigger change/input events in the browser.

method

(send a-ww-input disable)  void?

Disable the input (overrides base behaviour).

method

(send a-ww-input enable)  void?

Enable the input again (overrides base behaviour).

5 Webview windows🔗ℹ

5.1 Main windows🔗ℹ

class

ww-webview% : class?

  superclass: object%

Represents a WebUI window driven by the webui-wire backend.

constructor

(new ww-webview% 
    [[profile profile] 
    [settings settings] 
    [use-browser use-browser] 
    [parent-id parent-id] 
    [parent parent] 
    [title title]] 
    [x x] 
    [y y] 
    [width width] 
    [height height] 
    [[icon icon] 
    [menu menu] 
    [html-file html-file]] 
    ...superclass-args...) 
  (is-a?/c ww-webview%)
  profile : symbol? = 'default-profile
  settings : (or/c #f ww-settings%) = #f
  use-browser : boolean? = #f
  parent-id : (or/c #f ww-win?) = #f
  parent : (or/c #f ww-webview%) = #f
  title : string? = "Racket HTML Window"
  x : number?
  y : number?
  width : number?
  height : number?
  icon : (or/c #f path? string?) = #f
  menu : any/c = #f
  html-file : (or/c #f path? string?) = #f
The numeric geometry defaults (x, y, width, height) are taken either from settings or from builtin defaults.

If parent or parent-id is provided, a child window is created and positioned relative to the parent; otherwise a normal top-level window is created.

If menu, icon, or html-file are provided, they are applied after window creation.

Important behaviour during construction:

  • when the first window is created, ww-start is called;

  • the new window is registered in the global windows table;

  • an event handler is installed so that incoming events from webui-wire are dispatched to methods like handle-click and handle-change;

  • when the last window is destroyed, ww-stop is called.

Key public methods (grouped by responsibility):

Settings and cloning

method

(send a-ww-webview clone-settings section)

  (or/c #f ww-settings%)
  section : symbol?
If settings is non-#f, call clone on it to obtain a section-specific settings object, otherwise return #f.

Event handling hooks

These are normally invoked internally when events arrive from webui-wire, but can be overridden in subclasses.

method

(send a-ww-webview handle-click element-id    
  data)  void?
  element-id : symbol?
  data : jsexpr?
Find the element object for element-id (if present) and call its callback method with the 'click event.

method

(send a-ww-webview handle-change element-id    
  data)  void?
  element-id : symbol?
  data : jsexpr?
Find the element object and delegate a 'change event.

method

(send a-ww-webview handle-input element-id    
  data)  void?
  element-id : symbol?
  data : jsexpr?
Find the element object and delegate an input event.

method

(send a-ww-webview handle-navigate url    
  type    
  kind)  void?
  url : string?
  type : symbol?
  kind : symbol?
Handle navigation events (for example link clicks) as reported by webui-wire. The default implementation may call send-url depending on type and kind.

Element management

method

(send a-ww-webview get-win-id)  ww-win?

Return the underlying ww-win handle.

method

(send a-ww-webview element id)  ww-element%

  id : (or/c symbol? string?)
Return (and lazily create) an element wrapper for a given DOM id. The concrete class is chosen based on the element’s tag and type (ww-input% for input fields, or ww-element% otherwise).

method

(send a-ww-webview get-elements selector)

  (listof ww-element%)
  selector : selector?
Query elements matching selector via ww-get-elements and return a list of element wrapper objects.

method

(send a-ww-webview bind event    
  selector    
  [forced-cl])  void?
  event : symbol?
  selector : selector?
  forced-cl : (or/c #f (is-a?/c ww-element%)) = #f
Bind event to all elements matching selector.

A suitable element class is chosen automatically based on tag/type, unless forced-cl is provided; in that case the given class is used for all matched elements.

method

(send a-ww-webview bind-inputs)  boolean?

Convenience: bind 'change events for input and textarea elements and create ww-input% wrappers.

method

(send a-ww-webview bind-buttons)  void?

Convenience: bind 'click events for button elements.

Window geometry and title

method

(send a-ww-webview move x y)  void?

  x : number?
  y : number?
Move the window to (x, y).

method

(send a-ww-webview resize x y)  void?

  x : number?
  y : number?
Resize the window to width x, height y.

method

(send a-ww-webview get-x)  number?

Current x position (cached).

method

(send a-ww-webview get-y)  number?

Current y position (cached).

method

(send a-ww-webview get-width)  number?

Current width (cached).

method

(send a-ww-webview get-height)  number?

Current height (cached).

method

(send a-ww-webview geom)

  (list number? number? number? number?)
Return (list x y width height).

method

(send a-ww-webview set-title! t)  void?

  t : string?
Set the window title (cached locally and sent to the backend).

method

(send a-ww-webview get-title)  string?

Return the last title set.

Show/hide and lifetime

method

(send a-ww-webview show)  void?

Show the window (set show state to 'show).

method

(send a-ww-webview hide)  void?

Hide the window.

method

(send a-ww-webview maximize)  void?

Maximise the window.

method

(send a-ww-webview normalize)  void?

Restore the window to its normal state.

method

(send a-ww-webview minimize)  void?

Minimise the window.

method

(send a-ww-webview fullscreen)  void?

Request a fullscreen display (if supported).

method

(send a-ww-webview show-state)  symbol?

Return the current show state as reported by the backend.

method

(send a-ww-webview can-close?)  boolean?

Return #t if the window may be closed; subclasses can override this to veto close requests.

method

(send a-ww-webview close)  void?

Close the window, unregister it from the global tables, and if this is the last window, stop the backend by calling ww-stop.

Menus

method

(send a-ww-webview set-menu! menu-def)  void?

  menu-def : is-menu?
Set the window menu using a structure created with the helpers from "menu.rkt".

method

(send a-ww-webview connect-menu! id cb)  void?

  id : symbol?
  cb : (-> any)
Associate a callback cb with a menu item id. When the menu item is activated, the callback is called.

HTML and navigation

method

(send a-ww-webview set-icon! icn)  void?

  icn : (or/c path? string?)
Set the window icon; the file is validated by ww-set-icon.

method

(send a-ww-webview set-html-file! file)  void?

  file : (or/c path? string?)
Load the given HTML file. The directory part is used to adjust ww-cwd, and the file name is passed to ww-set-html-file.

method

(send a-ww-webview set-html html)  void?

  html : string?
Write html to a temporary file and call set-html-file! with it. Intended for simple dynamic content.

method

(send a-ww-webview set-url url)  void?

  url : string?
Open the given URL using send-url.

method

(send a-ww-webview html-loaded)  void?

Hook that is called when a page is fully loaded and matches the current HTML handle. The default implementation binds buttons and inputs by calling bind-buttons and bind-inputs.

File and directory dialogs

method

(send a-ww-webview file-open caption    
  base-dir    
  filters)  (or/c #f path?)
  caption : string?
  base-dir : string?
  filters : string?
Show a file-open dialog and return the selected path as a path, or #f if the operation failed or was cancelled.

method

(send a-ww-webview file-save caption    
  base-dir    
  filters    
  [overwrite])  (or/c #f path?)
  caption : string?
  base-dir : string?
  filters : string?
  overwrite : boolean? = #f
Show a file-save dialog. The overwrite flag controls whether overwriting existing files is allowed. Returns the chosen path, or #f if cancelled or if the command failed.

method

(send a-ww-webview choose-dir caption    
  base-dir)  (or/c #f path?)
  caption : string?
  base-dir : string?
Show a directory chooser dialog; return the chosen directory, or #f on cancel/failure.

Hook for subclasses

method

(send a-ww-webview inherit-checks)  boolean?

Called early during construction to allow subclasses to enforce preconditions (for example: “dialog windows must have a parent”). The default implementation simply returns #t.

5.2 Dialog windows🔗ℹ

class

ww-webview-dialog% : class?

  superclass: ww-webview%

Subclass of ww-webview% representing dialog-style windows.
The constructor is the same as ww-webview%, but inherit-checks is overridden to enforce that a parent was supplied:
(define/override (inherit-checks)
  (when (eq? parent #f)
    (error "A parent must be given")))
So, to create a dialog, always pass a parent window:
(define main (new ww-webview%))
(define dlg  (new ww-webview-dialog% [parent main]))

5.3 Message dialogs🔗ℹ

class

ww-webview-message% : class?

  superclass: ww-webview-dialog%

Simple message-dialog subclass.
The constructor:
  • calls the ww-webview-dialog% constructor;

  • sets a minimal HTML page containing a message header and a submessage, with known element ids (e.g. "msg" and "submsg").

You can then obtain element wrappers for those ids via send this element and update their inner HTML or text using the usual element methods.

6 Settings abstraction🔗ℹ

class

ww-settings% : class?

  superclass: object%

Abstract base class for storing and retrieving configuration values used by ww-webview% (for example window geometry).
The default implementation only signals errors; you are expected to subclass ww-settings% and override the methods.
Public methods:

method

(send a-ww-settings set key value)  void?

  key : symbol?
  value : any/c
Set a configuration value for key. Default implementation raises an error.

method

(send a-ww-settings get key default)  any/c

  key : symbol?
  default : any/c
Look up a configuration value for key. If the key is not present, return default (if provided) or raise an error. The default implementation always raises an error.

method

(send a-ww-settings clone new-section)  ww-settings%

  new-section : symbol?
Return a “cloned” settings object for a given section or profile. Used by ww-webview% to derive per-window settings from a shared base. Default implementation raises an error.

method

(send a-ww-settings set! key value)  void?

  key : symbol?
  value : any/c
Convenience that forwards to set. Provided for symmetry with Racket’s set! naming style.