wv-window
| (require wv-window) |
Window abstraction built on top of racket-webview.
This module exports the wv-window% class and re-exports the APIs from wv-element, wv-input, rgba, and wv-settings.
1 Overview
A wv-window% object represents one webview window.
A window belongs to a context, loads one HTML path within that context, manages its own title and geometry, dispatches incoming events, and provides access to element wrappers through element.
The class also provides synchronous wrappers around the asynchronous dialog interface by using continuations internally.
2 Class: wv-window%
constructor
(new wv-window% [ [parent parent]] [wv-context wv-context] [html-path html-path] [settings settings] [ [title title] [width width] [height height] [x x] [y y]]) → (is-a?/c wv-window%) parent : (or/c #f (is-a?/c wv-window%)) = #f
wv-context :
(if (eq? parent #f) (error "wv context is mandatory") (get-field wv-context parent)) html-path : path-string?
settings :
(send wv-context settings (string->symbol (format "~a" html-path))) title : string? = "Racket Webview Window" width : (or/c exact-integer? #f) = #f height : (or/c exact-integer? #f) = #f x : (or/c exact-integer? #f) = #f y : (or/c exact-integer? #f) = #f Creates a window.The constructor requires html-path. If parent is #f, wv-context is mandatory. If a parent window is supplied, wv-context defaults to the context of that parent.
The default value of settings is obtained as:
(send wv-context settings (string->symbol (format "~a" html-path)))
After construction, the class:
creates the underlying webview window
installs the internal event handler
sets the title
initializes position and size from settings or defaults
If a parent is supplied, the underlying lower-level parent window is passed to webview-create.
Returns the window context object supplied at construction.Returns the settings context key for this window.The value is computed as:
(string->symbol (format "~a" html-path))
Returns administrative information for the window as a hash table.The returned hash contains the keys 'wv-context, 'wv, 'html-path, and 'elements.
Returns the wrapper object associated with the DOM element identified by id.Element wrappers are cached in an internal weak hash table. If no wrapper exists yet, one is created and cached.
If no explicit type is supplied, the method asks the browser for the element’s type attribute. Depending on the resulting type, one of the following classes is instantiated:
wv-input/text% for 'text
wv-input/date% for 'date
wv-input/time% for 'time
wv-input/datetime% for 'datetime-local
wv-input/range% for 'range
wv-input/number% for 'number
wv-input/check% for 'checkbox
wv-input/radio% for 'radio
wv-input/color% for 'color
wv-element% otherwise
Newly created wrappers receive this window through the window init argument and the element id through element-id.
method
(send a-wv-window moved xx yy) → any/c
xx : exact-integer? yy : exact-integer? Updates the internal position and stores the new coordinates in settings under 'x and 'y.
method
(send a-wv-window resized w h) → any/c
w : exact-integer? h : exact-integer? Updates the internal size and stores the new dimensions in settings under 'width and 'height.Called when the underlying window state changes.The default implementation returns #t.
method
(send a-wv-window page-loaded oke) → any/c
oke : any/c Called when a 'page-loaded event is received.The default implementation returns #t.
method
(send a-wv-window can-close?) → any/c
Called when a 'can-close? event is received.If this method returns a true value, the window closes itself by calling close.
The default implementation returns #t.
method
(send a-wv-window closed) → any/c
Called when a 'closed event is received.The default implementation returns #t.
Dispatches a JavaScript event received from the browser.The incoming hash is expected to contain at least the keys 'evt, 'id, and optionally 'data. If the referenced element exists in the internal element cache, the event is dispatched to that element by calling its dispatch-event method.
If no matching element wrapper exists, the method returns 'wv-unhandled-js-event.
method
(send a-wv-window navigation-request type url) → (or/c 'internal 'external) type : symbol? url : url? Handles a navigation request event.If the requested URL starts with the context base URL, the request is treated as internal, loaded into the current window, and 'internal is returned.
Otherwise the URL is opened externally using send-url, and 'external is returned.
method
(send a-wv-window add-class! selector-or-id cl) → (is-a?/c wv-window%) selector-or-id : (or/c symbol? string?) cl : (or/c symbol? string? list?) Adds one or more CSS classes to the selected elements and returns this window.
method
(send a-wv-window remove-class! selector-or-id cl) → (is-a?/c wv-window%) selector-or-id : (or/c symbol? string?) cl : (or/c symbol? string? list?) Removes one or more CSS classes from the selected elements and returns this window.Opens the developer tools and returns this window.
method
x : exact-integer? y : exact-integer? Moves the window and returns this window.
method
w : exact-integer? h : exact-integer? Resizes the window and returns this window.Closes the window and returns this window.
method
(send a-wv-window bind! selector events callback) → list? selector : (or/c symbol? string?) events : (or/c symbol? list?) callback : procedure? Binds one or more DOM events.The method first delegates to webview-bind!. For each returned binding, it connects the corresponding element wrapper to callback.
If events is a symbol, it is treated as a single-element list.
The result is the list produced by mapping over the binding items. In practice, this yields the corresponding cached element wrapper objects.
Removes one or more DOM event bindings.The method first delegates to webview-unbind!, then disconnects the corresponding callbacks from cached element wrappers.
If an element wrapper no longer has any event callbacks, it is removed from the internal cache.
The returned value is the list produced by webview-unbind!.
Installs menu in this window and returns this window.The accepted argument follows the contract of webview-set-menu!. The method delegates to:
(webview-set-menu! wv menu)
method
(send a-wv-window connect-menu! id callback) → (is-a?/c wv-window%) id : symbol? callback : procedure? Connects callback to the menu item identified by id.The method installs a binding for the 'menu-item-choosen event by delegating to:
(send this bind! id 'menu-item-choosen (λ (el evt data) (callback))) The callback is invoked without arguments when the corresponding menu item is chosen.
Sets the window title.
method
(send a-wv-window file-dialog-done flag file dir filter) → any/c flag : symbol? file : any/c dir : any/c filter : any/c Internal continuation callback used by file and directory dialogs.The default implementation resumes the pending continuation stored by the window.
method
(send a-wv-window choose-dir title base-dir) → (or/c 'showing path-string? #f) title : string? base-dir : (or/c path-string? #f) Shows a directory chooser dialog.At most one file or directory dialog can be active at a time. Attempting to open another one raises an exception.
If base-dir is #f, the user’s home directory is used.
If the underlying dialog could not be started, the method returns #f. If the dialog is shown successfully, the method returns 'showing first and later resumes through the internal continuation mechanism.
When resumed after completion:
the selected directory is returned on success
#f is returned if the dialog was canceled
method
(send a-wv-window file-open title base-dir filters) → (or/c 'showing list? #f) title : string? base-dir : (or/c path-string? #f) filters : any/c Shows an open-file dialog.At most one file or directory dialog can be active at a time. Attempting to open another one raises an exception.
If base-dir is #f, the user’s home directory is used.
If the underlying dialog could not be started, the method returns #f. If the dialog is shown successfully, the method returns 'showing first and later resumes through the internal continuation mechanism.
When resumed after completion:
on success, the method returns (cdr r) from the internal dialog result
on cancellation, the method returns #f
This matches the current source code exactly.
method
(send a-wv-window file-save title base-dir filters) → (or/c 'showing list? #f) title : string? base-dir : (or/c path-string? #f) filters : any/c Shows a save-file dialog.At most one file or directory dialog can be active at a time. Attempting to open another one raises an exception.
If base-dir is #f, the user’s home directory is used.
If the underlying dialog could not be started, the method returns #f. If the dialog is shown successfully, the method returns 'showing first and later resumes through the internal continuation mechanism.
When resumed after completion:
on success, the method returns (cdr r) from the internal dialog result
on cancellation, the method returns #f
This matches the current source code exactly.
Internal continuation callback used by message boxes.The default implementation resumes the pending message-box continuation stored by the window.
method
(send a-wv-window message type title message [ #:sub submessage]) → (or/c 'showing symbol? #f) type : symbol? title : string? message : string? submessage : string? = "" Shows a message box.At most one message box can be active at a time. Attempting to show another one raises an exception.
If the underlying dialog could not be started, the method returns #f. If the dialog is shown successfully, the method returns 'showing first and later resumes through the internal continuation mechanism.
When resumed after completion, the result is the event symbol returned by the lower layer, such as the symbols corresponding to ok, cancel, yes, or no.
method
(send a-wv-window init-size) → any/c
Initializes the window position and size.The method reads 'x, 'y, 'width, and 'height from settings. If a value is missing, the constructor argument is used if present; otherwise a default is chosen.
The resulting geometry is then applied through move and resize.
3 Events
The window installs an internal event handler when it is created.
This handler reacts to the following event kinds produced by the lower layer:
'resize, forwarded to resized
'move, forwarded to moved
'page-loaded, forwarded to page-loaded
'show, 'hide, and 'maximize, forwarded to window-state-changed
'can-close?, causing can-close? to be queried
'closed, forwarded to closed
'js-evt, decoded and forwarded to js-event
'navigation-request, forwarded to navigation-request
file-dialog completion events, forwarded to file-dialog-done
message-box completion events, forwarded to message-done
Unhandled events are printed to the console by the current implementation.
4 Function: root-file-not-found-handler
procedure
(root-file-not-found-handler standard-file not-found-handler ...) → procedure? standard-file : string? not-found-handler : procedure?
The returned procedure has the shape:
Its behavior is as follows:
if file is "/", it retries using standard-file
if file is "/index.html", it falls back to standard-file when the target does not exist
if the requested path exists, it is returned
otherwise, if an extra not-found-handler was supplied, that handler is called
if no extra handler was supplied, the original path is returned
Only the first optional not-found-handler is used.
5 Re-exports
This module also re-exports the public APIs from:
It also re-exports webview-version.