Struct alcro::UI

source · []
pub struct UI { /* private fields */ }
Expand description

The browser window

Implementations

Returns true if the browser is closed

Wait for the browser to be closed

Close the browser window

Load content in the browser. It returns Err if it fails.

Bind a rust function so that JS code can use it. It returns Err if it fails. The rust function will be executed in a new thread and can be called asynchronously from Javascript

Arguments
  • name - Name of the function
  • f - The function. It should take a list of JSObject as argument and return a JSResult
Examples
#![windows_subsystem = "windows"]
use alcro::UIBuilder;
use serde_json::to_value;

let ui = UIBuilder::new().custom_args(&["--headless"]).run().expect("Unable to launch");
ui.bind("add",|args| {
    let mut sum = 0;
    for arg in args {
        match arg.as_i64() {
            Some(i) => sum+=i,
            None => return Err(to_value("Not number").unwrap())
        }
    }
    Ok(to_value(sum).unwrap())
}).expect("Unable to bind function");
assert_eq!(ui.eval("(async () => await add(1,2,3))();").unwrap(), 6);
assert!(ui.eval("(async () => await add(1,2,'hi'))();").is_err());

Bind a rust function callable from JS that can complete asynchronously. If you are using [tokio], you will probably want to be using [Self::bind_tokio()] instead.

Unlike bind(), this passes ownership of the arguments to the callback function f, and allows completing the javascript implementation after returning from f. This makes async behavior much simpler to implement.

For efficency, f will be executed in the message processing loop, and therefore should avoid blocking by moving work onto another thread, for example with an async runtime spawn method.

Arguments
  • name - Name of the function
  • f - The function. It should take a BindingContext that gives access to the arguments and allows returning results.
Examples

bind() approximately performs the following:

#![windows_subsystem = "windows"]
use alcro::UIBuilder;
use serde_json::to_value;

let ui = UIBuilder::new().custom_args(&["--headless"]).run().expect("Unable to launch");
ui.bind_async("add", |context| {
    std::thread::spawn(|| {
        // imagine this is very expensive, or hits a network...
        let mut sum = 0;
        for arg in context.args() {
            match arg.as_i64() {
                Some(i) => sum+=i,
                None => return context.err(to_value("Not number").unwrap())
            }
        }

        context.complete(Ok(to_value(sum).unwrap()));
    });
}).expect("Unable to bind function");
assert_eq!(ui.eval("(async () => await add(1,2,3))();").unwrap(), 6);
assert!(ui.eval("(async () => await add(1,2,'hi'))();").is_err());

Evaluates js code and returns the result.

Examples
#![windows_subsystem = "windows"]
use alcro::UIBuilder;
let ui = UIBuilder::new().custom_args(&["--headless"]).run().expect("Unable to launch");
assert_eq!(ui.eval("1+1").unwrap(), 2);
assert_eq!(ui.eval("'Hello'+' World'").unwrap(), "Hello World");
assert!(ui.eval("xfgch").is_err());

Evaluates js code and adds functions before document loads. Loaded js is unloaded on reload.

Arguments
  • script - Javascript that should be loaded
Examples
#![windows_subsystem = "windows"]
use alcro::UIBuilder;
let ui = UIBuilder::new().custom_args(&["--headless"]).run().expect("Unable to launch");
ui.load_js("function loadedFunction() { return 'This function was loaded from rust'; }").expect("Unable to load js");
assert_eq!(ui.eval("loadedFunction()").unwrap(), "This function was loaded from rust");

Loads CSS into current window. Loaded CSS is unloaded on reload.

Arguments
  • css - CSS that should be loaded
Examples
#![windows_subsystem = "windows"]
use alcro::UIBuilder;
let ui = UIBuilder::new().custom_args(&["--headless"]).run().expect("Unable to launch");
ui.load_css("body {display: none;}").expect("Unable to load css");

It changes the size, position or state of the browser window specified by the Bounds struct. It returns Err if it fails.

To change the window state alone use WindowState::to_bounds()

It gets the size, position and state of the browser window. It returns Err if it fails.

Trait Implementations

Closes the browser window

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.