WebView (webview_elixir v0.1.1)

Copy Markdown View Source

WebView is the main module for interacting with a single WebView instance.

To create a minimal webview:

WebView.create()
WebView.set_title(~c"App Window")
WebView.set_size(800, 600, WebView.Hint.none)
WebView.set_html(~c"<p>Example Webview</p>")
WebView.run()
WebView.destroy()

Summary

Functions

Creates a new Javascript function binding in the webview instance.

Returns a specification to start this module under a supervisor.

Creates a webview. Calling this function with default arguments creates and initializes a new webview instance.

Destroys the webview instance. A webview instance must be created and run before it can be destroyed.

Dispatch function to run on the main loop thread.

Evaluates Javascript code in the webview instance.

Gets a native handle reference to the webview window, browser, or browser controller. This handle can be passed into another NIF if such a library is created that takes a native handle as a parameter. Currently, this function is not used.

Gets a reference to the native window.

Initializes the webview with Javascript code to run prior to page load.

Navigates the webview to a URL.

Returns function call result to Javascript caller.

Runs the webview instance. A webview instance must be created first with WebView.create/0. To destroy the webview instance use WebView.destroy/0.

Set the html content of the webview instance.

Set the size of the webview instance's window.

Set the title of the webview instance's window.

This function can be used to start the WebView as part of a supervision tree.

Terminates the main loop of the webview instance.

Unbinds a previously created function binding from the webview instance.

Returns a map with the version of the webview library and other metadata.

Functions

bind(name, func, return_call \\ false, id_args \\ false)

Creates a new Javascript function binding in the webview instance.

Parameters

  • name: Charlist of the name of the Javascript function to register
  • func: anonymous Elixir function with a single argument designated args
  • return_call: If set to true, return function call result to JS
  • id_args: If set to true, pass {id, args} to the elixir function, where id is the call id of the Javascript call.

When the Javscript function is called from the webview, the Elixir function func is passed the Javascript function args as a single list of Elixir terms. The function arguments from Javascript are decoded as a JSON string.

For example:

someFunctionInJS(123, "test") => elixirFunction([123, "test"])

If return_call is true, return {:ok, result} from the elixir function if the call succeeds, otherwise return :err to indicate to the Javscript caller that the call was unsuccessful.

child_spec(arg)

Returns a specification to start this module under a supervisor.

arg is passed as the argument to Task.start_link/1 in the :start field of the spec.

For more information, see the Supervisor module, the Supervisor.child_spec/2 function and the t:Supervisor.child_spec/0 type.

create(enable_debugging \\ 0, handle \\ nil)

Creates a webview. Calling this function with default arguments creates and initializes a new webview instance.

Parameters

  • enable_debugging: Set to true to enable debugging tools (optional).
  • handle: Reference to native window handle (optional).

Returns

  • Reference to the webview instance or nil

Calling this function more than once without calling WebView.destroy/0 first raises an error. The webview backend must support debugging tools for the enable_debugging option to have any effect.

destroy()

Destroys the webview instance. A webview instance must be created and run before it can be destroyed.

Before calling this function call WebView.create/0 and WebView.run/0 first.

dispatch(f, arg \\ ~c"")

Dispatch function to run on the main loop thread.

Parameters

  • f: Atom webview function to call (i.e. :set_html)
  • arg: optional argument

eval(code)

Evaluates Javascript code in the webview instance.

Parameters

  • code: Charlist containing the Javascript code to run

get_native_handle(type)

Gets a native handle reference to the webview window, browser, or browser controller. This handle can be passed into another NIF if such a library is created that takes a native handle as a parameter. Currently, this function is not used.

Parameters

  • type: WebView.NativeHandle.window_ui, WebView.NativeHandle.browser_ui, WebView.NativeHandle.browser_controller

get_window()

Gets a reference to the native window.

init(code)

Initializes the webview with Javascript code to run prior to page load.

Parameters

  • code: Charlist containing the javascript code

return(id, status, result)

Returns function call result to Javascript caller.

Parameters

  • id: Charlist of the call id that was passed to the elixir function
  • status: Integer indicating if call was successful. Set to 0 if the call was a success or any other number otherwise.
  • result: Charlist containing the result of the elixir function call

run()

Runs the webview instance. A webview instance must be created first with WebView.create/0. To destroy the webview instance use WebView.destroy/0.

set_html(string)

Set the html content of the webview instance.

Parameters

  • string: Charlist containing the html text

set_size(width, height, hint)

Set the size of the webview instance's window.

Parameters

  • width: Integer specifying the window width
  • height: Integer specifying the window height
  • hint: Enumerated type that configures additional window options.

Can use the WebView.Hint module for possible values or pass the number directly:

WebView.Hint.none  (0) -> Don't set any options for the window
WebView.Hint.min   (1) -> Set the minimum window size to width x height
WebView.Hint.max   (2) -> Set the max window size to width x height
WebView.Hint.fixed (3) -> Set the window to be non-resizable

Note that not all of these parameters will have an effect on every platform.

set_title(title)

Set the title of the webview instance's window.

Parameters

  • title: Charlist containing the text of the title

start_link(options)

This function can be used to start the WebView as part of a supervision tree.

The keyword options :title, :width, :height, :hint, :html, :init, :eval can be set to configure the WebView parameters. If the :exit_on_close option is set to true, then shut down the application when the window is closed (default).

For example:

children = [
  {WebView, title: ~c"Application"}
]

Supervisor.start_link(children, strategy: :one_for_all)

The :bind option can be used to configure function bindings. If the value is set to a {name, func} tuple, then the function and name are passed to bind. It also accepts a list of {name, func} tuples to register multiple functions.

Example:

Supervisor.start_link([
  {WebView,
    bind: [
      {~c"f", fn _ -> IO.puts ~c'F called' end},
      {~c"quit", fn _ -> WebView.terminate() end}
    ],
    html: ~c"
      <button onclick='f()'>Click</button>
      <button onclick='quit()'>Quit</button>
    ",
    exit_on_close: false}
  ],
strategy: :one_for_all)

terminate()

Terminates the main loop of the webview instance.

unbind(name)

Unbinds a previously created function binding from the webview instance.

Parameters

  • name: Charlist containing the javascript function name

version()

Returns a map with the version of the webview library and other metadata.