Nim wrapper for WebUI
| author: | neroist |
|---|---|
| WebUI Version: | 2.5.0-Beta |
See Nim Docs: https://yesdrx.github.io/nim-webui/
Example:
import webui let window = newWindow() # Create a new Window window.show("<html>Hello</html>") # Show the window with html content in any browser wait() # Wait until the window gets closed
Types
Event = ref object internalImpl*: ptr bindings.Event
- When you use bind(), your application will receive an event every time the user clicks on the specified HTML element. The event comes with the element, which is The HTML ID of the clicked element, for example, "MyButton", "MyInput", etc. The event also comes with the element ID & the unique window ID. Source Edit
Procs
proc `bind`(window: Window; element: string; func: proc ()) {....raises: [], tags: [], forbids: [].}
- Source Edit
proc `bind`(window: Window; element: string; func: proc (): bool) {....raises: [], tags: [], forbids: [].}
- Source Edit
proc `bind`(window: Window; element: string; func: proc (): float) {....raises: [], tags: [], forbids: [].}
- Source Edit
proc `bind`(window: Window; element: string; func: proc (): int) {....raises: [], tags: [], forbids: [].}
- Source Edit
proc `bind`(window: Window; element: string; func: proc (): string) {. ...raises: [], tags: [], forbids: [].}
- Source Edit
proc `bind`(window: Window; element: string; func: proc (e: Event)) {. ...raises: [], tags: [], forbids: [].}
-
Bind a specific html element and a JavaScript object with a backend function. Empty element will bind all events.
Each element can have only one function bound to it.
Source Editwindow: The window to bind the function onto. element: The HTML element / JavaScript object to bind the function func to. For HTML elements, func will be called on click events. An empty element means func will be bound to all events. func: The function to bind to element. proc `bind`(window: Window; element: string; func: proc (e: Event): bool) {. ...raises: [], tags: [], forbids: [].}
-
Bind func to element element and automatically pass return value of func to Javascript.
Source Editwindow: The window to bind the function onto. element: The element to bind the function func to. func will be called on click events. An empty element means func will be called on all events. func: The function to bind to element. proc `bind`(window: Window; element: string; func: proc (e: Event): float) {. ...raises: [], tags: [], forbids: [].}
- Source Edit
proc browserExist(browser: bindings.WebuiBrowser): bool {....raises: [], tags: [], forbids: [].}
-
Check if a web browser is installed.
Returns true if the specified browser is available.
Source Edit proc childProcessId(window: Window): int {....raises: [], tags: [], forbids: [].}
-
Get the ID of the last child process.
Source Editwindow: The window proc closeClient(event: Event) {....raises: [], tags: [], forbids: [].}
-
Close a specific client.
Source Editevent: The event object. proc connectionId(event: Event): int {....raises: [], tags: [], forbids: [].}
- Source Edit
proc deleteAllProfiles() {....raises: [], tags: [], forbids: [].}
- Delete all local web-browser profiles folder. It should be called at the end. Source Edit
proc deleteProfile(window: Window) {....raises: [], tags: [], forbids: [].}
-
Delete a specific window web-browser local folder profile.
Source Editwindow: The window whose profile will be deleted proc eventBlocking=(window: Window; status: bool) {....raises: [], tags: [], forbids: [].}
-
Control if UI events comming from this window should be processed one a time in a single blocking thread (true), or process every event in a new non-blocking thread (false). This function only affects a single window You may use setConfig(wcUiEventBlocking, ...) to update all windows.
Source Editwindow: The window to configure event blocking for. status: Whether or not to process window events blockingly (single vs multi-threaded). proc eventNumber(event: Event): int {....raises: [], tags: [], forbids: [].}
- Source Edit
proc fileHandler=(window: Window; handler: proc (filename: string): string) {. ...raises: [], tags: [], forbids: [].}
-
Set a custom handler to serve files. This custom handler should return the full HTTP headers and body.
Source Editwindow: The window to set the file handler. runtime: The file handler callback/proc. proc getBestBrowser(window: Window): bindings.WebuiBrowser {....raises: [], tags: [], forbids: [].}
-
Get the recommended web browser to use. If running show(), this function will return the same browser that is already being used.
window: The window to get the best browser for Returns a value from the WebuiBrowser enum.
Source Edit proc getFreePort(): int {....raises: [], tags: [], forbids: [].}
- Get an available and usable free network port. Source Edit
proc getMimeType(file: string): string {....raises: [], tags: [], forbids: [].}
-
Get the HTTP mime type of a file.
Source Editfile: The name of the file (e.g. foo.png) proc getNewWindowId(): int {....raises: [], tags: [], forbids: [].}
-
Get a free window number that can be used in conjuction with newWindow(int).
Returns the first available free window number, starting from 1.
Source Edit proc highContrast=(window: Window; status: bool) {....raises: [], tags: [], forbids: [].}
-
Setup the window with high-contrast support. Useful when you want to build a better high-contrast theme with CSS.
Source Editwindow: The window to set the high contrast theme support for. status: Whether or not to support high contrast themes. proc isHighContrast(): bool {....raises: [], tags: [], forbids: [].}
-
Get the OS's high contrast preference.
Returns true if the OS prefers a high contrast theme.
Source Edit proc parentProcessId(window: Window): int {....raises: [], tags: [], forbids: [].}
-
Get the ID of the parent process (The web browser may re-create another new process).
Source Editwindow: The window proc port=(window: Window; port: int) {....raises: [], tags: [], forbids: [].}
-
Set a custom web-server network port to be used by WebUI. This can be useful to determine the HTTP link of webui.js in case you are trying to use WebUI with an external web-server like NGNIX
Source Editwindow: The window port: The web-server network port WebUI should use proc returnBool(event: Event; b: bool) {....raises: [], tags: [], forbids: [].}
-
Return the response to JavaScript as a boolean.
Source Editevent: The event to set the response for b: The bool to return back to Javascript. proc returnFloat(event: Event; f: float) {....raises: [], tags: [], forbids: [].}
-
Return the response to JavaScript as a float.
Source Editevent: The event to set the response for integer: The float to return back to Javascript. proc returnString(event: Event; str: string) {....raises: [], tags: [], forbids: [].}
-
Return the response to JavaScript as a string.
Source Editevent: The event to set the response for str: The string to return back to Javascript. proc rootFolder=(window: Window; path: string): bool {.discardable, ...raises: [], tags: [], forbids: [].}
-
Set the web-server root folder path.
Source Editwindow: The window to set the root folder for. path: The path to the root folder. proc script(window: Window; script: string; timeout: int = 0; bufferLen: static[int] = 1024 * 8): tuple[data: string, error: bool]
-
Run Javascript code script and return the result
Returns a tuple containing the response (data) and whether or not there was an error (error, true if an error occured, false otherwise). If an error occured, the error message will be held in data.
Source Editwindow: The window to run the Javascript code in. script: The Javascript code to execute. timeout: The execution timeout in seconds. bufferLen: How large to make the buffer for the response. Default is 8 kibibytes. (For larger responses make bufferLen larger) proc scriptClient(event: Event; script: string; timeout: int = 0; bufferLen: static[int] = 1024 * 8): tuple[data: string, error: bool]
-
Run Javascript code script and return the result
Returns a tuple containing the response (data) and whether or not there was an error (error, true if an error occured, false otherwise). If an error occured, the error message will be held in data.
Source Editevent: The event. script: The Javascript code to execute. timeout: The execution timeout in seconds. bufferLen: How large to make the buffer for the response. Default is 8 kibibytes. (For larger responses make bufferLen larger) proc sendRaw(window: Window; function: string; raw: pointer; size: uint) {. ...raises: [], tags: [], forbids: [].}
-
Safely send raw data to the UI. All clients.
Source Editwindow: The window to send the raw data to. function: The JavaScript function to receive raw data: function myFunc(myData){} raw: The raw data buffer. size: The size of the raw data in bytes. proc sendRawClient(event: Event; function: string; raw: pointer; size: uint) {. ...raises: [], tags: [], forbids: [].}
-
Safely send raw data to the UI. All clients.
Source Editevent: The event. function: The JavaScript function to receive raw data: function myFunc(myData){} raw: The raw data buffer. size: The size of the raw data in bytes. proc setConfig(option: bindings.WebuiConfig; status: bool) {....raises: [], tags: [], forbids: [].}
-
Control WebUI's behaviour via setting configuration option option to either true or false. It's better to this call at the beginning of your program.
Source Editoption: The desired option from the WebuiConfig enum status: The status of the option, true or false proc setConfig(options: openArray[bindings.WebuiConfig] or set[bindings.WebuiConfig]; status: bool)
-
Control WebUI's behaviour via setting configuration options options to either true or false. It's better to this call at the beginning of your program.
Source Editoptions: The desired options from the WebuiConfig enum status: The desired status of all of the options, true or false proc setDefaultRootFolder(path: string): bool {.discardable, ...raises: [], tags: [], forbids: [].}
-
Set the default web-server root folder path for all windows.Note: Should be used before webui_show().
path: The path to the root folder. Returns true on success.
Source Edit proc setFileHandler(window: Window; handler: proc (filename: string): string) {. ...raises: [], tags: [], forbids: [].}
- Same as fileHandler=, but targeted towards use with do notation Source Edit
proc setPosition(window: Window; x, y: int) {....raises: [], tags: [], forbids: [].}
-
Set window position
Source Editwindow: The window to set the size for. x: What to set the window's X to. y: What to set the window's Y to. proc setProfile(window: Window; name, path: string) {....raises: [], tags: [], forbids: [].}
-
Set the web browser profile to use. An empty name and path means the default user profile.Note: Needs to be called before webui_show().
window: The window to set the browser profile for. name: The web browser profile name. path: The web browser profile full path. Example:
window.setProfile("Bar", "/Home/Foo/Bar") window.setProfile("", "")
Source Edit proc setTimeout(timeout: int) {....raises: [], tags: [], forbids: [].}
-
Set the maximum time in seconds to wait for the window to connect. This will affect show() and wait(). Setting the timeout to 0 will cause WebUI to wait forever.
Source Edittimeout: The maximum time in seconds to wait for browser to start. Set to 0 to wait forever. proc setTlsCertificate(certificate_pem, private_key_pem: string): bool {. ...raises: [], tags: [], forbids: [].}
-
Set the SSL/TLS certificate and the private key content, both in PEM format. This works only with webui-2-secure library. If set empty, WebUI will generate a self-signed certificate.
certificate_pem: The SSL/TLS certificate content in PEM format private_key_pem: The private key content in PEM format Returns true if the certificate and the key are valid.
Source Edit proc show(window: Window; content: string): bool {.discardable, ...raises: [], tags: [], forbids: [].}
-
Show a window using embedded HTML, or a file. If the window is already open, it will be refreshed. This will refresh all windows in multi-client mode.Important: Please include <script src="/webui.js"></script> in the HTML for proper window communication.
window: The window to show content in. If the window is already shown, the UI will get refreshed in the same window. content: The content to show in window. Can be a file name, URL, or a static HTML script. Returns true if showing the window is a success.
Source Edit proc show(window: Window; content: string; browsers: openArray[ bindings.WebuiBrowser] or set[bindings.WebuiBrowser]): bool {.discardable.}
-
Same as show(), but with a specific set of web browsers to use.Important: Please include <script src="/webui.js"></script> in the HTML for proper window communication.
window: The window to show content in. If the window is already shown, the UI will get refreshed in the same window. content: The content to show in window. Can be a file name, or a static HTML script. browser: The browsers to open the window in. Returns true if showing the window is a success.
Source Edit proc show(window: Window; content: string; browser: bindings.WebuiBrowser): bool {. discardable, ...raises: [], tags: [], forbids: [].}
-
Same as show(), but with a specific web browser.Important: Please include <script src="/webui.js"></script> in the HTML for proper window communication.
window: The window to show content in. If the window is already shown, the UI will get refreshed in the same window. content: The content to show in window. Can be a file name, or a static HTML script. browser: The browser to open the window in. Returns true if showing the window is a success.
Source Edit proc showClient(event: Event; content: string): bool {.discardable, ...raises: [], tags: [], forbids: [].}
-
Show a window using embedded HTML, or a file. If the window is already open, it will be refreshed. Single client.Important: Please include <script src="/webui.js"></script> in the HTML for proper window communication.
event: The event to use. content: The content to show. Can be a file name, URL, or a static HTML script. Returns true if showing the window is a success.
Source Edit proc showWv(window: Window; content: string): bool {.discardable, ...raises: [], tags: [], forbids: [].}
-
Show a WebView window using embedded HTML, or a file. If the window is already open, it will be refreshed.Note: On Windows, you will need WebView2Loader.dll.
window: The window to show content in. If the window is already shown, the UI will get refreshed in the same window. content: The content to show in window. Can be a file name, or a static HTML script. Returns true if showing the WebView window succeeded.
Source Edit proc startServer(window: Window; path: string): string {....raises: [], tags: [], forbids: [].}
-
Same as show(), but it only starts the web server and returns the URL. This is useful for web apps.
No window will be shown.
window: The window to start the web server for path: The full path to the local root folder Returns the url of the window server.
Source Edit
Macros
macro webuiCb(procDef: untyped): untyped
-
proc pragma to automatically create a WebUI callback wrapper function.
Supported argument types: primitives + Event:
int : int, int8, int16, int32, int64, uint, uint, uint8, uint16, uint32, uint64, cint8, cint16, cint32, cint64, cshort, cushort, cint, cuint, clong, culong csize_t, byte float : float, float32, float64, cfloat, cdouble string : string, cstring, JsonNode bool : bool Event
Supported return types: primitives + voidExample:
import webui proc getSystemInfo(some_argument : JsonNode): string {.webuiCb.} = echo "Received argument: ", some_argument.pretty return %*{"key1" : 123, "key2" : "value2"} proc one() {.webuiCb.} = discard proc two(e1, e2 : Event) {.webuiCb.} = discard when isMainModule: let window = newWindow() window.bindCb("getInfo", getSystemInfo) let html = """ <!DOCTYPE html> <html> <head> <script src="/webui.js"></script> </head> <script> function fetchSystemInfo() { webui.call('getInfo', '{"a" : 1, "b" : "c"}').then( (response) => { document.body.innerHTML = "<pre>" + response + "</pre>"; } ); } setInterval(fetchSystemInfo, 2000); </script> </html> """ window.show(html) wait()
Source Edit